比较 Octave 和 SciPy 的 freqz 时,为什么绘图会翻转?

问题描述 投票:0回答:2

当我绘制这些系数的响应时:

b=[1.01063287e+00, -1.46490341e+01,  9.94030209e+01, -4.19168764e+02, ...
  1.22949513e+03, -2.66000588e+03,  4.39112431e+03, -5.64225597e+03, ...
  5.70320516e+03, -4.55022454e+03,  2.85602975e+03, -1.39550096e+03, ...
  5.20372994e+02, -1.43160328e+02,  2.74037105e+01, -3.26098385e+00, ...
  1.81735269e-01];
a=[1.00000000e+00, -1.45159238e+01,  9.86464912e+01, -4.16614074e+02, ...
  1.22391361e+03, -2.65216678e+03,  4.38533779e+03, -5.64421414e+03, ...
  5.71487734e+03, -4.56742504e+03,  2.87187255e+03, -1.40575405e+03, ...
  5.25150201e+02, -1.44741759e+02,  2.77584882e+01, -3.30950845e+00, ...
  1.84797453e-01];

八度音程(没问题):

  [h(:,1), w] = freqz(flip(b), flip(a),2048);
  
  plot((w/pi),20*log10(abs(h(:,1))));
  hold on;

Python 中:

    h = np.zeros(2048, float)
    w = np.zeros(2048, float)

    [h[:], w] = freqz(np.flip(b), np.flip(a), 2048)

    plt.plot((w/np.pi),20*np.log10(h[:]));
    plt.show()

,其他方面的结果大致相等,但从图中可以看出,与 Octave 的结果相比,后一个响应的绘制类似于两倍。

发生了什么事以及如何从 Python 中使用 Octave 获得相等的图?我尝试从 Python 代码中删除 Flip 命令,但是响应保持不变。

matplotlib plot scipy octave
2个回答
1
投票

scipy.signal.freqz
返回 (w,h),而不是您期望的 (h,w)。

这应该有效:

w, h = freqz(np.flip(b), np.flip(a), 2048)

plt.plot(w/np.pi,20*np.log10(np.abs(h)))
plt.show()

1
投票

来自

scipy.signal.freqz
的文档:

Returns
-------
w : ndarray
    The frequencies at which `h` was computed, in the same units as `fs`.
    By default, `w` is normalized to the range [0, pi) (radians/sample).
h : ndarray
    The frequency response, as complex numbers.


[...]


Notes
-----
Using Matplotlib's :func:`matplotlib.pyplot.plot` function as the callable
for `plot` produces unexpected results, as this plots the real part of the
complex transfer function, not the magnitude.
Try ``lambda w, h: plot(w, np.abs(h))``.
© www.soinside.com 2019 - 2024. All rights reserved.