-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.ifft.py
86 lines (66 loc) · 2.97 KB
/
3.ifft.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
# @.@ coding : utf-8 ^-^
# @Author : Leon Rein
# @Time : 2021-03-09 ~ 20:05
# @File : 3.ifft.py
# @Software : PyCharm
# @Notice : It's a WINDOWS version!
# 将并行数据进行调制. 先进行 IFFT 运算, 并加入循环前缀, 生成待发送的复包络的采样值.
# 注: 发送的 OFDM 信号为 s(t) = Re{a(t)*e^(j*2pi*f_c*t)}, a(t) 为模拟复包络
# 忽略上变频, 即, f_c = 0Hz 时, s(t) = Re{a(t)}
"""
输入: labels64_test.csv
labels64_train.npy
输出: after_cp80_test.npy
numpy 专用二进制储存格式.
1000 行 80 列, 每个元素为 complex128 格式, 每一行的前 16 列为最后 16 列的复制;
后 64 列为输入转换为复数后的 IFFT.
after_cp80_train.npy
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate as interp
'''1. Data pre-processing'''
# Read the integer data. Serial data.
original_64_test = np.loadtxt("./data_sets/labels64_test.csv", delimiter=",").astype(np.int).reshape(-1)
original_64_train = np.load("./data_sets/labels64_train.npy").astype(np.int).reshape(-1)
# Mapping int to complex. Real 4QAM modulation.
mQAM_list = [1 + 0j, 0 + 1j, -1 + 0j, 0 - 1j]
after_mapping = np.array([mQAM_list[orig] for orig in original_64_test]) # original_64_train
# Serial to parallel
to_ifft = after_mapping.reshape([-1, 64])
'''2. OFDM-modulation (IFFT)'''
# IFFT by row
after_ifft = np.fft.ifft(to_ifft, axis=-1) # Axis over which to compute the inverse DFT. The last axis(,64) is used.
# Parseval's theorem, been checked by the 0th symbol.
assert abs(sum(abs(to_ifft[0])**2)/64 - sum(abs(after_ifft[0])**2)) < 0.0001
'''3. CP(Cyclic Prefix)'''
# Concatenating numpy arrays horizontally
after_cp = np.concatenate((after_ifft[:, -16:], after_ifft), axis=1)
# Save the result
# np.save("./data_sets/after_cp80_test.npy", after_cp)
# after_cp = np.load("./data_sets/after_cp80_test.npy") # load the data set
'''4. Visualization'''
# Choose one of the OFDM symbols (after_cp, using the I/Q modulation)
sample = np.real(after_cp[0]) # real discrete wave to send, 150ms, f_c = 0Hz
x_axis = np.linspace(0, 150, 80) # 150ms for 80 points
time_axis = np.linspace(0, 150, 80*5) # 5*2 = 10 times the sampling rate (Negtive Frequency)
# Interpolate
f = interp.interp1d(x_axis, sample, kind='cubic')
after_DAC = f(time_axis)
# Show the wave
with plt.style.context(['ieee', 'grid']):
plt.rcParams['font.serif'] = ['Times New Roman']
plt.xlim((0, 150))
plt.ylim((-0.2, 0.25))
plt.xlabel(r"$Time\ in\ One\ OFDM\ Symbol\ (ms)$")
plt.ylabel(r"$Voltage (V)$")
plt.xticks([0, 30, 60, 90, 120, 150])
plt.title(r"$One\ OFDM\ Symbol$")
plt.vlines(30, -0.2, 0.25, colors="k", linestyles="dashed")
# plt.plot(x_axis, sample)
plt.plot(time_axis, after_DAC)
plt.gcf().subplots_adjust(left=0.15, bottom=0.15)
# plt.savefig('one_symbol.png', dpi=400)
plt.show()
from MyUtils import scatterplot as scp
scp(to_ifft[0:3])