Is there any general-purpose form of short-time Fourier transform with corresponding inverse transform built into SciPy or NumPy or whatever?
There's the pyplot specgram
function in matplotlib, which calls ax.specgram()
, which calls mlab.specgram()
, which calls _spectral_helper()
:
#The checks for if y is x are so that we can use the same function to #implement the core of psd(), csd(), and spectrogram() without doing #extra calculations. We return the unaveraged Pxy, freqs, and t.
but
This is a helper function that implements the commonality between the 204 #psd, csd, and spectrogram. It is NOT meant to be used outside of mlab
I'm not sure if this can be used to do an STFT and ISTFT, though. Is there anything else, or should I translate something like these MATLAB functions?
I know how to write my own ad-hoc implementation; I'm just looking for something full-featured, which can handle different windowing functions (but has a sane default), is fully invertible with COLA windows (istft(stft(x))==x
), tested by multiple people, no off-by-one errors, handles the ends and zero padding well, fast RFFT implementation for real input, etc.
librosa.core.stft
andistft
look pretty similar to what I was looking for, though they didn't exist at the time:They don't invert exactly, though; the ends are tapered.
I'm a little late to this, but realised scipy has inbuilt istft function as of 0.19.0
Here is my Python code, simplified for this answer:
Notes:
blkproc
in Matlab. Instead of afor
loop, I apply a command (e.g.,fft
) to each frame of the signal inside a list comprehension, and thenscipy.array
casts it to a 2D-array. I use this to make spectrograms, chromagrams, MFCC-grams, and much more.istft
. In order to reconstruct the original signal the sum of the sequential window functions must be constant, preferably equal to unity (1.0). In this case, I've chosen the Hann (orhanning
) window and a 50% overlap which works perfectly. See this discussion for more information.A test:
If you have access to a C binary library that does what you want, then use http://code.google.com/p/ctypesgen/ to generate a Python interface to that library.