Professional Documents
Culture Documents
Lab 5
Lab 5
EXPERIMENT NO 5
LAB ASSESSMENT:
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
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)');
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
% 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);
% 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);
% Original signal
subplot(4,1,1);
plot(t, audio);
title('Original Audio 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
windowSizes = [8 12 20 24];
mse_results = zeros(length(windowSizes), 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);
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.