Lab 2
Lab 2
We learned about Pulse Code Modulation (PCM), a technique that is used to convert an analog
signal to a digital signal. In this lab you will implement a Pulse Code Modulator as a C
program.
In this lab, you will implement the Pulse Code Modulator in a modular way where each
module will be responsible for a specific PCM function. Moreover, to test and showcase the
operation of your PCM program, you will need to implement a simple analog signal
generator. Thus, the following modules need to be implemented.
2. Sampler
3. Quantizer
4. Encoder
Rest of this document provides instructions regarding implementing these four components.
For simplicity, we assume only a sinusoidal analog will be used for this lab. As you should
already know, any sinusoidal signal can be represented with
𝑥(𝑡) = 𝐴 sin(𝜔𝑡 + 𝜎)
CS2033 Data Communication and Networks - Lab 2
Where t is time, A is the peak amplitude, 𝜔 is the angular frequency in radians, and 𝜎 is the
phase in radians. duration is the number of time units that the signal exists.
The analog signal generator function will take as input A, omega, sigma, and t, and
output the signal value x(t) at time t given the input values.
The sampler function takes as input a pointer to an array where samples will be stored, the
sampling interval and the analog signal and updates the samples array with the relevant
sample values.
The quantizer function takes as input a pointer to the samples array, a pointer to the
pcmpulses array where the output of quantizer will be stores, and the quantization levels,
and produces PCM pulses accordingly.
For simplicity, in this lab assignment, please take the “floor” value as the quantized value.
Quantization logic can be expressed with the following formula:
𝑥(𝑡) − 𝐴𝑚𝑖𝑛
𝑄𝑙𝑒𝑣𝑒𝑙 (𝑡) = ⌊(( ) × 𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑄𝑢𝑎𝑛𝑡𝑖𝑧𝑎𝑡𝑖𝑜𝑛 𝐿𝑒𝑣𝑒𝑙𝑠)⌋
𝐴𝑚𝑎𝑥 − 𝐴𝑚𝑖𝑛
When 𝑥(𝑡) = 𝐴𝑚𝑎𝑥 the quantization level should be set to maximum quantization level.
The encoder takes as input a pointer to pcmpulses array, a pointer to dsignal array where
output will be stored, and the number of bits used for a single PCM code. It produces the
PCM codes for each pulse recorded and stores in dsignal as a binary number stream.
In addition to the main components, you need to have a run() function that does the
following:
1. Reads the following input parameters from the stdin. These will be given as space-
separated values.
Example
A=3
omega = 2
sigma = 0
duration = 5
interval = 1
encoderbits = 2
Thus, you should generate the sine wave using the give values, take samples every 1 time
unit, assign samples into one of 4 quantization levels (since we are using 2 encoderbits as per
the input string), and produce the encoder output bitstream. Please note that the
quantization levels should span from the minimum peak to the maximum peak. In other
words, quantization levels should cover both -3 as well as +3. In this example, quantization
level 0 should start at -3 and, quantization level 3 should correspond to +3.
When t = 0; x(0) = 0
When t = 1; x(1) = 2.73
When t = 2; x(2) = -2.27
When t = 3; x(3) = -0.84
When t = 4; x(4) = 2.97
When t = 5; x(5) = -1.63
𝑥(𝑡)−𝐴𝑚𝑖𝑛
𝑄𝑙𝑒𝑣𝑒𝑙 (𝑡) = ⌊(( ) × 𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑄𝑢𝑎𝑛𝑡𝑖𝑧𝑎𝑡𝑖𝑜𝑛 𝐿𝑒𝑣𝑒𝑙𝑠)⌋
𝐴𝑚𝑎𝑥 −𝐴𝑚𝑖𝑛
When t = 0; x(0) = 0
0−(−3)
𝑄𝑙𝑒𝑣𝑒𝑙 (0) = ⌊(3−(−3)) × 4 ⌋ = 2
−2.27−(−3)
𝑄𝑙𝑒𝑣𝑒𝑙 (2) = ⌊( 3−(−3)
) ×4⌋=0
Thus, the PCM encoded output binary stream for the given analog signal is:
101100011100
Please try out different analog signals by changing the input parameters test the
correctness of your program before submitting.
Submission Instructions
You have been provided with a code template that consists of the following files:
lab2.c
mypcm.c
mypcm.h
Makefile
You should submit the completed source code into the coderunner autograded quiz on
moodle like you did for lab1.
Note: since this is a brand-new lab designed for your batch, there may be glitches. If you
come across any such glitches, kindly inform me ([email protected]).
Please note that this is an individual assignment, and those who are caught plagiarizing
will be penalized and will be reported for disciplinary action.