06 python 滤波实验(一)

00 载入python扩展库

import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt

01 定义信号

t=np.arange(0,10,1/256)
xt=1.5*np.sin(2*np.pi*40*t)+3*np.sin(2*np.pi*60*t)+2*np.sin(2*np.pi*80*t)

#40Hz,60Hz,80Hz的三个谐波叠加

02 傅里叶变换

xf=np.fft.fft(xt) #傅里叶变换
af=np.abs(xf)/len(xf) #求复数的模
afn=np.short(af.size/2) #整型
f=np.arange(0,256/2,256/len(t)) #定义频率点
plt.subplot(211) #子图1
plt.plot(t,xt) #时域信号
plt.subplot(212) #子图2
plt.plot(f,2*af[:afn]) #频域信号
plt.xlabel('Hz')

06 python 滤波实验(一)的图106 python 滤波实验(一)的图206 python 滤波实验(一)的图3

03 设计butterworth低通滤波器

N, Wn = sig.buttord(45, 55, 3, 30,fs=256) #数字滤波器
b, a = sig.butter(N, Wn, btype='lowpass',fs=256) #数字滤波器
w, h = sig.freqz(b,a,fs=256)
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('fre [Hz]')
plt.ylabel('response [dB]')

06 python 滤波实验(一)的图4

04 使用低通滤波器进行滤波

x2t=sig.lfilter(b,a,xt)
x2f=np.fft.fft(x2t) #傅里叶变换
a2f=np.abs(x2f)/len(x2f) #求复数的模
a2fn=np.short(a2f.size/2) #整型
f=np.arange(0,256/2,256/len(t)) #定义频率点
plt.subplot(211) #子图1
plt.plot(t,x2t)
plt.subplot(212) #子图2
plt.plot(f,2*a2f[:a2fn]) #频域信号

06 python 滤波实验(一)的图5可见,经过butter低通滤波,60Hz,80Hz的频率消失了;

05 设计butterworth 高通滤波器

N, Wn = sig.buttord(55, 45, 3, 30,fs=256) #数字滤波器
b, a = sig.butter(N, Wn, btype='highpass',fs=256) #数字滤波器
w, h = sig.freqz(b,a,fs=256)
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('fre [Hz]')
plt.ylabel('response [dB]')

06 python 滤波实验(一)的图6

06 使用高通滤波器进行滤波

06 python 滤波实验(一)的图7可见,经过butter高通滤波,40Hz的频率消失了;

07 设计butterworth 带通滤波器

N, Wn = sig.buttord([45,75], [40,80], 3, 30,fs=256) #数字滤波器
b, a = sig.butter(N, Wn, btype='bandpass',fs=256) #数字滤波器
w, h = sig.freqz(b,a,fs=256)
plt.plot(w, 20 * np.log10(abs(h)))
plt.xlabel('fre [Hz]')
plt.ylabel('response [dB]')

06 python 滤波实验(一)的图8

08 使用带通滤波器进行滤波

06 python 滤波实验(一)的图9可见,经过butter滤波,只有60Hz留下了;

登录后免费查看全文
立即登录
  • App下载
  • 项目客服
  • 培训客服
  • 平台客服

TOP

1
1