Loading...
Searching...
No Matches
stochasticencoder.h
Go to the documentation of this file.
#ifndef DYSCO_STOCHASTIC_ENCODER_H
#define DYSCO_STOCHASTIC_ENCODER_H
#include "uvector.h"
#include <algorithm>
#include <cmath>
#include <cstring>
namespace dyscostman {
template <typename ValueType = float>
class StochasticEncoder {
public:
StochasticEncoder(size_t quantCount, ValueType stddev,
bool gaussianMapping = true);
static StochasticEncoder StudentTEncoder(size_t quantCount, double nu,
double rms) {
StochasticEncoder<ValueType> encoder(quantCount);
encoder.initializeStudentT(nu, rms);
return encoder;
}
static StochasticEncoder TruncatedGausEncoder(size_t quantCount, double trunc,
double rms) {
StochasticEncoder<ValueType> encoder(quantCount);
encoder.initializeTruncatedGaussian(trunc, rms);
return encoder;
}
typedef unsigned symbol_t;
typedef ValueType value_t;
symbol_t Encode(ValueType value) const {
if (std::isfinite(value))
return _encDictionary.symbol(_encDictionary.lower_bound(value));
else
return QuantizationCount() - 1;
}
static std::uniform_int_distribution<unsigned> GetDitherDistribution() {
return std::uniform_int_distribution<unsigned>(0, ((1u << 31) - 1));
}
symbol_t EncodeWithDithering(ValueType value, unsigned ditherValue) const {
if (std::isfinite(value)) {
const typename Dictionary::const_iterator lowerBound =
_decDictionary.lower_bound(value);
if (lowerBound == _decDictionary.begin())
return _decDictionary.symbol(lowerBound);
if (lowerBound == _decDictionary.end())
return _decDictionary.symbol(lowerBound - 1);
const ValueType rightValue = _decDictionary.value(lowerBound);
const ValueType leftValue = _decDictionary.value(lowerBound - 1);
ValueType ditherMark =
ValueType(1u << 31) * (value - leftValue) / (rightValue - leftValue);
if (ditherMark > ditherValue)
return _decDictionary.symbol(lowerBound);
else
return _decDictionary.symbol(lowerBound - 1);
} else {
return _encDictionary.size();
}
}
value_t RightBoundary(symbol_t symbol) const {
if (symbol != _encDictionary.size())
return _encDictionary.value(symbol);
else
return 0.0;
}
ValueType Decode(symbol_t symbol) const {
return _decDictionary.value(symbol);
}
size_t QuantizationCount() const { return _decDictionary.size() + 1; }
ValueType MaxQuantity() const { return _decDictionary.largest_value(); }
ValueType MinQuantity() const { return _decDictionary.smallest_value(); }
private:
explicit StochasticEncoder(size_t quantCount)
: _encDictionary(quantCount - 1), _decDictionary(quantCount - 1) {}
void initializeStudentT(double nu, double rms);
void initializeTruncatedGaussian(double truncationValue, double rms);
class Dictionary {
public:
typedef value_t *iterator;
typedef const value_t *const_iterator;
Dictionary() : _values() {}
explicit Dictionary(size_t size) : _values(size) {}
void reserve(size_t size) { _values.reserve(size); }
void resize(size_t size) { _values.resize(size); }
const_iterator lower_bound(value_t val) const {
size_t p = 0, q = _values.size();
size_t m = (p + q) / 2;
if (_values[m] <= val)
p = m;
else
q = m;
while (p + 1 != q) {
size_t m = (p + q) / 2;
if (_values[m] <= val)
p = m;
else
q = m;
}
return (_values[p] < val) ? (&_values[q]) : (&_values[p]);
}
const_iterator lower_bound_fast(value_t val) const {
size_t p = 0, q = _values.size();
while (p + 1 != q) {
size_t m = (p + q) / 2;
if (_values[m] <= val)
p = m;
else
q = m;
}
return (_values[p] < val) ? (&_values[q]) : (&_values[p]);
}
const_iterator lower_bound_slow(value_t val) const {
const value_t *p = &*_values.begin(), *q = p + _values.size();
while (p + 1 != q) {
// This is a bit inefficient, but (p + q)/2 was not allowed, because
// operator+(ptr,ptr) is not allowed.
const value_t *m = p + (q - p) / 2;
if (*m <= val)
p = m;
else
q = m;
}
return p;
}
iterator begin() { return &*_values.begin(); }
const_iterator begin() const { return &*_values.begin(); }
const_iterator end() const { return &*_values.end(); }
symbol_t symbol(const_iterator iter) const { return (iter - begin()); }
symbol_t largest_symbol() const { return _values.size() - 1; }
value_t value(const_iterator iter) const { return *iter; }
value_t value(symbol_t sym) const { return _values[sym]; }
value_t largest_value() const { return _values.back(); }
value_t smallest_value() const { return _values.front(); }
size_t size() const { return _values.size(); }
size_t capacity(size_t) const { return _values.capacity(); }
private:
ao::uvector<value_t> _values;
};
typedef long double num_t;
static num_t cumulative(num_t x);
static num_t invCumulative(num_t c, num_t err = num_t(1e-13));
Dictionary _encDictionary;
Dictionary _decDictionary;
};
} // namespace dyscostman
#endif
ao::uvector
A container similar to std::vector, but one that allows construction without initializing its element...
Definition uvector.h:74
dyscostman::StochasticEncoder::Dictionary
Definition stochasticencoder.h:161
dyscostman::StochasticEncoder::Dictionary::smallest_value
value_t smallest_value() const
Definition stochasticencoder.h:249
dyscostman::StochasticEncoder::Dictionary::iterator
value_t * iterator
Definition stochasticencoder.h:163
dyscostman::StochasticEncoder::Dictionary::reserve
void reserve(size_t size)
Definition stochasticencoder.h:170
dyscostman::StochasticEncoder::Dictionary::value
value_t value(symbol_t sym) const
Definition stochasticencoder.h:247
dyscostman::StochasticEncoder::Dictionary::begin
iterator begin()
Definition stochasticencoder.h:241
dyscostman::StochasticEncoder::Dictionary::Dictionary
Dictionary()
Definition stochasticencoder.h:166
dyscostman::StochasticEncoder::Dictionary::end
const_iterator end() const
Definition stochasticencoder.h:243
dyscostman::StochasticEncoder::Dictionary::lower_bound
const_iterator lower_bound(value_t val) const
Returns an iterator pointing to the first element in the dictionary that is not less than (i....
Definition stochasticencoder.h:182
dyscostman::StochasticEncoder::Dictionary::size
size_t size() const
Definition stochasticencoder.h:250
dyscostman::StochasticEncoder::Dictionary::resize
void resize(size_t size)
Definition stochasticencoder.h:172
dyscostman::StochasticEncoder::Dictionary::begin
const_iterator begin() const
Definition stochasticencoder.h:242
dyscostman::StochasticEncoder::Dictionary::largest_symbol
symbol_t largest_symbol() const
Definition stochasticencoder.h:245
dyscostman::StochasticEncoder::Dictionary::Dictionary
Dictionary(size_t size)
Definition stochasticencoder.h:168
dyscostman::StochasticEncoder::Dictionary::value
value_t value(const_iterator iter) const
Definition stochasticencoder.h:246
dyscostman::StochasticEncoder::Dictionary::_values
ao::uvector< value_t > _values
Definition stochasticencoder.h:254
dyscostman::StochasticEncoder::Dictionary::symbol
symbol_t symbol(const_iterator iter) const
Definition stochasticencoder.h:244
dyscostman::StochasticEncoder::Dictionary::largest_value
value_t largest_value() const
Definition stochasticencoder.h:248
dyscostman::StochasticEncoder::Dictionary::lower_bound_slow
const_iterator lower_bound_slow(value_t val) const
Below is the first failed result of an attempt to beat the STL in performance.
Definition stochasticencoder.h:227
dyscostman::StochasticEncoder::Dictionary::const_iterator
const value_t * const_iterator
Definition stochasticencoder.h:164
dyscostman::StochasticEncoder::Dictionary::lower_bound_fast
const_iterator lower_bound_fast(value_t val) const
Returns an iterator pointing to the first element in the dictionary that is not less than (i....
Definition stochasticencoder.h:209
dyscostman::StochasticEncoder::Dictionary::capacity
size_t capacity(size_t) const
Definition stochasticencoder.h:251
dyscostman::StochasticEncoder::RightBoundary
value_t RightBoundary(symbol_t symbol) const
Will return the right boundary of the given symbol.
Definition stochasticencoder.h:131
dyscostman::StochasticEncoder::num_t
long double num_t
Definition stochasticencoder.h:257
dyscostman::StochasticEncoder::cumulative
static num_t cumulative(num_t x)
dyscostman::StochasticEncoder::invCumulative
static num_t invCumulative(num_t c, num_t err=num_t(1e-13))
dyscostman::StochasticEncoder::MaxQuantity
ValueType MaxQuantity() const
Definition stochasticencoder.h:149
dyscostman::StochasticEncoder::initializeTruncatedGaussian
void initializeTruncatedGaussian(double truncationValue, double rms)
dyscostman::StochasticEncoder::GetDitherDistribution
static std::uniform_int_distribution< unsigned > GetDitherDistribution()
Definition stochasticencoder.h:88
dyscostman::StochasticEncoder::Encode
symbol_t Encode(ValueType value) const
Get the quantized symbol for the given floating point value.
Definition stochasticencoder.h:81
dyscostman::StochasticEncoder::initializeStudentT
void initializeStudentT(double nu, double rms)
dyscostman::StochasticEncoder::QuantizationCount
size_t QuantizationCount() const
Definition stochasticencoder.h:147
dyscostman::StochasticEncoder::StochasticEncoder
StochasticEncoder(size_t quantCount)
Definition stochasticencoder.h:154
dyscostman::StochasticEncoder::StochasticEncoder
StochasticEncoder(size_t quantCount, ValueType stddev, bool gaussianMapping=true)
Construct encoder for given dictionary size and Gaussian stddev.
dyscostman::StochasticEncoder::MinQuantity
ValueType MinQuantity() const
Definition stochasticencoder.h:151
dyscostman::StochasticEncoder::StudentTEncoder
static StochasticEncoder StudentTEncoder(size_t quantCount, double nu, double rms)
Definition stochasticencoder.h:48
dyscostman::StochasticEncoder::_encDictionary
Dictionary _encDictionary
Definition stochasticencoder.h:262
dyscostman::StochasticEncoder::EncodeWithDithering
symbol_t EncodeWithDithering(ValueType value, unsigned ditherValue) const
Get the quantized symbol for the given floating point value.
Definition stochasticencoder.h:103
dyscostman::StochasticEncoder::value_t
ValueType value_t
Template type used for representing floating point values that are to be encoded.
Definition stochasticencoder.h:71
dyscostman::StochasticEncoder::symbol_t
unsigned symbol_t
Unsigned integer type used for representing the encoded symbols.
Definition stochasticencoder.h:65
dyscostman::StochasticEncoder::Decode
ValueType Decode(symbol_t symbol) const
Get the centroid value that belongs to the given symbol.
Definition stochasticencoder.h:143
dyscostman::StochasticEncoder::_decDictionary
Dictionary _decDictionary
Definition stochasticencoder.h:263
dyscostman::StochasticEncoder::TruncatedGausEncoder
static StochasticEncoder TruncatedGausEncoder(size_t quantCount, double trunc, double rms)
Definition stochasticencoder.h:55
dyscostman
Definition bytepacker.h:7
uvector.h
Header file for uvector and its relational and swap functions.