简体   繁体   English

FFT过滤器vs Python中的lfilter

[英]FFT filter vs lfilter in python

I have some troubles when applying bandpass filter to signal in Python. 将带通滤波器应用于Python中的信号时遇到一些麻烦。 Have tried the following to things: 尝试了以下方法:

  • Do the box window "by hand", ie do the FFT on the signal, apply the filter with a box window and the do the IFFT to get back to the time domain. “手动”执行框窗口,即对信号执行FFT,对框窗口应用滤波器,然后执行IFFT以返回到时域。
  • Use the scipy.signal module where I use firwin2 to construct the filter and then lfilter to to the filtering. 使用scipy.signal模块,其中我使用firwin2构造过滤器,然后使用lfilter进行过滤。

Futhermore I have done the same filtering in the audio program Cool Edit and compared the result from the above two tests. 此外,我在音频程序Cool Edit中进行了相同的过滤,并比较了上述两个测试的结果。

As can be seen (I am a new user so I can not post my png fig), the results from the FFT and scipy.signal are very different. 可以看出(我是新用户,所以我不能张贴png图),FFT和scipy.signal的结果非常不同。 When compare to the result from Cool edit, the FFT is close, however not identical. 与Cool编辑的结果进行比较时,FFT接近,但是不完全相同。 Code as below: 代码如下:

# imports 
from pylab import *
import os
import scipy.signal as signal

# load data 
tr=loadtxt('tr6516.txt',skiprows=1)

sr = 500            # [samples/s]
nf = sr/2.0         # Nyquist frequence
W = 512            # Window widht for filtering
N=float(8192)       # Fourier settings
Ns = len(tr[:,0])   # Total number of samples

# Create inpulse responce from the filter
fv=12.25
w    =0.5
r    =0.5*w
Hz=[0, fv-w-r, fv-w, fv+w, fv+w+r, nf]
ff=[0, 0,      1,    1,    0,      0]
b = signal.firwin2(W,Hz,ff,nfreqs=N+1,nyq=nf)
SigFilter = signal.lfilter(b, 1, tr[:,1])

# Fourier transform
X1 = fft(tr[:,1],n=int(N))
X1 = fftshift(X1)
F1 = arange(-N/2.0,N/2.0)/N*sr

# Filter data
ff=[0,1,1,0]
fv=12.25
w    =0.5
r    =0.5*w
Hz=[fv-w-r,fv-w,fv+w,fv+w+r]
k1=interp(-F1,Hz,ff)+interp(F1,Hz,ff)
X1_f=X1*k1
X1_f=ifftshift(X1_f)
x1_f=ifft(X1_f,n=int(N))

Can anyone explain to me why this difference? 谁能向我解释为什么会有这种差异? The filtering in Cool edit has been done using the same settings as in scipy.signal (hanning window, window width 512). Cool编辑中的过滤已使用与scipy.signal中相同的设置完成(汉宁窗口,窗口宽度512)。 Or have I got this all totaly wrong. 还是我完全错了。

Best regards, Anders 最好的问候,安德斯

Above code: 上面的代码:

在此处输入图片说明

Compared with Cool Edit: 与酷编辑相比:

在此处输入图片说明

在此处输入图片说明

Small differences can be explained by the libraries using different algorithms that accumulate error slightly differently. 库可以使用不同的算法来解释小差异,这些算法会累积略有不同的错误。

For example, if you compute the DFT using a radix-2 FFT, a split-radix FFT and an ordinary DFT, the results will all be slightly different. 例如,如果使用基数2 FFT,分基数FFT和普通DFT计算DFT,则结果都将略有不同。 In fact the ordinary DFT has worse accuracy than all decent implementations of an FFT because it uses many more floating point operations, and thus it accumulates more error. 实际上,普通DFT的精度比FFT的所有体面实现都要差,这是因为它使用了更多的浮点运算,因此累积了更多的错误。

Could this explain the close (but not identical) results you are seeing? 这可以解释您看到的接近(但不完全相同)结果吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM