MATLAB 和 Python 之间的不同频谱图
Different spectrogram between MATLAB and 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:
import numpy as np
from matplotlib import mlab
data = range(1,1000) #Dummy data. Just for testing
Fs = 8000
tWindow = 64e-3
NWindow = Fs*tWindow
window = np.hamming(NWindow)
NFFT = 512
NOverlap = NWindow/2
[s, f, t] = mlab.specgram(data, NFFT = NFFT, Fs = Fs, window = window, noverlap = NOverlap)
这是我在两次执行中得到的结果:
http://i.imgur.com/QSPvYsC.png
(两个程序中的F和T变量完全一样)
很明显它们是不同的;事实上,Python 执行甚至不会 return 复数。可能是什么问题呢?有什么办法可以修复它或者我应该使用另一个频谱图函数吗?
非常感谢您的帮助。
在matplotlib
中,specgram
默认returns功率谱密度(mode='PSD
')。在MATLAB
中,spectrogram
默认returns短时傅里叶变换,除非nargout==4
,在这种情况下它也会计算PSD
。要使 matplotlib
行为与 MATLAB
行为匹配,请设置 mode='complex'
我在 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:
import numpy as np
from matplotlib import mlab
data = range(1,1000) #Dummy data. Just for testing
Fs = 8000
tWindow = 64e-3
NWindow = Fs*tWindow
window = np.hamming(NWindow)
NFFT = 512
NOverlap = NWindow/2
[s, f, t] = mlab.specgram(data, NFFT = NFFT, Fs = Fs, window = window, noverlap = NOverlap)
这是我在两次执行中得到的结果:
http://i.imgur.com/QSPvYsC.png
(两个程序中的F和T变量完全一样)
很明显它们是不同的;事实上,Python 执行甚至不会 return 复数。可能是什么问题呢?有什么办法可以修复它或者我应该使用另一个频谱图函数吗?
非常感谢您的帮助。
在matplotlib
中,specgram
默认returns功率谱密度(mode='PSD
')。在MATLAB
中,spectrogram
默认returns短时傅里叶变换,除非nargout==4
,在这种情况下它也会计算PSD
。要使 matplotlib
行为与 MATLAB
行为匹配,请设置 mode='complex'