%% Moving Maximum of Sine Wave Signal
% Compute the moving maximum of a sum of three sine waves with varying amplitude. 
% Use a sliding window of length 30.
%% Initialization
% Set up an input signal that is a sum of three sine waves with frequencies 
% at 2 Hz, 5 Hz, and 10 Hz. The sampling frequency is 100 Hz. Create a |dsp.MovingMaximum| 
% object with a window length of 30. Create a time scope for viewing the output.

sin = dsp.SineWave('SampleRate',100,...
    'Frequency',[2 5 10],...
    'SamplesPerFrame',100);
movMax = dsp.MovingMaximum(30);
scope  = timescope('SampleRate',100,...
    'TimeSpanOverrunAction','Scroll',...
    'TimeSpanSource','Property',...
    'TimeSpan',10,'ShowGrid',true,...
    'YLimits',[-4.5 4.5]);
%% Compute the Moving Maximum
% Each sine wave component of the input signal has a different amplitude that 
% varies with the iteration. Use the |movMax| object to determine the maximum 
% value of the current sample and the past 29 samples of the input signal.
%%
for index = 1:100
    sin.Amplitude = rand(1,3);
    x = sum(sin(),2);
    xmax = movMax(x);
    scope([x,xmax])
end

%% 
% Copyright 2012 The MathWorks, Inc.