%% Decompose Table Data Using STL
% Decompose data in a table into its long-term trend, two seasonal trends, and remainder using the STL algorithm.
% Create a table T whose variable data 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);
data = trend + seasonal1 + seasonal2 + noise;
T = table(data)

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

D = trenddecomp(T,"stl",[20 30]);
D = addvars(D,data);
stackedplot(D)
xlabel("t")

% Copyright 2021 The MathWorks, Inc.