-
Notifications
You must be signed in to change notification settings - Fork 3
/
16QAM_Constellation_Phasor.PY
74 lines (51 loc) · 2.14 KB
/
16QAM_Constellation_Phasor.PY
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
from sympy.combinatorics.graycode import GrayCode
from numpy.matlib import repmat
import numpy as np
from commpy.modulation import QAMModem
import matplotlib.pyplot as plt
def GrayMapping(M):
L = int(np.sqrt(M) - 1)
bitsSymb = int(np.log2(M))
PAM = np.arange(-L, L + 1, 2)
PAM = np.array([PAM])
# generate complex square-QAM constellation
const = repmat(PAM, L + 1, 1) + 1j * repmat(np.flip(PAM.T, 0), 1, L + 1)
const = const.T
for ind in np.arange(1, L + 1, 2):
const[ind] = np.flip(const[ind], 0)
code = GrayCode(bitsSymb)
a = list(code.generate_gray())
const_ = np.zeros((M, 2), dtype=complex)
const = const.reshape(M, 1)
for ind in range(0, M):
const_[ind, 0] = const[ind, 0] # complex constellation symbol
const_[ind, 1] = int(a[ind], 2) # mapped bit sequence (as integer decimal)
# sort complex symbols column according to their mapped bit sequence (as integer decimal)
const = const_[const_[:, 1].real.argsort()]
return const
# Generate QAM16 constellation points
QAM16 = QAMModem(16)
QAM16.constellation = GrayMapping(16)[:, 0] # cambia M-QAM
# Create figure and two subplots for constellation and phasor diagrams
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
# Plot constellation diagram on first subplot
ax1.scatter(QAM16.constellation.real, QAM16.constellation.imag, color='blue')
ax1.set_xlim([-5, 5])
ax1.set_ylim([-5, 5])
ax1.grid(False)
ax1.set_title('QAM16 Constellation')
ax1.axhline(0, color='k', linestyle='--', linewidth=2)
ax1.axvline(0, color='k', linestyle='--',linewidth=2)
# plot QAM16 phasor diagram
angles = np.angle(QAM16.constellation, deg=True)
magnitudes = np.abs(QAM16.constellation)
for i in range(len(QAM16.constellation)):
ax2.quiver(0, 0, QAM16.constellation[i].real, QAM16.constellation[i].imag, angles='xy', scale_units='xy', scale=1, color='blue')
ax2.set_xlim([-4, 4])
ax2.set_ylim([-4, 4])
ax2.grid(False)
ax2.set_title('QAM16 Phasor Diagram')
ax2.axhline(0, color='k', linestyle='--', linewidth=2)
ax2.axvline(0, color='k', linestyle='--',linewidth=2)
# Display the figure
plt.show()