Hi Implemented a highpass filter following the this. But, my data is not predefined and it comes every in real-time. I am not sure if this correct.
def sine_generator(fs, sinefreq, duration):
T = duration
nsamples = int(fs * T)
w = 2. * np.pi * sinefreq
t_sine = np.linspace(0, T, nsamples, endpoint=False)
y_sine = np.sin(w * t_sine)
result = pd.DataFrame({
'sine' : y_sine} ,index=t_sine)
return result
def butter_highpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
print("Filter order is ={}, Normalized cutoff ={}".format(order, normal_cutoff))
b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
print(b,a)
return b, a
# signal generation
sampling_freq = int(1e5)
cutoff_freq = 100/2*np.pi
duration = .01
data = sine_generator(sampling_freq, 10, duration)
data['sine'] = 0.1*data['sine'] + 0.2*sine_generator(sampling_freq, int(1e3), duration)['sine'] + 0.2
t = 7 # signal.filtfilt requires atleast t values to filter. t increases as the order increases
b, a = butter_highpass(cutoff_freq, sampling_freq, order=1)
for i in range(np.shape(data.sine.values)[0]-t):
d = data.sine.values[i:i+t+1]
y = signal.filtfilt(b, a, d)
print(y)
I think this is not the rightway.