我必须使用NLMS方法制作程序,以最小化ECG信号中的噪音。
from __future__ import division
from numpy import *
import numpy as np
#import matplotlib as plt
import matplotlib.pyplot as plt
import os
clear = lambda: os.system('cls')
clear()
meu=1e-05
file=open('ecg.txt','r') #this file i attached at below
data=file.readlines()
x=data[:1000]
xx=data[1001:2001]
N=len(x)
NN=len(xx)
t=np.zeros((N,1),float)
X=np.zeros((4,1),float)
Z=np.array([0,0,0,0],float)
w=random.rand(4,1)
y=np.zeros((N,1),float)
yp=np.zeros((N,1),float)
e=np.zeros((N,1))
j=np.zeros((N,1),float)
for n in range(len(x)):
X[1:len(X)-1]=X[0:len(X)-2]
X[0]=x[n]
sum=0
for k in range(len(X)):
sum+=(w[k].T*X[k])
y[n]=sum
e[n]=subtract(X[0],y[n])
j[n]=e[n]*e[n]
for l in range(len(X)):
w[l] = w[l]+(meu*e[n]*X[l])/((X[l]*X[l])*(X[l]*X[l])); #when division takes place it give error
MSE=mean(j,1)
plt.plot(10 * log10(MSE))
plt.title('MSE')
plt.show()
#print(' output\n')
#print(y,'\n')
#print(' data\n')
#print(x,'\n')
print(w)
#y.astype(float)
#pp=np.int16(y)
plt.plot(x)
plt.yscale('log')
plt.title('x')
plt.show()
plt.plot(y)
plt.yscale('log')
plt.title('y')
plt.show()
plt.plot(w)
plt.yscale('log')
plt.title('w')
plt.show()
plt.plot(e)
#plt.yscale('log')
plt.title('e')
plt.show()
plt.plot(e)
plt.plot(x)
plt.plot(y)
plt.show()
plt.plot(yp)
plt.title('yp')
plt.show()
#plt.plot(t)
#plt.yscale('log')
#plt.show()
我一直收到这个错误:
Warning (from warnings module):
w[l] = w[l]+(meu*e[n]*X[l])/((X[l]*X[l])*(X[l]*X[l]));
RuntimeWarning: invalid value encountered in true_divide
结果:
>>w
array([[ 0.86035037],
[ 0.35119551],
[ 0.40570589],
[ nan]])
>>X
array([[ 0.19258605],
[ 0.19442064],
[ 0.19243968],
[ 0. ]])
>>w[l]
array([ nan])
>>X[l]
array([ 0.])
我无法弄明白,代码有什么问题?
心电图文件: