Reputation: 73
I have code in matlab:
data = 1:999;
Fs = 8000;
tWindow = 64e-3;
NWindow = Fs*tWindow;
window = hamming(NWindow);
NFFT = 512;
NOverlap = NWindow/2;
[S, F, T,P] = spectrogram(data, window, NOverlap, NFFT, Fs);
and in python
import numpy as np
from matplotlib import mlab
data = range(1,1000)
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, mode = 'complex')
The above code base on examples that I found on the internet. The problem is that I can not obtain the same results from python and MatLab. Where can be a problem?
Upvotes: 1
Views: 812
Reputation: 3711
I believe you compare the wrong result parameters, namely S
to s
.
The MATLAB spectrogram
documentation says (variable names changed according to your examples).
S = spectrogram(data)
returns the short-time Fourier transform of the input signal,data
. Each column ofS
contains an estimate of the short-term, time-localized frequency content ofdata
.
and further
[___,P] = spectrogram(___)
also returns a matrix,P
, containing an estimate of the power spectral density (PSD) or the power spectrum of each segment.
For comparison, the matplotlib.mlab.specgram
documentation says
Returns
s
: array_like 2-D array, columns are the periodograms of successive segments.
What it means is that MATLAB spectrogram
return value P
and matplotlib.mlab.specgram
return value s
contain the power spectral density values and are the parameters to be compared. Note that you have to use mode=psd
as kwarg in mlab.specgram
.
So MATLAB:
[S, F, T, P] = spectrogram(data, window, NOverlap, NFFT, Fs);
>> P(1:10, :)
ans =
1.0e+04 *
0.308534266716801 1.231732513971400
0.151013695839005 0.487744349480272
0.000300936865940 0.000301512606065
0.000011558094657 0.000011638920865
0.000032287898006 0.000032310876043
0.000031582990508 0.000031591963531
0.000026545275922 0.000026549494058
0.000021658792166 0.000021661034379
0.000017687762496 0.000017689063802
0.000014586747930 0.000014587554750
and mlab.specgram
>>> [s, f, t] = mlab.specgram(data, NFFT=NFFT, Fs=Fs, window=window,
noverlap=NOverlap, mode='psd')
>>> s[0:9]
array([[3.08534267e+03, 1.23173251e+04],
[1.51013696e+03, 4.87744349e+03],
[3.00936866e+00, 3.01512606e+00],
[1.15580947e-01, 1.16389209e-01],
[3.22878980e-01, 3.23108760e-01],
[3.15829905e-01, 3.15919635e-01],
[2.65452759e-01, 2.65494941e-01],
[2.16587922e-01, 2.16610344e-01],
[1.76877625e-01, 1.76890638e-01]])
are equal.
Upvotes: 1