Quantize.h
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Tipic
5 
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #ifndef QUANTIZE_H
16 #define QUANTIZE_H
17 
18 #include <vector>
19 #include <stdexcept>
20 
21 class Quantize
22 {
23 public:
24  class Parameters {
25  public:
26  std::vector<double> steps;
27  std::vector<double> weights;
28  Parameters() :
29  steps({ 0.4, 0.2, 0.1, 0.05 }),
30  weights({ 0.25, 0.25, 0.25, 0.25 }) { }
31  };
32 
33  Quantize(Parameters params) : m_params(params) {
34  if (params.steps.empty()) {
35  throw std::invalid_argument("Quantize steps must not be empty");
36  }
37  if (params.steps.size() != params.weights.size()) {
38  throw std::invalid_argument("Must have same number of quantize steps and weights");
39  }
40  }
41  ~Quantize() { }
42 
43  std::vector<double> process(const std::vector<double> &in) {
44  int n = in.size();
45  int m = m_params.steps.size();
46  std::vector<double> out(n, 0.0);
47  for (int i = 0; i < n; ++i) {
48  for (int j = 0; j < m; ++j) {
49  if (in[i] > m_params.steps[j]) {
50  out[i] += m_params.weights[j];
51  }
52  }
53  }
54  return out;
55  }
56 
57 private:
58  Parameters m_params;
59 };
60 
61 #endif
Definition: Quantize.h:24
Definition: Quantize.h:21