简体   繁体   English

频率响应Scipy.signal

[英]Frequency Response Scipy.signal

I'm learning digital signal processing to implement filters and am using python to easily implement a test ideas. 我正在学习数字信号处理以实现过滤器,并使用python轻松实现测试思路。 So I just started using the scipy.signal library to find the impulse response and frequency response of different filters. 所以我刚开始使用scipy.signal库来查找不同滤波器的脉冲响应和频率响应。

Currently I am working through the book "Digital Signals, Processors and Noise by Paul A. Lynn (1992)" (and finding it an amazing resource for learning this stuff). 目前我正在阅读“保罗·林恩(1992)的数字信号,处理器和噪声”一书(并且发现它是学习这些东西的一个惊人的资源)。 In this book they have a filter with the transfer functions shown below: 在本书中,他们有一个过滤器,其传递函数如下所示:

I divided the numerator and denominator by 我将分子和分母除以 in order to get the following equation: 为了得到以下等式:

I then implemented this with Scipy using: 然后我使用Scipy实现了这个:

NumeratorZcoefs = [1, -1, 1, -1]
DenominatorZcoefs = [1, 0.54048, -0.62519, -0.66354, 0.60317, 0.69341]

FreqResponse = scipy.signal.freqz(NumeratorZcoefs, DenominatorZcoefs)
fig = plt.figure(figsize = [8, 6])
ax = fig.add_subplot(111)
ax.plot(FreqResponse[0], abs(np.array(FreqResponse[1])))
ax.set_xlim(0, 2*np.pi)
ax.set_xlabel("$\Omega$")

and produce the plot shown below: 并生成如下图:

绘图显示由Scipy.sigal.freqz计算的频率响应

However in the book the frequency response is shown to be the following: 然而,在本书中,频率响应显示如下:

绘图显示上面引用的书中的频率响应

They are the same shape but the ratio of the peaks at ~2.3 and 0.5 are very different for the 2 plots, could someone suggest why this is? 它们具有相同的形状,但是对于2个图,峰值在~2.3和0.5处的比例非常不同,有人可能会说明为什么会这样吗?

Edit: 编辑:

To add to this, I've just implemented a function to calculate the frequency response by hand (by calculating the distance from the poles and zeros of the function) and I get a similar ratio to the plot generated by scipy.signal, however the numbers are not the same, does anyone know why this might by? 为了补充一点,我刚刚实现了一个函数来手动计算频率响应(通过计算函数的极点和零点的距离),并得到与scipy.signal生成的图形相似的比率,但是数字不一样,有谁知道为什么会这样?

Implementation is as follows: 实施如下:

def H(omega):
    z1 = np.array([0,0]) # zero at 0, 0
    z2 = np.array([0,0]) # Another zero at 0, 0
    z3 = np.array([0, 1]) # zero at i
    z4 = np.array([0, -1]) # zero at -i
    z5 = np.array([1, 0]) # zero at 1

    z = np.array([z1, z2, z3, z4, z5])

    p1 = np.array([-0.8, 0])
    p = cmath.rect(0.98, np.pi/4)
    p2 = np.array([p.real, p.imag])
    p = cmath.rect(0.98, -np.pi/4)
    p3 = np.array([p.real, p.imag])
    p = cmath.rect(0.95, 5*np.pi/6)
    p4 = np.array([p.real, p.imag])
    p = cmath.rect(0.95, -5*np.pi/6)
    p5 = np.array([p.real, p.imag])

    p = np.array([p1, p2, p3, p4, p5])

    a = cmath.rect(1,omega)
    a_2dvector = np.array([a.real, a.imag])

    dz = z-a_2dvector
    dp = p-a_2dvector

    dzmag = []
    for dis in dz:
           dzmag.append(np.sqrt(dis.dot(dis)))

    dpmag = []
    for dis in dp:
           dpmag.append(np.sqrt(dis.dot(dis)))        

    return(np.product(dzmag)/np.product(dpmag))

I then plot the frequency response like so: 然后我像这样绘制频率响应:

omegalist = np.linspace(0,2*np.pi,5000)
Hlist = []

for omega in omegalist:
    Hlist.append(H(omega))

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(omegalist, Hlist)
ax.set_xlabel("$\Omega$")
ax.set_ylabel("$|H(\Omega)|$")

and get the following plot: 得到以下情节:

通过手动计算频率响应得出的图。

The SciPy generated frequency response is correct. SciPy生成的频率响应是正确的。 In any case, I wouldn't trust the book's figure which appears to have been drawn by hand. 无论如何,我不相信这本书似乎是手工绘制的。

If you want to find the frequency response "manually", this can be simply done by defining a function returning the original Z-transform and evaluating it on the unit circle as follows 如果你想“手动”找到频率响应,可以通过定义一个返回原始Z变换并在单位圆上进行评估的函数来完成,如下所示

def H(z):
    num = z**5 - z**4 + z**3 - z**2
    denom = z**5 + 0.54048*z**4 - 0.62519*z**3 - 0.66354*z**2 + 0.60317*z + 0.69341
    return num/denom

import numpy as np
import matplotlib.pyplot as plt

w_range = np.linspace(0, 2*np.pi, 1000)
plt.plot(w_range, np.abs(H(np.exp(1j*w_range))))

The result is exactly the same as SciPy. 结果与SciPy完全相同。

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

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