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