Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

EXPERIMENT NO 5

Lab Title: FIXED POINT IMPLEMENTATION

Student Name: Abdur Rehman, M. Muneeb Khan, Noor-ul-Huda

Reg. No: 210312, 210276, 210294

Objective: Fixed point implementation and RMS error computation.

LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes (5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Ability to assimilate the
results
Effective use of lab
equipment and follows
the lab safety rules

Total Marks: ________________________ Obtained Marks: _______________________

LAB REPORT ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)
Data presentation
Experimental results
Conclusion

Total Marks: ________________________ Obtained Marks: _______________________

Date: ______________________________ark Signature: _______________________

Lab Tasks:
1. Task 1
a. Read a 2 sec audio signal in MATLAB.
b. Design a following filters in MATLAB using fdatool.
 LowPass
 BandPass
 HighPass
c. Import the coefficient to MATLAB workspace for each filter (as Num (b) or Den
(a)).
d. Perform equalizations by applying the designed filters on the input signal (using “
filter(b, a, signal)” as MATLAB command) and hear the resultant signal in
MATLAB.

Code:
clc
close all
clear all

% Read a 2-second audio signal


[audio, Fs] = audioread('audio.mp3');
duration = 2;
audio = audio(1:round(Fs*duration));

lowPassNum = [0.2 0.3];


lowPassDen = [1 0.8];

bandPassNum = [0.4 0.6];


bandPassDen = [1 0.5];

highPassNum = [0.7 1];


highPassDen = [1 0.2];

filteredLowPass = filter(lowPassNum, lowPassDen, audio);


filteredBandPass = filter(bandPassNum, bandPassDen, audio);
filteredHighPass = filter(lowPassNum, lowPassDen, audio);

time = linspace(0, duration, length(audio));

figure;

subplot(3,1,1);
plot(time, audio);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Original Audio');
subplot(3,1,2);
plot(time, filteredLowPass);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Low Pass Filtered');

subplot(3,1,3);
plot(time, filteredBandPass);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Band Pass Filtered (replace with High Pass for testing)');

suptitle('Audio Waveforms (Original and Filtered)');

sound(filteredLowPass, Fs);
pause(2);
sound(filteredBandPass, Fs);
pause(2);
sound(filteredHighPass, Fs);

Output:

Task 2:
a. Convert the read signal into 16 bit Fixed point precision using
appropriate Qn.m format (use sfi/fi/formula )
b. Convert the coefficients of each filter using appropriate Qn.m format
where w = 16.
c. Convert the code into fix point implementation
d. Run the converted code and hear the resultant sound
e. Calculate the MSE of floating point result and fixed point result
clc
close all
clear all

% Read a 2-second audio signal


[audio, Fs] = audioread('audio.mp3');
duration = 2;
audio = audio(1:round(Fs*duration));

% Design filters using fdatool (replace with actual filter design)

% Assuming you have designed filters and obtained coefficients


lowPassNum = [0.2 0.3];
lowPassDen = [1 0.8];

bandPassNum = [0.4 0.6];


bandPassDen = [1 0.5];

highPassNum = [0.7 1];


highPassDen = [1 0.2];

% Convert input signal to 16-bit signed fixed point with Q15.1 format
audio_fix = fi(audio, true, 16, 1);

% Convert filter coefficients to 16-bit signed fixed point with Q14.2 format
lowPassNum_fix = fi(lowPassNum, true, 16, 2);
lowPassDen_fix = fi(lowPassDen, true, 16, 2);

bandPassNum_fix = fi(bandPassNum, true, 16, 2);


bandPassDen_fix = fi(bandPassDen, true, 16, 2);

highPassNum_fix = fi(highPassNum, true, 16, 2);


highPassDen_fix = fi(highPassDen, true, 16, 2);

% Fixed-point implementation
filteredLowPass_fix = fi(zeros(size(audio)), true, 16, 1);
filteredBandPass_fix = fi(zeros(size(audio)), true, 16, 1);
filteredHighPass_fix = fi(zeros(size(audio)), true, 16, 1);

for n = 2:length(audio)
% Fixed-point low pass filtering
filteredLowPass_fix(n) = fi(lowPassNum_fix(1) * audio_fix(n-1) + lowPassNum_fix(2)
* audio_fix(n), true, 16, 1) + fi(lowPassDen_fix(2) * filteredLowPass_fix(n-1), true, 16,
1);

% Fixed-point band pass filtering (similar to low pass)


filteredBandPass_fix(n) = fi(lowPassNum_fix(1) * audio_fix(n-1) + lowPassNum_fix(2)
* audio_fix(n), true, 16, 1) + fi(bandPassDen_fix(2) * filteredBandPass_fix(n-1), true, 16,
1);

% Fixed-point high pass filtering (similar to low pass)


filteredHighPass_fix(n) = fi(highPassNum_fix(1) * audio_fix(n-1) +
highPassNum_fix(2) * audio_fix(n), true, 16, 1) + fi(lowPassDen_fix(2) *
filteredHighPass_fix(n-1), true, 16, 1);
end

% Convert fixed-point outputs to floating-point for playback and plotting


filteredLowPass = double(filteredLowPass_fix);
filteredBandPass = double(filteredBandPass_fix);
filteredHighPass = double(filteredHighPass_fix);

% Time vector for plotting


t = (0:length(audio)-1)/Fs;

% Plot original and filtered signals


figure(1);

% Original signal
subplot(4,1,1);
plot(t, audio);
title('Original Audio Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Low-pass filtered signal


subplot(4,1,2);
plot(t, filteredLowPass);
title('Low-Pass Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Band-pass filtered signal


subplot(4,1,3);
plot(t, filteredBandPass);
title('Band-Pass Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% High-pass filtered signal


subplot(4,1,4);
plot(t, filteredHighPass);
title('High-Pass Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');

Output:

Task 3:

a. Perform task 2 for w = 8, 12, 20, 24 and plot the resultant MSE
clc
close all
clear all

[audio, Fs] = audioread('audio.mp3');


duration = 2;
audio = audio(1:round(Fs*duration));

lowPassNum = [0.2 0.3];


lowPassDen = [1 0.8];

bandPassNum = [0.4 0.6];


bandPassDen = [1 0.5];

highPassNum = [0.7 1];


highPassDen = [1 0.2];

windowSizes = [8 12 20 24];
mse_results = zeros(length(windowSizes), 1);

for w_idx = 1:length(windowSizes)


w = windowSizes(w_idx);
audio_fix = fi(audio, true, 16, 1);

lowPassNum_fix = fi(lowPassNum, true, 16, 2);


lowPassDen_fix = fi(lowPassDen, true, 16, 2);

bandPassNum_fix = fi(bandPassNum, true, 16, 2);


bandPassDen_fix = fi(bandPassDen, true, 16, 2);

highPassNum_fix = fi(highPassNum, true, 16, 2);


highPassDen_fix = fi(highPassDen, true, 16, 2);

filteredLowPass_fix = fi(zeros(size(audio)), true, 16, 1);


filteredBandPass_fix = fi(zeros(size(audio)), true, 16, 1);
filteredHighPass_fix = fi(zeros(size(audio)), true, 16, 1);

for n = 2:length(audio)
filteredLowPass_fix(n) = fi(lowPassNum_fix(1) * audio_fix(n-1) + lowPassNum_fix(2) *
audio_fix(n), true, 16, 1) + fi(lowPassDen_fix(2) * filteredLowPass_fix(n-1), true, 16, 1);
filteredBandPass_fix(n) = fi(lowPassNum_fix(1) * audio_fix(n-1) + lowPassNum_fix(2) *
audio_fix(n), true, 16, 1) + fi(bandPassDen_fix(2) * filteredBandPass_fix(n-1), true, 16, 1);
filteredHighPass_fix(n) = fi(highPassNum_fix(1) * audio_fix(n-1) + highPassNum_fix(2) *
audio_fix(n), true, 16, 1) + fi(lowPassDen_fix(2) * filteredHighPass_fix(n-1), true, 16, 1);
end

filteredLowPass = double(filteredLowPass_fix);
filteredBandPass = double(filteredBandPass_fix);
filteredHighPass = double(filteredHighPass_fix);

mse_results(w_idx, 1) = immse(audio, filteredLowPass);


mse_results(w_idx, 2) = immse(audio, filteredBandPass);
mse_results(w_idx, 3) = immse(audio, filteredHighPass);
end

figure;
plot(windowSizes, mse_results(:,1), 'r', 'LineWidth', 2);
hold on;
plot(windowSizes, mse_results(:,2), 'g', 'LineWidth', 2);
plot(windowSizes, mse_results(:,3), 'b', 'LineWidth', 2);
legend('Low Pass', 'Band Pass', 'High Pass');
title('MSE for Different Window Siz es and Filters');
xlabel('Window Size');
ylabel('MSE');

Output:
Conclusion:

In this lab, we delved into the realm of fixed-point implementation, a crucial technique in digital
signal processing. We converted floating-point numbers into fixed-point format, and applied this
conversion to various signal processing tasks, including filtering and audio processing. This hands-
on exploration underscored the importance of fixed-point arithmetic in optimizing computational
efficiency and hardware resource utilization, especially in resource-constrained environments. It
also highlighted the trade-offs between precision and range when choosing the appropriate Q-
format. This experience reinforces our understanding of fixed-point implementation as a critical
skill in the design and optimization of digital systems.

You might also like