MATLAB与Python的谱图差异

2024-09-30 20:28:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个MATLAB程序,我想移植到Python。问题是在它中我使用了内置的spectrogram函数,尽管matplotlib specgram函数看起来是一样的,但是当我同时运行这两个函数时,得到的结果却不同。在

这就是我一直在运行的代码。在

MATLAB软件:

data = 1:999; %Dummy data. Just for testing.

Fs = 8000; % All the songs we'll be working on will be sampled at an 8KHz rate

tWindow = 64e-3; % The window must be long enough to get 64ms of the signal
NWindow = Fs*tWindow; % Number of elements the window must have
window = hamming(NWindow); % Window used in the spectrogram

NFFT = 512;
NOverlap = NWindow/2; % We want a 50% overlap

[S, F, T] = spectrogram(data, window, NOverlap, NFFT, Fs);

Python:

^{pr2}$

这是我两次执行的结果:

http://i.imgur.com/QSPvYsC.png

(两个程序中的F和T变量完全相同)

很明显,它们是不同的;事实上,Python的执行甚至不返回复数。有什么问题吗?有没有办法解决它,或者我应该使用另一个光谱功能?在

非常感谢你的帮助。在


Tags: ofthe函数程序databewindowfs
1条回答
网友
1楼 · 发布于 2024-09-30 20:28:17

matplotlib中,specgram默认返回功率谱密度(mode='PSD)。在MATLAB中,spectrogram默认返回短时傅立叶变换,除非nargout==4,在这种情况下,它还计算PSD。要使matplotlib行为与MATLAB行为相匹配,请设置mode='complex'

相关问题 更多 >