Last active
July 21, 2020 17:35
-
-
Save PeterWaIIace/067bd8483dd908d19cbf761c026f62b2 to your computer and use it in GitHub Desktop.
Short code with prepared long and short sequence for OFMD IEE802.11n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#prepare OFDM | |
import numpy as np | |
from matplotlib import pyplot as plt | |
import scipy | |
import scipy.signal | |
fs = 20e6 | |
class OFDM: | |
pass | |
ofdm = OFDM() | |
ofdm.K = 64 # Number of OFDM subcarriers | |
ofdm.Kon = 52 # Number of switched-on subcarriers | |
ofdm.CP = 16 # Number of samples in the CP | |
ofdm.ofdmSymbolsPerFrame = 30 # N, number of payload symbols in each frame | |
ofdm.L = ofdm.K//2 | |
ofdm.first_subcarrier=6 | |
ofdm.Middle_zero=33 | |
def IEEE802_11_short_sequence(): #without 0 in middle - generation is dealing with that | |
return np.sqrt(13/6)*np.array([0,0,1+1j,0,0,0,-1-1j,0,0,0,1+1j,0,0,0,-1-1j, | |
0,0,0,-1-1j,0,0,0,1+1j,0,0,0,0, | |
0,0,0,-1-1j,0,0,0,-1-1j,0,0,0,1+1j,0,0,0, | |
1+1j,0,0,0,1+1j,0,0,0,1+1j,0,0]) | |
def IEEE802_11_long_sequence(): #without 0 in middle - generation is dealing with that | |
return np.array([1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 0, | |
1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1]) | |
def random_qam(ofdm): | |
qam = np.array([1+1j, 1-1j, -1+1j, -1-1j]) / np.sqrt(2) | |
return np.random.choice(qam, size=(ofdm.Kon), replace=True) | |
def ofdm_modulate(ofdm, qam,n=None,cp=True,zero_included=False): | |
fd_data = np.zeros(ofdm.K, dtype=complex) | |
off = (ofdm.K - ofdm.Kon)//2 | |
print("off",off) | |
if False==zero_included: | |
assert (len(qam) == ofdm.Kon) | |
fd_data[off:(off+int(len(qam)/2))] = qam[:int(len(qam)/2)] # modulate in the center of the frequency | |
fd_data[(off+1+int(len(qam)/2)):off+1+2*int(len(qam)/2)] = qam[int(len(qam)/2):] | |
else: | |
fd_data[off:off+len(qam)] = qam # modulate in the center of the frequency | |
fd_data = np.fft.fftshift(fd_data) | |
if n!=None: | |
symbol = np.fft.ifft(fd_data,n) * np.sqrt(n) | |
else: | |
symbol = np.fft.ifft(fd_data) * np.sqrt(ofdm.K) | |
if True == cp: | |
print("time:",len(symbol)*1/fs + len(symbol[-ofdm.CP:])*1/fs) | |
return np.hstack([symbol[-ofdm.CP:], symbol]) | |
else: | |
print("time:",len(symbol)*1/fs) | |
return symbol | |
def IEEE802_11_ofdm_preamble(ofdm): | |
samples_short = samples_long = None | |
samples_short=64 | |
samples_long=64 | |
print("0.8e-6/fs",samples_short*1/fs,samples_long*1/fs) | |
short=[] | |
short = ofdm_modulate(ofdm, IEEE802_11_short_sequence(),samples_short,cp=False,zero_included=True) | |
for n in range(1): | |
short = np.hstack([short,ofdm_modulate(ofdm,IEEE802_11_short_sequence(),samples_short,cp=False,zero_included=True)]) | |
short = np.hstack([short[:int(len(short)/4)],short]) | |
long=[] | |
long = ofdm_modulate(ofdm, IEEE802_11_long_sequence(),samples_long,cp=False,zero_included=True) | |
for n in range(1): | |
long = np.hstack([long, ofdm_modulate(ofdm,IEEE802_11_long_sequence(),samples_long,cp=False,zero_included=True)]) | |
long=np.hstack([long[-ofdm.CP*2:],long]); | |
print(len(long)*1/fs,len(short)*1/fs) | |
preamble=np.hstack([short,long]); | |
return preamble | |
# preamble = short | |
preamble=IEEE802_11_ofdm_preamble(ofdm); | |
print("Preamble time: ",len(preamble)*1/fs) | |
plt.figure(figsize=(20,5)) | |
plt.subplot(121) | |
t = np.arange(0,len(preamble)/fs,1/fs); | |
plt.plot(abs(preamble)) | |
plt.subplot(122) | |
f = np.linspace(-ofdm.K/2, ofdm.K/2, 4*len(preamble[170:]), endpoint=False) | |
plt.plot(f,abs(np.fft.fftshift(np.fft.fft(preamble[170:], 4*len(preamble[170:]))/np.sqrt(len(preamble))))) | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment