Least Mean Square Algorithm

11 views (last 30 days)
curly133
curly133 on 24 Apr 2018
Edited: curly133 on 25 Apr 2018
Hello. I am trying to implement this pseudo code to make a Least mean square algorithm. I'm not too good at matlab yet and I got stuck with this algorithm. I need to make an LSM algorithm to help me determine my filter "h". Here is the pseudo code:
Here is what I have so far. I load a signal that gives me two variables x and y, both length 500, then I need to apply the algorithm. I am not too sure how to apply xn from the pseudo code or how to finish this off really.
load sig.mat; % loads variables x and y
N = 5; % filter length
u = .01; % learning rate
h = zeros(1,N);
for n = 0:499
xn = x(n-(N-1));
en = y(n) - (h.')*xn;
h = h + u*en*xn;
end

Accepted Answer

Ameer Hamza
Ameer Hamza on 25 Apr 2018
In your code, the way you are accessing the values of xn is wrong. x(n-(N-1)) will assign a single number to xn not an N element array. Additionally, in the algorithm you gave, the vector index starts from 0 while in MATLAB the vector index starts from 1, so you need to take care of that as I have done in the following code. Also, the filter is using past values from vector x. At beginning e.g. n=1 you don't have past values of x i.e. x(0), x(-1), ... therefore I have added an if condition which will add extra zeros to the to xn in those cases.
load sig.mat; % loads variables x and y
N = 5; % filter length
u = .01; % learning rate
h = zeros(1, N);
for n = 1:500
if n-N < 1
xn = [x(n:-1:1); zeros(N-n, 1)];
else
xn = x(n:-1:n-N+1);
end
en = y(n) - h*xn;
h = h + (u*en*xn)';
end
  1 Comment
curly133
curly133 on 25 Apr 2018
Edited: curly133 on 25 Apr 2018
Thank you! This makes a lot more sense now.

Sign in to comment.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!