IT++ Logo
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::count(const bool x)
77 {
78  if (x) {
79  errors++;
80  } else {
81  corrects++;
82  }
83 }
84 
85 void BERC::estimate_delay(const bvec &in1, const bvec &in2, int mindelay,
86  int maxdelay)
87 {
88  int num, start1, start2;
89  int min_input_length = std::min(in1.length(), in2.length());
90  int bestdelay = mindelay;
91  double correlation;
92  double bestcorr = 0;
93  for (int i = mindelay; i < maxdelay; i++) {
94  num = min_input_length - std::abs(i) - ignorefirst - ignorelast;
95  start1 = (i < 0) ? -i : 0;
96  start2 = (i > 0) ? i : 0;
97  correlation = fabs(sum(to_vec(elem_mult(in1.mid(start1, num),
98  in2.mid(start2, num)))));
99  if (correlation > bestcorr) {
100  bestdelay = i;
101  bestcorr = correlation;
102  }
103  }
104  delay = bestdelay;
105 }
106 
107 void BERC::report() const
108 {
109  std::cout.setf(std::ios::fixed);
110  std::cout << std::endl
111  << "==================================" << std::endl
112  << " Bit Error Counter Report " << std::endl
113  << "==================================" << std::endl
114  << " Ignore First = " << ignorefirst << std::endl
115  << " Ignore Last = " << ignorelast << std::endl
116  << " Delay = " << delay << std::endl
117  << " Number of counted bits = " << std::setprecision(0)
118  << (errors + corrects) << std::endl
119  << " Number of errors = " << std::setprecision(0)
120  << errors << std::endl
121  << "==================================" << std::endl
122  << " Error rate = " << std::setprecision(8)
123  << (errors / (errors + corrects)) << std::endl
124  << "==================================" << std::endl << std::endl;
125 }
126 
127 double BERC::count_errors(const bvec &in1, const bvec &in2, int indelay,
128  int inignorefirst, int inignorelast)
129 {
130  int countlength = std::min(in1.length(), in2.length()) - std::abs(indelay)
131  - inignorefirst - inignorelast;
132  int local_errors = 0;
133 
134  if (indelay >= 0) {
135  for (int i = 0; i < countlength; i++) {
136  if (in1(i + inignorefirst) != in2(i + inignorefirst + indelay)) {
137  local_errors++;
138  }
139  }
140  }
141  else {
142  for (int i = 0; i < countlength; i++) {
143  if (in1(i + inignorefirst - indelay) != in2(i + inignorefirst)) {
144  local_errors++;
145  }
146  }
147  }
148 
149  return local_errors;
150 }
151 
152 
153 //-----------------------------------------------------------
154 // The Block error rate counter class (BERC)
155 //-----------------------------------------------------------
156 
157 BLERC::BLERC(): setup_done(false), blocksize(0), errors(0),
158  corrects(0) {}
159 
160 
161 BLERC::BLERC(int inblocksize): setup_done(true), blocksize(inblocksize),
162  errors(0), corrects(0) {}
163 
164 
165 void BLERC::set_blocksize(int inblocksize, bool clear)
166 {
167  blocksize = inblocksize;
168  if (clear) {
169  errors = 0;
170  corrects = 0;
171  }
172  setup_done = true;
173 }
174 
175 void BLERC::count(const bvec &in1, const bvec &in2)
176 {
177  it_assert(setup_done == true,
178  "BLERC::count(): Block size has to be setup before counting errors.");
179  int min_input_length = std::min(in1.length(), in2.length());
180  it_assert(blocksize <= min_input_length,
181  "BLERC::count(): Block size must not be longer than input vectors.");
182 
183  for (int i = 0; i < (min_input_length / blocksize); i++) {
184  CORR = true;
185  for (int j = 0; j < blocksize; j++) {
186  if (in1(i * blocksize + j) != in2(i * blocksize + j)) {
187  CORR = false;
188  break;
189  }
190  }
191  if (CORR) {
192  corrects++;
193  }
194  else {
195  errors++;
196  }
197  }
198 }
199 
200 void BLERC::count(const bool x)
201 {
202  if (x) {
203  errors++;
204  } else {
205  corrects++;
206  }
207 }
208 
209 } // namespace itpp
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:22 for IT++ by Doxygen 1.8.2