IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
error_counters.cpp
Go to the documentation of this file.
1 
31 #include <itpp/base/matfunc.h>
32 #include <itpp/base/converters.h>
33 #include <iostream>
34 #include <iomanip>
35 #include <cstdlib>
36 
37 
38 namespace itpp
39 {
40 
41 //-----------------------------------------------------------
42 // The Bit error rate counter class (BERC)
43 //-----------------------------------------------------------
44 
45 BERC::BERC(int indelay, int inignorefirst, int inignorelast):
46  delay(indelay), ignorefirst(inignorefirst), ignorelast(inignorelast),
47  errors(0), corrects(0) {}
48 
49 void BERC::count(const bvec &in1, const bvec &in2)
50 {
51  int countlength = std::min(in1.length(), in2.length()) - std::abs(delay)
52  - ignorefirst - ignorelast;
53 
54  if (delay >= 0) {
55  for (int i = 0; i < countlength; i++) {
56  if (in1(i + ignorefirst) == in2(i + ignorefirst + delay)) {
57  corrects++;
58  }
59  else {
60  errors++;
61  }
62  }
63  }
64  else {
65  for (int i = 0; i < countlength; i++) {
66  if (in1(i + ignorefirst - delay) == in2(i + ignorefirst)) {
67  corrects++;
68  }
69  else {
70  errors++;
71  }
72  }
73  }
74 }
75 
76 void BERC::estimate_delay(const bvec &in1, const bvec &in2, int mindelay,
77  int maxdelay)
78 {
79  int num, start1, start2;
80  int min_input_length = std::min(in1.length(), in2.length());
81  int bestdelay = mindelay;
82  double correlation;
83  double bestcorr = 0;
84  for (int i = mindelay; i < maxdelay; i++) {
85  num = min_input_length - std::abs(i) - ignorefirst - ignorelast;
86  start1 = (i < 0) ? -i : 0;
87  start2 = (i > 0) ? i : 0;
88  correlation = fabs(sum(to_vec(elem_mult(in1.mid(start1, num),
89  in2.mid(start2, num)))));
90  if (correlation > bestcorr) {
91  bestdelay = i;
92  bestcorr = correlation;
93  }
94  }
95  delay = bestdelay;
96 }
97 
98 void BERC::report() const
99 {
100  std::cout.setf(std::ios::fixed);
101  std::cout << std::endl
102  << "==================================" << std::endl
103  << " Bit Error Counter Report " << std::endl
104  << "==================================" << std::endl
105  << " Ignore First = " << ignorefirst << std::endl
106  << " Ignore Last = " << ignorelast << std::endl
107  << " Delay = " << delay << std::endl
108  << " Number of counted bits = " << std::setprecision(0)
109  << (errors + corrects) << std::endl
110  << " Number of errors = " << std::setprecision(0)
111  << errors << std::endl
112  << "==================================" << std::endl
113  << " Error rate = " << std::setprecision(8)
114  << (errors / (errors + corrects)) << std::endl
115  << "==================================" << std::endl << std::endl;
116 }
117 
118 double BERC::count_errors(const bvec &in1, const bvec &in2, int indelay,
119  int inignorefirst, int inignorelast)
120 {
121  int countlength = std::min(in1.length(), in2.length()) - std::abs(indelay)
122  - inignorefirst - inignorelast;
123  int local_errors = 0;
124 
125  if (indelay >= 0) {
126  for (int i = 0; i < countlength; i++) {
127  if (in1(i + inignorefirst) != in2(i + inignorefirst + indelay)) {
128  local_errors++;
129  }
130  }
131  }
132  else {
133  for (int i = 0; i < countlength; i++) {
134  if (in1(i + inignorefirst - indelay) != in2(i + inignorefirst)) {
135  local_errors++;
136  }
137  }
138  }
139 
140  return local_errors;
141 }
142 
143 
144 //-----------------------------------------------------------
145 // The Block error rate counter class (BERC)
146 //-----------------------------------------------------------
147 
148 BLERC::BLERC(): setup_done(false), blocksize(0), errors(0),
149  corrects(0) {}
150 
151 
152 BLERC::BLERC(int inblocksize): setup_done(true), blocksize(inblocksize),
153  errors(0), corrects(0) {}
154 
155 
156 void BLERC::set_blocksize(int inblocksize, bool clear)
157 {
158  blocksize = inblocksize;
159  if (clear) {
160  errors = 0;
161  corrects = 0;
162  }
163  setup_done = true;
164 }
165 
166 
167 void BLERC::count(const bvec &in1, const bvec &in2)
168 {
169  it_assert(setup_done == true,
170  "BLERC::count(): Block size has to be setup before counting errors.");
171  int min_input_length = std::min(in1.length(), in2.length());
172  it_assert(blocksize <= min_input_length,
173  "BLERC::count(): Block size must not be longer than input vectors.");
174 
175  for (int i = 0; i < (min_input_length / blocksize); i++) {
176  CORR = true;
177  for (int j = 0; j < blocksize; j++) {
178  if (in1(i * blocksize + j) != in2(i * blocksize + j)) {
179  CORR = false;
180  break;
181  }
182  }
183  if (CORR) {
184  corrects++;
185  }
186  else {
187  errors++;
188  }
189  }
190 }
191 
192 } // namespace itpp
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:13 for IT++ by Doxygen 1.8.1.2