Python實現數字帶阻巴特沃斯濾波器
帶阻濾波器是一種濾波器,它拒絕或阻擋一定頻率範圍內的所有頻率,並透過該範圍外的頻率。巴特沃斯濾波器是一種旨在在通帶內儘可能平坦地濾波頻率的濾波器型別。以下是數字帶阻巴特沃斯濾波器的主要特點。
濾波器的取樣率約為12kHz。
通帶邊緣頻率範圍為2100Hz至4500Hz。
阻帶邊緣頻率範圍為2700Hz至3900Hz。
通帶的紋波點為0.6分貝。
阻帶的最小衰減為45分貝。
實現帶阻巴特沃斯濾波器
帶阻巴特沃斯濾波器與帶通巴特沃斯濾波器的功能相反。實現數字帶阻巴特沃斯濾波器需要遵循以下幾個步驟。讓我們一一來看。
步驟1 - 首先,我們必須定義給定輸入訊號的取樣頻率fs,以及下限截止頻率f1、上限截止頻率f2和濾波器的階數。為了避免陡峭的滾降導致相位失真,我們必須將濾波器的階數確定為儘可能低的數值。
步驟2 - 在這一步中,我們將使用以下公式計算已定義的下限截止頻率f1和上限截止頻率f2的歸一化頻率wn1和wn2。
wn = 2 ∗ fn / fs
步驟3 - 在python中,我們有scipy庫,它有一個名為scipy.signal.butter()的函式,該函式用於設計具有已定義階數和歸一化頻率的巴特沃斯濾波器,並將btype引數作為bandstop傳遞。
步驟4 - 現在,我們將使用scipy.signal.filtfilt()函式對給定的輸入訊號應用濾波器,以執行零相位濾波。
示例
在下面的示例中,我們將使用python中提供的函式和上述步驟來實現帶阻巴特沃斯濾波器。
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, filtfilt
def butter_bandstop(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='bandstop')
return b, a
def butter_bandstop_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandstop(lowcut, highcut, fs, order=order)
y = filtfilt(b, a, data)
return y
t = np.linspace(0, 1, 1000, endpoint=False)
data = np.sin(2*np.pi*10*t) + np.sin(2*np.pi*100*t)
fs = 1000
lowcut = 45
highcut = 55
order = 4
filtered_data = butter_bandstop_filter(data, lowcut, highcut, fs, order)
print("The length of the filtered_data:",len(filtered_data))
print("The first 60 frequencies output of the bandstop filter:",filtered_data[:60])
plt.plot(t, data, 'b-', label='data')
plt.plot(t, filtered_data, '.', linewidth=0.75, label='filtered data', c = ("orange"))
plt.xlabel('Time [sec]')
plt.grid()
plt.legend()
plt.show()
輸出
The length of the filtered_data: 1000 The first 60 frequencies output of the bandstop filter: [ 0.0538999 0.68581044 1.08999445 1.12963747 0.80670599 0.26172752 -0.27940415 -0.59133519 -0.53509911 -0.11114146 0.54072315 1.19441551 1.62350117 1.68713138 1.38324023 0.8487329 0.30664654 -0.01948728 0.00866697 0.3912569 0.99019459 1.58206033 1.94372501 1.9379504 1.56622785 0.96862788 0.37067549 -0.00249295 -0.01192794 0.34213815 0.9203407 1.49727013 1.8473413 1.83067626 1.44623031 0.83190402 0.21163048 -0.19032582 -0.23510605 0.07772592 0.61013477 1.13854044 1.43948479 1.37529278 0.94698224 0.29418691 -0.35789988 -0.78431234 -0.84607258 -0.54316965 -0.01451725 0.51510369 0.82086563 0.76371439 0.34342535 -0.30135441 -0.94608162 -1.36606715 -1.42224433 -1.11419611]
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP