Examples

Simple Example

Loading a file and calculating the spectrogram.

import stft
import scipy.io.wavfile as wav

fs, audio = wav.read('input.wav')
specgram = stft.spectrogram(audio)

See also

module stft.spectrogram()

Back and Forth Example

Loading a file and calculating the spectrogram, its inverse and saving the result.

import stft
import scipy.io.wavfile as wav

fs, audio = wav.read('input.wav')
specgram = stft.spectrogram(audio)
output = stft.ispectrogram(specgram)
wav.write('output.wav', fs, output)

Passing multiple transfer functions

stft.spectrogram() and stft.ispectrogram() allow passing multiple transform functions as a list.

STFT will pick each transform for each frame it processes, the list of transforms will be extended indefinitely for as long as many frames need to be processed.

import stft
import scipy.io.wavfile as wav

fs, audio = wav.read('input.wav')
specgram = stft.spectrogram(audio, transform=[scipy.fftpack.fft, numpy.fft.fft])
output = stft.ispectrogram(specgram, transform=[scipy.fftpack.ifft, numpy.fft.ifft])
wav.write('output.wav', fs, output)

In this case, each frame will be processed using scipy.fftpack.fft, then numpy.fft.fft, then scipy.fftpack.fft again etc.

Saving Settings Example

You do not need to pass the same settings to stft.spectrogram() and stft.ispectrogram() twice as the settings are saved in the array itself.

import stft
import scipy.io.wavfile as wav

fs, audio = wav.read('input.wav')
specgram = stft.spectrogram(audio, framelength=512, overlap=4)
output = stft.ispectrogram(specgram)
wav.write('output.wav', fs, output)

See also

modules stft.spectrogram() stft.ispectrogram() stft.types.SpectogramArray