-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMPSK.py
230 lines (178 loc) · 6.78 KB
/
MPSK.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import periodogram
from scipy.spatial import distance
from scipy.stats import norm
from sympy.combinatorics.graycode import GrayCode
# Carrier signal
f_c = 100.0
t_c = 1.0 / f_c
# Sampling rate
f_s = 10000.0
t_s = 1.0 / f_s
# MPSK Parameters
Tb = 0.01
Eb = 0.001
def bits_to_symbols(msg, k):
bucket_of_buckets = []
for i in range(k):
bucket_of_buckets.append(msg[i::k])
symbols = np.array(bucket_of_buckets)
return symbols
def constellation_angles(M):
return np.arange(0.0, 2.0 * np.pi, 2.0 * np.pi / M)
def graycode(k):
return list(GrayCode(k).generate_gray())
def generate_constellation_table(constellation, gray_code):
constellation_table = {}
for i, code in enumerate(gray_code):
constellation_table[code] = constellation[i]
return constellation_table
def generate_theta_vector(symbols, constellation_table):
theta = np.zeros(np.size(symbols, axis=1), dtype="float")
for j in range(np.size(symbols, axis=1)):
bits = []
for i in range(np.size(symbols, axis=0)):
bits.append(symbols[i, j])
bits_str = ""
for bit in bits:
bits_str += str(bit)
theta[j] = constellation_table[bits_str]
return theta
def generate_I_Q_signals(theta):
A = np.sqrt(Eb)
I = A * np.cos(theta) # in-phase component
Q = A * np.sin(theta) # quadrature component
return I, Q
def plot_constellation_diagram(I, Q):
plt.figure()
# Makes it look like a circle instead of an ellipse
plt.axes().set_aspect("equal", "datalim")
# Time vector for sine and cosine
t_csd = np.linspace(0.0, 2.0 * np.math.pi, 100)
plt.plot(
np.sqrt(Eb) * np.sin(t_csd), np.sqrt(Eb) * np.cos(t_csd)
) # sqrt(Eb)*sin and sqrt(Eb)*cos
plt.plot(I, Q, "ro", markersize=12)
plt.grid()
plt.title("Constellation diagram for QPSK", fontsize=14)
plt.tick_params(labelsize=12)
plt.show()
def modulate_signal(symbols, I, Q):
t = np.linspace(0.0, Tb, int(Tb * f_s))
modulated_signal = np.empty(
np.size(symbols, axis=1) * len(t), dtype="float")
phi_1 = np.sqrt(2 / Tb) * np.cos(2.0 * np.math.pi * f_c * t)
phi_2 = np.sqrt(2 / Tb) * np.sin(2.0 * np.math.pi * f_c * t)
for k in range(np.size(symbols, axis=1)):
# Calculates modulated signal for each symbol
# Page 12, Lecture 16
modulated_signal[k * len(t): (k + 1) * len(t)
] = I[k] * phi_1 - Q[k] * phi_2
return modulated_signal
def plot_modulated_signal(symbols, modulated_signal):
# Time vector for symbols
# t_sym = np.arange(0.0, np.size(symbols, axis=1)*2.0*t_c, t_s)
t_sym = np.linspace(
0, np.size(symbols, axis=1) *
Tb, int(np.size(symbols, axis=1) * Tb * f_s)
)
plt.figure()
plt.title("MPSK", fontsize=14)
plt.xlabel("t", fontsize=14)
plt.ylabel("Amplitude", fontsize=14)
plt.tick_params(labelsize=12)
plt.plot(t_sym, modulated_signal)
plt.show()
def add_noise(modulated_signal):
# Noise
ns = len(modulated_signal)
noise = np.random.normal(size=ns)
f, psd = periodogram(noise, f_s)
# Plot noise
# fig, ax = plt.subplots(2,1)
# ax[0].plot(noise)
# ax[1].plot(f, psd)
psd_av = np.mean(psd)
N0 = 2 * psd_av
# modulated_signal += noise
return N0, modulated_signal
def generate_decoding_table(gray_code, constellation_table):
decoding_table = {}
for code in gray_code:
amp = np.zeros(2, dtype="float")
amp[0] = np.cos(constellation_table[code])
amp[1] = np.sin(constellation_table[code])
decoding_table[code] = amp
return decoding_table
def demodulate_signal(modulated_signal, decoding_table, gray_code, k):
t = np.linspace(0, Tb, int(Tb * f_s))
phi_1 = np.sqrt(2 / Tb) * np.cos(2.0 * np.math.pi * f_c * t)
phi_2 = np.sqrt(2 / Tb) * np.sin(2.0 * np.math.pi * f_c * t)
N = len(modulated_signal) // len(t)
split_modulated_signal = np.array_split(modulated_signal, N)
decoded_symbols = [[] for i in range(k)]
constellation_points = []
for code in decoding_table:
constellation_points.append(decoding_table[code])
constellation_points = np.array(constellation_points)
for i in split_modulated_signal:
s_1 = i * phi_1
s_2 = i * phi_2
x = s_1.sum() / f_s
y = s_2.sum() / f_s
decoded_point = np.array([[x, y]])
distances = distance.cdist(
decoded_point, constellation_points, "euclidean")
code = gray_code[np.argmin(distances[0])]
for i, bit in enumerate(list(code)):
decoded_symbols[i].append(int(bit))
decoded_msg = []
for i in range(len(decoded_symbols[0])):
for j in range(len(decoded_symbols)):
decoded_msg.append(decoded_symbols[j][i])
return decoded_msg
def error_probabilities(msg, decoded_msg, Eb, N0, k, M):
# Bit Error Probability Calculations
# Pb = norm.sf(np.sqrt(2 * Eb / N0)) This is for BPSK/QPSK
# Symbol Error Probability Calculations
Pe = 2 * norm.sf(np.sqrt(2 * k * Eb / N0) * np.sin(np.math.pi / M))
Pb = Pe / k
Pb_pr = np.count_nonzero(np.array(msg) != np.array(decoded_msg)) / len(msg)
return Pe, Pb, Pb_pr
def modulate(msg, k, M):
symbols = bits_to_symbols(msg, k)
constellation = constellation_angles(M)
gray_code = graycode(k)
constellation_table = generate_constellation_table(
constellation, gray_code)
theta = generate_theta_vector(symbols, constellation_table)
I, Q = generate_I_Q_signals(theta)
return I, Q
plot_constellation_diagram(I, Q)
modulated_signal = modulate_signal(symbols, I, Q)
# plot_modulated_signal(symbols, modulated_signal, Tb, f_s)
N0, modulated_signal_with_noise = add_noise(modulated_signal)
return gray_code, constellation_table, modulated_signal_with_noise, N0
def demodulate(msg, k, M, gray_code, constellation_table, modulated_signal, N0):
decoding_table = generate_decoding_table(gray_code, constellation_table)
decoded_msg = demodulate_signal(
modulated_signal, decoding_table, gray_code, k)
return decoded_msg
if __name__ == "__main__":
# message to be transmitted
msg = np.array(
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0]
) # 8PSK demo signal
# msg = np.array([0, 1, 0, 0, 1, 1, 0, 1, 1, 0]) # QPSK demo signal
# msg = np.random.randint(low=0, high=2, size=int(1e3))
M = 8
k = int(np.log2(M))
gray_code, constellation_table, modulated_signal_with_noise, N0 = modulate(
msg, k, M
)
decoded_msg = demodulate(
msg, k, M, gray_code, constellation_table, modulated_signal_with_noise, N0
)
Pe, Pb, Pb_pr = error_probabilities(msg, decoded_msg, N0, k, M)