-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimulate.cpp
More file actions
148 lines (130 loc) · 6.24 KB
/
simulate.cpp
File metadata and controls
148 lines (130 loc) · 6.24 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**********************************************************************************/
/* Copyright © 2011, Kumar Appaiah, Radha Krishna Ganti, Kannan Srinivasan */
/* The University of Texas at Austin */
/* */
/* This file is part of Simple OFDM Modem. */
/* */
/* Simple OFDM Modem is free software: you can redistribute it and/or */
/* modify it under the terms of the GNU General Public License as */
/* published by the Free Software Foundation, either version 3 of the */
/* License, or (at your option) any later version. */
/* */
/* Simple OFDM Modem is distributed in the hope that it will be */
/* useful, but WITHOUT ANY WARRANTY; without even the implied */
/* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
/* See the GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with Simple OFDM Modem. If not, see */
/* <http://www.gnu.org/licenses/>. */
/* */
/**********************************************************************************/
#include <iostream>
#include "parameters.hpp"
#include "receive.hpp"
#include "transmit.hpp"
#define NBITS 1024
#define ITER 1000
#define FREQ_OFFSET_ON true
int
main(int argc, char *argv[])
{
RNG_randomize();
// Get command-line parameters
double snr_dB = (argc > 1) ? atof(argv[1]) : 3; // Get SNR from command line
int iter = (argc > 2) ? atoi(argv[2]) : ITER;
bool use_ldpc = (argc > 3) ? (argv[3][0] == '1') : false;
const char *ldpc_code = (argc > 4) ? argv[4] : "mackay_204.33.484_opt.it";
bool irregular = ldpc_code[0] == 'R';
double snr = inv_dB(snr_dB);
bvec bits;
bvec recv_bits, encoded_bits;
QAM qam(4);
AWGN_Channel awgn_channel(1.0 / snr / 2.0);
OFDM ofdm(NFFT, NCP);
cvec modulated_symbols, received_symbols, transmitted_symbols, received_symbols_equalized;
cvec estimation_sequence_symbol_bpsk = generate_special_estimation_sequence();
cvec estimation_sequence_symbol = ofdm.modulate(estimation_sequence_symbol_bpsk);
cvec channel_estimate_subcarriers;
BERC berc;
BLERC blerc;
bool blocksize_set = false;
// Transmit side
LDPC_Generator_Systematic G; // for codes created with ldpc_gen_codes since generator exists
LDPC_Code C(ldpc_code, &G);
C.set_exit_conditions(250);
// C.set_llrcalc(LLR_calc_unit(12,0,7));
// if (use_ldpc) { cout << C << endl; }
int pos; // Frame start marker
int pd; // Detected packet: yes/no
double cfo_hat, cfo_hat_giannakis; // frequency offset
int coarse_f = 0;
int n_successful_detects = 0;
int packet_length = 0;
cvec pilots, symbols, symbols_n, subvector;
vec softbits;
QLLRvec llr;
for (int i = 0; i < iter; ++i) {
symbols = "";
// Transmit side
encoded_bits = zeros_b(C.get_nvar());
if (!irregular) {
bits = use_ldpc ? randb(C.get_nvar() - C.get_ncheck()) : randb(NBITS);
encoded_bits = use_ldpc ? C.encode(bits) : bits;
}
else if (use_ldpc) {
bits = zeros_b(C.get_nvar() - C.get_ncheck());
}
if (!blocksize_set) {
blerc.set_blocksize(bits.size());
blocksize_set = true;
}
fill_bits_into_ofdm_symbols(encoded_bits, qam, ofdm, estimation_sequence_symbol, &packet_length, modulated_symbols);
// Channel
transmitted_symbols = concat(zeros_c(100), modulated_symbols, zeros_c(10));
received_symbols = awgn_channel(transmitted_symbols);
#if FREQ_OFFSET_ON == true
introduce_frequency_offset(received_symbols, 0.02);
#endif
// Receive side
spc_timing_freq_recovery_wrap(received_symbols, received_symbols.length(), PREAMBLE_LEN, NREPS_PREAMBLE, 0.1, &pos, &cfo_hat, &pd);
if (pd) { // If packet detected
received_symbols.del(0, pos - 2 + NREPS_PREAMBLE * PREAMBLE_LEN);
received_symbols = received_symbols.left(packet_length);
// Giannakis frequency offset estimation
cfo_hat_giannakis = estimate_frequency_offset(received_symbols.mid(16, 144), 1024);
#if FREQ_OFFSET_ON == true
introduce_frequency_offset(received_symbols, - 2 * M_PI * cfo_hat_giannakis);
#endif
received_symbols.del(0, 159+16);
// Frequency offset jugglery
coarse_f = double(channel_coarse_frequency_estimate(ofdm, received_symbols.left(NREP_ESTIMATION_SYMBOL * (NFFT + NCP)), estimation_sequence_symbol_bpsk, channel_estimate_subcarriers));
#if FREQ_OFFSET_ON == true
introduce_frequency_offset(received_symbols,-2*M_PI* coarse_f/NFFT);
#endif
received_symbols.del(0, NREP_ESTIMATION_SYMBOL * (NFFT + NCP) - 1);
channel_equalize_and_demodulate(ofdm, channel_estimate_subcarriers, received_symbols, received_symbols_equalized);
for (int n = 0; n < received_symbols_equalized.length() / NFFT; ++n) {
extract_ofdm_symbol(received_symbols_equalized.mid(n * NFFT, NFFT), pilots, symbols_n);
symbols = concat(symbols, symbols_n);
}
// cout << "x = " << symbols << ";" << endl;
symbols = fourth_power_derotate(symbols);
// cout << "y = " << symbols << ";" << endl;
// exit(1);
if (!use_ldpc) {
recv_bits = qam.demodulate_bits(symbols);
}
else {
softbits = qam.demodulate_soft_bits(symbols, 1.0 / snr / 2.0 / C.get_rate(), LOGMAP);
C.bp_decode(C.get_llrcalc().to_qllr(softbits.left(encoded_bits.length())), llr);
recv_bits = llr < 0;
}
berc.count(bits, recv_bits.left(bits.length()));
blerc.count(bits, recv_bits.left(bits.length()));
}
n_successful_detects = n_successful_detects + pd;
}
cout << snr_dB << "\t" << berc.get_errorrate() << "\t" << blerc.get_errorrate() << "\t" << n_successful_detects << "\t" << iter << endl;
return 0;
}