%% System Identification of FIR Filter Using Fast Transversal Filter
% System identification is the process of identifying the coefficients of an unknown system using an adaptive filter. The general overview of the process is shown in System Identification Overview. The main components involved are:
% The adaptive filter algorithm.
% An unknown system or process to adapt to. In this example, the filter designed by fir1 is the unknown system.
% Appropriate input data to exercise the adaptation process. In a generic system identification model, the desired signal d(k) and the input signal x(k) are used to exercise the adaptation process.
% The objective of the adaptive filter is to minimize the error signal between the output of the adaptive filter y(k) and the output of the unknown system (or the system to be identified) d(k). Once the error signal is minimized, the adapted filter resembles the unknown system. The coefficients of both the filters match closely.
% Note: If you are using R2016a or earlier, replace each call to the object with the equivalent step syntax. For example, obj(x) becomes step(obj,x).
%
%% Unknown System
% Create a dsp.FIRFilter object that represents the system to be identified. Use the fir1 function to design the filter coefficients. The designed filter is a 10th order lowpass digital filter with a cutoff frequency of 0.25.

filt = dsp.FIRFilter;
filt.Numerator = fir1(10,.25)

% Pass the signal x to the FIR filter. The desired signal d is the sum of the output of the unknown system (FIR filter) and an additive noise signal n.

x = randn(1000,1);
d = filt(x) + 0.01*randn(1000,1);

%
%% Adaptive Filter
% With the unknown filter designed and the desired signal in place, create and apply the fast transversal filter object to identify the unknown filter.
% Create a dsp.FastTransversalFilter object to represent an adaptive filter. Set the length of the adaptive filter to 11 taps and a forgetting factor of 0.99.

ftf1 = dsp.FastTransversalFilter(11,'ForgettingFactor',0.99)

% Pass the primary input signal x and the desired signal d to the fast transversal filter. Run the adaptive filter to determine the unknown system. The output y of the adaptive filter is the signal converged to the desired signal d, thereby minimizing the error e between the two signals.

[y,e] = ftf1(x,d);
w = ftf1.Coefficients

%
% Plot the results. The output signal matches the desired signal very closely making the error between the two close to zero.

subplot(2,1,1);
plot(1:1000,[d,y,e])
title('System Identification of an FIR filter');
legend('Desired','Output','Error');
xlabel('time index');
ylabel('signal value');

% The coefficients of the FIR filter match very closely with the coefficients of the adapted filter, thereby confirming the convergence.

subplot(2,1,2); 
stem([filt.Numerator; w].');
legend('Actual','Estimated'); 
xlabel('coefficient #'); 
ylabel('coefficient value');

% Copyright 2012 The MathWorks, Inc.