File: error_counters.cpp

package info (click to toggle)
libitpp 4.3.1-14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,952 kB
  • sloc: cpp: 73,628; makefile: 661; python: 548; sh: 261
file content (209 lines) | stat: -rw-r--r-- 6,041 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*!
 * \file
 * \brief Implementation of Bit Error Rate Counter (BERC) and
 *        BLock Error Rate Counter (BLERC) classes
 * \author Pal Frenger and Adam Piatyszek
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2010  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/comm/error_counters.h>
#include <itpp/base/matfunc.h>
#include <itpp/base/converters.h>
#include <iostream>
#include <iomanip>
#include <cstdlib>


namespace itpp
{

//-----------------------------------------------------------
// The Bit error rate counter class (BERC)
//-----------------------------------------------------------

BERC::BERC(int indelay, int inignorefirst, int inignorelast):
    delay(indelay), ignorefirst(inignorefirst), ignorelast(inignorelast),
    errors(0), corrects(0) {}

void BERC::count(const bvec &in1, const bvec &in2)
{
  int countlength = std::min(in1.length(), in2.length()) - std::abs(delay)
                    - ignorefirst - ignorelast;

  if (delay >= 0) {
    for (int i = 0; i < countlength; i++) {
      if (in1(i + ignorefirst) == in2(i + ignorefirst + delay)) {
        corrects++;
      }
      else {
        errors++;
      }
    }
  }
  else {
    for (int i = 0; i < countlength; i++) {
      if (in1(i + ignorefirst - delay) == in2(i + ignorefirst)) {
        corrects++;
      }
      else {
        errors++;
      }
    }
  }
}

void BERC::count(const bool x)
{
  if (x) {
    errors++; 
  } else {
    corrects++;
  }
}

void BERC::estimate_delay(const bvec &in1, const bvec &in2, int mindelay,
                          int maxdelay)
{
  int num, start1, start2;
  int min_input_length = std::min(in1.length(), in2.length());
  int bestdelay = mindelay;
  double correlation;
  double bestcorr = 0;
  for (int i = mindelay; i < maxdelay; i++) {
    num = min_input_length - std::abs(i) - ignorefirst - ignorelast;
    start1 = (i < 0) ? -i : 0;
    start2 = (i > 0) ?  i : 0;
    correlation = fabs(sum(to_vec(elem_mult(in1.mid(start1, num),
                                            in2.mid(start2, num)))));
    if (correlation > bestcorr) {
      bestdelay = i;
      bestcorr  = correlation;
    }
  }
  delay = bestdelay;
}

void BERC::report() const
{
  std::cout.setf(std::ios::fixed);
  std::cout << std::endl
            << "==================================" << std::endl
            << "     Bit Error Counter Report     " << std::endl
            << "==================================" << std::endl
            << " Ignore First           = " << ignorefirst << std::endl
            << " Ignore Last            = " << ignorelast << std::endl
            << " Delay                  = " << delay << std::endl
            << " Number of counted bits = " << std::setprecision(0)
            << (errors + corrects) << std::endl
            << " Number of errors       = " << std::setprecision(0)
            << errors << std::endl
            << "==================================" << std::endl
            << " Error rate             = " << std::setprecision(8)
            << (errors / (errors + corrects)) << std::endl
            << "==================================" << std::endl << std::endl;
}

double BERC::count_errors(const bvec &in1, const bvec &in2, int indelay,
                          int inignorefirst, int inignorelast)
{
  int countlength = std::min(in1.length(), in2.length()) - std::abs(indelay)
                    - inignorefirst - inignorelast;
  int local_errors = 0;

  if (indelay >= 0) {
    for (int i = 0; i < countlength; i++) {
      if (in1(i + inignorefirst) != in2(i + inignorefirst + indelay)) {
        local_errors++;
      }
    }
  }
  else {
    for (int i = 0; i < countlength; i++) {
      if (in1(i + inignorefirst - indelay) != in2(i + inignorefirst)) {
        local_errors++;
      }
    }
  }

  return local_errors;
}


//-----------------------------------------------------------
// The Block error rate counter class (BERC)
//-----------------------------------------------------------

BLERC::BLERC(): setup_done(false), blocksize(0), errors(0),
    corrects(0) {}


BLERC::BLERC(int inblocksize): setup_done(true), blocksize(inblocksize),
    errors(0), corrects(0) {}


void BLERC::set_blocksize(int inblocksize, bool clear)
{
  blocksize = inblocksize;
  if (clear) {
    errors = 0;
    corrects = 0;
  }
  setup_done = true;
}

void BLERC::count(const bvec &in1, const bvec &in2)
{
  it_assert(setup_done == true,
            "BLERC::count(): Block size has to be setup before counting errors.");
  int min_input_length = std::min(in1.length(), in2.length());
  it_assert(blocksize <= min_input_length,
            "BLERC::count(): Block size must not be longer than input vectors.");

  for (int i = 0; i < (min_input_length / blocksize); i++) {
    CORR = true;
    for (int j = 0; j < blocksize; j++) {
      if (in1(i * blocksize + j) != in2(i * blocksize + j)) {
        CORR = false;
        break;
      }
    }
    if (CORR) {
      corrects++;
    }
    else {
      errors++;
    }
  }
}

void BLERC::count(const bool x)
{
  if (x) {
    errors++; 
  } else {
    corrects++;
  }
}

} // namespace itpp