Poc Lab 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

American International University - Bangladesh

Department of Electrical and Electronic Engineering


EEE 3215: PRINCIPLES OF COMMUNICATION

Semester: Fall 2024-2025 Section: C

Experiment Number: 03.

Title: Analog Signal quantization using MATLAB.


Submitted to-

DR. SHUVRA MONDAL


ASSISTANT PROFESSOR, Dept. of EEE
Faculty of Engineering, AIUB

Submitted by-
Group Number: 03

Group Members-
SL NAME ID

11 DAIYAN, F.M. MAHIR 22-49716-3

12 MITU, RIFAT ARA 22-47744-2

13 AKTER,AIRIN 22-46334-1

14 LABIB, MUSHFIQUE REDWAN 22-47132-1

15 ZAMAN, MD. SHAHRIAR 22-47754-2

Submission Date:11/11/2024

Faculty of Engineering, AIUB Dept. of Electrical & Electronics Engineering (EEE)


Experimental Findings (MATLAB Commands):

Example of an analog signal quantization in MATLAB


Command: Am=4; %setting the sine wave amplitude
bit=3; % setting the number btis
f=1; %sine wave frequency
fs=30; % setting sampling frequency
t=0:1/fs:1*pi; %defining the signal duration for plotting
y=Am*sin(2*pi*f*t); %sampled sine wave y(30 samples in a cycle)
Nsamples=length(y); %calculating the total number of samples
quantised_out=zeros(1,Nsamples); %making an array of size=number of samples midrise
del=2*Am/(2^bit); %determining the step size
Llow=-Am+del/2;
Lhigh=Am-del/2;
for i=Llow:del:Lhigh %Iterating from lowest to the highest levels
for j=1:Nsamples %taking the whole sampled vector
if(((i-del/2)<y(j))&&(y(j)<(i+del/2))) % Quantizing the sampled value
quantised_out(j)=i; % to the quantization level if it
end % lies within the bound of -del/2 and
end % del/2 of the level.
end
stem(t,quantised_out); %plotting wave forms.
hold on;
plot(t,y,'color','r');
OUTPUT

Explanation: The provided MATLAB code simulates the quantization process for a sine wave.

1. Amplitude and Quantization Settings:


o Am = 4; sets the amplitude of the sine wave to 4.
o bit = 3; defines the number of bits for quantization, which determines the number of
quantization levels.
2. Frequency and Sampling Parameters:
o f = 1; sets the sine wave frequency to 1 Hz.
o fs = 30; sets the sampling frequency to 30 Hz.
o t = 0:1/fs:1*pi; defines the time vector over one period of the sine wave, from 0 to π,
sampled at intervals of 1/fs1/fs1/fs.
3. Sine Wave Generation:
o y = Am*sin(2*pi*f*t); generates the sampled sine wave with amplitude Am,
frequency f, over time vector t.
o Nsamples = length(y); calculates the total number of samples in the sine wave.
4. Quantization Settings:
o quantised_out = zeros(1,Nsamples); initializes an array to store quantized values, with
the same length as y.
o del = 2*Am/(2^bit); calculates the quantization step size del based on the amplitude
and the number of bits.
o Llow and Lhigh define the range of quantization levels from the minimum to the
maximum quantization level.
5. Quantization Process:
o The outer loop for i = Llow:del:Lhigh iterates through each quantization level.
o Inside this loop, for each sample y(j), it checks if the sample falls within the range
(i−del/2)(i - \text{del}/2)(i−del/2) to (i+del/2)(i + \text{del}/2)(i+del/2).
o If a sample falls within this range, it assigns i as the quantized output for that sample.
6. Plotting the Results:
o stem(t, quantised_out); plots the quantized output as a stem plot, showing the discrete
quantized levels.
o hold on; keeps the quantized plot on the figure.
o plot(t, y, 'color', 'r'); overlays the original sine wave in red on the same plot for
comparison.

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.

Another example quantization in MATLAB

Command: fs=40e3;% sampling frequency

fc=4e3;% frequency of the signal

t=0:1/fs:0.001;%discrete time

x=0.5*sin(2*pi*fc*t);% discrete signal

%--------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);% breaking the window figure to plot both graphs


stem(t,x,'b');% plot of discrete time signaltitle('Discrete time representation')% title of the figure

xlabel('time(s)')% label on the x-axis of the plot

ylabel('X[n]')% label on the y-axis of the plot

subplot(2,1,2);

stairs(t,xq,'b');% the quantized output

title('Quantized Signal')% title of the figure

xlabel('time')% label on the x-axis of the plot

ylabel('amplitude')% label on the y-axis of the plot

OUTPUT

Explanation: This MATLAB code simulates the process of quantizing a continuous-time


sinusoidal signal into a discrete-time signal with a finite number of levels.

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.

Performance Task for Lab Report: (ID = 22-47754-2)

Generate an analog signal using the following equation,

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,3);% breaking the window figure to plot both graphs

stem(t,X,'b');% plot of discrete time signal

title('Discrete time representation')

xlabel('time(s)')% label on the x-axis of the plot

ylabel('X[n]')% label on the y-axis of the plot

subplot(3,2,5);

stairs(t,xq,'b');% the quantized output

title('Quantized Signal')% title of the figure

xlabel('time')% label on the x-axis of the plot

ylabel('amplitude')% label on the y-axis of the plot

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,4);% breaking the window figure to plot both graphs

stem(t,X1,'b');% plot of discrete time signal

title('Discrete time representation')

xlabel('time(s)')% label on the x-axis of the plot

ylabel('X1[n]')% label on the y-axis of the plot

subplot(3,2,6);

stairs(t,xq1,'b');% the quantized output

title('Quantized Signal')% title of the figure

xlabel('time')% label on the x-axis of the plot

ylabel('amplitude')% label on the y-axis of the plot

OUTPUT
Explanation: This MATLAB code performs signal generation, sampling, and quantization on two
analog signals. Let's break down the functionality of each section.

Step 1: Initialize Constants

• 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.

Step 2: Generate Analog Signals

• 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

1. Quantization Level and Step Size Calculation:


o n = 4; sets the number of bits for quantization.
o L = (2^n) - 1; calculates the total quantization levels.
o del = (max(X) - min(X)) / L; determines the step size for quantization.
2. Quantization Process:
o xq = min(X) + (round((X - min(X)) / del)) .* del; quantizes X by mapping each value
of X to the nearest quantization level.
o xq3_2 = min(X) + round((xq3_2v - min(X)) / del) .* del; quantizes the value xq3_2v
(3.2) for comparison.
3. Binary Representation:
o b = round((X - min(X)) / del); computes the quantized levels as integers.
o dec2bin(b); converts the quantized levels to binary format.
Step 4: Plot Analog, Discrete, and Quantized Signals for X

• subplot(3,2,1); plot(t, X, 'R'); plots the original analog signal X in red.


• subplot(3,2,3); stem(t, X, 'b'); plots the sampled signal X as a discrete-time representation.
• subplot(3,2,5); stairs(t, xq, 'b'); plots the quantized version of X using stairs to show step
changes at each quantization level.

Step 5: Quantization of X1 (Similar to X Quantization)

• Steps similar to X are repeated to quantize X1.


o The step size (del) is recalculated based on X1.
o xq1 = min(X1) + (round((X1 - min(X1)) / del)) .* del; quantizes X1.
o xq13_2 computes the quantized version of 3.2 for comparison within the X1 range.
o b and dec2bin(b) are recalculated for binary conversion.

Step 6: Plot Analog, Discrete, and Quantized Signals for X1

• subplot(3,2,2); plot(t, X1, 'R'); plots X1 as an analog signal.


• subplot(3,2,4); stem(t, X1, 'b'); plots X1 in discrete-time.
• subplot(3,2,6); stairs(t, xq1, 'b'); plots the quantized version of X1.

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.

Discussion & Conclusion:

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

You might also like