-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_ary_qam_modulation.m
159 lines (147 loc) · 3.98 KB
/
m_ary_qam_modulation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
% Clearing previous data
clc;
clear all;
close all;
% Define M-ary for QAM modulation
M = 64;
% Input validation
fprintf('\n\n\n');
% Check if log2(M) is an integer
Ld = log2(M);
ds = ceil(Ld);
dif = ds - Ld;
if (dif ~= 0)
error('the value of M is only acceptable if log2(M) is an integer');
end
% Generate binary information
nbit = 1024; % number of information bits
msg = round(rand(nbit, 1)); % generate binary data
disp('Binary information at transmitter:');
disp(msg);
fprintf('\n\n');
% Digital Signal Representation of Binary Information
x = msg;
bp = 0.000001; % bit period
bit = [];
for n = 1:1:length(x)
if x(n) == 1
se = ones(1, 100);
else
se = zeros(1, 100);
end
bit = [bit se];
end
t1 = bp / 100:bp / 100:100 * length(x) * (bp / 100);
figure(1)
subplot(3, 1, 1);
plot(t1, bit, 'lineWidth', 2.5);
grid on;
axis([0 bp * length(x) -.5 1.5]);
ylabel('Amplitude (volt)');
xlabel('Time (sec)');
title('Transmitting information as digital signal');
% Reshape binary information for M-ary QAM modulation
msg_reshape = reshape(msg, log2(M), nbit / log2(M))';
disp('Information reshaped for conversion to symbolic form:');
disp(msg_reshape);
fprintf('\n\n');
size(msg_reshape);
for (j = 1:1:nbit / log2(M))
for (i = 1:1:log2(M))
a(j, i) = num2str(msg_reshape(j, i));
end
end
as = bin2dec(a);
ass = as';
figure(1)
subplot(3, 1, 2);
stem(ass, 'Linewidth', 2.0);
title('Serial symbol for M-ary QAM modulation at transmitter');
xlabel('n (discrete time)');
ylabel('Magnitude');
disp('Symbolic form information for M-ary QAM:');
disp(ass);
fprintf('\n\n');
% Mapping for M-ary QAM modulation
x1 = [0:M-1];
p = qammod(ass, M); % constellation design for M-ary QAM according to symbol
sym = 0:1:M-1; % considered symbol of M-ary QAM, just for scatter plot
pp = qammod(sym, M); % constellation diagram for M-ary QAM
scatterplot(pp), grid on;
title('Constellation diagram for M-ary QAM');
% M-ary QAM Modulation
RR = real(p);
II = imag(p);
sp = bp * 2; % symbol period for M-ary QAM
sr = 1 / sp; % symbol rate
f = sr * 2;
t = sp / 100:sp / 100:sp;
ss = length(t);
m = [];
for (k = 1:1:length(RR))
yr = RR(k) * cos(2 * pi * f * t); % in-phase or real component
yim = II(k) * sin(2 * pi * f * t); % quadrature or imaginary component
y = yr + yim;
m = [m y];
end
tt = sp / 100:sp / 100:sp * length(RR);
figure(1);
subplot(3, 1, 3);
plot(tt, m);
title('Waveform for M-ary QAM modulation according to symbolic information');
xlabel('Time (sec)');
ylabel('Amplitude (volt)');
% M-ary QAM Demodulation
m1 = [];
m2 = [];
for n = ss:ss:length(m)
t = sp / 100:sp / 100:sp;
y1 = cos(2 * pi * f * t); % in-phase component
y2 = sin(2 * pi * f * t); % quadrature component
mm1 = y1 .* m((n - (ss - 1)):n);
mm2 = y2 .* m((n - (ss - 1)):n);
z1 = trapz(t, mm1); % integration
z2 = trapz(t, mm2); % integration
zz1 = round(2 * z1 / sp);
zz2 = round(2 * z2 / sp);
m1 = [m1 zz1];
m2 = [m2 zz2];
end
% Demapping for M-ary QAM modulation
clear i;
clear j;
for (k = 1:1:length(m1))
gt(k) = m1(k) + j * m2(k);
end
gt
ax = qamdemod(gt, M);
figure(3);
subplot(2, 1, 1);
stem(ax, 'linewidth', 2);
title('Re-obtained symbol after M-ary QAM demodulation');
xlabel('n (discrete time)');
ylabel('Magnitude');
disp('Re-obtained symbol after M-ary QAM demodulation:');
disp(ax);
fprintf('\n\n');
% Representation of Receiving Binary Information as Digital Signal
x = ax;
bp = 0.000001; % bit period
bit = [];
for n = 1:1:length(x)
if x(n) == 1
se = ones(1, 100);
else
se = zeros(1, 100);
end
bit = [bit se];
end
t1 = bp / 100:bp / 100:100 * length(x) * (bp / 100);
figure(3)
subplot(2, 1, 2);
plot(t1, bit, 'lineWidth', 2.5);
grid on;
axis([0 bp * length(x) -.5 1.5]);
ylabel('Amplitude (volt)');
xlabel('Time (sec)');
title('Receiving information as digital signal after M-ary QAM demodulation');