Skip to content
Snippets Groups Projects
Commit 61e511e7 authored by Isabel Morales's avatar Isabel Morales
Browse files

respiration rate module working with python libraries

parent 1e19569e
Branches python_app
No related tags found
No related merge requests found
......@@ -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("muestra3s.txt", 'r')
df = open("ppg15.txt", 'r')
#df = open("c:\Users\ANGELICA\Documents\Workspace\proyecto-smart-watch\software\samples\ecg01_200fs.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 signal")
plt.title("PPG Signal")
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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment