tamarlev
tamarlev

Reputation: 81

Is there a way to invert a spectrogram back to signal

In my algorithm I created a spectogram and did manipulation on the data:

import scipy.signal as signal

data = spio.loadmat(mat_file, squeeze_me=True)['records'][:, i]
data = data- np.mean(data)
data = data/ np.max(np.abs(data))
freq, time, Sxx = signal.spectrogram(data, fs=250000, window=signal.get_window("hamming", 480), nperseg=None, noverlap=360, nfft=480)
// ...
// manipulation on Sxx
// ...

Is there anyway to revert the freq, time, and Sxx back to signal?

Upvotes: 5

Views: 4379

Answers (1)

Bas Swinckels
Bas Swinckels

Reputation: 18488

No, this is not possible. To calculate a spectrogram, you divide your input time-domain signal into (half overlapping) chunks of data, which each are multiplied by an appropriate window function, after which you do a FFT, which gives you a complex vector that indicates the amplitude and phase for every frequency bin. Each column of the spectrogram is finally formed by taking the absolute square of one FFT (and normally you throw away the negative frequencies, since a PSD is symmetric for a real input signal). By taking the absolute square, you lose any phase information. This makes it impossible to accurately reconstruct the original time-domain signal.

Since your ear doesn't care about phase information (your brain would sense something similar as a spectrogram), it might however be possible to reconstruct a signal that sounds approximately the same. This could basically be done by doing all the described steps in reverse, while choosing a random phase for the FFT.

Note that there is one issue with your code: you create a variable named signal, which 'shadows' the scipy.signal module which you import with the same name.

Upvotes: 7

Related Questions