--- tags: 名詞解釋 --- # Butterworth filter (巴特沃斯濾波器) ## 前情提要 - 濾波器的種類 >濾波器常用於訊號處理,例如擷取感測元件輸出的類比訊號時,很容易因外在因素產生雜訊導致失真,故需使用濾波器(filter)除去不必要的雜訊,將特定頻率的訊號顯示出來。<br> 濾波器分為被動(passive filters)與主動濾波器(active filters),頻率在 1MHz 以上大都會用被動濾波器, 此濾波器由電阻、電容、電感所組成,無功率增益,頻率較難調整,功能僅只過濾雜訊,頻率在1MHz 以下大都會用主動濾波器(active filters) 此濾波器由電阻、電容、主動元件所組成,有功率增益、頻率較容易調整,主動元件包括電晶體、運算放大器等,主動濾波器的優點如下:<br> 1. 利用運算放大器提出適當的訊號增益,使訊號通過濾波器不會衰減。<br> 2. 不使用電感,使濾波器可製成體積小的積體電路。<br> 3. 利用運算放大器的高輸入阻抗與低輸入阻抗,使阻抗匹配容易,不易造成阻抗被配不好而產生負載效應。<br> 4. 利用不同特性的組合,可設計成所想要的頻率響應。<br> --- - Low Pass Filter ==指低於截止頻率的信號都可通過== ![](https://i.imgur.com/0uJMDqZ.png =350x) - High Pass Filter ==指高於截止頻率的信號可通過== ![](https://i.imgur.com/OGBztzX.png =350x) - Band Pass Filter ==只允許某個頻帶寬度的信號通過== ![](https://i.imgur.com/C99zq96.png =350x) - Band Reject Filter ==不允許某個頻帶寬度的信號通過== ![](https://i.imgur.com/n6K7DmC.gif =350x) ## Low-Pass Filter Code ```python= import numpy as np from scipy.signal import butter, lfilter, freqz import matplotlib.pyplot as plt def butter_lowpass(cutoff, fs, order=5): # calculate the Nyquist frequency nyq = 0.5 * fs # design filter normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype='low', analog=False) return b, a def butter_lowpass_filter(data, cutoff, fs, order=5): b, a = butter_lowpass(cutoff, fs, order=order) y = lfilter(b, a, data) return y # Setting standard filter requirements. # # cutoff (float) : the cutoff frequency of the filter # fs (float) : the sampling rate (取樣頻率) # order (int) : order of the filter, by default defined to 5. # order = 6 fs = 30.0 cutoff = 3.667 b, a = butter_lowpass(cutoff, fs, order) # Plotting the frequency response. w, h = freqz(b, a, worN=8000) plt.subplot(2, 1, 1) plt.plot(0.5*fs*w/np.pi, 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() ``` ![](https://i.imgur.com/IBZjgV8.png) ```python= # Creating the data for filteration T = 5.0 # value taken in seconds n = int(T * fs) # indicates total samples t = np.linspace(0, T, n, endpoint=False) 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) # Filtering and plotting 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(hspace=0.35) plt.show() ``` ![](https://i.imgur.com/qI9hN38.png)