File Exchange

image thumbnail

Short-Time Fourier Transform (STFT) with Matlab

version (326 KB) by Hristo Zhivomirov
Time-Frequency analysis via Short-Time Fourier Transform (STFT).


Updated 07 Oct 2018

View License

The present code is a Matlab function that provides a Short-Time Fourier Transform (STFT) of a given signal x[n]. The function is an alternative of the Matlab command “spectrogram”. The output of the function is:
1) a matrix with the complex STFT coefficients with time across the columns and frequency across the rows;
2) a frequency vector;
3) a time vector.

An example is given in order to clarify the usage of the function. For convenience, the input and output arguments are given in the beginning of the function.

The code is based on the theory described in:

[1] J. Benesty, M. Sondhi, Y. Huang. Springer Handbook of Speech Processing. Berlin, Springer, 2008.

[2] J. Smith. Spectral Audio Signal Processing. W3K Publishing, 2011.

[3] T. Dutoit, F. Marquґes. Applied Signal Processing: A MATLAB-Based Proof of Concept. New York, Springer, 2009.

Comments and Ratings (37)

jan Kowsal

Intan Putri

Hi Brittny!
Of course there is a way! Just remove the "view(0, 90)" line in the example.m file.

Hristo Zhivomirov

Is there anyway that you could provide code to turn the plot in to a 3D plot instead of a 2D plot? Thanks.

Thanks alot. This is very good explanation to spectogram and short-fft

Sourin Dey

AJ Geiger

Great work, very interesting, I'm a huge fan of Springer. If I was an employer I would hire you on the spot. I can tell you put a lot of time and effort into this. For what it's worth Matlab sells there signal processing toolbox for thousands of dollars. So @Chris I can think of a few thousands of reasons why.

-- Alex Geiger


Chris (view profile)

What's the point of this? If I had the Matlab signal processing toolbox - I could just use the Spectrogram function. Since this tools requires the signal processing toolbox - it's not very helpful to me.


RRP (view profile)

hi how to use stft in a wave file of sound signal?


Hi, I applied STFT on my data, then apply the filter, and now I want to use ISTFT. because of removing some frequencies the original size of STFT result is changed, and I can not use this code (istft).. what is your suggestion?

Luca Cialdi

Hi Ali! You can find the dominant frequency of the signal using my code at:

Please, put this code lines at the end of it:

% find the dominant frequencies
[Smax, inds] = max(SA, [], 1);
fmax = f(inds);

Best regards,
Hristo Zhivomirov

Mario Aldag

I imported this function to github for further improvements:

hiHristo Zhivomirov,

I want to get fundamental components of current and voltage signals using it possible through this code?
i want to save the coefficients of fundamental components.

Aitor Escolar

It doesn't work for me (Matlab 2015a)

allll mmmmm

heehyeon kim

thanks a lot


I found this utility because I do not have the signal processing toolbox, so I cannot use 'spectrogram'. But to run this code you need the signal processing toolbox anyway (it uses the 'hamming' window command). So...

luong dang

Great, thanks

xie jiang


Bruno Kempf

Solved my problems. Thanks a lot!

Thank you. Very straight forward and easy to understand.and i really need it

Hi Luthfi! Thank you for the interest! Be aware - the stft matrix contains a complex numbers (the complex spectrum). There is no such thing like "positive" or "negative" complex number. If you want to get only the amplitude spectrum type abs(stft) and the values will be all positive.

Best regards,
Hristo Zhivomirov

Luthfi Azhari

This works really well. However, I'm a bit confused; I'm always told that spectrograms have positive values. So why does the function give negative values in all cells (of its' stft variable) when inspected in the workspace/variable editor? Forgive my lack of understanding on the matter, I hope someone here would enlighten me


Ist (view profile)

just what I need for a shorcut... Thank you very much!

Hi, MatlabUser. Thank you for asking this question! As Michael Schwartz said, my code is a straight forward and easy to understand implementation of the STFT algorithm. In advance the code is a fully compatible with one for inverse STFT:


You say it is similar to Malab's "spectrogram" command. What is the difference between the two?

Thank you. Very straight forward and easy to understand. Exactly what I needed. Thanks!


jim (view profile)


Change of the title of the submission.

A new version of the code has been uploaded.

A new version of the code has been uploaded.

A new version of the code has been uploaded.

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor