2
$\begingroup$

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
$\endgroup$

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
13
  • $\begingroup$ 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? $\endgroup$ Commented Nov 19, 2018 at 12:14
  • $\begingroup$ @AlfroJang80: Doesn't your code produce an error? How can you multiply w and xDelayed if they have different lengths? $\endgroup$
    – Matt L.
    Commented Nov 19, 2018 at 12:17
  • $\begingroup$ 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. $\endgroup$ Commented Nov 19, 2018 at 12:20
  • $\begingroup$ @AlfroJang80: yes, you must use the most recent $N$ values of the (delayed) data vector. $\endgroup$
    – Matt L.
    Commented Nov 19, 2018 at 12:22
  • $\begingroup$ 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. $\endgroup$ Commented Nov 19, 2018 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.