在网络上许多教程类似的,我已经尝试使用以下Python函数实现窗sinc函数的低通滤波器:为什么我的窗口函数具有非线性相位?
def black_wind(w):
''' blackman window of width w'''
samps = np.arange(w)
return (0.42 - 0.5 * np.cos(2 * np.pi * samps/ (w-1)) + 0.08 * np.cos(4 * np.pi * samps/ (w-1)))
def lp_win_sinc(tw, fc, n):
''' lowpass sinc impulse response
Parameters:
tw = approximate transition width [fraction of nyquist freq]
fc = cutoff freq [fraction of nyquest freq]
n = length of output.
Returns:
s = impulse response of windowed-sinc filter appended zero-padding
to make len(s) = n
'''
m = int(np.ceil(4./tw/2) * 2)
samps = np.arange(m+1)
shift = samps - m/2
shift[m/2] = 1
h = np.sin(2 * np.pi * fc * shift)/shift
h[m/2] = 2 * np.pi * fc
h = h * black_wind(m+1)
h = h/h.sum()
s = np.zeros(n)
s[:len(h)] = h
return s
对于输入:“TW = 0.05”,“FC = 0.2”,' n = 6000',fft的幅度似乎是合理的。
tw = 0.05
fc = 0.2
n = 6000
lp = lp_win_sinc(tw, fc, n)
f_lp = np.fft.rfft(lp)
plt.figure()
x = np.linspace(0, 0.5, len(f_lp))
plt.plot(x, np.abs(f_lp))
magnitude of lowpass filter response
然而,相位是以上〜FC非线性的。
plt.figure()
x = np.linspace(0, 0.5, len(f_lp))
plt.plot(x, np.unwrap(np.angle(f_lp)))
phase of lowpass filter response
鉴于脉冲响应的非零填充部分的对称性,我希望得到的相位是线性的。有人可以解释发生了什么吗?也许我错误地使用了numpy函数,或者我的期望不正确。我非常感谢任何帮助。
***********************编辑********************** *
基于对这个问题的一些有用的评论和一些更多的工作,我写了一个产生零相位延迟的函数,因此更容易解释np.angle()结果。
def lp_win_sinc(tw, fc, n):
m = int(np.ceil(2./tw) * 2)
samps = np.arange(m+1)
shift = samps - m/2
shift[m/2] = 1
h = np.sin(2 * np.pi * fc * shift)/shift
h[m/2] = 2 * np.pi * fc
h = h * np.blackman(m+1)
h = h/h.sum()
s = np.zeros(n)
s[:len(h)] = h
return np.roll(s, -m/2)
这里的主要变化是使用np.roll()将对称线置于t = 0处。
我投票结束这个问题作为题外话,因为它更适合http://dsp.stackexchange.com – mtrw