Professional Documents
Culture Documents
ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
Lab Number 05
Code:
% Parameters fd1 = 125; % Sampling frequency of the first signal fd2 = 150; % Sampling
frequency of the second signal N = 250; % Length of the signal digits = input('Enter number of
significant digits: '); % Number of significant digits method = input('Enter quantization method
(round-off/floor/ceil): ', 's'); % Quantization method
t = (0:N-1); x1 =
sin(2*pi*fd1*t/fd1)
; x2 =
sin(2*pi*fd2*t/fd2)
;
case 'round-off'
xq =
round(x*10^digits)/10^digits; case
'floor'
xq = floor(x*10^digits)/10^digits;
case 'ceil'
xq = ceil(x*10^digits)/10^digits;
otherwise
error('Invalid quantization
method!'); end
SQNR = 10*log10(var(x)/var(e));
figure;
subplot(2,1,1);
plot(t, x);
title('Original
Signal');
xlabel('Time
(samples)');
ylabel('Amplitude')
; subplot(2,1,2);
plot(t, xq);
title('Quantized
Signal');
xlabel('Time
(samples)');
ylabel('Amplitude');
ylabel('Amplitude');
Code:
f = 100;
fs = 10000;
t = (0:fs-1) / fs;
% Initialize figure
figure;
for i = 1:length(n_bits)
x = sin(2 * pi * f * t);
n = n_bits(i);
Output:
Audio signal quantization to various bits per
sample
Use your recorded voice in last session and quantize it at 1 bit /sample.
Change bit depth to 2,3,4 and then listen and take notes of your
observations. Decide no. of bits for audio until quality stops improving.
Code:
% Load audio file
[x, Fs] =
audioread('PinkPanther30.wav');
plot(x); title('Original
Audio Signal');
xlabel('Sample
Number');
ylabel('Amplitude');
q = 2^(num_bits(i)-1);
x_quantized = round(x*q)/q;
% Plot quantized audio signal
figure;
plot(x_quantized);
ylabel('Amplitude');
sound(x_quantized, Fs);
Output: