%% Cancel Noise Using RLS Filter
% Cancel additive noise n added to an unknown system using an RLS filter. The RLS filter adapts its coefficients until its transfer function matches the transfer function of the unknown system as closely as possible. The difference between the output of the adaptive filter and the output of the unknown system is the error signal e, which represents the additive white noise. Minimizing this error signal is the objective of the adaptive filter.
% Note: If you are using R2016a or an earlier release, replace each call to the object with the equivalent step syntax. For example, obj(x) becomes step(obj,x).
%% Initialization
% Create a dsp.FIRFilter System object™ to represent the unknown system. Create a dsp.RLSFilter object and set the length to 11 taps. Set the method to 'Householder RLS'. Create a sine wave to represent the noise added to the unknown system. View the signals in a time scope.

FrameSize = 100;
NIter = 10;

rls = dsp.RLSFilter('Length',11,'Method','Householder RLS');
filt = dsp.FIRFilter('Numerator',fir1(10,[.5,.75]));

sinewave = dsp.SineWave('Frequency',0.01, ...
    'SampleRate',1,'SamplesPerFrame',FrameSize);
scope = timescope('LayoutDimensions',[2 1],'NumInputPorts',2, ...
    'TimeUnits','Seconds','YLimits',[-2.5 2.5], ...
    'BufferLength',2*FrameSize*NIter, ...
    'ActiveDisplay',1,'ShowLegend',true,'ChannelNames',{'Noisy signal'}, ...
    'ActiveDisplay',2,'ShowLegend',true,'ChannelNames',{'Error signal'});

for k = 1:NIter
    x = randn(FrameSize,1);
    d = filt(x) + sinewave();
    [y,e] = rls(x,d);
    w = rls.Coefficients;
    scope(d,e)
end
release(scope)

%
% Copyright 2019 The MathWorks, Inc.