I'm having some trouble implementing my LMS Adaptive Filter in MATLAB to separate wideband and narrowband signals from a voice signal.

I'm using a delayed version of my input as a reference as well as the error term.

step = 0.01;
w = zeros(1, N);
xDelayed = [zeros(1, 100) x']'; % delaying input

for n=1:length(x)
e = x(n) - w(1)*xDelayed(n);
w = w - step*e*xDelayed(n);
end


It's essentially an implementation of this

$$w(n+1) = w(n) - \alpha e(n) x(n)$$

For some reason, my entire w (N long) vector is all the same value. UPDATE:

M = 5;
N = length(sound)
w = zeros(M, N);
STEP_SIZE = 0.01;
d = sound;
x = sound_delayed(1:N);

for i=(M+1):N
e(i) = d(i) -  x((i-(M)+1):i)*w(:,i);
w(:,i+1) = w(:,i) + mu * e(i) * x((i-(M)+1):i)';
end
for i=(M+1):N
yd(i) = x((i-(M)+1):i)*w(:,i);
end


There are several problems in your code. First, it looks like you're confusing iteration and vector indices. The computation of e should use all values of the current (delayed) data vector, filtered with the current filter coefficients. In the update equation, you subtract a scalar from a vector, which is not what you want. Again you should be using all values of the current data vector (the length of which must equal the chosen filter length).

Take a look at the Matlab code in this question (the second one in the EDIT-part). I haven't run it but it looks like it deals correctly with the vectors in the update loop.

• I've updated my code above to adjust the error calculation. Is that correct? Also for the filter update equation, w is a vector defined from before so when I do a calculation with it, it should also return a vector back not a scalar? – AlfroJang80 Nov 19 '18 at 12:14
• @AlfroJang80: Doesn't your code produce an error? How can you multiply w and xDelayed if they have different lengths? – Matt L. Nov 19 '18 at 12:17
• My apologies. I could select only the first N=length(x) values of xDelayed. Would that be a correct way of doing it. I'm sorry if there are some stupid questions, I've just gotten very confused with this. – AlfroJang80 Nov 19 '18 at 12:20
• @AlfroJang80: yes, you must use the most recent $N$ values of the (delayed) data vector. – Matt L. Nov 19 '18 at 12:22
• Does that mean I need to sort of reverse my xDelayed? Like I would do xDelayed(end:N)? (not sure if that's correct syntax) But then surely I could just use x itself. I mean my delay is just adding 100 zeros to the start. – AlfroJang80 Nov 19 '18 at 12:23