0% found this document useful (0 votes)
112 views

PCS LAB MANUAL 2024-25

The document is a lab manual for the Principles of Communication Systems course at Atria Institute of Technology, outlining the course objectives, outcomes, and a list of experiments for the fourth semester Electronics and Communication Engineering students. It covers various topics including analog modulation schemes, signal representation, and digital signal processing. The manual includes detailed explanations and MATLAB code for experiments related to signal generation and analysis.

Uploaded by

Manu Harsh T.S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

PCS LAB MANUAL 2024-25

The document is a lab manual for the Principles of Communication Systems course at Atria Institute of Technology, outlining the course objectives, outcomes, and a list of experiments for the fourth semester Electronics and Communication Engineering students. It covers various topics including analog modulation schemes, signal representation, and digital signal processing. The manual includes detailed explanations and MATLAB code for experiments related to signal generation and analysis.

Uploaded by

Manu Harsh T.S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

ATRIA INSTITUTE OF

TECHNOLOGY
(Affiliated To Visvesvaraya Technological University, Belgaum)
Anandanagar, Bangalore-24

Department of Electronics and


Communication Engineering
ESTD: 2000

PRINCIPLES OF COMMUNICATION
SYSTEMS LAB
MANUAL

FOURTH SEMESTER
SUBJECT CODE: BEC402

2023-2024

Scrutinized by:
Dr. Jagadeesh H S

HOD, ECE

Dept of ECE, Atria.I.T 1


ATRIA INSTITUTE OF TECHNOLOGY
(Affiliated o Visvesvaraya Technological University, Belgaum)Anandanagar,
Bangalore-24

DEPARTMENT OF
ELECTRONICS AND COMMUNICATION ENGINEERING

PRINCIPLES OF COMMUNICATION
SYSTEMS LAB MANUAL

Principles of Communication Systems Lab Manual pertaining to Fourth Semester


Electronics and Communication Engineering has been prepared as per the syllabus
prescribed by Visvesvaraya Technological University. All the experiments given in the Lab
Manual are conducted and verified as per the experiment list.

Dr. Keshavamurthy Prof. B. S. Somesh


Lab-In charge, Lab-In charge,
Professor Assistant Professor

Lab Instructor
Mrs.DivyashreHS
Mr.Shobith Dr. Jagadeesh H S
Professor & Head, Dept. of ECE
ATRIA INSTITUTE OF TECHNOLOGY
ASKB Campus, 1st Main Rd, AGS Colony, Anand Nagar, Hebbal, Bengaluru, Karnataka 560024, India
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING

Course: PRINCIPLES OF COMMUNICATION SYSTEMS LAB

Course Code: BEC402

Semester 4th

COURSE LEARNING OBJECTIVES

This course will enable students to:

1 Understand and analyse concepts of Analog Modulation schemes viz; AM, FM.

2 Design and analyse the electronic circuits for AM and FM modulation and demodulation.

3 Understand the concepts of random variable and random process to model communication
systems.
4 Understand and analyse the concepts of digitization of signals.

5 Evolve the concept of SNR in the presence of channel induced noise.

COURSE OUTCOMES

At the end of the course the student will be able to:

CO1 Understand the principles of analog communication systems and noise modelling.

CO2 Identify the schemes for analog modulation and demodulation and compare their performance.
CO3 Design of PCM systems through the processes sampling, quantization and encoding.

CO4 Describe the ideal condition, practical considerations of the signal representation for base
band transmission of digital signals.
CO5 Identify and associate the random variables and random process in Communication system
design.
ATRIA INSTITUTE OF TECHNOLOGY
ASKB Campus, 1st Main Rd, AGS Colony, Anand Nagar, Hebbal, Bengaluru, Karnataka 560024, India
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING

LIST OF EXPERIMENTS
1 Basic Signals and Signal Graphing: a) Unit Step, b) Rectangular, c)
Standard Triangle d)Sinusoidal and e) Exponential Signal.
Illustration of signal representation in time and frequency domains
2 for a rectangular pulse.
3 Amplitude Modulation and demodulation: Generation and display the
relevant signals and its spectrums.
4 Frequency Modulation and demodulation: Generation and
display the relevant signals and its spectrums.
5 Sampling and reconstruction of low pass signals. Display the signals
and its spectrum.
6 Time Division Multiplexing and demultiplexing.
7 PCM Illustration: Sampling, Quantization and Encoding.
8 Generate a)NRZ, RZ and Raised Cosine pulse, b) Generate and plot
eye diagram
Generate the Probability density function of Gaussian distribution
9 function.
10 Display the signal and its spectrum of an audio signal.
1. Basic Signals and Signal Graphing: a) Unit Step, b) Rectangular, c) Sinusoidal
d) Standard Triangle and e) Exponential Signal.

A. UNIT STEP FUNCTION:

% Define time vector (adjust range as needed)


t = -10:0.01:10;

% Generate unit step function using heaviside function


u_t = heaviside(t);

% Plot the unit step function


plot(t, u_t);
xlabel('Time (s)');
ylabel('Amplitude');
title('Unit Step Function (heaviside)');

Figure 1(a): Unit Step Function Plot

Code Explanation:

1. Defining the Time Vector: t = -5:0.1:5;


 This line creates a time vector named t.
 The syntax -5:0.1:5 defines a sequence of numbers starting from -5, ending at 5 (inclusive), with a step size of
0.1.
2. Generating the Unit Step Function: u_t = heaviside(t);
 This line generates the unit step function and stores it in a vector named u_t.
 The heaviside function is a built-in MATLAB function used to create the unit step function. It takes a vector as
input (in this case, t).
 The heaviside function works as follows:
o For elements in t that are less than 0, heaviside(t) outputs 0.
o For elements in t that are greater than or equal to 0, heaviside(t) outputs 1.
 Therefore, u_t will contain a sequence of 0s for negative time values and 1s for non-negative time values.
3. Plotting the Unit Step Function: plot(t, u_t);
 This line uses the plot function to visualize the unit step function stored in u_t.
 The plot function takes two arguments:
o The first argument (t) specifies the x-axis values (time in this case).
o The second argument (u_t) specifies the y-axis values (amplitude of the unit step function).
4.Adding Axis Labels and Title:
xlabel('Time (s)');
ylabel('Amplitude');
title('Unit Step Function (heaviside)');
 These lines add labels and a title to the plot for better understanding:
o xlabel('Time (s)'): Labels the x-axis as "Time (s)" indicating the units for the time values.
o ylabel('Amplitude'): Labels the y-axis as "Amplitude" representing the strength of the signal.
o title('Unit Step Function (heaviside)'): Sets the title of the plot to "Unit Step Function (heaviside)"
clarifying the type of function being visualized and the method used for generation.

B. RECTANGULAR FUNCTION:

% Define function parameters


t = -5:0.1:5; % Time vector (adjust range as needed)
a = -2; % Start time of rectangle
b = 1; % End time of rectangle

% Generate rectangular function using logical indexing


rect_func = ones(size(t)).*(t >= a & t <= b);

% Plot the rectangular function


plot(t, rect_func);
xlabel('Time (s)');
ylabel('Amplitude');
title('Rectangular Function');

Figure 1(b): Rectangular Function Plot


Code Explanation:

1. Defining Parameters:
o t = -5:0.1:5;: Creates a time vector t ranging from -5 to 5 with a step size of 0.1.
o a = -2;: Defines the start time (a) of the rectangle.
o b = 1;: Defines the end time (b) of the rectangle.
2. Generating Rectangular Function:
o ones(size(t)): Creates a vector of ones with the same size as t.
o t >= a & t <= b: Creates a logical vector where elements corresponding to t within the range [a, b] are
True (1), and those outside are False (0).
o The element-wise multiplication .* combines these vectors. Elements in rect_func become 1 only when
the corresponding elements in both vectors are 1 (i.e., within the time range [a, b]).
3. Plotting and Labelling:
o The rest of the code plots the function rect_func and adds labels and a title.

C. SINUSOIDAL FUNCTION:

% Define parameters
t = 0:0.01:2*pi; % Time vector (adjust range and step size as needed)
f = 10; % Frequency in Hz
A = 2; % Amplitude

% Generate sine wave


y = A*sin(2*pi*f*t);

% Plot the sine wave


plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sine Wave');

Figure 1(d): Sinusoidal Function Plot


Code Explanation:

1. Defining Parameters:
o f = 100;: Defines the frequency (f) of the sinusoid in Hz.
o t = 0:0.001:1;: Creates a time vector t starting from 0 and ending at 1 with a step size of 0.001 seconds.
o A = 2;: Defines the amplitude (A) of the sinusoid.
o phi = pi/3;: Defines the phase shift (phi) of the sinusoid in radians.
2. Generating Sinusoidal Function:
o y = A*sin(2*pi*f*t + phi);: This line calculates the values of the sinusoid at each time point in t.
3. Plotting the Function:

o plot(t, y);: This line plots the generated sinusoidal function (y) versus time (t) adds labels and a title.
o
D. STANDARD TRIANGLE FUNCTION: self-study

E. EXPONENTIAL SIGNAL FUNCTION: self-study


2.Illustration of signal representation in time and frequency domains for a rectangular pulse.

clc;
clear all;
% Define parameters
T = 10; % Pulse duration
Fs = 1; % Sampling frequency
t = -2*T:1/(10*Fs):2*T; % Time vector

% Generate rectangular pulse


rect_pulse = rectpuls(t, T);

% Compute Fourier transform


L = length(t);
f = Fs*(-L/2:(L/2-1))/L;
rect_pulse_fft = fftshift(fft(rect_pulse));

% Plot time domain representation


subplot(2,1,1);
plot(t, rect_pulse);
xlabel('Time (s)');
ylabel('Amplitude');
title('Time Domain Representation of Rectangular Pulse');

% Plot frequency domain representation


subplot(2,1,2);
plot(f, abs(rect_pulse_fft));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Frequency Domain Representation of Rectangular Pulse');
Code Explanation:

i. Defining Parameters:

o T: Pulse duration (10 seconds).


o Fs: Sampling frequency (1 Hz).
o t: Time vector spanning from -2T to 2T with a resolution determined by the sampling frequency (1/(10*Fs)).

ii. Generating Rectangular Pulse:

o rect_pulse = rectpuls(t, T): This line utilizes the built-in rectpuls function to generate the rectangular pulse.
The function takes the time vector t and pulse duration T as arguments and returns the corresponding
rectangular pulse values.

iii. Computing the Fourier Transform:

o L = length(t): Calculates the length of the time vector t.


o f = Fs*(-L/2:(L/2-1))/L: Creates a frequency vector f based on the sampling frequency Fs and the pulse signal
length L. This ensures the frequency range covers negative and positive frequencies up to the Nyquist limit
(half the sampling frequency).
o rect_pulse_fft = fftshift(fft(rect_pulse)): Calculates the Fast Fourier Transform (FFT) of the rectangular pulse
using the fft function. The fftshift function is applied to centre the zero-frequency component in the frequency
spectrum plot.

iv. Plotting:

o The code utilizes subplots to display both time and frequency domain representations:
o subplot(2,1,1): Plots the rectangular pulse in the time domain using plot(t, rect_pulse).
o subplot(2,1,2): Plots the magnitude spectrum (absolute value) of the FFT using plot(f, abs(rect_pulse_fft)).
3.Amplitude Modulation and demodulation: Generation and display the relevant signals and its
spectrums.

% Define parameters
Fs = 300; % Sampling frequency (Hz)
T = 1/Fs; % Sampling period
t = 0:T:3; % Time vector

% Message signal (single tone sinusoidal)


fm = 1; % Message frequency (Hz)
Am = 1; % Message amplitude
message = Am * sin(2*pi*fm*t); % Generate message signal

% Carrier signal (sinusoidal)


fc = 10; % Carrier frequency (Hz)
Ac = 1; % Carrier amplitude
carrier = Ac * sin(2*pi*fc*t); % Generate carrier signal

% Modulation (standard AM)


modulated_signal = (1 + 0.6*message) .* carrier;

% Demodulation
% Envelope detection
envelope = abs(hilbert(modulated_signal));

% Low-pass filtering to remove high-frequency components


cutoff_freq = 2 * fm; % Choose a cutoff frequency (you may need to adjust this)
[b, a] = butter(6, cutoff_freq / (Fs/2), 'low'); % 6th order low-pass Butterworth filter
demodulated_signal = filter(b, a, envelope);

% Normalize the demodulated signal by dividing by the carrier amplitude


demodulated_signal = demodulated_signal / Ac;

% Plotting
figure;

% Original Message Signal


subplot(4,2,1);
plot(t, message);
title('Message Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Carrier Signal
subplot(4,2,3);
plot(t, carrier);
title('Carrier Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Modulated Signal
subplot(4,2,5);
plot(t, modulated_signal);
title('Modulated Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Demodulated Signal
subplot(4,2,7);
plot(t, demodulated_signal);
title('Demodulated Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Spectrum Analysis
N = length(t); % Number of samples
f = (-N/2:N/2-1) * Fs / N; % Frequency vector

% Message Signal Spectrum


message_spectrum = fftshift(fft(message, N));
subplot(4,2,2);
plot(f, abs(message_spectrum));
title('Message Signal Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

% Carrier Signal Spectrum


carrier_spectrum = fftshift(fft(carrier, N));
subplot(4,2,4);
plot(f, abs(carrier_spectrum));
title('Carrier Signal Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

% Modulated Signal Spectrum


modulated_spectrum = fftshift(fft(modulated_signal, N));
subplot(4,2,6);
plot(f, abs(modulated_spectrum));
title('Modulated Signal Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

% Demodulated Signal Spectrum


demodulated_spectrum = fftshift(fft(demodulated_signal, N));
subplot(4,2,8);
plot(f, abs(demodulated_spectrum));
title('Demodulated Signal Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
Code Explanation:

i. Defining Parameters:
o Fs: sampling frequency
o T: sampling period
o t: time vector from 0 to 1 second.

ii. Generating Message, Carrier, Modulation and Demodulated Signal:

o message = Am * sin(2*pi*fm*t): Generate message signal with a peak voltage of 1V and 10Hz frequency
o carrier = Ac * sin(2*pi*fc*t): Generate carrier signal with a peak voltage of 1V and 1,000 Hz.
o modulated_signal = ((1 + 0.6*message) .* carrier): modulate the message signal onto the carrier signal
using the standard Amplitude Modulation (AM) formula.
o envelope = abs(hilbert(modulated_signal)): Demodulation using Hilbert Transforms (similar to envelope
detection)
o [b, a] = butter(6, cutoff_freq / (Fs/2), 'low'): 6th order Butterworth LPF specifications
o demodulated_signal = filter(b, a, envelope): the output of the envelope detector is passed through the
Butterworth LPF to remove high-frequency components
o demodulated_signal = demodulated_signal / Ac: normalize the demodulated signal by dividing it by the
carrier amplitude

iii. Plotting:
Figure with subplots to visualize the message signal, carrier signal, modulated signal, and demodulated signal,
as well as their respective spectra.

iv. Spectrum Analysis:


N = length(t): calculate the length of the signal (`N`)
f = (-N/2:N/2-1) * Fs / N: create a frequency vector `f` using the Nyquist theorem
message_spectrum = fftshift(fft(message, N)): compute the Fourier transforms of each signal using `fft`, and use
`fftshift` to shift the zero-frequency component to the center of the spectrum
4.Frequency Modulation and demodulation: Generation and display the relevant signals and its
spectrums.

clear all;
close all;
Fs = 8000; % Sampling rate of signal
Fc = 100; % Carrier frequency
t = linspace(0,1,10000); % Sampling times
x = sin(2*pi*10*t) % Channel 1
dev = 75; % Frequency deviation in modulated signal
y = fmmod(x,Fc,Fs,dev); % Modulate both channels.
z = fmdemod(y,Fc,Fs,dev); % Demodulate both channels.
subplot(411),plot(t,x);
xlabel('time(sec)');
ylabel('amplitude in volts(V)'); title('MODULATING SIGNAL');
subplot(412),plot(t,sin(2*pi*Fc*t));
xlabel('time(sec)'); ylabel('amplitude in volts(V)');
title('CARRIER SIGNAL');
subplot(413),plot(t,y);
xlabel('time(sec)');
ylabel('amplitude in volts(V)'); title('FREQUENCY MODULATED SIGNAL');
subplot(414),plot(t,z);
xlabel('time(sec)');
ylabel('amplitude in volts(V)');
title(' FM DEMODULATED SIGNAL');
5.Sampling and reconstruction of low pass signals. Display the signals and its spectrum.

% Parameters
Fs = 1000; % Sampling frequency for original signal (Hz)
T = 1; % Duration of the signal (seconds)
t = 0:1/Fs:T-1/Fs; % Time vector
f = 1; % Frequency of the low-pass signal (Hz)

% Generate a low-pass signal (sine wave)


x = sin(2*pi*f*t);

% Plot the original signalfigure;


subplot(3,2,1);
plot(t, x); title('Original Signal');
xlabel('Time (s)'); ylabel('Amplitude');

% Compute and plot the spectrum of the original signal


X = fft(x);
N = length(X);
frequencies = (0:N-1)*(Fs/N);subplot(3,2,2);
plot(frequencies, abs(X));
title('Spectrum of Original Signal');
xlabel('Frequency (Hz)'); ylabel('Magnitude');

% Sampling parameters
Fs_sampled = 20; % Sampling frequency for sampled signal (Hz)
Ts_sampled = 1/Fs_sampled; % Sampling period for sampled signal
t_sampled = 0:Ts_sampled:T-Ts_sampled; % Sampled time vector

% Sample the signal


x_sampled = sin(2*pi*f*t_sampled);

% Plot the sampled signalsubplot(3,2,3);


stem(t_sampled, x_sampled, 'r');
title('Sampled Signal'); xlabel('Time (s)');
ylabel('Amplitude');

% Compute and plot the spectrum of the sampled signal


X_sampled = fft(x_sampled, N); % Zero-padding to match length of originalsignal
subplot(3,2,4);
plot(frequencies, abs(X_sampled));
title('Spectrum of Sampled Signal');
xlabel('Frequency (Hz)');ylabel('Magnitude');

% Reconstruction using interpolation


t_reconstructed = t;
x_reconstructed = interp1(t_sampled, x_sampled, t_reconstructed, 'spline');

% Plot the reconstructed signalsubplot(3,2,5);


plot(t_reconstructed, x_reconstructed, 'g');
title('Reconstructed Signal');
xlabel('Time (s)'); ylabel('Amplitude');
% Compute and plot the spectrum of the reconstructed signal
X_reconstructed = fft(x_reconstructed);
subplot(3,2,6);
plot(frequencies, abs(X_reconstructed)); title('Spectrum of
Reconstructed Signal');xlabel('Frequency (Hz)');
ylabel('Magnitude');
1. Defining Parameters:
- `Fs`: Sampling frequency for the original continuous signal.
- T`: Total duration of the signal.
- `t`: Time vector, created from 0 to `T` with increments of `1/Fs`.
- `f`: Frequency of the sine wave.

2. Generating a low pass signal:


- `x`: The low-pass signal, a sine wave with frequency `f`.

3. Plot the Original Signal:


- Create a figure window.
- Use `subplot` to arrange multiple plots. This is the first plot showing the original signal inthe time
domain.

4. Computing and Plotting the Spectrum:


- `X`: Compute the FFT of the original signal to obtain its frequency components.
- `N`: Length of the FFT result.
- `frequencies`: Frequency vector corresponding to the FFT result.
- Plot the magnitude of the FFT result to show the spectrum.

5. Defining Sampling Parameters:


- `Fs_sampled`: New, lower sampling frequency for the sampled signal.
- `Ts_sampled`: Sampling period corresponding to `Fs_sampled`.
- `t_sampled`: Time vector for the sampled signal, with increments of `Ts_sampled`.
Sampling of the Signal:
- `x_sampled`: Values of the original sine wave at the sampled time points.Plot
the Sampled Signal:
- Use `stem` to plot the discrete samples of the signal. This is the third plot showing the
sampled signal in the time domain.

6. Computing and Plotting the Spectrum of Sampled Signal:


- `X_sampled`: Compute the FFT of the sampled signal, zero-padded to the length `N` tomatch
the original signal's length.
- Plot the magnitude of the FFT result to show the spectrum of the sampled signal.Signal
Reconstruction:
- `t_reconstructed`: Use the same time vector as the original signal for the reconstructedsignal.
- `x_reconstructed`: Use spline interpolation to reconstruct the signal from the sampledpoints.

7. Plotting the Reconstructed Signal:


- Plot the reconstructed signal in the time domain. This is the fifth plot.
Computing and Plotting the Spectrum of Reconstructed Signal**:
- `X_reconstructed`: Compute the FFT of the reconstructed signal.
- Plot the magnitude of the FFT result to show the spectrum of the reconstructed signal. Thisis the
sixth and final plot.
-`
6.Time Division Multiplexing and demultiplexing.
% Parameters
Fs = 1000; % Sampling frequency (Hz)
T = 1; % Duration of each signal (seconds)
t = 0:1/Fs:T-1/Fs; % Time vector

% Generate multiple signals


f1 = 1; % Frequency of first signal (Hz)
f2 = 2; % Frequency of second signal (Hz)
f3 = 3; % Frequency of third signal (Hz)
x1 = sin(2*pi*f1*t); % First signal: sine wave

% Second signal: rectangular wave


x2 = square(2*pi*f2*t);

% Third signal: triangular wave


x3 = sawtooth(2*pi*f3*t, 0.5);

% Plot the original signals


figure;
subplot(4,2,1);
plot(t, x1);
title('Original Signal 1: Sine Wave');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(4,2,3);
plot(t, x2);
title('Original Signal 2: Rectangular Wave');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(4,2,5);
plot(t, x3);
title('Original Signal 3: Triangular Wave');
xlabel('Time (s)');
ylabel('Amplitude');

% Time Division Multiplexing


x_tdm = zeros(1, length(t) * 3); % Preallocate multiplexed signal
for i = 1:length(t)
x_tdm(3*(i-1)+1) = x1(i);
x_tdm(3*(i-1)+2) = x2(i);
x_tdm(3*(i-1)+3) = x3(i);
end

% Time vector for multiplexed signal


t_tdm = linspace(0, T, length(x_tdm));

% Plot the TDM signal


subplot(4,2,7);
plot(t_tdm, x_tdm);
title('Time Division Multiplexed Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Time Division Demultiplexing
x1_demux = zeros(1, length(t));
x2_demux = zeros(1, length(t));
x3_demux = zeros(1, length(t));

for i = 1:length(t)
x1_demux(i) = x_tdm(3*(i-1)+1);
x2_demux(i) = x_tdm(3*(i-1)+2);
x3_demux(i) = x_tdm(3*(i-1)+3);
end

% Plot the demultiplexed signals


subplot(4,2,2);
plot(t, x1_demux);
title('Demultiplexed Signal 1: Sine Wave');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(4,2,4);
plot(t, x2_demux);
title('Demultiplexed Signal 2: Rectangular Wave');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(4,2,6);
plot(t, x3_demux);
title('Demultiplexed Signal 3: Triangular Wave');
xlabel('Time (s)');
ylabel('Amplitude');
7.Generate the Probability density function of Gaussian distribution function.

% Parameters for the Gaussian distribution


mu = 0; % Mean of the distribution
sigma = 1; % Standard deviation of the distribution
% Range of x values for plotting
x = linspace(mu - 4*sigma, mu + 4*sigma, 1000);

% Calculate the PDF of the Gaussian distribution


pdf = (1/(sigma * sqrt(2*pi))) * exp(-0.5 * ((x - mu)/sigma).^2);

% Plot the PDF


figure;
plot(x, pdf, 'LineWidth', 2);
title('Probability Density Function of Gaussian Distribution');
xlabel('x');
ylabel('Probability Density');
grid on;
8 PCM Illustration: Sampling, Quantization and Encoding
% Parameters
Fs = 10; % Sampling frequency (Hz)
T = 1; % Duration of the signal (seconds)
t = 0:1/Fs:T-1/Fs; % Time vector
f = 1; % Frequency of the signal (Hz)

% Generate an analog signal (sine wave)


analog_signal = 15*sin(2*pi*f*t);

% Plot the analog signal


figure;
subplot(3,1,1);
plot(t, analog_signal);
title('Analog Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Sampling
sampling_rate = 25; % Sampling rate (samples per second)
Ts = 1/sampling_rate; % Sampling period
n = 0:Ts:T-Ts; % Sample points
sampled_signal = 15*sin(2*pi*f*n);

% Plot the sampled signal


subplot(3,1,2);
stem(n, sampled_signal, 'r');
title('Sampled Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Quantization
num_levels = 10; % Number of quantization levels
max_val = max(sampled_signal);
min_val = min(sampled_signal);
quantization_levels = linspace(min_val, max_val, num_levels);
quantized_signal = zeros(size(sampled_signal));

for i = 1:length(sampled_signal)
[~, idx] = min(abs(quantization_levels - sampled_signal(i)));
quantized_signal(i) = quantization_levels(idx);
end

% Plot the quantized signal


subplot(3,1,3);
stem(n, quantized_signal, 'g');
title('Quantized Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Encoding
encoded_signal = zeros(1, length(quantized_signal) * log2(num_levels));

for i = 1:length(quantized_signal)
idx = find(quantization_levels == quantized_signal(i));
binary_code = de2bi(idx - 1, log2(num_levels), 'left-msb');
encoded_signal((i-1)*log2(num_levels)+1:i*log2(num_levels)) = binary_code;
end
% Display the encoded signal
disp('Encoded Signal:');
disp(encoded_signal);

Parameters and Signal Generation


Fs = 10; % Sampling frequency (Hz)
T = 1; % Duration of the signal (seconds)
t = 0:1/Fs:T-1/Fs; % Time vector
f = 1; % Frequency of the signal (Hz)

Generate an analog signal (sine wave)


analog_signal = 15*sin(2*pi*f*t);
- `Fs` is set to 10 Hz, determining the sampling frequency for the analog signal.
- `T` is the duration of the signal, set to 1 second.
- `t` is a time vector created from 0 to `T-1/Fs` with a step size of `1/Fs`.
- `f` is the frequency of the sine wave, set to 1 Hz.
- `analog_signal` is generated using the sine function, scaled by 15 to adjust the amplitude.

Plot the Analog Signal


figure;
subplot(3,1,1);
plot(t, analog_signal);
title('Analog Signal');
xlabel('Time (s)');
ylabel('Amplitude');

- The analog signal is plotted using the `plot` function.


- `subplot(3,1,1)` creates the first subplot in a 3-row, 1-column grid.
- The plot is titled "Analog Signal" with labeled x-axis (Time in seconds) and y-axis (Amplitude).

Sampling
sampling_rate = 25; % Sampling rate (samples per second)
Ts = 1/sampling_rate; % Sampling period
n = 0:Ts:T-Ts; % Sample points
sampled_signal = 15*sin(2*pi*f*n);
- `sampling_rate` is set to 25 samples per second.
- `Ts` is the sampling period calculated as the inverse of `sampling_rate`.
- `n` is a vector representing the sample points from 0 to `T-Ts` with a step size of `Ts`.
- `sampled_signal` is generated by sampling the sine wave at points defined by `n`.

Plot the Sampled Signal


subplot(3,1,2);
stem(n, sampled_signal, 'r');
title('Sampled Signal');
xlabel('Time (s)');
ylabel('Amplitude');
- The sampled signal is plotted using the `stem` function, which displays discrete data points.
- `subplot(3,1,2)` creates the second subplot.
- The plot is titled "Sampled Signal" with labeled x-axis (Time in seconds) and y-axis (Amplitude).
Quantization
num_levels = 10; % Number of quantization levels
max_val = max(sampled_signal);
min_val = min(sampled_signal);
quantization_levels = linspace(min_val, max_val, num_levels);
quantized_signal = zeros(size(sampled_signal));

for i = 1:length(sampled_signal)
[~, idx] = min(abs(quantization_levels - sampled_signal(i)));
quantized_signal(i) = quantization_levels[idx];
end

- `num_levels` defines the number of quantization levels, set to 10.


- `max_val` and `min_val` are the maximum and minimum values of the sampled signal, respectively.
- `quantization_levels` is a linearly spaced vector from `min_val` to `max_val` with `num_levels` elements.
- `quantized_signal` is initialized as a zero vector of the same size as `sampled_signal`.
- A loop iterates through each element of `sampled_signal`, finds the nearest quantization level, and assigns it
to the corresponding element in `quantized_signal`.

Plot the Quantized Signal


subplot(3,1,3);
stem(n, quantized_signal, 'g');
title('Quantized Signal');
xlabel('Time (s)');
ylabel('Amplitude');
- The quantized signal is plotted using the `stem` function.
- `subplot(3,1,3)` creates the third subplot.
- The plot is titled "Quantized Signal" with labeled x-axis (Time in seconds) and y-axis (Amplitude).

Encoding
encoded_signal = zeros(1, length(quantized_signal) * log2(num_levels));

for i = 1:length(quantized_signal)
idx = find(quantization_levels == quantized_signal(i));
binary_code = de2bi(idx - 1, log2(num_levels), 'left-msb');
encoded_signal((i-1)*log2(num_levels)+1:i*log2(num_levels)) = binary_code;
end
% Display the encoded signal
disp('Encoded Signal:');
disp(encoded_signal);
- `encoded_signal` is initialized as a zero vector with a length equal to the number of bits required to encode
all quantized levels.
- A loop iterates through each element of `quantized_signal`:
- `idx` finds the index of the current quantized value in `quantization_levels`.
- `de2bi` converts the index to a binary vector.
- The binary vector is placed in the appropriate position within `encoded_signal`.
- The encoded signal is displayed using `disp`.
9.MATLAB code to generate NRZ, RZ, and Raised Cosine wave forms for an 8-bit binary code

% Define the 8-bit binary number


binary_number = '10110011';

% Define parameters
bit_duration = 1; % duration of each bit
samples_per_bit = 100; % number of samples per bit
fs = samples_per_bit / bit_duration; % sampling frequency

% Generate time vector


t = 0:1/fs:length(binary_number)-1/fs;

% Preallocate signal vectors


nrz_signal = zeros(1, length(t));
rz_signal = zeros(1, length(t));

% Generate NRZ signal


for i = 1:length(binary_number)
if binary_number(i) == '1'
nrz_signal((i-1)*samples_per_bit+1:i*samples_per_bit) = 1;
else
nrz_signal((i-1)*samples_per_bit+1:i*samples_per_bit) = 0;
end
end

% Generate RZ signal
for i = 1:length(binary_number)
if binary_number(i) == '1'
rz_signal((i-1)*samples_per_bit+1:(i-1)*samples_per_bit+samples_per_bit/2) = 1;
rz_signal((i-1)*samples_per_bit+samples_per_bit/2+1:i*samples_per_bit) = 0;
else
rz_signal((i-1)*samples_per_bit+1:i*samples_per_bit) = 0;
end
end

% Generate Raised Cosine signal


rolloff = 0.5; % Roll-off factor
span = 6; % Filter span in symbols
raised_cosine_filter = rcosdesign(rolloff, span, samples_per_bit);
raised_cosine_signal = upfirdn(nrz_signal, raised_cosine_filter, 1);
raised_cosine_signal = raised_cosine_signal(span*samples_per_bit/2 + 1 : end - span*samples_per_bit/2); %
Truncate to match the length of t

% Adjust time vector for Raised Cosine Signal


t_rc = linspace(0, length(binary_number)*bit_duration, length(raised_cosine_signal));

% Plot waveforms
figure;

subplot(3,1,1);
plot(t, nrz_signal, 'LineWidth', 1.5);
title('NRZ Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;
subplot(3,1,2);
plot(t, rz_signal, 'LineWidth', 1.5);
title('RZ Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;

subplot(3,1,3);
plot(t_rc, raised_cosine_signal, 'LineWidth', 1.5);
title('Raised Cosine Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;
10.Display the signal and its spectrum of an audio signal.

% Load an audio file


[audio, Fs] = audioread('audiofile.wav'); % Replace with your audio file
% Time vector
t = (0:(length(audio)-1)) / Fs;
% Plot the audio signal in the time domain
figure;
subplot(2,1,1);
plot(t, audio);
title('Audio Signal in Time Domain');
xlabel('Time (s)');
ylabel('Amplitude');
% Compute the Fourier Transform (FFT)
audio_fft = fft(audio);
% Calculate the single-sided power spectrum
P2 = abs(audio_fft/length(audio));
P1 = P2(1:length(audio)/2+1);
P1(2:end-1) = 2*P1(2:end-1);
% Frequency vector
f = Fs*(0:(length(audio)/2))/length(audio);
% Plot the single-sided power spectrum
subplot(2,1,2);
plot(f, P1);
title('Single-Sided Power Spectrum of Audio Signal');
xlabel('Frequency (Hz)');
ylabel('Power/Frequency');

Explanation:
1. Load Audio File:
o audioread loads the audio file and stores the audio samples in audio and the sampling frequency in Fs.
2. Time Vector:
o Creates a time vector t corresponding to the audio samples.
3. Plot Audio Signal:
o Plots the audio signal in the time domain.
4. Compute Fourier Transform:
o Calculates the Fourier Transform of the audio signal using fft.
5. Calculate Power Spectrum:
o Computes the single-sided power spectrum using abs(audio_fft/length(audio)) and scales the positive
frequency components by 2 to account for the discarded negative frequencies.
6. Frequency Vector:
o Creates a frequency vector f corresponding to the frequency bins of the power spectrum.
7. Plot Power Spectrum:
o Plots the single-sided power spectrum of the audio signal.
Improvements:
 Clearer variable names: Uses more descriptive variable names like audio_fft, P2, P1, and f for better
readability.
 Single-Sided Spectrum: Calculates the single-sided power spectrum correctly by scaling the positive frequency
components.
 Power Spectrum: Uses abs(audio_fft/length(audio)) to calculate the power spectrum, which is more appropriate
for spectral analysis.
 Axis Labels: Provides informative labels for both plots.
 Title Updates: Includes "Time Domain" and "Single-Sided Power Spectrum" in the plot titles for clarity.

You might also like