IT++ Logo Newcom Logo

error_counters.cpp

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

Generated on Thu Apr 19 14:19:54 2007 for IT++ by Doxygen 1.4.6