python产生fir滤波器_Python中使用FIR滤波器firwin后信号的相移
所以,在我最后兩個問題之后,我來談談我的實際問題。也許有人在我的理論程序中發現了錯誤,或者我在編程上做了些錯事。在
我使用scipy.signal(使用firwin函數)在Python中實現帶通濾波器。我的原始信號包括兩個頻率(w_1=600Hz,w_2=800Hz)。可能會有更多的頻率所以我需要一個帶通濾波器。在
在這個例子中,我想過濾掉大約600hz的頻帶,所以我取了600+/-20Hz作為截止頻率。當我實現濾波器并使用lfilter在時域中再現信號時,正確的頻率被過濾了。在
為了消除相移,我用scipy.signal.freqz繪制了頻率響應圖,返回值為firwin的h作為分子,1作為預定義的denumerator。
如freqz文檔中所述,我還繪制了相位(=doc中的角度),并且能夠查看頻率響應圖,以獲得濾波信號頻率600hz的相移。在
所以相位延遲t
tΒp=—(Tetha(w))/(w)
不幸的是,當我把這個相位延遲加到濾波信號的時間數據中時,它并沒有得到與原始600hz信號相同的相位。在
我加了密碼。奇怪的是,在消除部分代碼以保持最小值之前,過濾后的信號以正確的振幅開始-現在情況更糟了。在################################################################################
#
# Filtering test
#
################################################################################
#
from math import *
import numpy as np
from scipy import signal
from scipy.signal import firwin, lfilter, lti
from scipy.signal import freqz
import matplotlib.pyplot as plt
import matplotlib.colors as colors
################################################################################
# Nb of frequencies in the original signal
nfrq = 2
F = [60,80]
################################################################################
# Sampling:
nitper = 16
nper = 50.
fmin = np.min(F)
fmax = np.max(F)
T0 = 1./fmin
dt = 1./fmax/nitper
#sampling frequency
fs = 1./dt
nyq_rate= fs/2
nitpermin = nitper*fmax/fmin
Nit = int(nper*nitpermin+1)
tps = np.linspace(0.,nper*T0,Nit)
dtf = fs/Nit
################################################################################
# Build analytic signal
# s = completeSignal(F,Nit,tps)
scomplete = np.zeros((Nit))
omg1 = 2.*pi*F[0]
omg2 = 2.*pi*F[1]
scomplete=scomplete+np.sin(omg1*tps)+np.sin(omg2*tps)
#ssingle = singleSignals(nfrq,F,Nit,tps)
ssingle=np.zeros((nfrq,Nit))
ssingle[0,:]=ssingle[0,:]+np.sin(omg1*tps)
ssingle[1,:]=ssingle[0,:]+np.sin(omg2*tps)
################################################################################
## Construction of the desired bandpass filter
lowcut = (60-2) # desired cutoff frequencies
highcut = (60+2)
ntaps = 451 # the higher and closer the signal frequencies, the more taps for the filter are required
taps_hamming = firwin(ntaps,[lowcut/nyq_rate, highcut/nyq_rate], pass_zero=False)
# Use lfilter to get the filtered signal
filtered_signal = lfilter(taps_hamming, 1, scomplete)
# The phase delay of the filtered signal
delay = ((ntaps-1)/2)/fs
plt.figure(1, figsize=(12, 9))
# Plot the signals
plt.plot(tps, scomplete,label="Original signal with %s freq" % nfrq)
plt.plot(tps-delay, filtered_signal,label="Filtered signal %s freq " % F[0])
plt.plot(tps, ssingle[0,:],label="original signal %s Hz" % F[0])
plt.grid(True)
plt.legend()
plt.xlim(0,1)
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
# Plot the frequency responses of the filter.
plt.figure(2, figsize=(12, 9))
plt.clf()
# First plot the desired ideal response as a green(ish) rectangle.
rect = plt.Rectangle((lowcut, 0), highcut - lowcut, 5.0,facecolor="#60ff60", alpha=0.2,label="ideal filter")
plt.gca().add_patch(rect)
# actual filter
w, h = freqz(taps_hamming, 1, worN=1000)
plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="designed rectangular window filter")
plt.xlim(0,2*F[1])
plt.ylim(0, 1)
plt.grid(True)
plt.legend()
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain')
plt.title('Frequency response of FIR filter, %d taps' % ntaps)
plt.show()'
總結
以上是生活随笔為你收集整理的python产生fir滤波器_Python中使用FIR滤波器firwin后信号的相移的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery 手型 鼠标穿过时_JS实现
- 下一篇: halt库卡_KUKA库卡机器人编程之字