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 __init__(self, num_samples, fs, data=None): self.__steps = 0 self.__num_samples = num_samples self.__fs = fs self.__l1 = CircularList(data, num_samples) self.__filtered = CircularList([], num_samples) self.__b, self.__a = filt.create_filter(3, 1.2, "lowpass", fs) self.__peak_arr = []
def add(self, ax, ay, az): l1 = filt.l1_norm(ax, ay, az) if isinstance(ax, int): num_add = 1 else: num_add = len(ax) l1 = l1.tolist() self.__l1.add(l1) self.__new_samples += num_add
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.__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)
t_high = 12 # upper peak threshold def load_data(filename): return np.genfromtxt(filename, delimiter=",") # Load the data as a 500x4 ndarray data = load_data("./data/offline_data.csv") t = data[:, 0] t = (t - t[0]) / 1e3 ax = data[:, 1] 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
import ECE16Lib.DSP as filt from matplotlib import pyplot as plt import numpy as np # Load the data as a 500x2 ndarray and extract the 2 arrays data = np.genfromtxt("./data/ramsink_01_13.csv", delimiter=",") t = data[:, 0] t = (t - t[0]) / 1e3 ppg = data[:, 1] # Detrend the signal and plot the result dt = filt.detrend(ppg, 25) plt.subplot(211) plt.title("Detrend") plt.plot(t, ppg) plt.subplot(212) plt.plot(t, dt) plt.show() ma = filt.moving_average(dt, 5) plt.subplot(211) plt.title("Moving Average") plt.plot(t, ppg) plt.subplot(212) plt.plot(t, ma) plt.show() grad = filt.gradient(ma)
def process(x): x = filt.detrend(x, 25) x = filt.moving_average(x, 5) x = filt.gradient(x) return filt.normalize(x)