我有一个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的执行甚至不返回复数。有什么问题吗?有没有办法解决它,或者我应该使用另一个光谱功能?在
非常感谢你的帮助。在
在
matplotlib
中,specgram
默认返回功率谱密度(mode='PSD
)。在MATLAB
中,spectrogram
默认返回短时傅立叶变换,除非nargout==4
,在这种情况下,它还计算PSD
。要使matplotlib
行为与MATLAB
行为相匹配,请设置mode='complex'
相关问题 更多 >
编程相关推荐