%% Cross-Spectrogram of Linear Chirps
% Generate two linear chirps sampled at 1 MHz for 10 milliseconds.
% The first chirp has an initial frequency of 150 kHz that increases to 350 kHz by the end of the measurement.
% The second chirp has an initial frequency of 200 kHz that increases to 300 kHz by the end of the measurement.
% Add white Gaussian noise such that the signal-to-noise ratio is 40 dB.

nSamp = 10000;
Fs = 1000e3;
SNR = 40;
t = (0:nSamp-1)'/Fs;

x1 = chirp(t,150e3,t(end),350e3);
x1 = x1+randn(size(x1))*std(x1)/db2mag(SNR);

x2 = chirp(t,200e3,t(end),300e3);
x2 = x2+randn(size(x2))*std(x2)/db2mag(SNR);

%
% Compute and plot the cross-spectrogram of the two chirps. Divide the signals into 200-sample segments and window each segment with a Hamming window. Specify 80 samples of overlap between adjoining segments and a DFT length of 1024 samples.

xspectrogram(x1,x2,hamming(200),80,1024,Fs,'yaxis')

%
% Modify the second chirp so that the frequency rises from 50 kHz to 350 kHz during the measurement. Use a 500-sample Kaiser window with shape factor \beta=5 to window the segments. Specify 450 samples of overlap and a DFT length of 256. Compute and plot the cross-spectrogram.

x2 = chirp(t,50e3,t(end),350e3);
x2 = x2+randn(size(x2))*std(x2)/db2mag(SNR);

xspectrogram(x1,x2,kaiser(500,5),450,256,Fs,'yaxis')

% In both cases, the function highlights the frequency content that the two signals have in common.
% Copyright 2012 The MathWorks, Inc.