MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CharMatrixXfEncoder.cpp
Go to the documentation of this file.
1 
24 
25 #include <logger/Log.hpp>
26 
27 #include <cstring> // for memset
28 #ifdef _WIN32
29 #include <string.h>
30 #endif
31 
32 namespace mic {
33 namespace encoders {
34 
35 mic::types::MatrixXfPtr CharMatrixXfEncoder::encodeSample(const std::shared_ptr<char>& sample_) {
36  // Create returned SDR - a vector, i.e. a matrix with number of columns equal to 1.
38  // Set all zeros.
39  sdr->setZero();
40 
41  // Convert ASCII char to int.
42  char sample = *sample_;
43  int a = sample - 32;
44  //std::cout <<" input_ = " << input_ <<" a = " << a;
45  if (a < 0)
46  LOG(LERROR) << "Could not properly encode character '" <<sample << "' ("<<a<<")!";
47  else if ((size_t) a >= (size_t) sdr_length)
48  LOG(LERROR) << "The SDR is too short for proper encoding of the character '" <<sample << "' ("<<a<<")!";
49  else
50  (*sdr)(a) = 1;
51  return sdr;
52 }
53 
54 std::shared_ptr<char> CharMatrixXfEncoder::decodeSample(const mic::types::MatrixXfPtr& sdr_) {
55  // SDR must be in fact a vector, with number of columns equal to 1.
56  assert(sdr_->cols() == 1);
57 
58  // Find index of max value.
59  mic::types::MatrixXf::Index maxRow, maxCol;
60  float max_value = sdr_->maxCoeff(&maxRow, &maxCol);
61  LOG(LDEBUG) << "(maxRow, maxCol) = max_value [ (" << maxRow << "," << maxCol <<") = " << max_value << "]";
62 
63  // Convert int to ASCII.
64  char decoded = maxRow + 32;
65 
66  return std::make_shared<char>(decoded);
67 }
68 
69 
70 } /* namespace encoders */
71 } /* namespace mic */
std::shared_ptr< mic::types::MatrixXf > MatrixXfPtr
Shared pointer to matrix with single precision floats (of dynamic size).
virtual std::shared_ptr< char > decodeSample(const mic::types::MatrixXfPtr &sdr_)
virtual mic::types::MatrixXfPtr encodeSample(const std::shared_ptr< char > &sample_)
Method responsible for encoding of input data sample into SDR (a vector of floats).
Template-typed Matrix of dynamic size. Uses OpenBLAS if found by CMAKE - overloaded, specializations of * operator for types: float, double.
Definition: Matrix.hpp:64