python - 为什么我的加窗 sinc 函数具有非线性相位?

标签 python filtering signal-processing lowpass-filter

与网络上的许多教程类似,我尝试使用以下 python 函数实现窗口正弦低通滤波器:

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 处。

最佳答案

阻带中的幅度过零。过零后系数的相位会跳跃 180 度,这会混淆 np.angle()/np.unwrap()。 -1*180° = 1*0°

关于python - 为什么我的加窗 sinc 函数具有非线性相位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41473545/

相关文章:

django:过滤对象列表

matlab - 如何使用Matlab查找声音强度?

python - Django - 在设置中使用反向 url 映射

python - os.walk() 缓存/加速

.net - .net 中 Python 的 urllib 等效项

python - 如何防止从 Python 向 MySQLdb 语句中插入额外的引号?

search - 如何过滤掉elasticsearch中不存在的字段?

Java servlet 过滤器丢失格式信息,如何修复?

multithreading - 多线程波表合成器单击。这是由于阅读中断造成的吗?

ios - Swift FFT - 复杂的拆分问题