您好,登錄后才能下訂單哦!
小編給大家分享一下python中經典數字濾波的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
數字濾波分為 IIR 濾波,和FIR 濾波。
FIR 濾波:
import scipy.signal as signal import numpy as np import pylab as pl import matplotlib.pyplot as plt import matplotlib from scipy import signal b = signal.firwin(80, 0.5, window=('kaiser', 8)) w, h = signal.freqz(b) import matplotlib.pyplot as plt fig, ax1 = plt.subplots() ax1.set_title('Digital filter frequency response') ax1.plot(w, 20 * np.log10(abs(h)), 'b') ax1.set_ylabel('Amplitude [dB]', color='b') ax1.set_xlabel('Frequency [rad/sample]') ax2 = ax1.twinx() angles = np.unwrap(np.angle(h)) ax2.plot(w, angles, 'g') ax2.set_ylabel('Angle (radians)', color='g') ax2.grid() ax2.axis('tight') plt.show()
運行結果:
IIR 濾波器:
from scipy import signal import matplotlib.pyplot as plt import matplotlib.ticker import numpy as np # 藍色的是頻譜圖,綠色的是相位圖 wp = 0.2 ws = 0.3 gpass = 1 gstop = 40 system = signal.iirdesign(wp, ws, gpass, gstop) w, h = signal.freqz(*system) fig, ax1 = plt.subplots() ax1.set_title('Digital filter frequency response') ax1.plot(w, 20 * np.log10(abs(h)), 'b') ax1.set_ylabel('Amplitude [dB]', color='b') ax1.set_xlabel('Frequency [rad/sample]') ax1.grid() ax1.set_ylim([-110, 10]) nticks = 8 ax1.yaxis.set_major_locator(matplotlib.ticker.LinearLocator(nticks)) plt.show()
運行結果:
IIR 濾波器中cheyb2 濾波器的運用
from scipy import signal import matplotlib.pyplot as plt import numpy as np b, a = signal.cheby2(4, 40, 100, 'low', analog=True) w, h = signal.freqs(b, a) plt.semilogx(w, 20 * np.log10(abs(h)))#用于繪制折線圖,兩個函數的 x 軸、y 軸分別是指數型的。 #plt.plot(w, 20 * np.log10(abs(h))) plt.title('Chebyshev Type II frequency response (rs=40)') plt.xlabel('Frequency [radians / second]') plt.ylabel('Amplitude [dB]') plt.margins(0, 0.1)# not sure plt.grid(which='both', axis='both') t = np.linspace(0, 1, 1000, False) # 1 second sig = np.sin(2*np.pi*10*t) + np.sin(2*np.pi*20*t) fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True) ax1.plot(t, sig) ax1.set_title('10 Hz and 20 Hz sinusoids') ax1.axis([0, 1, -2, 2]) sos = signal.cheby2(12, 20, 17, 'hp', fs=1000, output='sos') filtered = signal.sosfilt(sos, sig) ax2.plot(t, filtered) ax2.set_title('After 17 Hz high-pass filter') ax2.axis([0, 1, -2, 2]) ax2.set_xlabel('Time [seconds]') plt.show()
以上是“python中經典數字濾波的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。