Skip to content

Commit

Permalink
add ofdm functions
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel7fontes committed Feb 28, 2023
1 parent a7e2008 commit a947740
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 47 deletions.
30 changes: 18 additions & 12 deletions examples/test_ofdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,42 @@
BER = np.zeros((len(EbN0dB_), len(qamOrder)))
BERth = np.zeros(BER.shape)

discard = 100
for ii, M in enumerate(qamOrder):
print('run sim: M = ', M)

for indSNR in tqdm(range(EbN0dB_.size)):
EbN0dB = EbN0dB_[indSNR]

# Generate random bits
if hermitSym:
bits = np.random.randint(2, size = int(np.log2(M)*(Nfft//2 - 1 - Np))*2**8)
else:
bits = np.random.randint(2, size = int(np.log2(M)*(Nfft - Np))*2**8)

# Map bits to constellation symbols
symbTx = modulateGray(bits, M, 'qam')
symbTx = pnorm(symbTx)

# Pilot symbol choice
pilot = max(symbTx.real) + 1j*max(symbTx.imag)

# OFDM symbols modulation
symbTx_OFDM = modulateOFDM(Nfft, G, pilot, pilotCarriers, symbTx, hermitSym)

# AWGN channel
snrdB = EbN0dB + 10*np.log10(np.log2(M))
symbRx_OFDM = awgn(symbTx_OFDM, snrdB)

# OFDM symbols demodulation
symbRx = demodulateOFDM(Nfft, G, pilot, pilotCarriers, symbRx_OFDM, hermitSym)

discard = 100

ind = np.arange(discard, len(symbRx) - discard)

# BER calculation
BER[indSNR, ii], _, _ = fastBERcalc(symbRx[ind], symbTx[ind], M, 'qam')
BERth[indSNR, ii] = theoryBER(M, EbN0dB, 'qam')

if BER[indSNR, ii] == 0:
break

Expand All @@ -87,12 +87,18 @@
plt.figure(figsize=(12, 8))

for ii, M in enumerate(qamOrder):
plt.plot(EbN0dB_, np.log10(BER[:,ii]),'o', ms = 8, label = str(M)+'-QAM Monte Carlo')
plt.plot(
EbN0dB_,
np.log10(BER[:, ii]),
'o',
ms=8,
label=f'{str(M)}-QAM Monte Carlo',
)

plt.gca().set_prop_cycle(None)

for ii, M in enumerate(qamOrder):
plt.plot(EbN0dB_, np.log10(BERth[:,ii]), label = str(M)+'-QAM Theory')
plt.plot(EbN0dB_, np.log10(BERth[:,ii]), label=f'{str(M)}-QAM Theory')

plt.ylim(-5, 0)
plt.xlim(min(EbN0dB_))
Expand Down
50 changes: 15 additions & 35 deletions optic/ofdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,8 @@ def calcSymbolRate(M, Rb, Nfft, Np, G, hermitSym):
OFDM symbol rate
"""

if not hermitSym:
nDataSymbols = (Nfft - Np)
else:
nDataSymbols = (Nfft//2 - 1 - Np)

Rs = Rb / (nDataSymbols/(Nfft + G) * np.log2(M))

return Rs
nDataSymbols = (Nfft//2 - 1 - Np) if hermitSym else (Nfft - Np)
return Rb / (nDataSymbols/(Nfft + G) * np.log2(M))


def modulateOFDM(Nfft, G, pilot, pilotCarriers, symbTx, hermitSym):
Expand Down Expand Up @@ -85,27 +79,23 @@ def modulateOFDM(Nfft, G, pilot, pilotCarriers, symbTx, hermitSym):

# Number of pilot subcarriers
Np = len(pilotCarriers)

# Number of subcarriers
if hermitSym:
N = Nfft//2 - 1
else:
N = Nfft

N = Nfft//2 - 1 if hermitSym else Nfft
numSymb = len(symbTx)
numOFDMframes = numSymb//(N - Np)

Carriers = np.arange(0, N)
dataCarriers = np.array(list(set(Carriers) - set(pilotCarriers)))

symbTx_par = np.reshape(symbTx, (numOFDMframes, N - Np))
symbTx_OFDM_par = np.zeros( (numOFDMframes, Nfft + G), complex)

for indFrame in range(numOFDMframes):
# Pilot subcarriers inclusion
symbTx_OFDM_par[indFrame, G : G + N][dataCarriers] = symbTx_par[indFrame, :]
symbTx_OFDM_par[indFrame, G : G + N][pilotCarriers] = pilot

# Hermitian symmetry
if hermitSym:
symbTx_OFDM_par[indFrame, G : G + Nfft] = hermit(symbTx_OFDM_par[indFrame, G : G + N])
Expand All @@ -116,10 +106,7 @@ def modulateOFDM(Nfft, G, pilot, pilotCarriers, symbTx, hermitSym):
# Cyclic prefix addition
symbTx_OFDM_par[indFrame, 0 : G] = symbTx_OFDM_par[indFrame, Nfft : Nfft + G].copy()

# Parallel -> Serial conversion
symbTx_OFDM = symbTx_OFDM_par.reshape(1,-1).reshape(-1,)

return symbTx_OFDM
return symbTx_OFDM_par.reshape(1,-1).reshape(-1,)


def demodulateOFDM(Nfft, G, pilot, pilotCarriers, symbRx_OFDM, hermitSym):
Expand Down Expand Up @@ -148,16 +135,12 @@ def demodulateOFDM(Nfft, G, pilot, pilotCarriers, symbRx_OFDM, hermitSym):

# Number of pilot subcarriers
Np = len(pilotCarriers)

# Number of subcarriers
if hermitSym:
N = Nfft//2 - 1
else:
N = Nfft

N = Nfft//2 - 1 if hermitSym else Nfft
Carriers = np.arange(0, N)
dataCarriers = np.array(list(set(Carriers) - set(pilotCarriers)))

H_abs = 0
H_pha = 0

Expand All @@ -168,15 +151,15 @@ def demodulateOFDM(Nfft, G, pilot, pilotCarriers, symbRx_OFDM, hermitSym):

# Cyclic prefix extraction
symbRx_OFDM_par = symbRx_OFDM_par[:, G : G + Nfft]

# FFT operation
for indFrame in range(numOFDMframes):
symbRx_OFDM_par[indFrame, :] = fft(symbRx_OFDM_par[indFrame,:]) / np.sqrt(Nfft)

if hermitSym:
# Removal of hermitian symmetry
symbRx_OFDM_par = symbRx_OFDM_par[:, 1 : 1 + N]

# Equalization
if Np != 0:
# Channel estimation
Expand All @@ -196,7 +179,4 @@ def demodulateOFDM(Nfft, G, pilot, pilotCarriers, symbRx_OFDM, hermitSym):
# Pilot extraction
symbRx_OFDM_par = symbRx_OFDM_par[:, dataCarriers]

# Parallel -> Serial conversion
symbRx = symbRx_OFDM_par.reshape(1,-1).reshape(-1,)

return symbRx
return symbRx_OFDM_par.reshape(1,-1).reshape(-1,)

0 comments on commit a947740

Please sign in to comment.