Reputation: 53
As in my last question AttributeError: 'module' object has no attribute 'spectrogram', I'm writing a little python script which reads a Wav Audio File, computes the spectrogram and writes the corresponding data back into a Wav File again. Here's a bit of code:
Scipy Code:
windowSize = 512
nOverlap_py = 160
nFFT = 1024
Fpy,Tpy,Spy=signal.spectrogram(s_orig_py,fs=fs_rate,window='hamming',nperseg=
windowSize,noverlap=nOverlap_py,nfft=nFFT,detrend=False,mode='complex')
The First 10 Elements of Scipy:
The Last 10 Elements of Scipy:
Matlab Code:
[S,F,T] = spectrogram(s_orig,window,nOverlap,fftParams.nFFT,fs);
The First 10 Elements of Matlab:
The Last 10 Elements of Matlab:
In both cases the parameters are the same. There is just a little problem: The values of Spy are not even close to the values of S and I don't understand why. I get that they can't be identical due the fact that both functions use a different algorithm to compute the FFT but as I mentioned before they are not even close.
Furthermore with Matlab, the resulting Wav File sounds 'exactly' the same as the original. With python it's just noise.
Upvotes: 2
Views: 1658
Reputation: 26
Before answering your question, may I ask you which library are you using to read a wav input scipy.io.wavfile?
Actually, I recently came across this mismatch in values and back traced the problem to scipy.io.wavfile
In Matlab, usually audioread(fname) is used. The equivalent read library is pySoundfile as given in this post. (Also i used it and got same matrices from Matlab and Python) How to read a audio file in Python similar to Matlab audioread?
Upvotes: 0