Import NumPy as NP;
Import Matplotlib.pyplot as plt import cv2;
Import Math; From Cmath import sin from numpy.core.numeric import dtype to scipy.signal import butter, Lfilter, freqz from Scipy.fftp
ACK import FFT;
' A few comments:the Nyquist frequency is half the sampling rate. You are are working with regularly sampled data, so want a digital filter, not a analog filter. This means your should not use Analog=true in the call to butter, and your should use SCIPY.SIGNAL.FREQZ (don't freqs) to Gene
Rate the frequency response. One goal of those short utility functions are to allow, you to leave all your frequencies expressed in Hz. You are shouldn ' t have to convert to rad/sec. As long as you express your frequencies with consistent units, the scaling in the utility functions takes care of the norm
Alization for you.
Here's my modified version of your script, followed by the plot that it generates. ' Def butter_lowpass (cutoff, FS, order=5): NYQ = 0.5 * FS Normal_cutoff = CutOff/nyq B, a = butter (order, Normal_cutoff, btype= "Low", Analog=false) return B, a def butter_lowpass_filter (d
ATA, Cutoff, FS, order=3): b, a = Butter_lowpass (cutoff, FS, order=order) y = Lfilter (b, A, data) return y
# Filter requirements. Order = 3 fs = 30.0 # sample rate, Hz cutoff = 3.667 # desired cutoff frequency of the filter, Hz # Get the Filte
R coefficients so we can check its frequency response.
B, a = Butter_lowpass (cutoff, FS, order) print B;
Print A;
# Plot the frequency response.
W, h = freqz (b, A, worn=8000) plt.figure (1); Plt.subplot (2, 1, 1) plt.plot (0.5*fs*w/np.pi, Np.abs (h), ' B ') #plt. Plot (W, Np.abs (h), ' B ') Plt.plot (cutoff, 0.5*NP.SQRT ( 2), ' Ko ') plt.axvline (cutoff, color= ' K ') Plt.xlim (0, 0.5*fs) plt.title ("Lowpass Filter Frequency Response") Plt.xlabel ('
Frequency [Hz] ') Plt.grid () # Demonstrate the use of the filter.
# Some data to is filtered. t = 5.0 # seconds n = Int (T * FS) # Total number of samples T =Np.linspace (0, T, N, endpoint=false) # "noisy" data.
We want to recover the 1.2 Hz signal from. data = Np.sin (1.2*2*np.pi*t) + 1.5*np.cos (9*2*np.pi*t) + 0.5*np.sin (12.0*2*np.pi*t) # Filter The data, and plot both the
Original and filtered signals. y = butter_lowpass_filter (data, cutoff, FS, order) Plt.subplot (2, 1, 2) plt.plot (t, data, ' B ', label= ' data ') Plt.plot (t , Y, ' G ', linewidth=2, label= ' filtered data ') Plt.xlabel (' Time [sec] ') Plt.grid () Plt.legend () () Plt.subplots_adjust (HSPA
ce=0.35) #show the signal spetrum before and after filter.
Plt.figure (2);
Plt.subplot (2,1,1);
YF=FFT (data);
Xf=np.linspace (0.0,FS/2,N/2);
Plt.plot (XF, (Np.abs (YF[0:N/2)));
Plt.subplot (2,1,2);
Yf_filter=fft (y);
Plt.plot (XF, (Np.abs (YF_FILTER[0:N/2)));
Plt.grid ();
Plt.show ()