def process(self): # Grab only the new samples into a NumPy array x = np.array(self.__l1[-self.__new_samples:]) # Filter the signal (detrend, LP, MA, etc…) # ... # ... # ... # Store the filtered data ma = filt.moving_average(x, 20) # Compute Moving Average dt = filt.detrend(ma) # Detrend the Signal lp = filt.filter(self.__b, self.__a, dt) # Low-pass Filter Signal grad = filt.gradient(lp) # Compute the gradient x = filt.moving_average( grad, 20) # Compute the moving average of the gradient self.__filtered.add(x.tolist()) # Count the number of peaks in the filtered data count, peaks = filt.count_peaks(x, self.__thresh_low, self.__thresh_high) # Update the step count and reset the new sample count self.__steps += count self.__new_samples = 0 # Return the step count, peak locations, and filtered data return self.__steps, peaks, np.array(self.__filtered)
def process(self): # Grab only the new samples into a NumPy array x = np.array(self.__l1[-self.__new_samples:]) # Filter the signal (detrend, LP, MA, etc…) x = filt.detrend(x) # x = filt.filter(self.__b, self.__a, x) x, self.__xi, self.__yi = filt.filter_ic(self.__b, self.__a, x, self.__xi, self.__yi) x = filt.gradient(x) x = filt.moving_average(x, 25) # Store the filtered data self.__filtered.add(x.tolist()) # Count the number of peaks in the filtered data count, peaks = filt.count_peaks(x, self.__thresh_low, self.__thresh_high) # Update the step count and reset the new sample count self.__steps += count self.__new_samples = 0 # Return the step count, peak locations, and filtered data return self.__steps, peaks, np.array(self.__filtered)
def process(self): # Grab only the new samples into a NumPy array x = np.array(self.__ppg[-self.__new_samples:]) # Filter the signal (feel free to customize!) x = filt.detrend(x, 25) x = filt.moving_average(x, 5) x = filt.gradient(x) x = filt.normalize(x) # Store the filtered data self.__filtered.add(x.tolist()) # Find the peaks in the filtered data _, peaks = filt.count_peaks(x, self.__thresh, 1) # Update the step count and reset the new sample count self.__hr = self.compute_heart_rate(peaks) self.__new_samples = 0 # Return the heart rate, peak locations, and filtered data return self.__hr, peaks, np.array(self.__filtered)
ay = data[:, 2] az = data[:, 3] l1 = filt.l1_norm(ax, ay, az) # Compute the L1-Norm print(len(l1)) ma = filt.moving_average(l1, 20) # Compute Moving Average dt = filt.detrend(ma) # Detrend the Signal freqs, power = filt.psd(l1, len(l1), 50) # Power Spectral Density bl, al = filt.create_filter(3, 1, "lowpass", fs) # Low-pass Filter Design lp = filt.filter(bl, al, dt) # Low-pass Filter Signal grad = filt.gradient(lp) # Compute the gradient grad_avg = filt.moving_average( grad, 20) # Compute the moving average of the gradient count, peaks = filt.count_peaks(grad_avg, t_low, t_high) # Find & Count the Peaks # Plot the results plt.plot(t, grad_avg) plt.title("Detected Peaks = %d" % count) plt.plot(t[peaks], grad_avg[peaks], 'rx') plt.plot(t, [t_low] * len(grad_avg), "b--") plt.plot(t, [t_high] * len(grad_avg), "b--") plt.show() plt.plot(freqs, power) plt.title("Power Spectrum Density") plt.show()
plt.plot(t, ppg) plt.subplot(212) plt.plot(t, grad) plt.show() # Normalize the signal norm = filt.normalize(grad) plt.subplot(211) plt.title("Normalize") plt.plot(t, ppg) plt.subplot(212) plt.plot(t, norm) plt.show() thresh = 0.6 count, peaks = filt.count_peaks(norm, thresh, 1) plt.plot(t, norm) plt.title("Detected Peaks = %d" % count) plt.plot(t[peaks], norm[peaks], 'rx') plt.plot(t, [thresh] * len(norm), "b--") plt.show() # Compute the heart rate avg_beat_time = np.mean(np.diff(t[peaks])) bpm = 60 / avg_beat_time print("Estimated heart rate: {:.2f} bpm".format(bpm)) # Estimated heart rate was 74.37 bpm