-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofdm_simulation.m
128 lines (102 loc) · 4.11 KB
/
ofdm_simulation.m
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
% Parameters for QPSK
M = 4; % Modulation alphabet for QPSK
numSC = 512; % Number of OFDM subcarriers
cpLen = 64; % OFDM cyclic prefix length
maxBitErrors = 1000; % Maximum number of bit errors
maxNumBits = 1e7; % Maximum number of bits transmitted
% Additional Parameters for 16-QAM
M16QAM = 16; % Modulation alphabet for 16-QAM
EbNoVec16QAM = (0:10)'; % Eb/No Vector for 16-QAM
snrVec16QAM = EbNoVec16QAM + 10*log10(log2(M16QAM)); % SNR Vector for 16-QAM
% Initialize figure
figure
% QPSK Modulator and Demodulator
qpskMod = comm.QPSKModulator('BitInput', true);
qpskDemod = comm.QPSKDemodulator('BitOutput', true);
% OFDM Modulator and Demodulator for QPSK
ofdmMod = comm.OFDMModulator('FFTLength', numSC, 'CyclicPrefixLength', cpLen);
ofdmDemod = comm.OFDMDemodulator('FFTLength', numSC, 'CyclicPrefixLength', cpLen);
% AWGN Channel
channel = comm.AWGNChannel('NoiseMethod', 'Variance', ...
'VarianceSource', 'Input port');
% Error Rate Calculator for QPSK
errorRateQPSK = comm.ErrorRate('ResetInputPort', true);
% Get OFDM dimensions
ofdmDims = info(ofdmMod);
numDC = ofdmDims.DataInputSize(1);
frameSize = [log2(M) * numDC 1];
% Eb/No Vector calculation for QPSK
EbNoVecQPSK = (0:10)';
snrVecQPSK = EbNoVecQPSK + 10*log10(log2(M)) + 10*log10(numDC/numSC);
% BER Vector initialization for QPSK
berVecQPSK = zeros(length(EbNoVecQPSK), 3);
errorStatsQPSK = zeros(1, 3);
% Simulation loop for different Eb/No values for QPSK
for m = 1:length(EbNoVecQPSK)
snr = snrVecQPSK(m);
while errorStatsQPSK(2) <= maxBitErrors && errorStatsQPSK(3) <= maxNumBits
% Generate binary data
dataIn = randi([0, 1], frameSize);
% Apply modulation and demodulation for QPSK
qpskTx = qpskMod(dataIn);
txSig = ofdmMod(qpskTx);
powerDB = 10*log10(var(txSig));
noiseVar = 10^(0.1*(powerDB-snr));
rxSig = channel(txSig, noiseVar);
qpskRx = ofdmDemod(rxSig);
dataOut = qpskDemod(qpskRx);
% Collect error statistics for QPSK
errorStatsQPSK = errorRateQPSK(dataIn, dataOut, 0);
end
% Save BER data for QPSK
berVecQPSK(m, :) = errorStatsQPSK;
errorStatsQPSK = errorRateQPSK(dataIn, dataOut, 1);
end
% Theoretical BER calculation for QPSK
berTheoryQPSK = berawgn(EbNoVecQPSK, 'qam', M);
% Plotting for QPSK
semilogy(EbNoVecQPSK, berTheoryQPSK, '^')
hold on
semilogy(EbNoVecQPSK, berTheoryQPSK)
% Reset and release OFDM objects for QPSK
reset(ofdmMod);
reset(ofdmDemod);
release(ofdmMod);
release(ofdmDemod);
% Error Rate Calculator for 16-QAM
errorRate16QAM = comm.ErrorRate('ResetInputPort', true);
% BER Vector initialization for 16-QAM
berVec16QAM = zeros(length(EbNoVec16QAM), 3);
errorStats16QAM = zeros(1, 3);
% Simulation loop for different Eb/No values for 16-QAM
for m = 1:length(EbNoVec16QAM)
snr = snrVec16QAM(m);
while errorStats16QAM(2) <= maxBitErrors && errorStats16QAM(3) <= maxNumBits
% Generate binary data
dataIn = randi([0, 1], frameSize);
% Apply modulation and demodulation for 16-QAM
qamTx = qammod(dataIn, M16QAM, 'bin');
txSig = ofdmMod(qamTx);
powerDB = 10*log10(var(txSig));
noiseVar = 10^(0.1*(powerDB-snr));
rxSig = channel(txSig, noiseVar);
qamRx = ofdmDemod(rxSig);
dataOut = qamdemod(qamRx, M16QAM, 'bin');
% Collect error statistics for 16-QAM
errorStats16QAM = errorRate16QAM(dataIn, dataOut, 0);
end
% Save BER data for 16-QAM
berVec16QAM(m, :) = errorStats16QAM;
errorStats16QAM = errorRate16QAM(dataIn, dataOut, 1);
end
% Theoretical BER calculation for 16-QAM
berTheory16QAM = berawgn(EbNoVec16QAM, 'qam', M16QAM);
% Plotting for 16-QAM
semilogy(EbNoVec16QAM, berTheory16QAM, 'o')
semilogy(EbNoVec16QAM, berTheory16QAM)
legend('Simulation QPSK', 'Theory QPSK', 'Simulation 16-QAM', ...
'Theory 16-QAM', 'Location', 'Best')
xlabel('Eb/No (dB)')
ylabel('Bit Error Rate')
grid on
hold off