FIR AND IIR Report Using Matlap
FIR AND IIR Report Using Matlap
Prepared by:
Ahmed Mohey mohamed
Hesham Fathy Saber
Seif Mostafa Kamel
Mina Maher Roshdy
1. Code Initialization
Before starting the simulation, the environment is cleaned to ensure accurate results:
• clc → Clears the command window.
• clear → Removes all variables from the MATLAB workspace.
• close all → Closes all open figure windows.
This ensures that previous data or plots do not interfere with the current session.
2. Simulation Parameters
The following parameters are defined to control the simulation:
These parameters form the time base for signal generation and filtering.
3. Signal Generation
To simulate a real-world scenario, two signals are created:
The goal is to train the filter to recover the clean signal from the noisy input.
• Averaging Error:
After each epoch, the average error is stored for plotting a learning curve, showing how the
filter improves over time.
%% PARAMETERS
N = 300;
Fs = 100;
Ts = 1/Fs;
n = 1:N;
order = 5;
mu = 0.01;
%% TRAINING PHASE
% Training signals
Desired_train = sin(2 * pi * Ts * n);
Noise_train = 0.5 * (rand(1, N) - 0.5);
X_train = Desired_train + Noise_train;
% Initialize
w = zeros(order, 1);
y_train = zeros(1, N);
e_train = zeros(1, N);
% LMS Training
for i = order+1:N
x_vec = X_train(i-1:-1:i-order)';
y_train(i) = w' * x_vec;
e_train(i) = Desired_train(i) - y_train(i);
w = w + mu * x_vec * e_train(i);
end
% Save weights
save('fir_weights.mat', 'w');
%% TESTING PHASE
% New test signals
Desired_test = sin(2 * pi * Ts * n);
Noise_test = 0.5 * (rand(1, N) - 0.5);
X_test = Desired_test + Noise_test;
% Parameters
N = 300; % total samples
Fs = 100; % Sampling frequency
Ts = 1/Fs;
n = 1:N;
% Filter parameters
L = 10; % Filter taps
mu0 = 0.05;
epsilon = 1e-6;
Matlab code
clc; clear;
close all;
% Parameters
N = 300; % Number of samples
Fs = 100; % Sampling frequency
Ts = 1/Fs; n
= 1:N;
% Generate signals
Desired = sin(2 * pi * Ts * n); % Clean desired signal
Noise = 0.5 * (rand(1, N) - 0.5); % Random noise
X = Desired + Noise; % Input = desired + noise
e = Desired(i) - y(i);
epoch_error = epoch_error + abs(e);
% LMS-like adaptation
b1(i) = b1(i) + u * e * X(i-1);
b2(i) = b2(i) + u * e * X(i-2);
b3(i) = b3(i) + u * e * X(i-3);
a1(i) = a1(i) - u * e * y(i-1);
a2(i) = a2(i) - u * e * y(i-2);
a3(i) = a3(i) - u * e * y(i-3); end
error_history(epoch) = epoch_error / (N - 3); end
matlab result
Matlap Testing code
- Test at the different input signal
clc;
clear;
close all;
% Parameters
N = 300; % Number of samples
Fs = 100; % Sampling frequency
Ts = 1/Fs;
n = 1:N;
%% Training Phase
for epoch = 1:75
epoch_error = 0;
u = u0 / sqrt(epoch); % Decreasing learning rate
for i = 4:N
y_train(i) = b1(i)*X_train(i-1) + b2(i)*X_train(i-2) +
b3(i)*X_train(i-3) ...
- a1(i)*y_train(i-1) - a2(i)*y_train(i-2) -
a3(i)*y_train(i-3);
e = Desired_train(i) - y_train(i);
epoch_error = epoch_error + abs(e);
% LMS-like adaptation
norm_factor = 1e-6 + X_train(i-1)^2 + X_train(i-2)^2 + X_train(i-
3)^2 + y_train(i-1)^2 + y_train(i-2)^2 + y_train(i-3)^2;
b1(i) = b1(i) + (u * e * X_train(i-1)) / norm_factor;
b2(i) = b2(i) + (u * e * X_train(i-2)) / norm_factor;
b3(i) = b3(i) + (u * e * X_train(i-3)) / norm_factor;
a1(i) = a1(i) - (u * e * y_train(i-1)) / norm_factor;
a2(i) = a2(i) - (u * e * y_train(i-2)) / norm_factor;
a3(i) = a3(i) - (u * e * y_train(i-3)) / norm_factor;
end
train_error_history(epoch) = epoch_error / (N - 3);
end
%% Testing Phase
Desired_test = sin(2 * pi * 3 * Ts * n) + 0.4 * sin(2 * pi * 6 * Ts * n);
Noise_test = 0.5 * (rand(1, N) - 0.5);
X_test = Desired_test + Noise_test;
%% Plot outputs
figure;
plot(n, Desired_train, 'g', 'LineWidth', 1.5); hold on;
plot(n, X_train, 'r--');
plot(n, y_train, 'b');
legend('Desired (Train)', 'Noisy Input (Train)', 'IIR Output (Train)');
title('Training Phase Output');
xlabel('Sample Index'); ylabel('Amplitude');
figure;
plot(n, Desired_test, 'k--', 'LineWidth', 1.5); hold on;
plot(n, X_test, 'm:');
plot(n, y_test, 'c');
legend('Desired (Test)', 'Noisy Input (Test)', 'IIR Output (Test)');
title('Testing Phase Output');
xlabel('Sample Index'); ylabel('Amplitude');