I am writing LMS filter to suppress noise in wav file (I know there are many modules to do this but I need to write LMS manually now as I will translate it into C later).

According to this answer[1], the inputs will be the noisy voice and a shifted version of it here is my python code:

import numpy as np
from scipy.io import wavfile

#d= vector of first channel samples
d = np.transpose(data)[0]
print(data.shape)
#delay= 2 seconds
xd= np.roll(d,2*fs)

M=5
L=d.size
w=np.zeros(M)
y=np.zeros(L-M)
e=np.zeros(L-M)
meu=0.1

for i in range(0,L-M):
xPart=xd[i:i+M]
#print(xPart.shape)
y[i]=xPart.dot(w)
e[i]=d[i]-y[i]
w = w+ meu*e[i]*xPart

print(w)


now, the final value if filter is [nan,nan,...,nan] and a lot of overflows happens.

I don't know what is wrong.

• is this implementation right?
• I see good reason to implement things yourself; but there's a lot of signal processing libraries for C, so C shouldn't be the reason you've got to do this in python first (makes so little sense). – Marcus Müller Feb 28 at 16:16
• I have to do it totally without using any external library even in C. So, I work first in python close to what I will do in C – Mahiro Feb 28 at 16:18
• I'm all for you doing it in Python to understand how the algorithm works. I'm just telling you that Python is not a good practice ground to learn how to write C. – Marcus Müller Feb 28 at 16:33
• You might be running into stability issues. make sure 0 < mu < 2/max(y(t)^2), where y(t) is your received signal – BigBrownBear00 Feb 28 at 16:37
• Marcus Müller u r right – Mahiro Feb 28 at 16:40