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