%% Spectrogram and Instantaneous Frequency
% Use the spectrogram function to measure and track the instantaneous frequency of a signal.
% Generate a quadratic chirp sampled at 1 kHz for two seconds. Specify the chirp so that its frequency is initially 100 Hz and increases to 200 Hz after one second.

fs = 1000;
t = 0:1/fs:2-1/fs;
y = chirp(t,100,1,200,'quadratic');

%
% Estimate the spectrum of the chirp using the short-time Fourier transform implemented in the spectrogram function. Divide the signal into sections of length 100, windowed with a Hamming window. Specify 80 samples of overlap between adjoining sections and evaluate the spectrum at \lfloor100/2+1\rfloor=51 frequencies.

spectrogram(y,100,80,100,fs,'yaxis')

%
% Track the chirp frequency by finding the time-frequency ridge with highest energy across the \lfloor(2000-80)/(100-80)\rfloor = 96 time points. Overlay the instantaneous frequency on the spectrogram plot.

[~,f,t,p] = spectrogram(y,100,80,100,fs);

[fridge,~,lr] = tfridge(p,f);

hold on
plot3(t,fridge,abs(p(lr)),'LineWidth',4)
hold off

% Copyright 2015 The MathWorks, Inc.