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

93 Downloads

Updated 28 Feb 2019

View Version History

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

Comments and Ratings (57)

Hristo Zhivomirov

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.

All best,
Hristo

Ali Almakrami

Hello Hristo,
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?

guang xu

Hristo Zhivomirov

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.

All best,
Hristo

Mojisola Jimoh

Hi Hristo.
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

Le Long

Kevin Santana Araujo

CHUNG-HAO CHAO

jhonatan Caicedo

Carlos Revelo

Muhammad Bintang

Nahom Nigatu

Hristo Zhivomirov

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

All best,
Hristo

Nahom Nigatu

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

Hristo Zhivomirov

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

Nahom Nigatu

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

Hristo Zhivomirov

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

Leon Gunther

I received an errror message

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

>> stft
Not enough input arguments.

Yumou Wei

jan Kowsal

Intan Putri

Jacobus Hanekom

Hristo Zhivomirov

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

Regards,
Hristo Zhivomirov

Brittny Freeman

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

Kyriakos Skaramangas

mohammad karami

selcuk caglar

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?

shuangyong zhuang

Luca Cialdi

Hristo Zhivomirov

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.

Aitor Escolar

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

allll mmmmm

heehyeon kim

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

Filipa dos Santos

Great, thanks

xie jiang

thx

Johan Ibrahimowich

Bruno Kempf

Solved my problems. Thanks a lot!

aris damarsyah

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

Hristo Zhivomirov

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

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

Jamiullh Khan

Hristo Zhivomirov

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?

Michael Schwartz

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

jim

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!