%% Visualize Residual and Intrinsic Mode Functions of Signal
% Load and visualize a nonstationary continuous signal composed of sinusoidal waves with a distinct change in frequency. The vibration of a jackhammer and the sound of fireworks are examples of nonstationary continuous signals. The signal is sampled at a rate fs.

load('sinusoidalSignalExampleData.mat','X','fs')
t = (0:length(X)-1)/fs;
plot(t,X)
xlabel('Time(s)')

% The mixed signal contains sinusoidal waves with different amplitude and frequency values.
%
% Perform empirical mode decomposition to plot the intrinsic mode functions and residual of the signal. Since the signal is not smooth, specify 'pchip' as the interpolation method.

emd(X,'Interpolation','pchip','Display',1)

% emd generates an interactive plot with the original signal, the first 3 IMFs, and the residual. The table generated in the command window indicates the number of sift iterations, the relative tolerance, and the sift stop criterion for each generated IMF. You can hide the table by removing the 'Display' name-value pair or specifying it as 0.
%
% Right-click on the white space in the plot to open the IMF selector window. Use IMF selector to selectively view the generated IMFs, the original signal, and the residual.
%
% Select the IMFs to be displayed from the list. Choose whether to display the original signal and residual on the plot.
%
% The selected IMFs are now displayed on the plot.
%
% Use the plot to visualize individual components decomposed from the original signal along with the residual. Note that the residual is computed for the total number of IMFs, and does not change based on the IMFs selected in the IMF selector window.
% Copyright 2012 The MathWorks, Inc.