matlab关于LMS算法的程序close all% 周期信号的产生 t=0:99;xs=10*sin(0.5*t);figure(1);subplot(2,1,1)plot(t,xs)ylabel('幅值')title('it{输入周期性信号}')grid% 噪声信号的产生randn('state',sum(100*clock));xn=rand(1,100);subplot

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/03 04:23:18
matlab关于LMS算法的程序close all% 周期信号的产生 t=0:99;xs=10*sin(0.5*t);figure(1);subplot(2,1,1)plot(t,xs)ylabel('幅值')title('it{输入周期性信号}')grid% 噪声信号的产生randn('state',sum(100*clock));xn=rand(1,100);subplot

matlab关于LMS算法的程序close all% 周期信号的产生 t=0:99;xs=10*sin(0.5*t);figure(1);subplot(2,1,1)plot(t,xs)ylabel('幅值')title('it{输入周期性信号}')grid% 噪声信号的产生randn('state',sum(100*clock));xn=rand(1,100);subplot
matlab关于LMS算法的程序
close all
% 周期信号的产生
t=0:99;
xs=10*sin(0.5*t);
figure(1);
subplot(2,1,1)
plot(t,xs)
ylabel('幅值')
title('it{输入周期性信号}')
grid
% 噪声信号的产生
randn('state',sum(100*clock));
xn=rand(1,100);
subplot(2,1,2);
plot(t,xn)
ylabel('幅值')
xlabel('时间')
title('it{随机噪声信号}')
grid
% 信号滤波
xn = xs+xn;
xn = xn.' ; % 输入信号序列
dn = xs.' ; % 预期结果序列
M = 5; % 滤波器的阶数
%rho_max = max(eig(xn*xn.')); % 输入信号相关矩阵的最大特征值
%mu = randn()*(1/rho_max) ; % 收敛因子 0 < mu < 1/rho;
mu=0.001;
W=zeros(M,length(xn));
%迭代计算
for k=M:length(xn)
xn=xn(k:-1:k-M+1); %滤波器M个抽头的输入
yn= W(:,k-1).' * xn; % 滤波器的输出
en(k) = dn(k) - yn ; % 第k次迭代的误差
% 滤波器权值计算的迭代式
W(:,k) = W(:,k-1) + mu*en(k)*xn;
end
% 绘制滤波器输入信号
hold on;
figure;
subplot(2,1,1);
plot(t,xn)
ylabel('幅值')
xlabel('时间')
title('it{滤波器输入信号}')
grid
% 绘制自适应滤波器输出信号
subplot(2,1,2);
plot(t,yn);
ylabel('幅值')
xlabel('时间')
title('it{自适应滤波器输出信号}')
grid
% 绘制自适应滤波器输出信号,预期输出信号和两者的误差
hold on;
figure;
plot(t,yn,'b',t,dn,'g',t,dn-yn,'r')
legend('自适应滤波器输出','预期输出','误差')
ylabel('幅值')
xlabel('时间')
title('it{自适应滤波器}')
grid
在这个程序中中出现错误 Index exceeds matrix dimensions.
Error in ==> lms at 33
xn=xn(k:-1:k-M+1); %滤波器M个抽头的输入

matlab关于LMS算法的程序close all% 周期信号的产生 t=0:99;xs=10*sin(0.5*t);figure(1);subplot(2,1,1)plot(t,xs)ylabel('幅值')title('it{输入周期性信号}')grid% 噪声信号的产生randn('state',sum(100*clock));xn=rand(1,100);subplot
调试信息说的很明确了,xn的下标越界
第一个循环中 xn=xn(5:1) %xn是一个1*5的double阵(或者叫行向量)
第二次循环时 k=6,M=5 xn=xn(6:-1:2) %注意此时xn下标取值为1~5
因此在解释执行xn=xn(6)时机器不知怎么办,于是就出错了……