%% Extract High-Energy Ridges from Multicomponent Signal
% Generate a signal that is sampled at 3 kHz for one second. The signal consists of two tones and a quadratic chirp.
% The first tone has a frequency of 1000 Hz and unit amplitude.
% The second tone has a frequency of 1200 Hz and unit amplitude.
% The chirp has an initial frequency of 500 Hz and reaches 750 Hz at the end of the sampling. It has an amplitude of six.

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

x1 = 6*chirp(t,fs/6,t(end),fs/4,'quadratic');
x2 = sin(2*pi*fs/3*t);
x3 = sin(2*pi*fs/2.5*t);

x = x1+x2+x3;

%
% Compute and display the Fourier synchrosqueezed transform of the signal.

[sst,f] = fsst(x,fs);
mx = max(abs(sst(:)))*ones(size(t));

mesh(t,f,abs(sst))
view(2)

%
% Extract and plot the two highest-energy signal components. Set no penalty for changing frequency.

penval = 0;

fridge = tfridge(sst,f,penval,'NumRidges',2);

hold on
plot3(t,fridge,mx,'w','linewidth',5)
hold off

%
% The two tones have the same amplitude, and the algorithm jumps between them. Set the penalty for changing frequency to 1.

penval = 1;

fridge = tfridge(sst,f,penval,'NumRidges',2);

mesh(t,f,abs(sst))
view(2)
xlabel('Time (s)')
ylabel('Frequency (Hz)')

hold on
plot3(t,fridge,mx,'w','linewidth',5)
hold off

%
% Set the penalty to a high value for comparison. The chirp is penalized because its frequency is not constant.

penval = 1000;

fridge = tfridge(sst,f,penval,'NumRidges',2);

mesh(t,f,abs(sst))
view(2)
xlabel('Time (s)')
ylabel('Frequency (Hz)')

hold on
plot3(t,fridge,mx,'w','linewidth',5)
hold off

% Copyright 2012 The MathWorks, Inc.