https://github.com/tejasa97/ECG-Signal- ... ter/ECG.py
But graps shown empty
Can you please check the code and fix it ?
-5.0 -5.5 -6.0 -6.5 -7.0 -7.5 2 0 -2 0.0 0.0 A Before filtering ww 0.5 1.0 3.0 3.5 After filtering 2.5 ſichachachachechsechuchrechnchache 0.5 1.0 1.5 2.0 2.5 3.0 3.5
93 mov_avg= [(0.5+x) for x in mov_avg] 94 95 mov_avg [x*1.2 for x in mov_avg] #For now we raise the average by 28% to prevent the secondary heart contraction from interfering, in part 2 we will do this dynamically 96 dataset['filt_rollingmean'] = mov_avg #Append the moving average to the dataframe 97 98 #Mark regions of interest 99 window[] 100 peaklist = [] 101 listpos = 8 #We use a counter to move over the different data columns 102 for datapoint in dataset.filt: 103 rollingmean = dataset.filt_rollingmean [listpos] #Get local mean 104 105 if (datapoint do nothing listpos + 1 106 107 108 elif (datapoint > rollingmean): #If signal comes above local mean, mark ROI 109 window.append(datapoint) 110 listpos + 1 111 112 else: #If signal drops below local mean -> determine highest point 113 maximum = max(window) 114 - beatposition = listpos len(window) + (window.index(max(window))) #Notate the position of the point on the X-axis peaklist.append(beatposition) # Add detected peak to list 115 116 window = [] #Clear marked ROI 117 listpos + 1 118 119 ybeat [dataset.filt[x] for x in peaklist] #Get the y-value of all peaks for plotting purposes = 120 121 fig_hr- plt.figure() 122 fig_hr.canvas.set_window_title('Peak detector') 123 axs fig_hr.add_subplot (111) = 124 ax5.set_title("Detected peaks in signal"). 125 #ax6.set_xlim (0,2500) 126 axs.plot(dataset.filt, alpha=0.5, color='blue') #Plot semi-transparent HR 127 ax5.plot(mov_avg, color = 'green') #Plot moving average 128 ax5.scatter (peaklist, ybeat, color='red') #Plot detected peaks 129 #Compute heart rate 130 RR list [1] 131 cnt = 0 132 while (cnt < (len (peaklist)-1)): 133 134 RR interval= (peaklist[cnt+1] peaklist[cnt]) #Calculate distance between beats in # of samples ms_dist = ((RR_interval / fs) * 1000.0) #Convert sample distances to ms distances RR_list.append(ms_dist) #Append to list 135 136 cnt += 1 137 138 bpm = 60000 / np.mean(RR_list) # 60000 ms (1 minute) / average R-R interval of signal 139 print("\n\n\nAverage Heart Beat is: %.e1f\n" % (bpm)) #Round off to 1 decimal and print print("No of peaks in sample are (0)".format(len(peaklist))) 140 141 142 plt.show()
46 ax3.set_ylim([0, 0.2]) 47 48 49 #band_filt= np.array([45, 55]) 50 #b, a = signal.butter(2, band_filt/(Fs/2), 'bandstop', analog=False) 51 b, a = signal.butter (4, 50/(Fs/2), 'low') 52 53 ###ax3.plot(w, 20 np.log10 (abs (h))) 54 #Compute filtered signal 55 tempf = signal.filtfilt(b,a, y) 56 #b, a = signal.butter (1, band_filt/(Fs/2), 'bandstop') 57 tempf signal.filtfilt(b,a, y) = 58 yff = scipy.fftpack.fft(tempf) 59 60 61 nya_rate= Fs/ 2.0. 62 # The desired width of the transition from pass to stop. 63 width 5.0/nyq_rate 64 # The desired attenuation in the stop band, in dB. 65 ripple_db 60.0 66 # Compute the order and Kaiser parameter for the FIR filter. 67 0, beta = signal.kaiserord (ripple_db, width) 68 # The cutoff frequency of the filter. 69 cutoff_hz = 4.0 70 71 taps = signal. firwin (0, cutoff_hz/nyq_rate, window=('kaiser', beta), pass_zero=False) 72 # Use lfilter to filter x with the FIR filter.. 73 y_filt signal.lfilter (taps, 1.0, tempf) 74 yff = scipy.fftpack.fft(y_filt) 75 #Plot filtered outputs 76 ax4.plot(xf, 2.0/N* np.abs(yff[:N//2]), color='g', linewidth=0.7) 77 ax4.set_ylim ([0, 0.2]) 78 ax2.plot(x,y_filt, color='g', linewidth=0.7); 79 80 81 ### Compute beats### 82 83 dataset ['filt']=y_filt 84 #Calculate moving average with 0.75s in both directions, then append do dataset 85 hrw = 1 # One-sided window size, as proportion of the sampling frequency 86 fs= 333 #The example dataset was recorded at 300Hz 87 #mov_avg = pd.rolling_mean(dataset.filt, window=(int (hrw*fs))) #Calculate moving average mov_avg = dataset.filt.rolling(int (hrw * fs)).mean() 88 89 90 #Impute where moving average function returns NaN, which is the beginning of the signal where x hrw. avg_hr (np.mean(dataset.filt)) 91 92 mox aug [ava hr if math isnan(x) else y for y in mov augl ###Compute filtering co-efficients to eliminate 50hz brum noise### ### Compute Kaiser window co-effs to eliminate baseline drift noise ### ###Use firwin with a Kaiser window to create a lowpass FIR filter.###
1 ### Import required packages 2 import csv 3 import math 4 import pandas as pd 5 import numpy as np. 6 import matplotlib.pyplot as plt 7 # Helps to obtain the FFT 8 import scipy.fftpack 9 # Various operations on signals (waveforms) 10 import scipy.signal as signal 11 12 13 dataset = pd.read_csv ("noise.csv") 14 y = [e for e in dataset.hart] 15 16 # Number of samplepoints 17 N = len(y) 18 # sample spacing 19 Fs = 1000 20 T = 1.0 / Fs 21 # Compute x-axis 22 x = np.linspace(0.0, N³T, N) 23 24 # Compute FFT 25 yf = scipy.fftpack.fft(y) 26 #Compute frequency x-axis 27 xf = np.linspace(0.0, 1.0/(2.0*T), N/2) 28 29 30 fig_td = plt.figure() 31 fig_td.canvas.set_window_title('Time domain signals') 32 fig_fd = plt.figure() 33 fig_fd.canvas.set_window_title('Frequency domain signals') 34 ax1 = fig_td.add_subplot (211) 35 axl.set_title('Before filtering') 36 ax2 = fig_td.add_subplot(212) 37 ax2.set_title('After filtering') 38 ax3 = fig_fd.add_subplot (211) 39 ax3.set_title('Before filtering') 40 ax4 = fig_fd.add_subplot(212) 41 ax4.set_title('After filtering') 42 43 #Plot non-filtered inputs 44 ax1.plot(x, y, color='r', linewidth=0.7) 45 ax3.plot(xf, 2.0/N * np.abs(yf[:N//2]), color='r', linewidth=0.7, label='raw') 46 ax3.set_ylim([0, 0.2]) 47 ###Obtain ecg sample from csv file using pandas### ##Declare plots for time-domain and frequency-domain plots##
0.20 0.15 0.10 0.05 0.00 0.20 0.15 0.10 0.05 0.00 0 100 100 Before filtering After filtering 200 300 400 400 500 500
-5.0 -5.5 -6.0 -6.5 -7.0 -7.5 2 0 -2 0.0 0.0 A Before filtering ww 0.5 1.0 3.0 3.5 After filtering 2.5 ſichachachachechs
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am