0

I am trying to replicate spectrogram function of matlab in python. But getting differnt result.

MATLAB:

NFFT=500
fs = 5000.0;
T = 0.2;
nsamples = T * fs;
t = 0:T/nsamples:T;
a = 0.02;
f0 = 600.0;
x = 0.1 * sin(2 * pi * 100 * sqrt(t));
x = x + 0.01 * cos(2 * pi * 312 * t + 0.1);
x = x + a * cos(2 * pi * f0 * t + .11);
x = x + 0.03 * cos(2 * pi * 2000 * t);
[s,f,t,p] = spectrogram(x,NFFT,NFFT/2,NFFT,5000);

PYTHON:

NFFT=500
fs = 5000.0
T = 0.2
nsamples = T * fs
t = np.linspace(0, T, nsamples, endpoint=False)
a = 0.02
f0 = 600.0
x = 0.1 * np.sin(2 * np.pi * 100 * np.sqrt(t))
x += 0.01 * np.cos(2 * np.pi * 312 * t + 0.1)
x += a * np.cos(2 * np.pi * f0 * t + .11)
x += 0.03 * np.cos(2 * np.pi * 2000 * t)

data, freqs, bins, im  = plt.specgram(x, NFFT = NFFT, noverlap = NFFT/2, Fs = 1, window=np.hamming(500))

Result:

MATLAB

S=
 0.0368969219138888 + 0.00000000000000i -0.00640287039709597 + 0.00000000000000i    -0.0181549472749261 + 0.00000000000000i
0.0369942577239430 + 0.00170365180409300i   -0.00641777088047979 + 0.00428573564618973i -0.0182063528070969 + 0.00274096675203278i
0.0372896370931328 + 0.00344259840091865i   -0.00646260590694859 + 0.00867058500148446i -0.0183624726101678 + 0.00557504096906337i
0.0377933861634262 + 0.00525402384041138i   -0.00653763975858664 + 0.0132575016923955i  -0.0186289946974940 + 0.00858859529669296i
0.0385234169373355 + 0.00717891972835675i   -0.00664254963166661 + 0.0181548506564059i  -0.0190141648512203 + 0.0118363853277666i
0.0395062707383408 + 0.00926391658745421i   -0.00677353364940275 + 0.0234699904806308i  -0.0195183133838138 + 0.0152501468340448i
0.0407785183683386 + 0.0115624333151930i    -0.00691159529875475 + 0.0292769375278352i  -0.0200647549689701 + 0.0183051374501858i
0.0423883860819913 + 0.0141330978976240i    -0.00697197074630109 + 0.0354985169054746i  -0.0200244774847677 + 0.0187115254551018i
0.0443976643813540 + 0.0170289306590550i    -0.00656079684499655 + 0.0414950924830313i  -0.0143283399420626 + 0.00681504947795196i
0.0468866463315933 + 0.0202566255549439i    -0.00362762076440728 + 0.0446491817744357i  0.0442385383621713 - 0.0533252107891292i
0.0499842193163613 + 0.0236392798816589i    0.0139330864345486 + 0.0366213115473641i    0.527338157145056 - 0.0836969198502891i 
...

PYTHON:

data = [[  6.86495745e-06   2.06731663e-07   1.66206330e-06]
 [  1.38317226e-05   6.00631470e-07   3.41874732e-06]
 [  1.41432665e-05   1.17941515e-06   3.71401683e-06]
 [  1.46835979e-05   2.20365273e-06   4.24391644e-06]
 [  1.54868467e-05   3.76908832e-06   5.05916002e-06]
 [  1.66060600e-05   6.01809511e-06   6.18763178e-06]
 [  1.81189701e-05   9.12625469e-06   7.43962653e-06]
 [  2.01354425e-05   1.31991294e-05   7.57505593e-06]
 [  2.28041802e-05   1.77993612e-05   2.53892889e-06]
 [  2.63093302e-05   2.02382047e-05   4.84156179e-05]
 [  3.08330710e-05   1.54834259e-05   2.87521702e-03]
 [  3.64954192e-05   1.39170326e-04   6.93036838e-02]
 [  4.41668850e-05   3.42923492e-03   6.18474716e-01]
 [  6.75599360e-05   4.29740823e-02   1.22383644e+00]
 [  2.45072168e-04   2.62533450e-01   5.03953385e-01]
 [  1.60544262e-03   6.69584111e-01   7.36184588e-02]
 [  9.56093743e-03   7.39616465e-01   7.15771700e-03]
 [  4.13991958e-02   4.64625686e-01   6.45374390e-04]
 [  1.18842376e-01 ...

Both are different. Any help? May be I have missed some configuration. Bu tI am not able to find it.

user3852441
  • 310
  • 3
  • 14
  • 2
    possible duplicate of [Different spectrogram between MATLAB and Python](http://stackoverflow.com/questions/31101987/different-spectrogram-between-matlab-and-python) – TheBlackCat Aug 07 '15 at 12:08
  • @TheBlackCat Thanks for your comment. I think you are right. But when I add mode='complex', I get error "ValueError: Cannot plot a complex specgram" – user3852441 Aug 07 '15 at 12:17
  • 1
    You need to use `matplotlib.mlab.specgram` to just get the values without plotting them. – TheBlackCat Aug 07 '15 at 12:59
  • @TheBlackCat Thanks for your comment. But how does MATALB plot STFT in spectrogram? – user3852441 Aug 07 '15 at 13:04
  • I think it uses the magnitude spectrum. You can get this in `matplotlib.specgram` by using the `mode='magnitude'` option. – TheBlackCat Aug 07 '15 at 14:15

0 Answers0