### MATLAB >> Matlab code for LMS algorithm

I am trying to design an adaptive filter using the LMS algorithm as
written below. Can anyone advice me on where I'm going wrong. I'm
using noise as the desired response as it is unknown.

I include code which terminates at the line
E(k) = d(k) - A(k-1,:)*X(k,:).'; with the error Index exceeds matrix
dimensions.

Inputs are
d=rand(1000,1)%to simulate noise
mu=0.001
nord=3
a0=1

%LMS Adaptive filtering using the Widrow-Hoff LMS algorithm.
%---
%USAGE [A,E] = lms(x,d,mu,nord,a0)
%
% x : input data to the adaptive filter.
% d : desired output
% mu : adaptive filtering update (step-size) parameter
% nord : number of filter coefficients
% a0 : (optional) initial guess for FIR filter
% coefficients - a row vector. If a0 is omitted
% then a0=0 is assumed.
%
% The output matrix A contains filter coefficients.
% - The n'th row contains the filter coefficients at time n
% - The m'th column contains the m'th filter coeff vs. time.
% - The output vector E contains the error sequence versus
time.

function [A,E] = lms(x,d,mu,nord,a0)
X=convm(x,nord);
[M,N] = size(X);
if nargin < 5, a0 = zeros(1,N); end
a0 = a0(:).';
E(1) = d(1) - a0*X(1,:).';
A(1,:) = a0 + mu*E(1)*conj(X(1,:));
if M>1
for k=2:M;
E(k) = d(k) - A(k-1,:)*X(k,:).';
A(k,:) = A(k-1,:) + mu*E(k)*conj(X(k,:));
end;
end;

```Dear all,

I am working on the acoustic echo cancellation.I have
implemented the time domain LMS algorithm for acoustic echo
cancellation and it is working fine only for some audio files and not
working for some other audio files which has the large eigen value
spread.So to avoid this eigen value spread now I am working on the
subband LMS algorithm in frequency domain for acoustic echo
cancellation.So now I am searching for the  documents of subband LMS
algorithm and frequency domain LMS algorithm in which it is mentioned
clearly which samples are to be considered and why.But I didnt find
those details.But I understood the overall view of frequency domain LMS
algorithm from the documents that I collected and its steps are as
follows:

Step1:For every sample of a frame (ie., 10 m sec data), that sample and
its previous samples, a total of samples equal to number of weights of
the filter are taken(as a full band) and they are converted to
frequency domain using DFT.

Step2:Initially all the weights of the filter are taken as zeros in
frequency domain.

Step3:Then to get the echo sample of kth subsequent sample position its
corresponding input sample in frequency domain(farend in frequency
domain) are multiplied with its corresponding  weight in frequency
domain (its corresponding weight).
N(k)  = X(k) * W(k)

Step4:Then the corresponding error sample is obtained by subtracting
this echo sample from the desired sample in frequency domain.
Ek  = Yk - Nk
where Yk is the desired signal sample in frequency domain.

Step5:Using this error sample in frequency domain all the weights of
the filter are updated using the following formula:

Wk+1(i) = Wk(i) + 2 * muk * Ek * Xk
where Xk is column vector containing the current sample and its
previous samples.
Wk is the column vector of all the weights of the filter.

Step6:All the above steps are repeated for each and every sample of the
frame untill the end of the frame and is repeated for all the frames of
the audio file.

My doubt is in the calculation of desired samples in
frequency domain ie.,Yk .According to logic, I think these frequency
components are calculated using all the samples of desired signal of a
frame.But it is nowhere mentioned in the documents that which samples I
need to consider for this frequncy band desired samples Okay if we
generate frequency domain desired samples equal to number of desired
samples in time domain using all these time domain samples.If I
integrate this idea with the subband LMS algorithm then again I am
getting the doubt that for each subband of farend signal(ie., obtained
by dividing a full band  of farend signal consisting of current sample
of the frame and its previous samples to a particular number of
subbands)whether we need to have the same  desired signal frequency
component for all the subbands or different frequency component for
each subband and how it is calculated.I have these type of doubts and
if we think of that there may be several possibilities.

So I am searching for that, but I am not getting the
required points.

I think one solution for this is converting the code of
time domain LMS algorithm to frequency domain and checking the code for
all the possibilities by trial and error method and seeing that for
which combination we will get the perfect output.But I am not getting
the output.

Here I have converted the farend signal, desired signal
and weights of the filter are converted to freqency domain and after
performing the operation the error signal is reconverted to time
domain.And the convergence weight factor is given in different ways in
different documents.I am in total confusion to proceed further.

I am very greatful to you if any one give me suggestions
and guidence  regarding this.

Regards,
Aparna Ram.K.

```

```hallo firends,

Can any body help me write this below code into MATLAB object oriented code or C code form please?

M=50; % total number of periods. (choose for 1s duration)
f0 = 50; % fundamental AC frequency
T0 = 1/f0; %fundamental AC period
T = M*T0; %Time for M periods (integer M)
N=30; % sample points per period.
dt = T0/N; % Sample at N points per period (integer N > 20)
t = dt*[1:M*N-1]; % Sampling time array
A0=120/2; % amplitude of AC signal. Divide by 2 or maybe sqrt (2) ?
AC=A0*sin(f0*t*2*pi); % create AC signal.
DC0=50; % DC amplitude
DC=DC0*ones(1,length(t)); % create a DC signal that is constant of length t
S=AC+DC; % total signal is AC signal + DC signal
figure(1);
plot(t,S); % plot signal.
xlabel('time');ylabel('Signal');
title('Total Signal');
% filter components
fAC=S-mean(S); % here is the AC component
fDC=mean(S); % here is the DC component

figure(2);
plot(t,fAC,'r',t,fDC,'b'); % plot AC and DC signals
legend('AC','DC');
xlabel('time');ylabel('Signal');
title('Signal with only AC component');

figure(3);
plot(t,fDC,'b'); % plot DC signal
legend('DC');
xlabel('time');ylabel('Signal');
title('Signal with only DC component');

figure(4);
% best to plot in Fourier domain
u=abs(fftshift(fft(S)));
NumPoints=length(t);
freq=(1/dt)*[1:NumPoints]/NumPoints; % create frequency axis
plot(freq,u);
xlabel('frequency');ylabel('signal');
title('Fourier transform');

with best regards,
Gopal
```

```Dear All,

I need the Matlab code of LMS predictor with some explanation.

Do you have any examples or links ? I've found some code on the Matlab web site but I'm not sure there are complete prediction examples.

I'm especially interested how the prediction is made.

Best Regards,
Marcin
```

```Hi!

I'm using adaptfilt.filtxlms function (Filter Design Toolbox in Matlab) in
a system with active noise control. My question is if any of you have a
good idea how to choose the mu constant wich is an input parameter in this
function? All I know is that it should somehow be related to the energy in
the reference signal (the X-signal).

Sorry if this forum is not the right place to post.

regards
--
Sverre
```