T = 10; % second
alpha = 10; % Hz/second/second
fimax = alpha*T; % maximum instantaneous frequency
Fs = 20*fimax; % sample rate [samples/second]

N = floor(T*Fs); % convert start and end times to samples
n = [-N:N].'; % discrete-time index vector
t = n/Fs; % time vector

x = exp(1j*pi*alpha*t.^2); % chirp
h = conj(x(end:-1:1)); % matched-filter = conjugate and time reversal

y = real(conv(x,h)); % compute matched filter output

% Make some plots
figure(1); clf;
subplot(211);
plot(t,[real(x), imag(x)]);
grid on;
xlabel('time [seconds]');

subplot(212);
plot([-2*N:2*N]/Fs,y);
grid on;
xlabel('time [seconds]');

shg;

figure(2);
NFFT = 2^nextpow2(length(y));
freq = [0:NFFT-1]/NFFT - 0.5;
freq = freq * Fs;

subplot(211);
plot(freq,abs(fftshift(fft(x,NFFT))));
grid on;
xlabel('frequency [Hz]');

subplot(212);
plot(freq,abs(fftshift(fft(y,NFFT))));
grid on;
xlabel('frequency [Hz]');

shg

figure(3);
clf;
M = 256;
[S,F,T] = spectrogram(x,hamming(M),round(0.95*M),M,Fs,'centered');
imagesc(T,F,20*log10(abs(S)),[-20 10]);
colorbar;
xlabel('time [seconds]');
ylabel('frequency [Hz]');
%ylim([-400 400]);

soundsc(real(x),Fs*10);


