问题描述
我有以下MATLAB代码来计算信号的PSD:
I have the following MATLAB code to compute the PSD of a signal:
x = linspace(0, 10, 100001);
dt = x(2) - x(1);
Fs = 1 / dt;
a1 = 1;
f1 = 500;
a2 = 10;
f2 = 2000;
y = a1 * sin(2*pi*f1*x) + a2 * sin(2*pi*f2*x);
nblock=1024;
overlap=128;
windowsel=hann(nblock);
[Pxx,f]=pwelch(y,windowsel,overlap,nblock,Fs,'onesided');
figure()
semilogy(f,Pxx, '-o')
我尝试使用 scipy.signal
中的 welch
重现相同的计算结果.但是,对于低频,其行为显然不相同.我检查了汉宁窗口在两个窗口中是否相同.为了重现结果,我还可以更改其他什么参数?
I have tried to reproduce the same calculation using welch
in scipy.signal
. However, for low frequency, the behavior is clearly not the same. I have checked that the hanning window is the same in both. What other parameter can I change in order to reproduce the results?
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import welch, hanning
x = np.linspace(0, 10, 100001)
dt = x[1] - x[0]
fs = 1 / dt
a1 = 1
f1 = 500
a2 = 10
f2 = 2000
y = a1 * np.sin(2*np.pi*f1*x) + a2 * np.sin(2*np.pi*f2*x)
datos = y
nblock = 1024
overlap = 128
win = hanning(nblock, True)
f, Pxxf = welch(datos, fs, window=win, noverlap=overlap, nfft=nblock, return_onesided=True)
plt.semilogy(f, Pxxf, '-o')
plt.grid()
plt.show()
MATLAB:
PYTHON:
推荐答案
使用 detrend = False
可以解决此问题,如GitHub问题 https://github.com/scipy/scipy/issues/8045#issuecomment-337319294
The problem is solved when using detrend=False
as stated in the GitHub issue https://github.com/scipy/scipy/issues/8045#issuecomment-337319294
这篇关于等价scipy.signal welch到matlab pwelch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!