0
$\begingroup$

I'm doing a signal processing to put data into my neural network.

I noticed a difference between MATLAB and Python spectrogram Value. I don't know why and ask questions.

This is MATLAB code

[y, Fs] = audioread('left (1).wav'); %1sec 16000 sample read

nwin  = 256; %16ms
novlp = 128; %8ms
nfft  = 256;

left = spectrogram(y, nwin, novlp, nfft, Fs, 'yaxis');

256-fft_point, 256 hamming window, 50% over lab, 16000 sampling rate

And This is Python code

import numpy as np
import soundfile as sf
from matplotlib import mlab

audio_sample, sampling_rate = sf.read("left (1).wav")

win = np.hamming(256)
Sxx, f, t = mlab.specgram(x=audio_sample, NFFT=256, Fs=sampling_rate, window=win, noverlap=128, mode='complex',
                          sides='default', detrend='none')

np.savetxt('C:/Users/HOME/Desktop/left_spec (1).txt', Sxx, delimiter=',')

It is also 256-fft_point, 256 hamming window, 50% over lab, 16000 sampling rate

However, the values are totally different as shown below.

enter image description here

In addition to the first four values, all values show different values.

When I read the wav file, the value seems to be the problem of the spectrogram function because both MATLAB and Python are the same.

I also changed the parameters of Python spectrogram function, tried post-processing such as Sxx = 10 * np.log10 (Sxx), and tried the spectrogram function of the scipy kit, but not all the same values as MATLAB.

Please help me, thank you for reading.

$\endgroup$
  • $\begingroup$ have you compared the documentations in Matlab and from matplotlib? my guess is that they simply do different things. $\endgroup$ – Marcus Müller Mar 5 at 9:19
  • $\begingroup$ You might want to check what data types are used in the derivation of the spectrogram too. $\endgroup$ – A_A Mar 5 at 10:34
  • $\begingroup$ Have you tried dividing the two? It looks to me like there might just be a constant factor between them, due to the way normalization is done. $\endgroup$ – Florian Mar 5 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.