%% Decompose Vector of Data Using SSA
% Decompose a vector of data into its long-term trend, two seasonal trends, and remainder using singular spectrum analysis.
% Create a vector of data A that contains a long-term trend, two seasonal trends with different periods, and a noise component.

t = (1:200)';
trend = 0.001*(t-100).^2;
period1 = 20;
period2 = 30;
seasonal1 = 2*sin(2*pi*t/period1);
seasonal2 = 0.75*sin(2*pi*t/period2);
noise = 2*(rand(200,1) - 0.5);
A = trend + seasonal1 + seasonal2 + noise;

% Decompose the data using the SSA algorithm. Plot the data, the trends, and the remainder.

[LT,ST,R] = trenddecomp(A);
plot([A LT ST R]);
legend("Data","Long-term","Seasonal1","Seasonal2","Remainder")

% Copyright 2021 The MathWorks, Inc.