04 使用python設(shè)計(jì)模擬濾波器
01 butterworth filter
低通濾波器:在60rad/s處衰減3dB,在80rad/s處衰減40dB;
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.buttord(60, 80, 3, 40, analog=True) #
b, a = sig.butter(N, Wn, 'lowpass', analog=True) #
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')

高通濾波器:在55rad/s處衰減3dB,在45rad/s處衰減40dB;
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.buttord(55, 45, 3, 40, analog=True) #
b, a = sig.butter(N, Wn, 'highpass', analog=True) #
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')

帶通濾波器:在20-50rad/s內(nèi)衰減3dB,在14rad/s以下和60rad/s以上衰減40dB;
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.buttord([20, 50], [14, 60], 3, 40, analog=True)
b, a = sig.butter(N, Wn, 'bandpass', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')

import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.buttord([10,80], [20,70], 3, 30, analog=True) #
b, a = sig.butter(N, Wn, 'bandstop', analog=True) #
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')

02 chebyshev filter (type one)
低通濾波器:在60rad/s處衰減3dB,在80rad/s處衰減40dB,波動(dòng)3dB;
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.cheb1ord(60, 80, 3, 40, analog=True)
b, a = sig.cheby1(N,3, Wn, 'lowpass', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')

高通濾波器:在55rad/s處衰減3dB,在45rad/s處衰減40dB,波動(dòng)3dB;
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.cheb1ord(55, 45, 3, 40, analog=True)
b, a = sig.cheby1(N, 3,Wn, 'highpass', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')
帶通濾波器:在20-50rad/s內(nèi)衰減3dB,在14rad/s以下和60rad/s以上衰減40dB,波動(dòng)3dB;
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.cheb1ord([20, 50], [14, 60], 3, 40, analog=True)
b, a = sig.cheby1(N,3,Wn, 'bandpass', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')

帶阻濾波器:在20-70rad/s內(nèi)衰減30dB,在15rad/s以下和75rad/s以上衰減3dB,波動(dòng)3dB;
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.cheb1ord([15,75], [20,70], 3, 30, analog=True)
b, a = sig.cheby1(N,3,Wn, 'bandstop', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')

03 chebyshev filter (type two)
低通濾波器
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.cheb2ord(60, 80, 3, 40, analog=True)
b, a = sig.cheby2(N,40, Wn, 'lowpass', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')
plt.grid()

高通濾波器
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.cheb2ord(55, 45, 3, 40, analog=True)
b, a = sig.cheby2(N,40,Wn, 'highpass', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')
plt.grid()

帶通濾波器
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.cheb2ord([20, 50], [14, 60], 3, 40, analog=True)
b, a = sig.cheby2(N,40,Wn, 'bandpass', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')
plt.grid()

帶阻濾波器
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.cheb2ord([15,75], [20,70], 3, 30, analog=True)
b, a = sig.cheby2(N,30,Wn, 'bandstop', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')
plt.grid()

04 ellipse filter
低通濾波器
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.ellipord(60, 80, 3, 40, analog=True)
b, a = sig.ellip(N,3,40, Wn, 'lowpass', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')
plt.grid()

高通濾波器
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.ellipord(55, 45, 3, 40, analog=True)
b, a = sig.ellip(N,3,40,Wn, 'highpass', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')
plt.grid()

帶通濾波器
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.ellipord([20, 50], [14, 60], 3, 40, analog=True)
b, a = sig.ellip(N,3,40,Wn, 'bandpass', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')
plt.grid()

帶阻濾波器
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
N, Wn = sig.ellipord([15,75], [20,70], 3, 30, analog=True)
b, a = sig.ellip(N,3,30,Wn, 'bandstop', analog=True)
w, h = sig.freqs(b, a, np.logspace(1, 2, 500))
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('angular fre [rad/s]')
plt.ylabel('response [dB]')
plt.grid()

05 以上函數(shù)也可以設(shè)計(jì)數(shù)字濾波器,將analog=false,查看頻響用freqz,即可;
另外,數(shù)字濾波器還有兩個(gè)特別類型:陷波濾波器(點(diǎn)阻),共振濾波器(點(diǎn)通)
陷波濾波器
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
b,a=sig.iirnotch(60,30,512)
w,h=sig.freqz(b,a,fs=512)
plt.plot(w,20*np.log10(abs(h)))
plt.xlabel('Hz')
plt.ylabel('dB')
plt.grid()

共振濾波器
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
b,a=sig.iirpeak(100,30,512)
w,h=sig.freqz(b,a,fs=512)
plt.plot(w,20*np.log10(abs(h)))
plt.xlabel('Hz')
plt.ylabel('dB')
plt.grid()

工程師必備
- 項(xiàng)目客服
- 培訓(xùn)客服
- 平臺(tái)客服
TOP




















