File Exchange

image thumbnail

STFT, MDCT and inverses. Onset and pitch detection

version (8.83 KB) by Antoine Liutkus
Short Time Fourier Transform, MDCT and their inverse. CQT. Onset and Pitch detection.


Updated 27 Aug 2012

View Version History

View License

Class name : Signal
handles basic Signal operations and transforms.
* wave loading
* STFT with any window and overlap ratio from 0 to 1
* MDCT and its inverse
* constant Q transform
* splitting into frames
* onset detection
* pitch detection

Main properties that are read/write
* s : signal
* windowLength (ms)
* nfft (samples)
* overlapRatio (>=0 and <1)
* S : stft data

Main properties that are read only :
* sLength : signal length
* nChans : number of channels
* nfftUtil : number of bins in the positive frequency domain
* framesPositions, nFrames : positions and number of frames
* sWin, sWeights : windowed data

example that produced the description figure :

% Create a Signal object from a wav file
s = Signal('linktomyfile.wav');

%set window length to 50ms
s.windowLength = 50;

%set window overlap to 75%
s.overlapRatio = 0.75;

%compute STFT

%display log-spectrogram
subplot 311
ylabel('frequency bin')
axis xy

%compute f0 between 200 and 500Hz
pitchs = s.mainPitch(200,500);

%display it
subplot 312
ylabel('f0 (Hz)')
grid on
title('f0 detection')

%Compute onsets that both appear and low and high frequencies
onsets = s.getOnsets(0,500).*s.getOnsets(6000,15000);

%display them
subplot 313
ylabel('onset presence')
grid on
title('onset detection')

Note that :
* all properties are automatically set relevantly in case of modifications. for example, when nfft is set, windowLength is changed accordingly
* the pitch detection algorithm is designed to work with a considerable amount of background superimposed to the lead signal. It should hence give reasonable results for popular music. Note that the lead signal ought to be more or less harmonic.

Cite As

Antoine Liutkus (2021). STFT, MDCT and inverses. Onset and pitch detection (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (19)

Foss Guru

Antoine Liutkus

Hi Stavros, thanks for your comment. I don't know about updating, are there some things on this class not working with the new version ? cheers

Stavros Skouras

Hi Antoine,
Thank you for your valuable contribution.
Are you planning on updating for more recent Matlab versions (e.g. 2015-2016)?

Rebecca Burrell

alexis benichoux

Antoine Liutkus

pitch detection function is back, along with MDCT and inverse

Antoine Liutkus

The version currently uploaded does not have the pitch detection function due to a mistake I made. I'll update it shortly

Antoine Liutkus

Hi, thanks for the comment/rating.

There are several possible reasons for your problem:
* the pitch detection system tries to find the most predominant and stable pitch within a background. It is mostly targeted for voice or harmonic signals. I have not really tested it for fundamental frequencies higher than 500-600Hz.
* If you have a knowledge that your fundamental frequency is likely to lie in some given bound, specify it the most tightly as you can. For example, looking for pitch from 100 to 500 may lead to catch the bass, whereas looking within [250-500] may catch the voice. This is due to the assumption that the pitch you are looking for has the higher energy.

If you provide me with your wavfile, I could give you some more hints

I hope this helps

cheers and good luck


Hi, your sharing attracts me very much, but the value of F0 between 200 and 500 eludes me, why? and I find that if I use 1000 or larger number to replace 500, F0 will be constant, can you help me?



Thanks for your kind answer!

Antoine Liutkus

Hi, yes the onset detection algorithm is described in :
author = {C. Duxbury and J.P. Bello and M. Davies and M. Sandler},
title = {Complex Domain Onset Detection for Musical Signals},
booktitle = {In Proc. Digital Audio Effects Workshop (DAFx},
year = {2003},
address = {London, UK},
month = sep

Concerning the pitch detection, I developped it it, but unfortunately I still didn't took time to publish it somewhere..

cheers, thanks for your interest


Are there some papers about it?If you have it,please give me a website.Thank you very much!


Thank you.My matlab version is 7.1.I will run it again with 2008a.

Antoine Liutkus

Hello, it seems to me that it must be a problem of Matlab version. What version do you use ? I think that object capabilities from this code come with 2008a


There's a error when I use "s = Signal('myfile.wav')".
Error is as followed:
??? Error: File: Signal.m Line: 62 Column: 9
Function definitions are not permitted at the prompt or in scripts.

MATLAB Release Compatibility
Created with R2010a
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!