% Vanshika Chauhan
% 23103340
% B 12
clc;
clear;
disp('Generate 0.02-second sine wave of 100 Hz');
f = 100;
amp = 4;
sf = 3200;
T = 1/sf;
t = 0:T:0.02;
y = amp * sin(2*pi*f*t);
y1 = y;
nbits = 4;
l = 2^nbits;
mn = min(y);
mx = max(y);
s = (mx - mn) / l;
% Mid-rise quantizer
for j = mn : s : mx - s
y(y >= j & y < j + s) = j + s/2;
end
% Figure 1: Subplots for signal, error, and error vs quantized signal
figure;
subplot(3,1,1);
stem(1:length(y), y, 'r');
title('Quantized (Sampled) Signal');
xlabel('Sample Number');
ylabel('Amplitude');
grid on;
subplot(3,1,2);
e = y1 - y;
plot(e, 'm');
xlabel('Sample Number');
ylabel('Quantization Error');
title('Quantization Error');
grid on;
subplot(3,1,3);
plot(y, e, '.');
xlabel('Quantized Signal');
ylabel('Quantization Error');
title('Quantization Error vs. Input Signal');
grid on;
% Figure 2: Transfer characteristics of quantizers
figure;
x = -5:0.01:5; % Input range
% Mid-tread quantizer
s_tread = 1;
y_tread = s_tread * round(x / s_tread);
subplot(1,2,1);
plot(x, y_tread, 'k', 'LineWidth', 1.5);
1
grid on;
title('Mid-Tread Quantizer');
xlabel('Input level');
ylabel('Output level');
xlim([-5 5]);
ylim([-5 5]);
% Mid-rise quantizer
s_rise = 1;
y_rise = s_rise * floor(x / s_rise) + s_rise/2;
subplot(1,2,2);
plot(x, y_rise, 'b', 'LineWidth', 1.5);
grid on;
title('Mid-Rise Quantizer');
xlabel('Input level');
ylabel('Output level');
xlim([-5 5]);
ylim([-5 5]);
Generate 0.02-second sine wave of 100 Hz
2
3
Published with MATLAB® R2024b