IT++ Logo

error_counters.cpp

Go to the documentation of this file.
00001 
00031 #include <itpp/comm/error_counters.h>
00032 #include <itpp/base/matfunc.h>
00033 #include <itpp/base/converters.h>
00034 #include <iostream>
00035 #include <iomanip>
00036 #include <cstdlib>
00037 
00038 
00039 namespace itpp {
00040 
00041   //-----------------------------------------------------------
00042   // The Bit error rate counter class (BERC)
00043   //-----------------------------------------------------------
00044 
00045   BERC::BERC(int indelay, int inignorefirst, int inignorelast)
00046   {
00047     delay       = indelay;
00048     ignorefirst = inignorefirst;
00049     ignorelast  = inignorelast;
00050     errors      = 0;
00051     corrects    = 0;
00052   }
00053 
00054   void BERC::count(const bvec &in1, const bvec &in2)
00055   {
00056     int countlength = std::min(in1.length(), in2.length()) - std::abs(delay)
00057       - ignorefirst - ignorelast;
00058 
00059     if (delay >= 0) {
00060       for (int i = 0; i < countlength; i++) {
00061   if (static_cast<short>(in1(i + ignorefirst)) ==
00062       static_cast<short>(in2(i + ignorefirst + delay))) {
00063     corrects++;
00064   }
00065   else {
00066     errors++;
00067   }
00068       }
00069     }
00070     else {
00071       for (int i = 0; i < countlength; i++) {
00072   if (static_cast<short>(in1(i + ignorefirst - delay)) ==
00073       static_cast<short>(in2(i + ignorefirst))) {
00074     corrects++;
00075   }
00076   else {
00077     errors++;
00078   }
00079       }
00080     }
00081   }
00082 
00083   void BERC::estimate_delay(const bvec &in1, const bvec &in2, int mindelay,
00084           int maxdelay)
00085   {
00086     int num, start1, start2;
00087     int min_input_length = std::min(in1.length(), in2.length());
00088     int bestdelay = mindelay;
00089     double correlation;
00090     double bestcorr = 0;
00091     for (int i = mindelay; i < maxdelay; i++) {
00092       num = min_input_length - std::abs(i) - ignorefirst - ignorelast;
00093       start1 = (i < 0) ? -i : 0;
00094       start2 = (i > 0) ?  i : 0;
00095       correlation = fabs(sum(to_vec(elem_mult(in1.mid(start1, num),
00096                 in2.mid(start2, num)))));
00097       if (correlation > bestcorr) {
00098   bestdelay = i;
00099   bestcorr  = correlation;
00100       }
00101     }
00102     delay = bestdelay;
00103   }
00104 
00105   void BERC::report()
00106   {
00107     std::cout.setf(std::ios::fixed);
00108     std::cout << std::endl
00109         << "==================================" << std::endl
00110         << "     Bit Error Counter Report     " << std::endl
00111         << "==================================" << std::endl
00112         << " Ignore First           = " << ignorefirst << std::endl
00113         << " Ignore Last            = " << ignorelast << std::endl
00114         << " Delay                  = " << delay << std::endl
00115         << " Number of counted bits = " << std::setprecision(0)
00116         << (errors + corrects) << std::endl
00117         << " Number of errors       = " << std::setprecision(0)
00118         << errors << std::endl
00119         << "==================================" << std::endl
00120         << " Error rate             = " << std::setprecision(8)
00121         << (errors / (errors + corrects)) << std::endl
00122         << "==================================" << std::endl << std::endl;
00123   }
00124 
00125   double BERC::count_errors(const bvec &in1, const bvec &in2, int indelay,
00126           int inignorefirst, int inignorelast)
00127   {
00128     int countlength = std::min(in1.length(), in2.length()) - std::abs(indelay)
00129       - inignorefirst - inignorelast;
00130     int local_errors = 0;
00131 
00132     if (indelay >= 0) {
00133       for (int i = 0; i < countlength; i++) {
00134   if (static_cast<short>(in1(i + inignorefirst)) !=
00135       static_cast<short>(in2(i + inignorefirst + indelay))) {
00136     local_errors++;
00137   }
00138       }
00139     }
00140     else {
00141       for (int i = 0; i < countlength; i++) {
00142   if (static_cast<short>(in1(i + inignorefirst - indelay)) !=
00143       static_cast<short>(in2(i + inignorefirst))) {
00144     local_errors++;
00145   }
00146       }
00147     }
00148 
00149     return local_errors;
00150   }
00151 
00152 
00153   //-----------------------------------------------------------
00154   // The Block error rate counter class (BERC)
00155   //-----------------------------------------------------------
00156 
00157   BLERC::BLERC(void): setup_done(false), errors(0), corrects(0) {}
00158 
00159 
00160   BLERC::BLERC(int inblocksize): setup_done(true), blocksize(inblocksize),
00161           errors(0), corrects(0) {}
00162 
00163 
00164   void BLERC::set_blocksize(int inblocksize, bool clear)
00165   {
00166     blocksize = inblocksize;
00167     if (clear) {
00168       errors = 0;
00169       corrects = 0;
00170     }
00171     setup_done = true;
00172   }
00173 
00174 
00175   void BLERC::count(const bvec &in1, const bvec &in2)
00176   {
00177     it_assert(setup_done == true,
00178         "BLERC::count(): Block size has to be setup before counting errors.");
00179     int min_input_length = std::min(in1.length(), in2.length());
00180     it_assert(blocksize <= min_input_length,
00181         "BLERC::count(): Block size must not be longer than input vectors.");
00182 
00183     for (int i = 0; i < (min_input_length / blocksize); i++) {
00184       CORR = true;
00185       for (int j = 0; j < blocksize; j++) {
00186   if (static_cast<short>(in1(i * blocksize + j)) !=
00187       static_cast<short>(in2(i * blocksize + j))) {
00188     CORR = false;
00189     break;
00190   }
00191       }
00192       if (CORR) {
00193   corrects++;
00194       }
00195       else {
00196   errors++;
00197       }
00198     }
00199   }
00200 
00201 } // namespace itpp
SourceForge Logo

Generated on Sun Dec 9 17:31:02 2007 for IT++ by Doxygen 1.5.4