%% Active Noise Control of Random Noise Signal
% 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).
% Generate noise, create FIR primary path system model, generate observation noise, filter the primary path system model output with added noise, and create FIR secondary path system model.

x  = randn(1000,1);
g  = fir1(47,0.4);
n  = 0.1*randn(1000,1);
d  = filter(g,1,x) + n;
b  = fir1(31,0.5);

%
% Use the dsp.FilteredXLMSFilter System object™ to compute the filtered output and the filter error for the input and the signal to be canceled.

mu = 0.008;
fxlms = dsp.FilteredXLMSFilter(32, 'StepSize', mu, 'LeakageFactor', ...
     1, 'SecondaryPathCoefficients', b);
[y,e] = fxlms(x,d);

%
% Plot the results.

plot(1:1000,d,'b',1:1000,e,'r');
title('Active Noise Control of a Random Noise Signal');
legend('Original','Attenuated');
xlabel('Time Index'); ylabel('Signal Value');  grid on;

% Copyright 2016 The MathWorks, Inc.