✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.

Short-Time Fourier Transform in MATLAB®

An example of basic audio analysis with the STFT Spectrogram in MATLAB®.


sigtext = urlread('https://raw.githubusercontent.com/plotly/documentation/master/aux/fft-matlab');

sig = str2num(sigtext);

fs = 44100;

dur = 1;

t = linspace(0,dur,fs);

N = 4096;

hop = N/4;

overlap = N - hop;

S = stft(sig,overlap);

maxFreq = N/8;

time = linspace(0,dur,size(S,2));

freq= linspace(0,fs*maxFreq/N,size(S(1:maxFreq,:),1));

clims = [-100 60];

fig = figure;
imagesc(time,freq,20*log10(abs(S(1:maxFreq,:))),clims)
colorbar
axis xy
xlabel('TIME (s.)')
ylabel('FREQUENCY (Hz.)')
title(['C4 GUITAR: MAGNITUDE SPECTROGRAM ANALYSIS']);

fig2plotly(fig);