Filtro adaptativo con LMS
24 views (last 30 days)
Show older comments
function [err,w]=miLMS(d,x,Lw, mu) % [err,w]=miLMS(d,x,Lw,mu); % Parametros de entrada a la funcion son: % - d: vector con las muestras de entrada de la senyal deseada. % - x: vector con las muestras de entrada al filtro adaptativo. % - Lw: numero de coeficientes del filtro adaptativo % - mu: constante que controla la velocidad de convergencia. % % Parametros de salida: % - err: vector que contiene las muestras de la senyal de error % - w: Matriz con N filas y Lw columnas (siendo N el tamano de las senales % de entrada). Cada columna representa un coeficiente y cada fila % representa una iteracion. %% Inicializacion N=min([length(x),length(d)]); % Numero total de muestras de entrada % Inicializa las variables de salida err=zeros(N,1); %Evolucion de los coeficientes en w. Tantas filas como muestras %y tantas columnas como numero de coeficientes w=zeros(N,Lw); %% Algoritmo LMS % % Inicializa para la primera iteracion % % xinput es la entrada del filtro adaptativo (cambia cada iteracion), contiene la % muestra actual x[n] y las Lw-1 anteriores es decir: % Lw = [x[n], x[n-1],... x[n-(Lw-1) ]] % Los valores anteriores a la primera muestra de x son cero xinput=zeros(Lw,1); % vector columna % vector wn de los pesos en cada iteracion wn=zeros(Lw,1); % pesos iniciales a cero, vector columna % %% Bucle iterativo for n=1:N % Actualizacion de xinput para la iteracion n-esima xinput = [x(n);xinput(1:3)]; % Algoritmo LMS % Paso 1: Obtiene la salida del filtro y[n] con los coeficientes wn y xinput y = wn'*xinput; % Paso 2: Obtiene el valor del error e[n] err(n) = d(n) - y; % Paso 3: Actualiza los coeficientes del filtro wn para la siguiente iteracion wn = wn + 2*mu*err(n).*xinput; % Paso 4: Almacena los valores de los pesos actualizados en las filas de w w(n,:) = wn.'; end
0 Comments
Answers (1)
Manikanta Aditya
on 3 Jul 2024
Edited: Manikanta Aditya
on 3 Jul 2024
Hi,
Please share if you have any queries or doubts regarding Adaptive filter with LMS.
Here is the code you shared arranged properly:
function [err, w] = myLMS(d, x, Lw, mu)
% Input parameters to the function are:
% - d: vector with the input samples of the desired signal.
% - x: vector with the input samples to the adaptive filter.
% - Lw: number of adaptive filter coefficients
% - mu: constant that controls the speed of convergence.
% Output parameters:
% - err: vector containing the error signal samples
% - w: Matrix with N rows and Lw columns (N being the size of the input
% signals). Each column represents a coefficient and each
% row represents an iteration.
% Initialization
N = min([length(x), length(d)]); % Total number of input samples
% Initializes the output variables
err = zeros(N, 1); % Evolution of the coefficients in w. As many rows as samples
% and as many columns as number of coefficients
w = zeros(N, Lw);
% LMS Algorithm
% Initializes for the first iteration
% xinput is the input of the adaptive filter (it changes each iteration), it contains the
% current sample x[n] and the previous Lw-1, that is:
% Lw = [x[ n], x[n-1],... x[n-(Lw-1) ]]
% Values before the first sample of x are zero
xinput = zeros(Lw, 1); % column vector
% vector wn of the weights in each iteration
wn = zeros(Lw, 1); % initial weights to zero, column vector
% Iterative loop
for n = 1:N
% Update xinput for the nth iteration
xinput = [x(n); xinput(1:Lw-1)];
% LMS Algorithm
% Step 1: Get the output of the filter y[n] with the coefficients wn and xinput
y = wn' * xinput;
% Step 2: Get the error value e[n]
err(n) = d(n) - y;
% Step 3: Update the filter coefficients wn for the next iteration
wn = wn + 2 * mu * err(n) * xinput;
% Step 4: Store the updated weight values in the rows of w
w(n, :) = wn.';
end
end
The function 'myLMS' implements the Least Mean Squares (LMS) algorithm for an adaptive filter in MATLAB. It takes four inputs: the desired signal, the input samples, the number of filter coefficients, and a convergence constant. The function iteratively updates the filter coefficients and calculates the error value for each input sample.
I hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!