Matlab Script File
Matlab Script File
% % Depending on the variable 'algorithm', this file performs % 'LMS' least-mean square (LMS) algorithm % 'RLMS' recursive LMS algorithm % 'FXLMS' filtered-X LMS algorithm % 'FULMS' filtered-U LMS algorithm (to be added) % 'FX_ONL' filtered-X LMS algorithm with online sys id (to be added). % Signals and transfer paths are generated by this script file. For % simulations, type eg. % >> algorithm = 'LMS'; % >> homework7; % System and algorithm parameters have to be set in this file. See below. % % Notation and variables % N_x number quantities (taps, iterations,...) % lower case signals and constants % upper case tap-delay lines and filter coefficients; % tap-delay lines are organized in time-reversed % order ! % Recommended parameter settings % % LMS N_a=40; mu=0.01; % RLMS N_a=5; N_b=35; mu=0.01; % FXLMS N_a=40; mu=0.01; % FULMS N_a=15; N_b=25; mu=0.005; % FX_ONL N_a=40; mu=0.005; mu_c=0.1; var(z) = 0.05; % % Questions % please feel free to contact: % Stephan Weiss EEB 426, Tel. (213) 740 4676 % email : saweiss@sipi.usc.edu
% ***** parameters ***** N_i = 2000; % number of iterations N_a = 40; % number of forward coeff. in main adapt. filter N_b = 35; % number of feedback coeff. " N_c = 5; % taps in error path N = max([N_a N_b N_c])+1; % important for synchronization of signals mu = 0.01; % step size for main adaptive filter mu_c= 0.1; % step size for online system identification
% ***** create implicitly primary path h and desired signal d(n)***** rho = 0.85; theta = 0.25*pi; gain = 0.25; % set characteristics d = gain*filter(1,[1 -2*rho*cos(theta) rho^2],x);
%***** create error path C and initialize the estimate C_hat ***** C = [zeros(1,N_c-1) 1]'; % error path impulse response: delay if strcmp(algorithm,'FX_ONL'), C_hat = zeros(size(N_c)); % online system identification else C_hat = C; % perfect error path model end;
% ***** adaptive noise cancellation ***** A = zeros(N_a,1); % initialize adaptive filter coeffs to zero B = zeros(N_b,1); clear y e1 e2 e3 x_hat; % to allow the tap delay lines to be filled with zeros, we start with % an index if strcmp(algorithm,'LMS'), % ***** LMS Algorithm ***** for n = 1:N_i-N, % [B.Widrow] X_a = x(n+N-1:-1:n+N-N_a); % built tap-delay line of filter y = X_a'*A; % calculate filter output e1(n+N-1) = d(n+N-1)-y; % calculate error A = A + (2*mu*e1(n+N-1))*X_a; % LMS weight update end; elseif strcmp(algorithm,'RLMS'), % ***** Recursive LMS **** y = zeros(N_i,1); % [P.Feintuch] for n = 1:N_i-N, X_a = x(n+N-1:-1:n+N-N_a); % built tap-delay line of filter A Y_b = y(n+N-2:-1:n+N-N_b-1); % built tap-delay line of filter B y(n+N-1) = X_a'*A + Y_b'*B; % calculate filter output
e1(n+N-1) = d(n+N-1)-y(n+N-1); % calculate error A = A + (2*mu*e1(n+N-1))*X_a; % LMS weight update filter A B = B + (2*mu*e1(n+N-1))*Y_b; % LMS weight update filter B end; elseif strcmp(algorithm,'FXLMS'), % ***** filtered-X LMS ***** for n = 1:N_i-N, % [B.Widrow] X_a = x(n+N-1:-1:n+N-N_a); % update tap-delay line of filter
y = X_a'*A; % calculate filter output e1(n+N-1) = d(n+N-1)+y; % calculate superposition E1 = e1(n+N-1:-1:n+N-N_c)'; % built tap-delay line e2(n+N-1) = E1'*C; % filter error signal by error path % e2 is the error visible to the % algorithm X_c = x(n+N-1:-1:n+N-N_c); % built tap-delay line delay to x_hat(n+N-1) = X_c'*C_hat; % filter signal by error path model % x_hat is the "filtered-X" signal X_hat = x_hat(n+N-1:-1:n+N-N_a)';% tap delay line for algorithm A = A - (2*mu*e2(n+N-1))*X_hat; % filtered-X LMS weight update end; elseif strcmp(algorithm,'FULMS'), % ***** filtered-U LMS ***** % [Erikkson] % ***** please add your owm omplementation ***** % ***** of the filtered-U LMS algorithm here ***** N_a = 15; N_b = 25; N = max([N_a N_b N_c])+1; mu = 0.005; A = zeros(N_a,1); B = zeros(N_b,1); y = zeros(N_i,1); for n = 1:N_i-N, X_a = x(n+N-1:-1:n+N-N_a); Y_b = y(n+N-2:-1:n+N-N_b-1); y(n+N-1) = X_a'*A + Y_b'*B; e1(n+N-1) = d(n+N-1)+y(n+N-1); E1 = e1(n+N-1:-1:n+N-N_c)'; % built tap-delay line e2(n+N-1) = E1'*C_hat; % filter error signal by error path % e2 is the error visible to the % algorithm X_c = x(n+N-1:-1:n+N-N_c); % built tap-delay line delay to x_hat(n+N-1) = X_c'*C_hat; % filter signal by error path model % x_hat is the "filtered-X" signal X_hat = x_hat(n+N-1:-1:n+N-N_a)';% tap delay line for algorithm Y_c = y(n+N-2:-1:n+N-N_c-1); y_hat(n+N-2) = Y_c'*C_hat;
elseif strcmp(algorithm,'FX_ONL'), % ***** filtered-X with online % system identification ***** % [Eriksson] % ***** please add your owm omplementation ***** % ***** of the filtered-X LMS algorithm with ***** % ***** online system identification *****
else error('algorithm type unknown'); end; % ***** Display **** % calculate optimum filter impulse response (truncated) dirac = [1.0 zeros(1,N-2)]'; h = gain*filter(1,[1 -2*rho*cos(theta) rho.^2],dirac); % % the optimum weight will be shown as red stars, while % the true adapted filter weight appear as yellow stem plots. % clg; if strcmp(algorithm,'LMS'), subplot(211); e1 = e1(N:N_i-1);
plot(10*log10(e1.^2)); title('mean squared error - LMS'); ylabel('MSE / [dB]'); xlabel('iterations'); subplot(212); stem(A); hold on; plot(h,'r*'); title('filter coefficients'); ylabel('coeff. value'); xlabel('taps'); print -dps lms; end; if strcmp(algorithm,'RLMS'), subplot(211); plot(10*log10(e1.^2)); title('mean squared error - RLMS'); ylabel('MSE / [dB]'); xlabel('iterations'); subplot(212); dirac = [1.0 zeros(1,N-2)]'; w = filter(A,[1 -B'],dirac); plot(h,'r*'); hold on; stem(w); title('filter coefficients'); ylabel('coeff. value'); xlabel('taps'); print -dps rlms; end; if strcmp(algorithm,'FXLMS'); subplot(211); plot(10*log10(e1.^2)); title('mean squared error - FXLMS'); ylabel('MSE / [dB]'); xlabel('iterations'); subplot(212) stem(A); hold on; plot(-h,'r*'); title('filter coefficients'); ylabel('coeff. value'); xlabel('taps'); print -dps fxlms;
end; if strcmp(algorithm,'FULMS'); subplot(211); plot(10*log10(e1.^2)); title('mean squared error - FULMS'); ylabel('MSE / [dB]'); xlabel('iterations'); subplot(212) dirac = [1.0 zeros(1,N-2)]'; w = filter(A,[1 -B'],dirac); plot(-h,'r*'); hold on; stem(w); title('filter coefficients'); ylabel('coeff. value'); xlabel('taps'); print -dps fulms; end;
Here is the LMS and RLS matlab codes..... LMS xLen = 2000; %sequence length hLen = 10; %filter length sigma_w2=0.0; %AWGN power D=0; delta=0.06; c = [1; 0.8]; %channel impulse response x = rand_sym(sqrt(2), xLen+200, 1); %x=real(x); d = [zeros(D,1); x]; %take delay into consideration y = conv(x,c); %y(1:D)=[]; %take delay into consideration w=sqrt(sigma_w2)/2*(randn(length(y),1)+j*randn(length(y),1)); y=y+w; ryy=y(101Len+100)'*y(101Len+100)/xLen h=zeros(hLen,1); h(D+1)=1; for ii=101Len+100 xhat(ii)=h.'*y(ii:-1:ii-hLen+1); e(ii)=d(ii)-xhat(ii); h=h+delta*e(ii)*conj(y(ii:-1:ii-hLen+1)); end hopt=h eq=conv(c,hopt) figure(1) stem(abs(eq)) figure(2) plot(10*log10(abs(e).^2)) figure(3) plot((abs(e).^2)) RLS xLen = 2000; %sequence length hLen = 10; %filter length sigma_w2=0.0; %AWGN power D=0; weight=1; c = [1; 0.8]; %channel impulse response x = rand_sym(sqrt(2), xLen+200, 1); %x=real(x); d = [zeros(D,1); x]; %take delay into consideration y = conv(x,c); %y(1:D)=[]; %take delay into consideration w=sqrt(sigma_w2)/2*(randn(length(y),1)+j*randn(length(y),1)); y=y+w; ryy=y(101Len+100)'*y(101Len+100)/xLen h=zeros(hLen,1); h(D+1)=1; Rinv=eye(hLen); for ii=101Len+100 yvec=y(ii:-1:ii-hLen+1); xhat(ii)=h.'*yvec; e(ii)=d(ii)-xhat(ii); K=Rinv*conj(yvec)/(weight+yvec.'*Rinv*conj(yvec)); Rinv=(Rinv-K*yvec.'*Rinv)/weight; h=h+K*e(ii);