Poc Lab 3
Poc Lab 3
Poc Lab 3
Submitted by-
Group Number: 03
Group Members-
SL NAME ID
13 AKTER,AIRIN 22-46334-1
Submission Date:11/11/2024
Explanation: The provided MATLAB code simulates the quantization process for a sine wave.
This MATLAB code generates and quantizes a sine wave, where the quantized values are based
on a mid-rise quantizer. It plots both the original sine wave and the quantized output to illustrate
the quantization effect.
t=0:1/fs:0.001;%discrete time
%--------Quantization------------%
n=8;
L=(2^n)-1;
delta=(max(x)-min(x))/L;
xq=min(x)+(round((x-min(x))/delta)).*delta;
%--------END------------%
subplot(2,1,1)
stem(t,x,'R');
subplot(2,1,2);
OUTPUT
1. Signal Creation:
o fs = 40e3;
Sets the sampling frequency to 40 kHz.
o fc = 4e3;
Defines the frequency of the signal as 4 kHz.
o t = 0:1/fs:0.001;
Creates a time vector t with discrete time steps from 0 to 1 ms (0.001 seconds),
sampled at fs (40 kHz).
o x = 0.5*sin(2*pi*fc*t);
Generates a sinusoidal signal x with amplitude 0.5 and frequency 4 kHz over the
time vector t.
2. Quantization:
o n = 8;
Sets the bit depth of the quantizer to 8 bits (i.e., 256 levels).
o L = (2^n) - 1;
Calculates the number of quantization levels as 255 (2^8 - 1), assuming the signal
uses a signed representation.
o delta = (max(x) - min(x)) / L;
Computes the quantization step size delta, which is the difference between
consecutive quantization levels. It is determined by the range of the signal (max(x) -
min(x)) divided by the number of levels L.
o xq = min(x) + (round((x - min(x)) / delta)) * delta;
Quantizes the signal x by mapping its values to the nearest quantization level. The
signal is first shifted to the minimum value, scaled by the quantization step size
delta, and then rounded to the nearest integer, effectively quantizing it to the set
levels.
3. Plotting:
o subplot(2,1,1)
Divides the figure into 2 subplots (2 rows, 1 column), and activates the first subplot.
o stem(t, x, 'R');
Plots the original discrete signal x as a stem plot in red color ('R').
o subplot(2,1,2);
Activates the second subplot to plot the quantized signal.
o stairs(t, xq, 'b');
Plots the quantized signal xq as a stair-step plot in blue color ('b').
o title('Quantized Signal')
Adds a title to the second subplot showing the quantized signal.
o xlabel('time') and ylabel('amplitude')
Labels the x-axis as time and the y-axis as amplitude for both plots.
The MATLAB code demonstrates how to take a continuous signal (x), quantize it into discrete
levels (xq), and then compare the original signal with the quantized signal through visual plots.
The first subplot shows the original signal, and the second subplot displays the quantized signal.
x1(t) = A1 cos(2π(CDE*100)t )
Command:
A1 = 96;
A2 = 24;
CDE = 469;
f_s=400e3;
t=0:1/f_s:10e-5;
X=A1*cos(2*pi*CDE*100*t);
X1=A2*cos(2*pi*CDE*100*t);
xq3_2v=3.2;
n=4;
L=(2^n)-1
del=(max(X)-min(X))/L;
StepSize=del
xq=min(X)+(round((X-min(X))/del)).*del;
xq3_2=min(X)+round((xq3_2v-min(X))/del).*del
b = round((X-min(X))/del);
dec2bin(b)
subplot(3,2,1);
plot(t,X,'R');
title('Analog Signal')
xlabel('time(s)')
ylabel('X[t]')
subplot(3,2,5);
del=(max(X1)-min(X1))/L;
StepSize=del
xq1=min(X1)+(round((X1-min(X1))/del)).*del;
xq13_2=min(X1)+round((xq3_2v-min(X1))/del).*del
b = round((X1-min(X1))/del);
dec2bin(b)
subplot(3,2,2);
plot(t,X1,'R');
title('Analog Signal')
xlabel('time(s)')
ylabel('X1[t]')
subplot(3,2,6);
OUTPUT
Explanation: This MATLAB code performs signal generation, sampling, and quantization on two
analog signals. Let's break down the functionality of each section.
• A1 = 47;, A2 = 25;, and CDE = 477; are constants representing amplitudes and frequency
components of the signals.
• f_s = 400e3; sets the sampling frequency to 400,000 Hz.
• t = 0:1/f_s:10e-5; creates a time vector with samples spaced at 1/f_s, covering up to 10
microseconds.
• X = A1 * cos(2 * pi * CDE * 100 * t); generates the first analog signal X as a cosine wave
with amplitude A1 and frequency CDE * 100.
• X1 = A2 * cos(2 * pi * CDE * 100 * t); generates a second analog signal X1 with amplitude
A2 and the same frequency.
Step 3: Quantization of X
In this MATLAB code generates, samples, and quantizes two analog signals, then visualizes the
analog, discrete-time, and quantized signals in a multi-plot layout for comparison.
In this lab, we explored analog signal quantization in MATLAB, focusing on the effects of
sampling rate, bit depth, and quantization error. We generated a sinusoidal signal at 4 kHz with a
40 kHz sampling rate and quantized it using an 8-bit quantizer, resulting in a digital signal with
256 discrete levels. Quantization maps each analog sample to the nearest level, introducing
quantization error—the difference between the original and quantized values. This error, which can
add noise to the signal, is more noticeable with lower bit depths.Higher bit depths improve signal
accuracy by reducing quantization error, though they increase computational demands. This lab
highlighted the balance between signal fidelity and resource efficiency in analog-to-digital
conversion, providing insights applicable in digital communications, audio engineering, and other
fields reliant on accurate digital signal representation.
Software:
MATLAB2016a