4

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?

tamarlev
  • 71
  • 2
  • 6
  • in principle yes ... this is a major benefit of the Fourier Transform ... it always has an inverse Fourier Transform ... time domain --> frequency domain --> time domain --> frequency domain ...I have a project where I use this to convert an image into audio then from audio back to an image which matches the source input image ... keep your chin up and continue to do battle ... good luck – Scott Stensland Oct 16 '19 at 11:01
  • 1
    Yes, see https://stackoverflow.com/questions/56931834/creating-wave-data-from-fft-data/57323359#57323359 – Jon Nordby Oct 23 '19 at 19:03

1 Answers1

6

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.

Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62