# BPSK Bit Error Rate Calculation Using Python

Have you ever thought about how life would be without MATLAB. As it turns out there are free and open source options such as Python. We have so far restricted ourself to MATLAB in this blog but now we venture out to find out what are the other options. Given below is a most basic Pyhton code that calculates the Bit Error Rate of Binary Phase Shift Keying (BPSK). Compare this to our MATLAB implementation earlier [BPSK BER].

There are various IDEs available for writing your code but I have used Enthought Canopy Editor (32 bit) which is free to download and is also quite easy to use [download here]. So as it turns out that there is life beyond MATLAB. In fact there are several advantages of using Python over MATLAB which we will discuss later in another post. Lastly please note the indentation in the code below as there is no “end” statement in a for loop in Python.

```from numpy import sqrt
from numpy.random import rand, randn
import matplotlib.pyplot as plt

N = 5000000
EbNodB_range = range(0,11)
itr = len(EbNodB_range)
ber = [None]*itr

for n in range (0, itr):

EbNodB = EbNodB_range[n]
EbNo=10.0**(EbNodB/10.0)
x = 2 * (rand(N) >= 0.5) - 1
noise_std = 1/sqrt(2*EbNo)
y = x + noise_std * randn(N)
y_d = 2 * (y >= 0) - 1
errors = (x != y_d).sum()
ber[n] = 1.0 * errors / N

print "EbNodB:", EbNodB
print "Error bits:", errors
print "Error probability:", ber[n]

plt.plot(EbNodB_range, ber, 'bo', EbNodB_range, ber, 'k')
plt.axis([0, 10, 1e-6, 0.1])
plt.xscale('linear')
plt.yscale('log')
plt.xlabel('EbNo(dB)')
plt.ylabel('BER')
plt.grid(True)
plt.title('BPSK Modulation')
plt.show()
```

MATLAB vs PYTHON A COMPARISON

# Theoretical BER of M-QAM in Rayleigh Fading

We have previously discussed the Bit Error Rate of M-QAM in Rayleigh Fading using Monte Carlo Simulation. We now turn our attention to calculation of Bit Error Rate (BER) of M-QAM in Rayleigh fading using analytical techniques. In particular we look at the method used in MATLAB function berfading.m. In this function the BER of 4-QAM, 16-QAM and 64-QAM is calculated from series expressions having 1, 3 and 5 terms respectively. These are given below (M is the constellation size and must be a power of 2).

```if (M == 4)
ber = 1/2 * ( 1 - sqrt(gamma_c/k./(1+gamma_c/k)) );
elseif (M == 16)
ber = 3/8 * ( 1 - sqrt(2/5*gamma_c/k./(1+2/5*gamma_c/k)) ) ...
+ 1/4 * ( 1 - sqrt(18/5*gamma_c/k./(1+18/5*gamma_c/k)) ) ...
- 1/8 * ( 1 - sqrt(10*gamma_c/k./(1+10*gamma_c/k)) );
elseif (M == 64)
ber = 7/24 * ( 1 - sqrt(1/7*gamma_c/k./(1+1/7*gamma_c/k)) ) ...
+ 1/4 * ( 1 - sqrt(9/7*gamma_c/k./(1+9/7*gamma_c/k)) ) ...
- 1/24 * ( 1 - sqrt(25/7*gamma_c/k./(1+25/7*gamma_c/k)) ) ...
+ 1/24 * ( 1 - sqrt(81/7*gamma_c/k./(1+81/7*gamma_c/k)) ) ...
- 1/24 * ( 1 - sqrt(169/7*gamma_c/k./(1+169/7*gamma_c/k)) );
```

Although using these expressions we get very accurate BER but it is not that simple to calculate (the expressions become even more complicated for higher constellation sizes such as 256-QAM). Therefore we try to simplify these expressions by using only the first term in each expression. To our surprise the results match quite well with the results using the exact formulae. There is very minor difference at low signal to noise ratios but that can be easily bargained for the ease of calculation.

So here is our program for calculating the BER using the approximate method.

```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION TO CALCULATE THE BER OF M-QAM IN RAYLEIGH FADING
% M: Input, Constellation Size
% EbNo: Input, Energy Per Bit to Noise Power Spectral Density
% ber: Output, Bit Error Rate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ber]= BER_QAM_fading (M, EbNo)

k=log2(M);
EbNoLin=10.^(EbNo/10);
gamma_c=EbNoLin*k;

if M==4
%4-QAM
ber = 1/2 * ( 1 - sqrt(gamma_c/k./(1+gamma_c/k)) );
elseif M==16
%16-QAM
ber = 3/8 * ( 1 - sqrt(2/5*gamma_c/k./(1+2/5*gamma_c/k)) );
elseif M==64
%64-QAM
ber = 7/24 * ( 1 - sqrt(1/7*gamma_c/k./(1+1/7*gamma_c/k)) );
else
%Warning
warning('M=4,16,64')
ber=zeros(1,length(EbNo));
end

semilogy(EbNo,ber,'o-')
xlabel('EbNo(dB)')
ylabel('BER')
axis([0 24 0.001 1])
grid on

return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
```

So we see that the results match quite well with the results previously obtained through simulation. We will next tackle the problem of simplifying the expression for higher order modulations such as 256-QAM in both Rayleigh and Ricean channels.

# M-QAM Bit Error Rate in Rayleigh Fading

We have previously discussed the bit error rate (BER) performance of M-QAM in AWGN. We now discuss the BER performance of M-QAM in Rayleigh fading. The one-tap Rayleigh fading channel is generated from two orthogonal Gaussian random variables with variance of 0.5 each. The complex random channel coefficient so generated has an amplitude which is Rayleigh distributed and a phase which is uniformly distributed. As usual the fading channel introduces a multiplicative effect whereas the AWGN is additive.

The function “QAM_fading” has three inputs, ‘n_bits’, ‘M’, ‘EbNodB’ and one output ‘ber’. The inputs are the number of bits to be passed through the channel, the alphabet size and the Energy per Bit to Noise Power Spectral Density in dB respectively whereas the output is the bit error rate (BER).

```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION THAT CALCULATES THE BER OF M-QAM IN RAYLEIGH FADING
% n_bits: Input, number of bits
% M: Input, constellation size
% EbNodB: Input, energy per bit to noise power spectral density
% ber: Output, bit error rate
% Copyright RAYmaps (www.raymaps.com)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[ber]= QAM_fading(n_bits, M, EbNodB)
% Transmitter
k=log2(M);
EbNo=10^(EbNodB/10);
x=transpose(round(rand(1,n_bits)));
h1=modem.qammod(M);
h1.inputtype='bit';
h1.symbolorder='gray';
y=modulate(h1,x);

% Channel
Eb=mean((abs(y)).^2)/k;
sigma=sqrt(Eb/(2*EbNo));
w=sigma*(randn(n_bits/k,1)+1i*randn(n_bits/k,1));
h=(1/sqrt(2))*(randn(n_bits/k,1)+1i*randn(n_bits/k,1));
r=h.*y+w;

r=r./h;
h2=modem.qamdemod(M);
h2.outputtype='bit';
h2.symbolorder='gray';
h2.decisiontype='hard decision';
z=demodulate(h2,r);
ber=(n_bits-sum(x==z))/n_bits
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
```

The bit error rates of four modulation schemes 4-QAM, 16-QAM, 64-QAM and 256-QAM are shown in the figure above. All modulation schemes use Gray coding which gives a few dB of margin in the BER performance. As with the AWGN case each additional bit per symbol requires about 1.5-2 dB in signal to ratio to achieve the same BER.

Although not shown here similar behavior is observed for higher order modulation schemes such as 1024-QAM and 4096-QAM (the gap in the signal to noise ratio for the same BER is increased to about 5dB).

Lastly we explain some of the terms used above.

Rayleigh Fading is a commonly used term in simulation of Digital Communication Systems but it tends to differ in meaning in different contexts. The term Rayleigh Fading as used above means a single tap channel that varies from one symbol to the next. It has an amplitude which is Rayleigh distributed and a phase which is Uniformly distributed. A single tap channel means that it does not introduce any Inter Symbol Interference (ISI). Such a channel is also referred to as a Flat Fading Channel. The channel can also be referred to as a Fast Fading Channel since each symbol experiences a new channel state which is independent of its previous state (also termed as uncorrelated).

Gray Coding

When using QAM modulation, each QAM symbol represents 2,3,4 or higher number of bits. That means that when a symbol error occurs a number of bits are reversed. Now a good way to do the bit-to-symbol assignment is to do it in a way such that no neighboring symbols differ by more than one bit e.g. in 16-QAM, a symbol that represents a binary word 1101 is surrounded by four symbols representing 0101, 1100, 1001 and 1111. So if a symbol error is made, only one bit would be in error. However, one must note that this is true only in good signal conditions. When the SNR is low (noise has a higher magnitude) the symbol might be displaced to a location that is not adjacent and we might get higher number of bits in error.

Hard Decision

The concept of hard decision decoding is important when talking about channel coding, which we have not used in the above simulation. However, we will briefly explain it here. Hard decision is based on what is called “Hamming Distance” whereas soft decision is based on what it called “Euclidean Distance”. Hamming Distance is the distance of a code word in binary form, such as 011 differs from 010 and 001 by 1. Whereas the Euclidean distance is the distance before a decision is made that a bit is zero or one.  So if the received sequence is 0.1 0.6 0.7 we get a Euclidean distance of 0.8124 from 010 and 0.6782 from 001. So we cannot make a hard decision about which sequence was transmitted based on the received sequence of 011. But based on the soft metrics we can make a decision that 001 was the most likely sequence that was transmitted (assuming that 010 and 001 were the only possible transmitted sequences).

# M-QAM Bit Error Rate in AWGN

Quadrature Amplitude Modulation has been adopted by most wireless communication standards such as WiMAX and LTE. It provides higher bit rates and consequently higher spectral efficiencies. It is usually used in conjunction with Orthogonal Frequency Division Multiplexing (OFDM) which provides a simple technique to overcome the time varying frequency selective channel.

We have previously discussed the formula for calculating the bit error rate (BER) of QAM in AWGN. We now calculate the same using a simple Monte Carlo Simulation.

```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION THAT CALCULATES THE BER OF M-QAM IN AWGN
% n_bits: Input, number of bits
% M: Input, constellation size
% EbNodB: Input, energy per bit to noise power spectral density
% ber: Output, bit error rate
% Copyright RAYmaps (www.raymaps.com)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function[ber]= QAM_AWGN(n_bits, M, EbNodB)

% Transmitter
k=log2(M);
EbNo=10^(EbNodB/10);
x=transpose(round(rand(1,n_bits)));
h1=modem.qammod(M);
h1.inputtype='bit';
h1.symbolorder='gray';
y=modulate(h1,x);

% Channel
Eb=mean((abs(y)).^2)/k;
sigma=sqrt(Eb/(2*EbNo));
w=sigma*(randn(1,n_bits/k)+1i*randn(1,n_bits/k));
r=y+w';

h2=modem.qamdemod(M);
h2.outputtype='bit';
h2.symbolorder='gray';
h2.decisiontype='hard decision';
z=demodulate(h2,r);
ber=(n_bits-sum(x==z))/n_bits
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
```

The above function basically has three inputs and one output. The inputs are the number of bits to be passed through the channel, the size of the constellation and the signal to noise ratio in dB. The output is the bit error rate (BER). The simulation can be divided into three section namely the transmitter, the channel and the receiver. In this simulation we have used Gray coding which gives us about a dB of improvement at low to medium signal to noise ratio.

As seen above the BER obtained through our simulation matches quite well with the BER obtained through the theoretical formula. Each additional bit per symbol required about 2dB extra in signal to noise ratio to achieve the same bit error rate.

Lastly we explain some of the terms used above.

AWGN

All wireless receivers suffer from thermal noise which is a function of absolute temperature and bandwidth of the receiver. This noise is added to the received signal and makes detection of weak signals a major challenge. Just to given you an idea typical GSM receivers have a noise floor of -113 dBm. Therefore, if the received signal has a power of -100 dBm we get a signal to noise ratio (SNR) of 13 dB. In simulation this noise is usually modeled as a Gaussian Random Process. It is additive, as opposed to channel impairments which are multiplicative and has a flat spectrum (thus called White Noise).

Gray Coding

When using QAM modulation, each QAM symbol represents 2,3,4 or higher number of bits. That means that when a symbol error occurs a number of bits are reversed. Now a good way to do the bit-to-symbol assignment is to do it in a way such that no neighboring symbols differ by more than one bit e.g. in 16-QAM, a symbol that represents a binary word 1101 is surrounded by four symbols representing 0101, 1100, 1001 and 1111. So if a symbol error is made, only one bit would be in error. However, one must note that this is true only in good signal conditions. When the SNR is low (noise has a higher magnitude) the symbol might be displaced to a location that is not adjacent and we might get higher number of bits in error.

Hard Decision

The concept of hard decision decoding is important when talking about channel coding, which we have not used in the above simulation. However, we will briefly explain it here. Hard decision is based on what is called “Hamming Distance” whereas soft decision is based on what it called “Euclidean Distance”. Hamming Distance is the distance of a code word in binary form, such as 011 differs from 010 and 001 by 1. Whereas the Euclidean distance is the distance before a decision is made that a bit is zero or one.  So if the received sequence is 0.1 0.6 0.7 we get a Euclidean distance of 0.8124 from 010 and 0.6782 from 001. So we cannot make a hard decision about which sequence was transmitted based on the received sequence of 011. But based on the soft metrics we can make a decision that 001 was the most likely sequence that was transmitted (assuming that 010 and 001 were the only possible transmitted sequences).

# QAM Theoretical BER in AWGN

Quadrature Amplitude Modulation (QAM) is an important modulation scheme as it allows for higher data rates and spectral efficiencies. The bit error rate (BER) of QAM can be calculated through Monte Carlo simulations. However this becomes quite complex as the constellation size of the modulation schemes increases. Therefore a theoretical approach is sometimes preferred. The BER for Gray coded QAM, for even number of bits per symbol, is shown below.

Gray coding ensures that a symbol error results in a single bit error. The code for calculating the theoretical QAM BER for k even (even number of bits per symbol) is given below. The formula for calculating the BER for k odd is different, however, the formula given below can be used a first estimate.

```EbNodB=-6:2:24
EbNo=10.^(EbNodB/10);
k=8;
M=2^k;
x=sqrt(3*k*EbNo/(M-1));
Pb=(4/k)*(1-1/sqrt(M))*(1/2)*erfc(x/sqrt(2));
semilogy(EbNodB,Pb)```

Note:
1. Each additional bit/symbol requires about 2dB extra in SNR to achieve the same BER.
2. 4-QAM is essentially QPSK modulation.

# Can We Do Without a Cyclic Prefix

Have you ever thought that Cyclic Prefix in OFDM is just a gimmick and we could do equally well by using a guard period i.e. a period of no transmission between two OFDM symbols. Well, one way to find out if this is true is by running a bit error rate simulation with and without a cyclic prefix (only a vacant guard period). We use the 64-QAM OFDM simulation that we developed previously. The channel is modeled as 7-tap FIR filter with each tap having a Rayleigh distribution.

BER with and without Cyclic Prefix

We simulate the case of a vacant guard period by inserting zeros in the time slot dedicated for the CP (32 samples). It is observed that there is a vast difference in the bit error rate (BER) for the two cases. In fact in the case of no CP the BER hits an error floor at around 20 dB. Increasing the signal to noise ratio does not improve the BER performance any further.

Now to answer the question “why does the CP work” we have to revisit the concept of circular convolution from our DSP course. It is well known that performing circular convolution of two sequences in the time domain is equivalent to multiplication of their DFT’s in the frequency domain. So if a wireless channel performed circular convolution we could do simple division to recover the signal after the FFT operation in the receiver.

Y(k)=X(k)*H(k) Effect of Wireless Channel

X(k)=Y(k)/H(k) Recovery of the Signal

But the wireless channel does not perform circular convolution, it performs linear convolution. So the trick is to make this linear convolution appear as circular convolution by appending a cyclic prefix. The result is that equalization can be performed at the receiver by simple division.

For a more elaborate discussion on this you may visit CP-1 or for a mathematical description you may visit CP-2.

Note: In a actual system there would be AWGN noise added to the received signal as well. Giving us the following relationships.

Y(k)=X(k)*H(k)+W(k) Effect of Wireless Channel

X(k)’=Y(k)/H(k)=X(k)+W(k)/H(k) Recovery of the Signal

# BER of 64-QAM OFDM in Frequency Selective Fading-II

In the previous post we had considered a static frequency-selective channel. We now consider a time-varying frequency selective channel with 7 taps. Each tap of the time domain filter has a Gaussian distributed real component with variance 1/(2*n_tap) and a Gaussian distributed imaginary component with variance (1/2*n_tap). The amplitude of each tap is thus Rayleigh distributed and the phase is Uniformly distributed. Since the power in each component is normalized by the filter length (n_tap) the BER performance would remain the same even if the filter length is changed (this has been verified experimentally).

```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION TO SIMULATE PERFORMANCE OF 64-OFDM IN TIME VARYING FREQUENCY SELECTIVE CHANNEL
% n_bits: Input, length of binary sequence
% n_fft: Input, length of FFT (Fast Fourier Transform)
% EbNodB: Input, energy per bit to noise power spectral density ratio
% ber: Output, bit error rate
% Copyright RAYmaps (www.raymaps.com)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Eb=7;
M=64;
k=log2(M);
n_cyc=32;
EbNo=10^(EbNodB/10);
x=transpose(round(rand(1,n_bits)));
h1=modem.qammod(M);
h1.inputtype='bit';
h1.symbolorder='gray';
y=modulate(h1,x);
n_sym=length(y)/n_fft;
n_tap=7;

for n=1:n_sym;
s_ofdm=sqrt(n_fft)*ifft(y((n-1)*n_fft+1:n*n_fft),n_fft);
s_ofdm_cyc=[s_ofdm(n_fft-n_cyc+1:n_fft); s_ofdm];
ht=(1/sqrt(2))*(1/sqrt(n_tap))*(randn(1,n_tap)+j*randn(1,n_tap));
Hf=fft(ht,n_fft);
r_ofdm_cyc=conv(s_ofdm_cyc,ht);
r_ofdm_cyc=(r_ofdm_cyc(1:n_fft+n_cyc));
wn=sqrt((n_fft+n_cyc)/n_fft)*(randn(1,n_fft+n_cyc)+j*randn(1,n_fft+n_cyc));
r_ofdm_cyc=r_ofdm_cyc+sqrt(Eb/(2*EbNo))*wn.';
r_ofdm=r_ofdm_cyc(n_cyc+1:n_fft+n_cyc);
s_est((n-1)*n_fft+1:n*n_fft)=(fft(r_ofdm,n_fft)/sqrt(n_fft))./Hf.';
end

h2=modem.qamdemod(M);
h2.outputtype='bit';
h2.symbolorder='gray';
h2.decisiontype='hard decision';
z=demodulate(h2,s_est.');
ber=(n_bits-sum(x==z))/n_bits
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
```

As before we have used an FFT size of 128 and cyclic prefix of 32 samples. The FFT and IIFT operations are normalized to maintain the signal to noise ratio (SNR). The extra energy transmitted in the cyclic prefix is also accounted for in the SNR calibration.

It is observed that the BER performance of 64-QAM OFDM in the time-varying frequency-selective channel is quite similar to that in the static frequency-selective channel with complex filter taps. It must be noted that with 64-QAM the goal is to achieve higher bit rate, error rates can be improved using antenna diversity and channel coding schemes.

Given below is the wrapper that should be used along with the above code. The wrapper basically calls the above routine for each value of EbNodB. The length of the binary sequence and the FFT size are other inputs to the function. The bit error rate at the specific EbNodB is the output of the function.

```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
close all;
k=6;
n_fft=128;
l=k*n_fft*1e3;
EbNodB=0:2:20;
for n=1:length(EbNodB);n
end;
semilogy(EbNodB,ber,'O-');
grid on
xlabel('EbNo')
ylabel('BER')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%```

In future we would use the standard LTE channel models, namely EPA, EVA and ETU in our simulation.

# BER of 64-QAM OFDM in Frequency Selective Fading

The real benefits of OFDM become apparent in a frequency selective channel. The introduction of the cyclic prefix (guard period) allows us to remove the Intersymbol Interference (ISI) in the time domain and frequency domain equalization allows us to overcome the channel variations in the frequency domain.

We consider a simple FIR filter for our channel model with coefficients ht=[0.8 0.54 0.24 0.10 0.04]. This is a simplistic approach since the channel coefficients are all real which means that all multipath components are co-phase. To model a more realistic channel we then introduce a uniform phase shift to all the channel coefficients.

We use an FFT size of 128 and cyclic prefix of 32 samples (16.67usec) in the simulation given below.

```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION TO SIMULATE THE PERFORMANCE OF 64-QAM OFDM IN STATIC FREQUENCY SELECTIVE CHANNEL
% n_bits: Input, length of binary sequence
% n_fft: Input, length of FFT (Fast Fourier Transform)
% EbNodB: Input, energy per bit to noise power spectral density ratio
% ber: Output, bit error rate
% Copyright RAYmaps (www.raymaps.com)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Eb=7;
M=64;
k=log2(M);
n_cyc=32;
EbNo=10^(EbNodB/10);
x=transpose(round(rand(1,n_bits)));
h1=modem.qammod(M);
h1.inputtype='bit';
h1.symbolorder='gray';
y=modulate(h1,x);
n_sym=length(y)/n_fft;

for n=1:n_sym;
s_ofdm=sqrt(n_fft)*ifft(y((n-1)*n_fft+1:n*n_fft),n_fft);
s_ofdm_cyc=[s_ofdm(n_fft-n_cyc+1:n_fft); s_ofdm];
ht=[0.8 0.54 0.24 0.10 0.04];
Hf=fft(ht,n_fft);
r_ofdm_cyc=conv(s_ofdm_cyc,ht);
r_ofdm_cyc=(r_ofdm_cyc(1:n_fft+n_cyc));
wn=sqrt((n_fft+n_cyc)/n_fft)*(randn(1,n_fft+n_cyc)+j*randn(1,n_fft+n_cyc));
r_ofdm_cyc=r_ofdm_cyc+sqrt(Eb/(2*EbNo))*wn.';
r_ofdm=r_ofdm_cyc(n_cyc+1:n_fft+n_cyc);
s_est((n-1)*n_fft+1:n*n_fft)=(fft(r_ofdm,n_fft)/sqrt(n_fft))./Hf.';
end

h2=modem.qamdemod(M);
h2.outputtype='bit';
h2.symbolorder='gray';
h2.decisiontype='hard decision';
z=demodulate(h2,s_est.');
ber=(n_bits-sum(x==z))/n_bits
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
```

We also have accounted for the extra energy transmitted for the cyclic prefix in our signal to noise calibration.

It can be seen that up to 12dB the BER performance for the two cases is quite similar, however, after 12dB the BER for the real case drops significantly whereas the BER for the complex case goes down in linear fashion. The error rate can be significantly improved by employing channel coding and antenna diversity schemes.

# BER of 64-QAM OFDM in AWGN

64-QAM is an important component of the LTE Air Interface that promises higher data rates and spectral efficiencies. Combined with OFDM and MIMO it successfully combats the detrimental effects of the wireless channels and provides data rates in excess of 100Mbps (peak data rate). Here, we discuss a simple example of 64-QAM modulation with OFDM in an AWGN channel. We assume a bandwidth of 1.25MHz which corresponds to an FFT size of 128.

LTE Bandwidth

Given below is the code for this scheme.

```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION TO CALCULATE BER OF 64-QAM OFDM IN AWGN
% n_bits: Input, number of bits
% n_fft: Input, FFT size
% EbNodB: Input, energy per bit to noise PSD
% ber: Output, bit error rate
% Copyright RAYmaps (www.raymaps.com)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[ber]= M_QAM(n_bits,n_fft,EbNodB);
Eb=7;
M=64;
k=log2(M);
EbNo=10^(EbNodB/10);
x=transpose(round(rand(1,n_bits)));
h1=modem.qammod(M);
h1.inputtype='bit';
h1.symbolorder='gray';
y=modulate(h1,x);
n_sym=length(y)/n_fft;
s_ofdm=zeros(1,n_fft);
r_ofdm=zeros(1,n_fft);
for n=1:n_sym;
s_ofdm=sqrt(n_fft)*ifft(y((n-1)*n_fft+1:n*n_fft),n_fft);
wn=randn(1,n_fft)+j*randn(1,n_fft);
r_ofdm=s_ofdm+sqrt(Eb/(2*EbNo))*wn.';
s_est((n-1)*n_fft+1:n*n_fft)=fft(r_ofdm,n_fft)/sqrt(n_fft);
end
h2=modem.qamdemod(M);
h2.outputtype='bit';
h2.symbolorder='gray';
h2.decisiontype='hard decision';
z=demodulate(h2,s_est.');
ber=(n_bits-sum(x==z))/n_bits;
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
```

As discussed previously, with proper normalization of IFFT and FFT operations the performance of OFDM in AWGN is the same as the performance of the underlying modulation scheme. We have not even introduced the cyclic prefix in our simulation because without a fading channel there is no ISI and cyclic prefix (CP) is of no use. We will introduce the CP when we turn our attention to fading channels.

OFDM 64-QAM

It must be noted that although IFFT and FFT are linear inverses of each other proper normalization is required to maintain the signal levels at the transmitter and receiver.

# Bit Error Rate of 64-QAM in AWGN

64-QAM is an important modulation scheme being used in WiMAX and LTE. It allows for transmission of 6 bits symbol which results in higher bit rate and spectral efficiency. The calculation of bit error rate of 64-QAM is a bit tricky as there are many different formulas available with varying degrees of accuracy. Here, we first calculate the bit error rate (BER) of 64-QAM using a simulation and then compare it to the theoretical curve for 64-QAM.

```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION TO CALCULATE 64-QAM BER USING SIMULATION
% n_bits: Input, number of bits
% EbNodB: Input, energy per bit to noise PSD
% ber: Output, bit error rate
% Copyright RAYmaps (www.raymaps.com)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[ber]= M_QAM(n_bits,EbNodB);
M=64;
k=log2(M)
EbNo=10^(EbNodB/10);
x=transpose(round(rand(1,n_bits)));
h1=modem.qammod(M);
h1.inputtype='bit';
h1.symbolorder='gray';
y=modulate(h1,x);
n=randn(1,n_bits/k)+j*randn(1,n_bits/k);
y=y+sqrt(7/(2*EbNo))*n.';
h2=modem.qamdemod(M)
h2.outputtype='bit';
h2.symbolorder='gray';
h2.decisiontype='hard decision';
z=demodulate(h2,y);
ber=(n_bits-sum(x==z))/n_bits
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
```
```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CALCULATE 64-QAM BER USING FORMULA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
EbNodB=0:2:16;
EbNo=10.^(EbNodB/10);
k=6;
M=64;
x=sqrt(3*k*EbNo/(M-1));
Pb=(4/k)*(1-1/sqrt(M))*(1/2)*erfc(x/sqrt(2));
semilogy(EbNodB,Pb)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
```

Using the above functions the BER of 64-QAM is calculated as shown below. Also shown is the constellation diagram of 64-QAM after addition of noise.

It is observed that the theoretical curve almost overlaps the simulation results. There is only a very small difference at very low signal to noise ratio. The BER of 64-QAM at 16dB is approximately equal to the BER for QPSK at 8dB. Therefore the 64-QAM can only be used in scenarios where there is a very good signal to noise ratio.

In this post we have used built in MATLAB functions for modulation and demodulation. In future posts we try to build up the simulation without using these functions!