Currently, I'm writing a Python script, which should do the following:

  • read an audio file respectively a wav file via scipy.io.wavfile.read().
  • calculate the spectrogram of given wav file.
  • write the data from spectrogram back into a wav file.

Here's a bit of Code:

# Define FFT params:-------------------------------------------------------
windowSize = 512
shiftSize = 160
nFFT = 1024
window_py = signal.hamming(windowSize)
nOverlap_py = windowSize-shiftSize

# Load wav file into memory:------------------------------------------------
fs_rate,s_orig = wav.read('demo.wav')

# Type Casting:-------------------------------------------------------------
s_orig_py = np.asarray(s_orig,dtype=np.float64)

# Spectrogram:--------------------------------------------------------------

P_py = np.angle(Spy)                    # Phase extraction:
X = np.absolute(Spy)                    # Needed for neural network!
X1 = X*np.cos(P_py)+1j*X*np.sin(P_py)   # "orig." spectrum. Needed for resyn

# Resynthesize to wav:------------------------------------------------------
X1 = np.append(X1,np.conjugate(X1[-1:1:-1,:]),axis=0)
x_opt_py = synthSpectrogram(X1,shiftSize,nFFT,window_py,nOverlap_py)

But there's is a huge problem: The data in "Spy" is not useable. When I try to write the data back in a wav file, the result is noisy respectively there's nothing at all. Furthermore, I have a Matlab file, which does the same as the code above and it works just fine and in both cases the parameters are the same.

The values in of S in Python and Matlab are not even close 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.

Matlab-Values enter image description here

Python-Values enter image description here


2 Answers 2


A spectrogram only contains magnitude info, not phase info.

That means half of the original's signal information is lost; thus, the original signal cannot be recovered.


Not sure any of those tips will help, but:

1) Values in Spy are complex numbers, so the spectogram does in fact contain phase info in your python code. I am not sure how you dumped it into float 2D array showed in the images, but those are not the actual elements in Spy.

2) Surely you converted complex values in Spy into reals by using its norm $\lVert \cdot \rVert$ or its $\lVert \cdot \rVert^2$ or some $\log$ as for showing dB values. If you do it differently in each languaje, you get quite different numbers.

3) FFT output (Spy) is related to a list of frequencies (Fpy), if mathlab gives other frequencies or just ordered differently, the output will look different when inspected value by value, but not when plotted as freq vs signal.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.