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