close all;clear;clc;
N = 500; %伯努利序列个数
M = 11; %滤波器长度
W = 3.2; n = [1;2;3];
h = 0.5*(1+cos(2*pi*(n-2)/W)); %信道单位冲激响应
mu = 0.05; %步长参数
for num = 1:200 %独立实验200次
x = binornd(1,0.5,N,1);
loc = find(x==0);
x(loc) = -1; %1,-1的概率都为0.5的伯努利序列
deltv1 = 0.1;
v1 = sqrt(deltv1)*randn(N+2,1); %均值为0,方差为0.1的高斯白噪声
u = conv(h(:,1),x)+v1; %滤波器的输入序列
%%LMS算法%%
w = zeros(M,1); %初始化滤波器系数
for k = 1:N-M+1
utemp = u(k+M-1:-1:k); %存放u(n)的临时向量
y(k) = w.'*utemp; %滤波器的输出
e(k) = x(k+4)-y(k); %估计误差
LPe(num,k) = e(k).*e(k); %估计误差平方
w = w+mu*utemp*conj(e(k)); %更新滤波器系数
end
w = zeros(M,1); %滤波器初始化
delt = 0.004;
P = 1/delt*eye(M); %初始化逆相关矩阵
lambda = 1; %指数加权因子
for k = 1:N-M+1
utemp = u(k+M-1:-1:k); %u(n)的临时向量
Pai = P*utemp;
kn = Pai/(lambda+utemp'*Pai); %增益向量
xi = x(k+4)-w'*utemp; %先验估计误差
w = w+kn*conj(xi); %更新滤波器系数
P = 1/lambda*P-1/lambda*kn*utemp'*P; %更新逆相关矩阵
RPe(num,k) = xi.*xi; %误差平方
end
end
figure; %画图
semilogy(mean(LPe),'b','LineWidth',1);hold on;
semilogy(mean(RPe),'r','LineWidth',1);
xlabel('迭代次数','FontSize',16);ylabel('集平均平方误','FontSize',16);set(gca,'FontSize',16);
ylim([0.1 10]);legend('LMS','RLS');grid on;
title("LMS算法和RLS算法的学习曲线")
text(300,0.7,'LMS','FontSize',16);
text(300,0.2,'RLS','FontSize',16);
close all;clear;clc;
N = 210; %伯努利序列的个数
M = 11; %滤波器长度
W0 = [2.8,3.0,3.2,3.4];
W = W0; W = 1./W; n = [1;2;3];
h = 0.5*(1+cos(2*pi*(n-2)*W)); %信道的冲激响应函数
delt = 0.004; lambda = 1; %指数加权因子
for num = 1:200 %独立进行200次实验
x = binornd(1,0.5,N,1); %产生1,-1概率都为0.5的伯努利序列
loc = find(x==0);
x(loc) = -1;
deltv1 = 0.001;
v1 = sqrt(deltv1)*randn(N+2,1); %均值为0,方差为0.001的白噪声
u(:,1) = conv(h(:,1),x)+v1; %不同参数W下的u(n);
u(:,2) = conv(h(:,2),x)+v1;
u(:,3) = conv(h(:,3),x)+v1;
u(:,4) = conv(h(:,4),x)+v1;
for wk = 1:4 %4种参数W进行循环计算
w = zeros(M,1); %滤波器的初始值
P = 1/delt*eye(M); %初始化逆相关矩阵
for k = 1:N-M+1
utemp = u(k+M-1:-1:k,wk); %u(n)的临时向量
Pai = P*utemp;
kn = Pai/(lambda+utemp'*Pai); %增益向量
xi = x(k+4)-w'*utemp; %先验估计误差
w = w+kn*conj(xi); %更新滤波器系数
P = 1/lambda*P-1/lambda*kn*utemp'*P; %更行逆相关矩阵
Pe(num,k,wk) = xi.*xi; %误差平方
end
end
end
figure; %画图
semilogy(mean(Pe(:,:,1)),'k','LineWidth',1);hold on;
semilogy(mean(Pe(:,:,2)),'--b','LineWidth',1);hold on;
semilogy(mean(Pe(:,:,3)),':m','LineWidth',1);hold on;
semilogy(mean(Pe(:,:,4)),'-.r','LineWidth',1);hold on;
xlabel('迭代次数','FontSize',16);ylabel('集平均平方误差','FontSize',16);
set(gca,'FontSize',16);
title('RLS算法的学习曲线');
legend(num2str(W0(1)),num2str(W0(2)),num2str(W0(3)),num2str(W0(4)));grid on;
text(200,0.0041,' W=3.4','FontSize',10);
text(200,0.0025,' W=3.2','FontSize',10);
text(200,0.0018,' W=3.0','FontSize',10);
text(200,0.0013,' W=2.8','FontSize',10);