I am working on FxLMS Algorithm for noise cancellation what changes i have to do in my code so it work properly

7 views (last 30 days)
clc;
clear;
M = 16;
mu = 0.001;
f = 1000;
t = 0.01:0.001:0.5;
b = 1:200;
correct = sin(2*pi*t*f);
len = length(correct);
rng(0);
intrupt = 0.002 *randn(len);
x = correct + intrupt;
xref = x;
primary = [0.8 0.6 -0.2 -0.5 -0.1 0.4 -0.05];
y_d = filter(primary,1,x);
secondary = [0.9 0.6 0.1 -0.4 -0.1 0.2 0.1 0.01 0.001];
w = zeros(1,M);
xn = zeros(1,M);
e = zeros(1,M);
xhx =zeros(1,M);
for j = 1:len
xn = [xref(j) xn(1:end-1)];%secondary path adaptive filter
y_Out = sum(w.* xn);%conv of adaptive
y_ldsp = [y_Out secondary(1:end-1)];
y_final = sum(w.* secondary);%here w means weight of ldsp
e(j) = y_d(j) - y_final;
fil_x = [xref(j) secondary(1:end-1)];
LMS_x = [sum(fil_x.*secondary) xhx(1:end-1)];
w = w + mu*e(j)*LMS_x;
end

Answers (1)

Hitesh
Hitesh on 18 Oct 2024
Hi Rajendra,
You are on the right track to implementing the FxLMS Algorithm for Noise Cancellation. Kindly ensure that reference signal is filtered through secondary path before the weight updates and recheck the weight updating equation in your code. Here are some potential improvements and corrections to your code:
Secondary Path Filtering:
  • You need to ensure that the reference signal "xn" is filtered through the secondary path model before the weight update. This is crucial for the FxLMS algorithm.
Correct Weight Update:
  • You can recheck the weight updating equation to use the filtered reference signal xhx instead of the raw reference signal xref.
Signal Initialization:
  • You need to change "intrupt" and "x" vectors to column vectors for consistency.
Error Calculation:
  • The error "e(j)" is now computed using the first element of the filtered output "y_final" to ensure proper alignment with the desired response "y_d(j)".
% Parameters
M = 16; % Adaptive filter length
mu = 0.001; % Step size
f = 1000; % Frequency of the input signal
t = 0.01:0.001:0.5; % Time vector
len = length(t);
% Generate signals
correct = sin(2*pi*t*f); % Desired signal
rng(0); % Seed for reproducibility
intrupt = 0.002 * randn(len, 1); % Noise
x = correct' + intrupt; % Noisy signal
xref = x; % Reference signal
% Primary and secondary path filters
primary = [0.8 0.6 -0.2 -0.5 -0.1 0.4 -0.05];
y_d = filter(primary, 1, x); % Desired response
secondary = [0.9 0.6 0.1 -0.4 -0.1 0.2 0.1 0.01 0.001];
% Initialize adaptive filter
w = zeros(1, M); % Weights
e = zeros(len, 1); % Error signal
% Adaptive filtering
for j = 1:len
% Shift and update the input vector
xn = [xref(j); zeros(M-1, 1)];
% Output of adaptive filter
y_Out = w * xn;
% Filter output through the secondary path
y_final = filter(secondary, 1, y_Out);
% Error calculation
e(j) = y_d(j) - y_final(1);
% Filter the reference signal through the secondary path
xhx = filter(secondary, 1, xn);
% Update weights using filtered reference signal
w = w + mu * e(j) * xhx';
end
% Plot results
figure;
subplot(2, 1, 1);
plot(t, correct);
title('Original Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2, 1, 2);
plot(t, e);
title('Error Signal');
xlabel('Time (s)');
ylabel('Amplitude');
For more information on "Active Noise Control Using a Filtered-X LMS FIR Adaptive Filter", refer to the below MATLAB documentation:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!