LMS(least mean square)自适应滤波算法matlab实现

来源:互联网 发布:macbook如何下载软件 编辑:程序博客网 时间:2024/05/21 22:57

以下是matlab帮助文档中lms示例程序,应用在一个系统辨识的例子上.整个滤波的过程就两行,用红色标识.

x  = randn(1,500);     % Input to the filter
b  = fir1(31,0.5);     % FIR system to be identified
n  = 0.1*randn(1,500); % Observation noise signal
d  = filter(b,1,x)+n;  % Desired signal
mu = 0.008;            % LMS step size.
ha = adaptfilt.lms(32,mu);
[y,e] = filter(ha,x,d);
subplot(2,1,1); plot(1:500,[d;y;e]);
title('System Identification of an FIR Filter');
legend('Desired','Output','Error');
xlabel('Time Index'); ylabel('Signal Value');
subplot(2,1,2); stem([b.',ha.coefficients.']);
legend('Actual','Estimated');
xlabel('Coefficient #'); ylabel('Coefficient Value');
grid on;

这实在看不出什么名堂,就学习目的而言,远不如自己写一个出来.整个滤波的过程用红色标识.

%% System Identification (SID)
% This demonstration illustrates the application of LMS adaptive filters to
% system identification (SID). 
%
% Author(s): X. Gumdy
% Copyright 2008 The Funtech, Inc.
%% 信号产生
clear all;
N  = 1000 ;
x  = 10 * randn(N,1);     % 输入信号
b  = fir1(31,0.5);     % 待辨识系d
n  = randn(N,1);
d  = filter(b,1,x)+n;  % 待辨识系统的加噪声输出

%% LMS 算法手工实现
sysorder = 32;
maxmu = 1 / (x'*x / N * sysorder);% 通过估计tr(R)来计算mu的最大值
mu = maxmu / 10;
w = zeros ( sysorder , 1 ) ;
for n = sysorder : N
    u = x(n-sysorder+1:n) ;
    y(n)= w' * u;
    e(n) = d(n) - y(n) ;
    w = w + mu * u * e(n) ;
end
y = y';
e = e';

%% 画图
figure(1);
subplot(2,1,1); plot((1:N)',[d,y,e]);
title('System Identification of an FIR Filter');
legend('Desired','Output','Error');
xlabel('Time Index'); ylabel('Signal Value');
subplot(2,1,2); stem([b', w]);
legend('Actual','Estimated');
xlabel('Coefficient #'); ylabel('Coefficient Value');
grid on;

自适应滤波也许算得上信号处理理论中比较有意思的部分了.