Python中的數字低通巴特沃斯濾波器
低通濾波器是一種電子濾波器,它允許頻率低於定義截止頻率的訊號透過,而頻率高於截止頻率的訊號則會被衰減。高通巴特沃斯濾波器具有一些專門的特性,定義如下。
給定輸入訊號的取樣率為 40 kHz。
通帶的截止頻率為 4 kHz。
阻帶的截止頻率為 8 kHz。
通帶的紋波為 0.5 dB。
阻帶的最小衰減為 40 dB,每個頻率的衰減都基於濾波器設計。
實現低通巴特沃斯濾波器
Numpy 庫提供了名為 butter() 的函式,該函式屬於 scipy.signal 模組,用於透過將 btype 引數定義為 low 來建立低通巴特沃斯濾波器。以下是使用 Python 實現低通巴特沃斯濾波器的步驟。
步驟 1 - 在此步驟中,我們將定義用於傳遞較低頻率的截止頻率 f、取樣頻率 fs 和濾波器的階數。
步驟 2 - 在 Python 中,我們有 scipy 庫,它有一個名為 scipy.signal.butter() 的函式,用於使用定義的階數和歸一化頻率設計巴特沃斯濾波器,將 btype 引數傳遞為 low 以實現低通巴特沃斯濾波器。
步驟 3 - 在此步驟中,我們將使用 scipy.signal.filtfilt() 函式建立給定輸入訊號頻率的濾波器,以執行零相位濾波。
步驟 4 - 現在,我們將繪製低通巴特沃斯濾波器的輸出頻率和濾波後的輸出頻率。
示例
在以下示例中,我們透過組合所有提到的步驟來實現低通巴特沃斯濾波器。
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, filtfilt
t = np.linspace(0, 1, 1000, False)
signal = np.sin(2*np.pi*10*t) + np.sin(2*np.pi*100*t)
cutoff_freq = 50
nyquist_freq = 0.5 * 1000
order = 4
b, a = butter(order, cutoff_freq/nyquist_freq, btype='low')
print("The output of the Low band pass Butterworth filter:",b,a)
filtered_signal = filtfilt(b, a, signal)
print("The output of the filtered Low band pass Butterworth filter:",filtered_signal[:60])
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.plot(t, signal)
ax1.set(title='Original signal')
ax2.plot(t, filtered_signal)
ax2.set(title='Filtered signal')
plt.show()
輸出
The output of the Low band pass Butterworth filter: [0.0004166 0.0016664 0.0024996 0.0016664 0.0004166] [ 1. -3.18063855 3.86119435 -2.11215536 0.43826514] The output of the filtered Low band pass Butterworth filter: [ 2.57011434e-02 8.61005841e-02 1.44535721e-01 2.01004914e-01 2.55827118e-01 3.09620894e-01 3.63131838e-01 4.16969733e-01 4.71355911e-01 5.25981148e-01 5.80034880e-01 6.32402662e-01 6.81965142e-01 7.27892755e-01 7.69831093e-01 8.07912298e-01 8.42592726e-01 8.74381837e-01 9.03567319e-01 9.30041579e-01 9.53294974e-01 9.72576767e-01 9.87160288e-01 9.96608860e-01 1.00093881e+00 1.00061547e+00 9.96382240e-01 9.88986904e-01 9.78909138e-01 9.66193004e-01 9.50448481e-01 9.31021733e-01 9.07269534e-01 8.78833540e-01 8.45810150e-01 8.08751539e-01 7.68497795e-01 7.25904493e-01 6.81569842e-01 6.35665536e-01 5.87935734e-01 5.37864244e-01 4.84945669e-01 4.28956498e-01 3.70122109e-01 3.09115413e-01 2.46887151e-01 1.84392199e-01 1.22315971e-01 6.09049996e-02 -3.39902671e-05 -6.09665336e-02 -1.22358749e-01 -1.84405645e-01 -2.46863503e-01 -3.09050695e-01 -3.70016926e-01 -4.28816577e-01 -4.84782072e-01 -5.37693155e-01]
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP