IT++ Logo
ofdm.cpp
Go to the documentation of this file.
00001 
00031 #include <itpp/comm/ofdm.h>
00032 #include <itpp/base/specmat.h>
00033 #include <itpp/base/operators.h>
00034 #include <itpp/signal/transforms.h>
00035 
00036 
00037 namespace itpp
00038 {
00039 
00040 OFDM::OFDM(int inNfft, int inNcp, int inNupsample)
00041 {
00042   set_parameters(inNfft, inNcp, inNupsample);
00043 }
00044 
00045 void OFDM::set_parameters(const int inNfft, const int inNcp, const int inNupsample)
00046 {
00047   it_assert(inNfft >= 2, "OFDM: Nfft must be >=2.");
00048   it_assert(inNcp >= 0 && inNcp <= inNfft, "OFDM: Ncp must be >=0 and <=Nfft.");
00049   it_assert(inNupsample >= 1 && inNupsample <= 100, "OFDM: Ncp must be >=1 and <=100.");
00050   Nfft = inNfft;
00051   Ncp = inNcp;
00052   Nupsample = inNupsample;
00053   norm_factor = std::sqrt(static_cast<double>(Nupsample * Nfft * Nfft) / (Nfft + Ncp));
00054   setup_done = true;
00055 }
00056 
00057 void OFDM::modulate(const cvec &input, cvec &output)
00058 {
00059   it_assert(setup_done == true, "OFDM::modulate: You must set the length of the FFT and the cyclic prefix!");
00060   const int N = input.length() / Nfft;
00061   it_assert(N*Nfft == input.length(), "OFDM::modulate: Length of input vector is not a multiple of Nfft.");
00062 
00063   output.set_length(Nupsample*N*(Nfft + Ncp));
00064   cvec outtemp(Nfft);
00065 
00066   for (int i = 0; i < N; i++) {
00067     outtemp = ifft(concat(input.mid(i * Nfft, Nfft / 2), zeros_c(Nfft * (Nupsample - 1)),
00068                           input.mid(i * Nfft + Nfft / 2, Nfft / 2))) * norm_factor;
00069     output.replace_mid(Nupsample*(Nfft + Ncp)*i, concat(outtemp.right(Nupsample*Ncp), outtemp));
00070   }
00071 }
00072 
00073 cvec OFDM::modulate(const cvec &input)
00074 {
00075   cvec output;
00076   modulate(input, output);
00077   return output;
00078 }
00079 
00080 void OFDM::demodulate(const cvec& input, cvec &output)
00081 {
00082   it_assert(setup_done == true, "OFDM::demodulate: You must set the length of the FFT and the cyclic prefix!");
00083   const int N = input.length() / (Nfft + Ncp) / Nupsample;
00084   it_assert(Nupsample*N*(Nfft + Ncp) == input.length(), "OFDM: Length of input vector is not a multiple of Nfft+Ncp.");
00085 
00086   output.set_length(N*Nfft);
00087   // normalize also taking the energy loss into the cyclic prefix into account
00088   for (int i = 0; i < N; i++) {
00089     cvec x = fft(input.mid(Nupsample * (i * (Nfft + Ncp) + Ncp), Nupsample * Nfft));
00090     output.replace_mid(Nfft*i, concat(x.left(Nfft / 2), x.right(Nfft / 2)) / norm_factor);
00091   }
00092 }
00093 
00094 cvec OFDM::demodulate(const cvec &input)
00095 {
00096   cvec output;
00097   demodulate(input, output);
00098   return output;
00099 }
00100 
00101 } //namespace itpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SourceForge Logo

Generated on Wed Jul 27 2011 16:27:05 for IT++ by Doxygen 1.7.4