%% Signal with Crossed Ridges
% Generate two chirps sampled at f_s = 3 kHz for one second. One of the chirps has an initial frequency of f_s/6 that increases linearly to f_s/3. The other chirp has an initial frequency of f_s/4 that decreases linearly to f_s/7. The first chirp has twice the amplitude of the second.

fs = 3000;
t = 0:1/fs:1-1/fs;

x1 = 4*chirp(t,fs/6,t(end),fs/3);
x2 = 2*chirp(t,fs/4,t(end),fs/7);

%
% Compute and plot the synchrosqueezed Fourier transform of the signal. Use a 256-sample Blackman window.

ws = blackman(256);

[s,f,t] = fsst(x1+x2,fs,ws);

fsst(x1+x2,fs,ws,'yaxis')

%
% Extract the two time-frequency ridges from the transform s. Penalize changes in frequency by 10. Specify that the three frequency bins adjacent to the highest-energy ridge be removed along with the ridge before extracting the other one. Plot the ridges.

[tfr,ifr] = tfridge(s,f,10,'NumRidges',2,'NumFrequencyBins',3);

plot(t,tfr)
xlabel('Time (s)')
legend('High energy','Low energy')

%
% Invert the synchrosqueezed Fourier transform to obtain the waveforms corresponding to the ridges. Compare the results with the original chirps.

k = ifsst(s,ws,ifr,'NumFrequencyBins',3);

subplot(2,1,1)
plot(t,x1,t,k(:,1))
ylabel('x_1')
legend('Original','Reconstructed')

subplot(2,1,2)
plot(t,x2,t,k(:,2))
ylabel('x_2')
legend('Original','Reconstructed')

% Copyright 2012 The MathWorks, Inc.