IT++ Logo
ofdm.cpp
Go to the documentation of this file.
1 
31 #include <itpp/comm/ofdm.h>
32 #include <itpp/base/specmat.h>
33 #include <itpp/base/operators.h>
34 #include <itpp/signal/transforms.h>
35 
36 
37 namespace itpp
38 {
39 
40 OFDM::OFDM(int inNfft, int inNcp, int inNupsample)
41 {
42  set_parameters(inNfft, inNcp, inNupsample);
43 }
44 
45 void OFDM::set_parameters(const int inNfft, const int inNcp, const int inNupsample)
46 {
47  it_assert(inNfft >= 2, "OFDM: Nfft must be >=2.");
48  it_assert(inNcp >= 0 && inNcp <= inNfft, "OFDM: Ncp must be >=0 and <=Nfft.");
49  it_assert(inNupsample >= 1 && inNupsample <= 100, "OFDM: Ncp must be >=1 and <=100.");
50  Nfft = inNfft;
51  Ncp = inNcp;
52  Nupsample = inNupsample;
53  norm_factor = std::sqrt(static_cast<double>(Nupsample * Nfft * Nfft) / (Nfft + Ncp));
54  setup_done = true;
55 }
56 
57 void OFDM::modulate(const cvec &input, cvec &output)
58 {
59  it_assert(setup_done == true, "OFDM::modulate: You must set the length of the FFT and the cyclic prefix!");
60  const int N = input.length() / Nfft;
61  it_assert(N*Nfft == input.length(), "OFDM::modulate: Length of input vector is not a multiple of Nfft.");
62 
63  output.set_length(Nupsample*N*(Nfft + Ncp));
64  cvec outtemp(Nfft);
65 
66  for (int i = 0; i < N; i++) {
67  outtemp = ifft(concat(input.mid(i * Nfft, Nfft / 2), zeros_c(Nfft * (Nupsample - 1)),
68  input.mid(i * Nfft + Nfft / 2, Nfft / 2))) * norm_factor;
69  output.replace_mid(Nupsample*(Nfft + Ncp)*i, concat(outtemp.right(Nupsample*Ncp), outtemp));
70  }
71 }
72 
73 cvec OFDM::modulate(const cvec &input)
74 {
75  cvec output;
76  modulate(input, output);
77  return output;
78 }
79 
80 void OFDM::demodulate(const cvec& input, cvec &output)
81 {
82  it_assert(setup_done == true, "OFDM::demodulate: You must set the length of the FFT and the cyclic prefix!");
83  const int N = input.length() / (Nfft + Ncp) / Nupsample;
84  it_assert(Nupsample*N*(Nfft + Ncp) == input.length(), "OFDM: Length of input vector is not a multiple of Nfft+Ncp.");
85 
86  output.set_length(N*Nfft);
87  // normalize also taking the energy loss into the cyclic prefix into account
88  for (int i = 0; i < N; i++) {
89  cvec x = fft(input.mid(Nupsample * (i * (Nfft + Ncp) + Ncp), Nupsample * Nfft));
90  output.replace_mid(Nfft*i, concat(x.left(Nfft / 2), x.right(Nfft / 2)) / norm_factor);
91  }
92 }
93 
94 cvec OFDM::demodulate(const cvec &input)
95 {
96  cvec output;
97  demodulate(input, output);
98  return output;
99 }
100 
101 } //namespace itpp
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:23 for IT++ by Doxygen 1.8.2