0% found this document useful (0 votes)
3 views11 pages

4704 LAB2 Code

The document outlines a MATLAB simulation for M-ary QAM modulation and demodulation, detailing the setup of parameters, validation of inputs, generation of binary information, and conversion to digital signals. It includes steps for reshaping binary data into symbols, performing QAM modulation, and subsequently demodulating the signal back to its original binary form. The process is visualized through various plots, demonstrating the transmission and recovery of information.

Uploaded by

rawadjashid
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)
3 views11 pages

4704 LAB2 Code

The document outlines a MATLAB simulation for M-ary QAM modulation and demodulation, detailing the setup of parameters, validation of inputs, generation of binary information, and conversion to digital signals. It includes steps for reshaping binary data into symbols, performing QAM modulation, and subsequently demodulating the signal back to its original binary form. The process is visualized through various plots, demonstrating the transmission and recovery of information.

Uploaded by

rawadjashid
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

clc;

close all;
clear all;

%{
****************** Parameters Setup **********************
We define the parameters directly without prompting the user.
'M' is the modulation order for QAM, which must be a power of 2.
'nbit' is the total number of bits in the binary message to be modulated.
'bp' is the bit period, defining the time interval for each bit.
%}
M = 16; % M-array for QAM (e.g., 16-QAM)
nbit = 64; % Total number of bits in the message
bp = 0.000001; % Bit period (time duration for each bit)

%{
*************** Validate the value of M ********************
We need to ensure that 'M' is a valid value for QAM modulation.
The value of M must be a power of 2, so we check if log2(M) is an integer.
If it isn't, the program throws an error.
%}
Ld = log2(M); % Calculate log2(M), i.e., bits per symbol
ds = ceil(Ld); % Round up the result
dif = ds - Ld; % Check if the result is already an integer
if (dif ~= 0)
error('The value of M must be such that log2(M) is an integer');
else
disp('Legitimate value! Proceeding with simulation.');
end

Legitimate value! Proceeding with simulation.

%{
*************** Validate the message size **********************
The number of bits in the message must be divisible by log2(M)
to form a complete set of symbols. We check if nbit is divisible
by log2(M) and throw an error if it is not.
%}
Ld1 = nbit / log2(M); % Calculate the number of symbols required
ds1 = ceil(Ld1); % Round the result
dif1 = ds1 - Ld1; % Check if the message size is divisible by log2(M)
if (dif1 ~= 0)
error('The total number of bits must be divisible by log2(M)');
else
disp('Legitimate message size! Proceeding with simulation.');
end

Legitimate message size! Proceeding with simulation.

1
%{
******************* Binary Information Generation *****************
We generate a random binary sequence of length 'nbit' to simulate
the transmission of information.
%}
msg = round(rand(nbit, 1)); % Random binary sequence (0s and 1s)
disp('Binary information at the transmitter:');

Binary information at the transmitter:

disp(msg); % Display the generated binary message

1
0
1
0
0
1
1
0
0
0
0
1
1
1
0
0
1
0
1
0
1
0
1
1
0
0
1
0
1
1
1
0
1
0
0
1
1
1
0
0
1
0
0
0
1
0
0
1
0
1

2
1
0
0
0
1
0
0
0
1
0
0
0
0
0

%{
** Convert binary information into a digital signal representation **
Each bit in the binary message is represented as either a high
voltage ('1') or a low voltage ('0'). We create a signal that
represents this binary sequence.
%}
x = msg; % Copy the binary message
bit = []; % Initialize the array to store the digital signal

% Loop through each bit and convert it to either a high or low signal
for n = 1:length(x)
if x(n) == 1
se = ones(1, 100); % 100 samples representing bit '1'
else
se = zeros(1, 100); % 100 samples representing bit '0'
end
bit = [bit se]; % Append the high or low signal to the bit array
end

% Time vector for plotting the digital signal


t1 = bp/100:bp/100:100*length(x)*(bp/100);

% Plot the binary signal as a digital signal


figure(1)
subplot(2,1,1); % First subplot in the figure
plot(t1, bit, 'LineWidth', 2.5); grid on;
axis([0 bp*length(x) -.5 1.5]); % Set axis limits for better visualization
ylabel('Amplitude (Volt)');
xlabel('Time (Sec)');
title('Transmitting information as digital signal'); % Title of the plot

%{
************ Binary to Symbols for M-ary QAM ********************
The binary message is reshaped into groups of log2(M) bits.
Each group of bits represents one symbol for the M-ary QAM modulation.
%}
msg_reshape = reshape(msg, log2(M), nbit / log2(M))';

3
disp('Reshaped message for symbols:');

Reshaped message for symbols:

disp(msg_reshape); % Display the reshaped binary sequence

1 0 1 0
0 1 1 0
0 0 0 1
1 1 0 0
1 0 1 0
1 0 1 1
0 0 1 0
1 1 1 0
1 0 0 1
1 1 0 0
1 0 0 0
1 0 0 1
0 1 1 0
0 0 1 0
0 0 1 0
0 0 0 0

% Convert each group of bits into a decimal symbol


for j = 1:nbit/log2(M)
for i = 1:log2(M)
a(j, i) = num2str(msg_reshape(j, i)); % Convert each bit group to a string
end
end

% Convert the binary strings to decimal values representing the symbols


as = bin2dec(a); % Convert binary to decimal
bss = as'; % Store the result in the symbol array

% Plot the symbols for M-ary QAM modulation


figure(1)
subplot(2,1,2); % Second subplot in the figure
stem(bss, 'LineWidth', 2.0); % Stem plot for the symbols
title('Symbols for M-ary QAM modulation at the transmitter');
xlabel('n (Discrete time)');
ylabel('Magnitude');

4
disp('Symbols for M-ary QAM:');

Symbols for M-ary QAM:

disp(bss); % Display the symbols

10 6 1 12 10 11 2 14 9 12 8 9 6 2 2 0

%{
***************** M-ary QAM Modulation ****************************
We perform QAM modulation by mapping each symbol to a point
in the complex plane. The function qammod() takes the symbols
and returns the corresponding modulated signal.
%}
p = qammod(bss, M); % QAM modulation
sym = 0:1:M-1; % Generate a set of M symbols
pp = qammod(sym, M); % Generate the constellation diagram for M-ary QAM

% Plot the constellation diagram


scatterplot(pp) % Plot the constellation points
title('Constellation diagram for M-ary QAM');

5
% Real and imaginary components of the modulated signal
RR = real(p); % In-phase (real) part
II = imag(p); % Quadrature (imaginary) part

%{
***************** Modulated Signal Generation **********************
The modulated signal is generated by combining the real and
imaginary parts, which are modulated with cosine and sine waves
respectively. The result is the final modulated QAM signal.
%}
sp = bp * log2(M); % Symbol period (based on M and the bit period)
sr = 1 / sp; % Symbol rate
f = sr * 2; % Carrier frequency
t = sp/100:sp/100:sp; % Time vector for one symbol period

% Initialize an empty array to store the modulated signal


m = [];

% Loop through each symbol and generate the corresponding modulated signal
for k = 1:length(RR)
yr = RR(k) * cos(2*pi*f*t); % In-phase component (cosine modulation)
yim = II(k) * sin(2*pi*f*t); % Quadrature component (sine modulation)
y = yr + yim; % Combine in-phase and quadrature components
m = [m y]; % Append the result to the signal array
end

6
% Time vector for plotting the modulated signal
tt = sp/100:sp/100:sp*length(RR);

% Plot the modulated signal waveform


figure(3);
plot(tt, m); % Plot the modulated QAM signal
title('Waveform for M-ary QAM modulation according to symbols');
xlabel('Time (Sec)');
ylabel('Amplitude (Volt)');

%{
****************** M-ary QAM Demodulation **************************
In this step, we demodulate the received signal back to its
original symbols. This is done by extracting the in-phase and
quadrature components and comparing them with the original signal.
%}
m1 = []; m2 = []; % Initialize arrays for in-phase and quadrature components

% Loop through the modulated signal and extract the original components
for n = length(t):length(t):length(m)
t = sp/100:sp/100:sp; % Time vector for one symbol period
y1 = cos(2*pi*f*t); % In-phase demodulation (cosine)
y2 = sin(2*pi*f*t); % Quadrature demodulation (sine)
mm1 = y1 .* m(n-length(t)+1:n); % Multiply with in-phase signal
mm2 = y2 .* m(n-length(t)+1:n); % Multiply with quadrature signal

7
z1 = trapz(t, mm1); % Integrate in-phase component
z2 = trapz(t, mm2); % Integrate quadrature component
m1 = [m1 round(2*z1/sp)]; % Store in-phase result
m2 = [m2 round(2*z2/sp)]; % Store quadrature result
end

% Combine the demodulated in-phase and quadrature components


for k = 1:length(m1)
gt(k) = m1(k) + j*m2(k); % Create complex symbols
end

% Demodulate the complex symbols back into their original form


ax = qamdemod(gt, M); % QAM demodulation

% Plot the demodulated symbols


figure(4);
subplot(2,1,2);
stem(ax, 'LineWidth', 2); % Stem plot for demodulated symbols
title('Re-obtained symbol after M-array QAM demodulation');
xlabel('n (Discrete time)');
ylabel('Magnitude');

%{
****************** Binary Recovery **************************
Once the symbols have been demodulated, they are converted
back to their binary form.
%}
bi_in = dec2bin(ax); % Convert symbols to binary strings
[row, col] = size(bi_in); % Get the size of the binary matrix

% Convert the binary strings back to a binary sequence


p = 1;
for i = 1:row
for j = 1:col
re_bi_in(p) = str2num(bi_in(i,j)); % Convert binary strings to numbers
p = p + 1;
end
end

disp('Re-obtained binary information after M-ary QAM demodulation:');

Re-obtained binary information after M-ary QAM demodulation:

disp(re_bi_in'); % Display the recovered binary sequence

0
0
0
1
0
0
0

8
1
1
0
0
1
1
0
0
1
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1
0
0
0
1
0
0
0
1
0
0
0
1
1
0
0
1

%{
******* Convert Re-obtained Binary Message to Digital Signal ********
The recovered binary message is converted back to a digital
signal for visualization, just like in the transmission step.

9
%}
x = re_bi_in; % Store the binary sequence
bit = []; % Initialize an array for the digital signal

% Loop to convert the binary sequence to a digital signal


for n = 1:length(x)
if x(n) == 1
se = ones(1, 100); % High signal for binary '1'
else
se = zeros(1, 100); % Low signal for binary '0'
end
bit = [bit se]; % Append the signal
end

% Time vector for plotting the digital signal


t1 = bp/100:bp/100:100*length(x)*(bp/100);

% Plot the received digital signal after demodulation


figure(4)
subplot(2,1,1);
plot(t1, bit, 'LineWidth', 2.5); grid on;
axis([0 bp*length(x) -.5 1.5]); % Set axis limits
ylabel('Amplitude (Volt)');
xlabel('Time (Sec)');
title('Received information as digital signal after M-array QAM demodulation');

10
11

You might also like