File Exchange

image thumbnail

Short-Time Fourier Transform (STFT) with Matlab

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

171 Downloads

Updated 28 Feb 2019

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] 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)

Cite As

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 (2019). 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 .

Comments and Ratings (52)

Le Long

Hello Nahom! Please, send me your files to my e-mail. I'll see what can I do about the topic...

All best,
Hristo

Hi Hristo,

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.

Regards,
Nahom

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.

Regards,
Hristo

Hello,
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!
Regards
Nahom

Hi Leon!
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.

All best,
Hristo

I received an errror message

Error in stft (line 26)
x = x(:);

>> stft
Not enough input arguments.

Yumou Wei

jan Kowsal

Intan Putri

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

Regards,
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

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

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

rabbasi

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: 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);

Best regards,
Hristo Zhivomirov

Mario Aldag

I imported this function to github for further improvements:
https://github.com/maldag/stft

hiHristo Zhivomirov,

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)

allll mmmmm

thanks a lot

Vittorio

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

thx

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

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

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

MatlabUser

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

Updates

6.0.0.0

A new reference literature has been added.

5.0.0.0

Change of the title of the submission.

4.0.0.0

A new version of the code has been uploaded.

3.0.0.0

A new version of the code has been uploaded.

2.0.0.0

A new version of the code has been uploaded.

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