ape  0.5.0
Audio Programming Environment
dsp.h
Go to the documentation of this file.
1 #ifndef CPPAPE_DSP_H
2 #define CPPAPE_DSP_H
3 
4 #include <cmath>
5 #include <cstddef>
6 #include "misc.h"
7 #include <vector>
8 #include <complex>
9 #include <algorithm>
10 
11 namespace ape
12 {
13 
14 #if 0
15 
16  constexpr inline long double operator ""dB(long double arg)
17  {
18  return std::pow(10, arg / 20);
19  }
20 
21  constexpr inline long double operator ""_dB(long double arg)
22  {
23  return std::pow(10, arg / 20);
24  }
25 
26 #endif
27 
31  class dB
32  {
33  public:
37  template<typename T>
38  static inline T from(T arg)
39  {
40  return std::pow(10, arg / 20);
41  }
42 
46  template<typename T>
47  static inline T to(T arg)
48  {
49  return 20 * std::log10(arg);
50  }
51  };
52 
57  template<typename T>
58  T lanczos(T x, int size)
59  {
60  constexpr T pi = static_cast<T>(M_PI);
61  return x ? (size * std::sin(pi * x) * std::sin(pi * x / size)) / (pi * pi * x * x) : 1;
62  }
63 
68  template<typename T>
69  T sinc(T x)
70  {
71  constexpr T pi = static_cast<T>(M_PI);
72  return x ? (std::sin(pi * x)) / (pi * x) : 1;
73  }
74 
78  template<typename T, typename Container>
79  auto to_complex(const Container& c) -> decltype(std::begin(c), std::end(c), std::vector<std::complex<T>>())
80  {
81  return to_complex(c, std::distance(std::begin(c), std::end(c)));
82  }
83 
87  template<typename T, typename Container>
88  auto to_complex(const Container& c, std::size_t size) -> decltype(std::begin(c), std::end(c), std::vector<std::complex<T>>())
89  {
90  std::vector<std::complex<T>> ret;
91  ret.reserve(size);
92 
93  std::size_t count = 0;
94 
95  for (auto it = std::begin(c); count < size && it != std::end(c); it++, count++)
96  {
97  ret.emplace_back(static_cast<T>(*it));
98  }
99 
100  for (; count < size; ++count)
101  ret.emplace_back(T());
102 
103  return std::move(ret);
104  }
105 
109  template<typename T, typename Container>
110  auto normalize(Container& c, T scale, double threshold = 1e-8f) -> decltype(std::begin(c), std::end(c), void())
111  {
112  if (scale < threshold)
113  return;
114 
115  const T normalization = T(1) / std::sqrt(scale);
116 
117  for (auto it = std::begin(c); it != std::end(c); it++)
118  *it *= normalization;
119  }
120 
124  template<typename Container>
125  auto accumulate_norm(const Container& c) -> decltype(std::begin(c), std::end(c), std::norm(c[0]))
126  {
127  using namespace std;
128 
129  using T = decltype(norm(c[0]));
130 
131  T acc{};
132 
133  for (auto it = begin(c); it != end(c); it++)
134  {
135  acc += norm(*it);
136  }
137 
138  return acc;
139  }
140 
144  template<typename T, typename Container>
145  auto multiply(Container& c, T scale) -> decltype(std::begin(c), std::end(c), void())
146  {
147  for (auto it = std::begin(c); it != std::end(c); it++)
148  *it *= scale;
149  }
150 }
151 
152 #endif
ape::dB::to
static T to(T arg)
Converts arg to decibels
Definition: dsp.h:47
ape::accumulate_norm
auto accumulate_norm(const Container &c) -> decltype(std::begin(c), std::end(c), std::norm(c[0]))
Returns an accumulation of the norm (square) of each element in the c
Definition: dsp.h:125
ape::normalize
auto normalize(Container &c, T scale, double threshold=1e-8f) -> decltype(std::begin(c), std::end(c), void())
Normalize each element in c by the reciprocal square root of scale
Definition: dsp.h:110
ape::to_complex
auto to_complex(const Container &c) -> decltype(std::begin(c), std::end(c), std::vector< std::complex< T >>())
Converts the entire container c to a vector of complex numbers
Definition: dsp.h:79
misc.h
ape
Definition: audiofile.h:7
ape::dB::from
static T from(T arg)
Converts arg as decibels to a scalar value
Definition: dsp.h:38
ape::lanczos
T lanczos(T x, int size)
Evaluates a lanczos kernel of size size at x
Definition: dsp.h:58
ape::sinc
T sinc(T x)
Evaluates the sinc() function at x at x
Definition: dsp.h:69
ape::dB
Provides methods for converting back and forth from decibels
Definition: dsp.h:31
ape::multiply
auto multiply(Container &c, T scale) -> decltype(std::begin(c), std::end(c), void())
Multiplies each element in c by scale
Definition: dsp.h:145