casacore
weightencoder.h
Go to the documentation of this file.
1 #ifndef DYSCO_WEIGHT_ENCODER_H
2 #define DYSCO_WEIGHT_ENCODER_H
3 
4 #include <cmath>
5 #include <vector>
6 
7 namespace dyscostman {
8 
15 template <typename T>
17  public:
19  typedef T value_t;
21  typedef unsigned symbol_t;
22 
26  explicit WeightEncoder(unsigned quantCount) : _quantCount(quantCount) {}
27 
37  void Encode(value_t &scaleVal, std::vector<symbol_t> &dest,
38  const std::vector<value_t> &input) const {
39  // Find max value (implicit assumption input is not empty)
40  typename std::vector<value_t>::const_iterator i = input.begin();
41  scaleVal = *i;
42  ++i;
43  while (i != input.end()) {
44  if (*i > scaleVal) scaleVal = *i;
45  ++i;
46  }
47 
48  i = input.begin();
49  typename std::vector<symbol_t>::iterator d = dest.begin();
50  const value_t scaleFact = value_t(_quantCount - 1) / scaleVal;
51  while (i != input.end()) {
52  *d = roundf(scaleFact * (*i));
53 
54  ++i;
55  ++d;
56  }
57  }
58 
66  void Decode(std::vector<value_t> &dest, value_t scaleVal,
67  const std::vector<symbol_t> &input) const {
68  typename std::vector<symbol_t>::const_iterator i = input.begin();
69  typename std::vector<value_t>::iterator d = dest.begin();
70  const value_t scaleFact = scaleVal / value_t(_quantCount - 1);
71  while (i != input.end()) {
72  *d = (*i) * scaleFact;
73 
74  ++i;
75  ++d;
76  }
77  }
78 
79  private:
80  unsigned _quantCount;
81 };
82 
83 } // namespace dyscostman
84 
85 #endif
Encoder for visibility weights.
Definition: weightencoder.h:16
unsigned symbol_t
Value type of the symbols to which the weights are encoded.
Definition: weightencoder.h:21
T value_t
Value type of the original weights (a floating point value).
Definition: weightencoder.h:19
void Decode(std::vector< value_t > &dest, value_t scaleVal, const std::vector< symbol_t > &input) const
Decode a previously encoded value.
Definition: weightencoder.h:66
void Encode(value_t &scaleVal, std::vector< symbol_t > &dest, const std::vector< value_t > &input) const
Encodes a vector of values.
Definition: weightencoder.h:37
WeightEncoder(unsigned quantCount)
Construct a new encoder with the given quantization count.
Definition: weightencoder.h:26