python - 在Python中使用FIR滤波器firwin后信号的相移

标签 python scipy filtering

所以在我最后两个问题之后,我遇到了我的实际问题。也许有人发现我的理论程序有错误,或者我在编程中做错了什么。

我正在Python中使用scipy.signal(使用firwin函数)实现带通滤波器。我的原始信号由两个频率组成(w_1=600Hz,w_2=800Hz)。可能有更多的频率,这就是我需要带通滤波器的原因。

在本例中,我想过滤 600 Hz 左右的频段,因此我将 600 +/- 20Hz 作为截止频率。当我实现滤波器并使用 lfilter 在时域中再现信号时,正确的频率被过滤。

为了消除相移,我使用 scipy.signal.freqz 绘制了频率响应,其中 firwin 的返回 h 作为分子,1 作为预定义的分子。 正如 freqz 文档中所述,我还绘制了相位(== 文档中的角度),并且能够查看频率响应图以获取滤波信号频率 600 Hz 的相移。

所以相位延迟t_p为

t_p=-(Tetha(w))/(w)

不幸的是,当我将此相位延迟添加到滤波信号的时间数据中时,它没有获得与原始 600 Hz 信号相同的相位。

我添加了代码。奇怪的是,在消除代码的某些部分以保持最小值之前,滤波后的信号以正确的幅度开始 - 现在情况更糟。

################################################################################
#
# 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()'

最佳答案

FIR 滤波器的延迟仅为 0.5*(n - 1)/fs,其中 n 是滤波器系数(即“抽头”)的数量, fs 是采样率。您对这种延迟的实现没有问题。

问题是您的时间值数组tps不正确。看一看 在 1.0/(tps[1] - tps[0]);你会发现它不等于fs

更改此:

tps  = np.linspace(0.,nper*T0,Nit)

例如:

T = Nit / fs
tps  = np.linspace(0., T, Nit, endpoint=False)

原始和滤波后的 60 Hz 信号图将完美地排列在一起。


有关另一个示例,请参阅 http://wiki.scipy.org/Cookbook/FIRFilter 。 在该脚本中,延迟是在第 86 行计算的。在此之下,延迟用于绘制与滤波信号对齐的原始信号。

注意:说明书示例使用scipy.signal.lfilter来应用过滤器。更有效的方法是使用 numpy.convolve。

关于python - 在Python中使用FIR滤波器firwin后信号的相移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22687585/

相关文章:

python - 如何将耗时添加到列表中

python - 我可以在 django-tables2 中制作二维表吗?

python - 使用 python 对音频样本应用过滤器

python - 使用PIL,python过滤部分图像

python - 为什么程序中的列表会给出意外的输出?

python - 在 Python 中浏览字典

python - Python 中的多元正态 CDF

python - 从 `scipy.ndimage.labels` 结果中删除计数较低的标签

json - JQ:筛选键

matlab - 在 MATLAB 中设计窄带通滤波器