Friday, September 29, 2017

audio - Wav To Spectrogram, Back To Wav


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:--------------------------------------------------------------
Fpy,Tpy,Spy=signal.spectrogram(s_orig_py,fs=fs_rate,window=window_py,
noverlap=nOverlap_py,nfft=nFFT,detrend='constant',return_onesided=True,
scaling='spectrum',mode='complex')

#---------------------------------------------------------------------------
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)
wav.write('demo.wav',fs_rate,x_opt_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





No comments:

Post a Comment

digital communications - Understanding the Matched Filter

I have a question about matched filtering. Does the matched filter maximise the SNR at the moment of decision only? As far as I understand, ...