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:
 H. Zhivomirov. On the Development of STFT-analysis and ISTFT-synthesis Routines and their Practical Implementation. TEM Journal, ISSN: 2217-8309, DOI: 10.18421/TEM81-07, Vol. 8, No. 1, pp. 56-64, Feb. 2019. (http://www.temjournal.com/content/81/TEMJournalFebruary2019_56_64.pdf)
H. Zhivomirov. On the Development of STFT-analysis and ISTFT-synthesis Routines and their Practical Implementation. TEM Journal, ISSN: 2217-8309, DOI: 10.18421/TEM81-07, Vol. 8, No. 1, pp. 56-64, Feb. 2019. (http://www.temjournal.com/content/81/TEMJournalFebruary2019_56_64.pdf)
Hristo Zhivomirov (2021). Short-Time Fourier Transform (STFT) with Matlab (https://www.mathworks.com/matlabcentral/fileexchange/45197-short-time-fourier-transform-stft-with-matlab), MATLAB Central File Exchange. Retrieved .
Hi, Ali Almakrami! Thank you about your interest on my submission! You can analyse every sound file (e.g., .wav or .mp3) using the example .m file. You must put the file name on line 4 as follows: [x, fs] = audioread('name_of_the_sound_file.wav'); or [x, fs] = audioread('name_of_the_sound_file.mp3');. Also, you can check my other submission on https://mathworks.com/matlabcentral/fileexchange/45465-time-frequency-analysis-of-signals-with-matlab.
Thank for doing such a great job. Is it possible to paly a sound on this code if download a file named it 'track.wav'? If yes , how?
Hi Mojisola! The possible reason for this error is that the example.m file and the stft.m function are not in one and the same directory (folder). If you have any other troubles with the software, you can write me on my e-mail.
Thanks for the good work.
I used your example.m file but I keep getting the error 'undefined function or variable 'stft' . Can you please help?
Thanks in anticipation
Hello Nahom! Please, send me your files to my e-mail. I'll see what can I do about the topic...
Thank you very much for your reply. I have fs=10KHz and wlen=1024 (it should be a window size of 100ms) I expected an amplitude of 0.15 but i have got 0.11. I tried to change the wlen, if i decrease the value of wlen, the amp goes down but if i goese above 1024 the signal will distorted but there is no change in the amp. I do not know how can i compensate the differnce. Do you have some Idea? Thanks a gain for your support.
Hi Nahom! Thank you for the interest! Of course, you can use any window function available in Matlab, with any size smaller or equal to the signal length. Refer to example.m file, particularly the code line "win = blackman(wlen, 'periodic');", where wlen is the window size (length). Also you must scale the S matrix properly in order to obtain the real amplitude of the frequency components - code lines 16-31 in the example.m file.
I used your function and trying to deternine the max S and plot its amplitude and frequency but the amplitude is smaller than what i expected is ther any way to use window size in your function? Thanks!
You may not run stft.m file like a regular Matlab program. It is a user-defined function, and like any function it has input arguments. Please, refer to the example file about the proper usage of the stft function.
I received an errror message
Error in stft (line 26)
x = x(:);
Not enough input arguments.
Of course there is a way! Just remove the "view(0, 90)" line in the example.m file.
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
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
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.
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?
Hi Ali! You can find the dominant frequency of the signal using my code at: https://www.mathworks.com/matlabcentral/fileexchange/45465-time-frequency-analysis-of-non-stationary-signals-with-matlab-implementation
Please, put this code lines at the end of it:
% find the dominant frequencies
[Smax, inds] = max(SA, , 1);
fmax = f(inds);
I imported this function to github for further improvements:
I want to get fundamental components of current and voltage signals using STFT.is it possible through this code?
i want to save the coefficients of fundamental components.
It doesn't work for me (Matlab 2015a)
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...
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.
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
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: https://www.mathworks.com/matlabcentral/fileexchange/45577-inverse-short-time-fourier-transformation--istft--with-matlab-implementation
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!
Inspired: Signal Framing (Segmentation) with Matlab, Image Manipulation Toolbox, Short-time Cepstrum (Cepstrogram) with Matlab, Speech Recognition gui, Cross-Synthesis Example with Matlab Implementation, Inverse Short-Time Fourier Transform (ISTFT) with Matlab
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!