gpt4 book ai didi

python - scipy.signal welch 与 matlab pwelch 等价

转载 作者:行者123 更新时间:2023-12-01 02:31:28 25 4
gpt4 key购买 nike

我有以下 MATLAB 代码来计算信号的 PSD:

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 重现相同的计算。然而,对于低频,行为显然不一样。我已经检查过两者的汉宁窗是相同的。为了重现结果,我还可以更改哪些其他参数?

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:

Matlab pwelch Matlab pwelch-zoom

Python: enter image description here

最佳答案

按照 GitHub 问题 https://github.com/scipy/scipy/issues/8045#issuecomment-337319294 中所述使用 detrend=False 即可解决问题

关于python - scipy.signal welch 与 matlab pwelch 等价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46775223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com