Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Proyecto Smart Watch
Manage
Activity
Members
Labels
Plan
Issues
7
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Leonardo Martinez Hornak
Proyecto Smart Watch
Commits
61e511e7
Commit
61e511e7
authored
3 years ago
by
Isabel Morales
Browse files
Options
Downloads
Patches
Plain Diff
respiration rate module working with python libraries
parent
1e19569e
Branches
python_app
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
software/read_txt.py
+59
-102
59 additions, 102 deletions
software/read_txt.py
with
59 additions
and
102 deletions
software/read_txt.py
+
59
−
102
View file @
61e511e7
...
...
@@ -14,21 +14,30 @@ from numpy.core.arrayprint import LongFloatFormat
import
pandas
as
pd
#path = 'C:/Users/ANGELICA/Desktop/PySW/samples/'
#home = str(Path.home())
#filenames = os.listdir(os.path.join(home, path))
#print(filenames)
# FFT
def
fft
(
signal
,
Fs
):
PSD
=
abs
(
np
.
fft
.
fft
(
signal
))
PSD
=
PSD
[
1
:
int
(
len
(
PSD
)
/
2
)]
PSD
=
PSD
/
np
.
argmax
(
PSD
)
L
=
len
(
PSD
)
freq
=
np
.
arange
(
0
,
L
,
1
)
*
((
Fs
/
2
)
/
L
)
return
PSD
,
freq
# RATE
def
rate_calculation
(
peak_array
):
r
=
np
.
diff
(
peak_array
)
bpm
=
(
1.
/
r
)
*
60
rate
=
np
.
mean
(
abs
(
bpm
))
return
bpm
,
rate
#data = pd.read_excel (r'C:/Users/ANGELICA/Desktop/PySW/samples/test8.xlsx')
#df = pd.DataFrame(data)
#print (df)
ppg_ir
=
[]
location
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
print
(
location
)
#df = open("
muestra3
s.txt", 'r')
df
=
open
(
"
ppg15
.txt
"
,
'
r
'
)
#df = open("
c:\Users\ANGELICA\Documents\Workspace\proyecto-smart-watch\software\samples\ecg01_200f
s.txt", 'r')
df
=
open
(
"
c:
\\
Users\ANGELICA\Documents\Workspace\proyecto-smart-watch\software\samples\ppg15_125fs
.txt
"
,
'
r
'
)
ppg
=
[]
for
i
in
df
.
readlines
():
...
...
@@ -49,42 +58,11 @@ Ts = 1/Fs # s
N
=
len
(
ppg_ir
)
time
=
Ts
*
(
np
.
array
(
np
.
arange
(
0
,
N
,
1
)))
plt
.
figure
(
figsize
=
(
15
,
15
))
#plt.subplot(3,1,1)
#plt.plot(time,ppg_ir)
#plt.title("Amplitude adjusted")
#plt.ylabel('Amplitude (V)')
#plt.xlabel('Time (s)')
## Signal to zero
ppg_ir
=
(
ppg_ir
-
np
.
mean
(
ppg_ir
))
/
np
.
std
(
ppg_ir
)
plt
.
subplot
(
2
,
2
,
1
)
plt
.
plot
(
time
,
ppg_ir
)
plt
.
title
(
"
Signal to zero
"
)
plt
.
ylabel
(
'
Amplitude (V)
'
)
plt
.
xlabel
(
'
Time (s)
'
)
# FFT
def
fft
(
signal
,
Fs
):
PSD
=
abs
(
np
.
fft
.
fft
(
signal
))
PSD
=
PSD
[
1
:
int
(
len
(
PSD
)
/
2
)]
PSD
=
PSD
/
np
.
argmax
(
PSD
)
L
=
len
(
PSD
)
freq
=
np
.
arange
(
0
,
L
,
1
)
*
((
Fs
/
2
)
/
L
)
return
PSD
,
freq
PSD
,
freq
=
fft
(
ppg_ir
,
Fs
)
plt
.
subplot
(
2
,
2
,
2
)
plt
.
plot
(
freq
,
PSD
)
plt
.
title
(
"
Normalized frequency
"
)
plt
.
ylabel
(
'
Magnitude
'
)
plt
.
xlabel
(
'
Frequency (Hz)
'
)
#FIR
numtaps
=
21
f1
=
53
...
...
@@ -99,19 +77,18 @@ ppg_ir_filter= signal.filtfilt(b,a,ppg_ir)
PSD_1
,
freq_1
=
fft
(
ppg_ir_filter
,
Fs
)
plt
.
subplot
(
2
,
2
,
3
)
plt
.
figure
(
figsize
=
(
15
,
15
))
plt
.
subplot
(
2
,
2
,
1
)
plt
.
plot
(
time
,
ppg_ir_filter
)
plt
.
title
(
"
Filtered s
ignal
"
)
plt
.
title
(
"
PPG S
ignal
"
)
plt
.
ylabel
(
'
Amplitude (V)
'
)
plt
.
xlabel
(
'
Time (s)
'
)
plt
.
subplot
(
2
,
2
,
4
)
plt
.
subplot
(
2
,
2
,
2
)
plt
.
plot
(
freq_1
,
PSD_1
)
plt
.
title
(
"
Filtered signal
"
)
plt
.
ylabel
(
'
Amplitude (V)
'
)
plt
.
xlabel
(
'
Time (s)
'
)
plt
.
show
()
plt
.
title
(
"
Normalized frequency
"
)
plt
.
ylabel
(
'
Magnitude
'
)
plt
.
xlabel
(
'
Frequency (Hz)
'
)
#Heart rate
ppg_ir_p
=
ppg_ir
...
...
@@ -119,26 +96,22 @@ ppg_ir_p = ppg_ir
thres
=
3
*
np
.
mean
(
abs
(
ppg_ir_p
))
/
3
dist
=
(
60
/
200
)
*
Fs
peaks
,
_
=
find_peaks
(
ppg_ir_p
,
distance
=
dist
,
height
=
thres
)
plt
.
figure
(
figsize
=
(
15
,
15
))
plt
.
subplot
(
3
,
1
,
1
)
plt
.
subplot
(
2
,
2
,
3
)
plt
.
plot
(
time
,
ppg_ir
)
plt
.
plot
(
time
[
peaks
],
ppg_ir
[
peaks
],
"
x
"
)
#heart rate calculation
#derivada discreta
hr
=
np
.
diff
(
time
[
peaks
])
#time difference
plt
.
subplot
(
3
,
1
,
2
)
hr
,
heart_rate
=
rate_calculation
(
time
[
peaks
])
print
(
heart_rate
)
plt
.
subplot
(
2
,
2
,
4
)
plt
.
stem
(
hr
)
bpm
=
(
1.
/
hr
)
*
60
plt
.
subplot
(
3
,
1
,
3
)
plt
.
stem
(
bpm
)
plt
.
title
(
"
Heart Rate
"
)
plt
.
ylabel
(
'
Beats/min
'
)
plt
.
xlabel
(
'
Values
'
)
plt
.
show
()
heart_rate
=
np
.
mean
(
abs
(
bpm
))
print
(
heart_rate
)
#interpolation because is not good enough distributed
#rearange time
time_hr
=
[]
time_hr
.
append
(
time
[
0
])
c
=
0
...
...
@@ -162,64 +135,48 @@ f = interpolate.interp1d(x, y)
#xnew = np.arange(0, 9, 0.1)
#ynew = f(xnew) # use interpolation function returned by `interp1d`
#plt.plot(x, y, 'o', xnew, ynew, 'x')
#plt.show()
#Respiration rate
rr
=
ppg_ir
[
peaks
]
ppg_
rr
=
ppg_ir
[
peaks
]
time_rr
=
time
[
peaks
]
plt
.
figure
(
figsize
=
(
15
,
15
))
plt
.
subplot
(
2
,
2
,
1
)
plt
.
plot
(
time_rr
,
rr
)
PSD_r
,
freq_r
=
fft
(
rr
,
Fs
)
plt
.
subplot
(
2
,
2
,
2
)
plt
.
plot
(
freq_r
,
PSD_r
)
plt
.
title
(
"
Normalized frequency
"
)
plt
.
ylabel
(
'
Magnitude
'
)
plt
.
xlabel
(
'
Frequency (Hz)
'
)
PSD_r
,
freq_r
=
fft
(
ppg_rr
,
Fs
)
## Signal to zero
rr
=
(
rr
-
np
.
mean
(
rr
))
/
np
.
std
(
rr
)
plt
.
subplot
(
2
,
2
,
3
)
plt
.
plot
(
time_rr
,
rr
)
ppg_rr
=
(
ppg_rr
-
np
.
mean
(
ppg_rr
))
/
np
.
std
(
ppg_rr
)
plt
.
figure
(
figsize
=
(
15
,
15
))
plt
.
subplot
(
2
,
2
,
1
)
plt
.
plot
(
time_rr
,
ppg_rr
)
plt
.
title
(
"
Signal to zero
"
)
plt
.
ylabel
(
'
Amplitude (V)
'
)
plt
.
xlabel
(
'
Time (s)
'
)
PSD_r1
,
freq_r1
=
fft
(
rr
,
Fs
)
plt
.
subplot
(
2
,
2
,
4
)
PSD_r1
,
freq_r1
=
fft
(
ppg_rr
,
Fs
)
plt
.
subplot
(
2
,
2
,
2
)
plt
.
plot
(
freq_r1
,
PSD_r1
)
plt
.
title
(
"
Normalized frequency
"
)
plt
.
ylabel
(
'
Magnitude
'
)
plt
.
xlabel
(
'
Frequency (Hz)
'
)
plt
.
show
()
#Heart rate
#Respiration rate
#for peak detection
thres
=
3
*
np
.
mean
(
abs
(
rr
))
/
3
#
thres = 3*np.mean(abs(
ppg_
rr))/3
#dist = (60/30)*Fs
peaks
,
_
=
find_peaks
(
rr
,
height
=
0
)
plt
.
figure
(
figsize
=
(
15
,
15
))
plt
.
subplot
(
3
,
1
,
1
)
plt
.
plot
(
time_rr
,
rr
)
plt
.
plot
(
time_rr
[
peaks
],
rr
[
peaks
],
"
x
"
)
#heart rate calculation
#derivada discreta
rrr
=
np
.
diff
(
time_rr
[
peaks
])
#time difference
plt
.
subplot
(
3
,
1
,
2
)
plt
.
stem
(
rrr
)
bpm
=
(
1.
/
rrr
)
*
60
plt
.
subplot
(
3
,
1
,
3
)
plt
.
stem
(
bpm
)
respiration_rate
=
np
.
mean
(
abs
(
bpm
))
print
(
respiration_rate
)
peaks
,
_
=
find_peaks
(
ppg_rr
,
height
=
0
)
plt
.
subplot
(
2
,
2
,
3
)
plt
.
plot
(
time_rr
,
ppg_rr
)
plt
.
plot
(
time_rr
[
peaks
],
ppg_rr
[
peaks
],
"
x
"
)
plt
.
title
(
"
RR Signal
"
)
plt
.
ylabel
(
'
Amplitude (V)
'
)
plt
.
xlabel
(
'
Time (s)
'
)
#respiration rate calculation
rr
,
respiration_rate
=
rate_calculation
(
time_rr
[
peaks
])
print
(
respiration_rate
)
plt
.
subplot
(
2
,
2
,
4
)
plt
.
stem
(
rr
)
plt
.
title
(
"
Respiration Rate
"
)
plt
.
ylabel
(
'
Breaths/min
'
)
plt
.
xlabel
(
'
Values
'
)
plt
.
show
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment