clear;
clc;
N=128;
length=10000;
t=0:1/(N+length):1-1/(N+length);
s=sin(4*pi*t);
%n=randn(size(t));
n=awgn(s,1);
x=s+n;
y=zeros(1,length);
y(1:N)=x(1:N);
w=zeros(1,N);
e=zeros(1,length);
E=zeros(1,length);
u=0.0001;
for i=(N+1):length
y(i)=x((i-N+1):i)*w';
e(i)=s(i)-y(i);
w=w+2*u*e(i)*x(i:i+N-1);
E(i)=sum(e(N+1:i).^2)/(i-N);
end
%for i=1:length
% y(i)=x(i:i+N-1)*w';
% e(i)=s(i)-y(i);
% w=w+2*u*e(i)*x(i:i+N-1);
% E(i)=sum(e(1:i).^2)/(i);
%end
figure(1);
subplot(3,1,1);
plot(t,x);
axis([0 1 -5 5]);
title('带噪声信号');
subplot(3,1,2);
plot(t,s);
axis([0 1 -1.1 1.1]);
title('期望信号');
subplot(3,1,3);
plot(0:1/(N+length):(length-1)/(N+length),y);
axis([0 1 -1.1 1.1]);
title('滤波后信号');
figure(2);
plot((N+1:length),E(N+1:length));
%plot((1:length),E(1:length));
axis([0 10000 0 0.05]);
title('误差')