function [Wts, sout] = lms_fixed_use_emlc(rin, sinp)
% Copyright 2008 The MathWorks, Inc.
% Paramter initialization and memory allocation
persistent w;
persistent fifo;
persistent mu;
% Using Fixed Point arithmetics
filter_length = 32;
Wl = 16; % word length
Wb = 14; % fractional size
Ws = 1; % signed
% create a numeric TYPE for the processor data
fi_type = numerictype('WordLength',Wl,...
'FractionLength',Wb,...
'Signed',Ws);
% Define how math will be performed
M = 16; % 32 bit multiply output
A = 16; % 32 bit accumulator, so we can generate C code
fi_math= fimath('ProductMode','SpecifyPrecision',...
'ProductWordLength',M,...
'ProductFractionLength',M-2,...
'SumMode','SpecifyPrecision',...
'SumWordLength',A,...
'SumFractionLength',M-2,...
'RoundMode','nearest',...
'OverflowMode','wrap',...
'CastBeforeSum',true);
N = int16(length(rin));
if isempty(w)
fifo = fi(zeros(filter_length,1),fi_type,fi_math);
y = fi(0,1,16,14,fi_math);
mu = fi(300/32768,1,16,15,fi_math);
w = fi(zeros(1,filter_length),1,16,15,fi_math);
end
% must have this re-initialization of sout
sout = fi(zeros(N,1),fi_type,fi_math);
% end sout init
% Loop over input vector
for n = 1:N
% Update fifo
fifo(1:filter_length-1) = fifo(2:filter_length);
fifo(filter_length) = rin(n);
% Update filter
y(:) = w*fifo;
sout(n) = sinp(n) - y;
err = mu * sout(n);
w(:) = w + err*fifo';
end
Wts = w'; % output a column vector, not a row