ape  0.5.0
Audio Programming Environment
interpolation.h
Go to the documentation of this file.
1 #ifndef CPPAPE_INTERPOLATION_H
2 #define CPPAPE_INTERPOLATION_H
3 
4 #include <cmath>
5 #include <utility>
6 #include "dsp.h"
7 
8 namespace ape
9 {
24  template<typename T, typename Signal>
25  inline T lanczosFilter(const Signal& s, double x, long long wsize)
26  {
27  T resonance = 0;
28  long long start = std::floor<long long>(x);
29  for (auto i = start - wsize + 1; i < (start + wsize + 1); ++i)
30  {
31  auto impulse = s(i);
32  auto response = lanczos<T>(x - i, wsize);
33  resonance += impulse * response;
34  }
35  return resonance;
36  }
37 
52  template<typename T, typename Signal>
53  inline T sincFilter(const Signal& s, double x, long long wsize)
54  {
55  T resonance = 0;
56  long long start = std::floor<long long>(x);
57  for (auto i = start - wsize + 1; i < (start + wsize + 1); ++i)
58  {
59  auto impulse = s(i);
60  auto response = sinc<T>(x - i);
61  resonance += impulse * response;
62  }
63  return resonance;
64  }
65 
75  template<typename T>
76  inline const T hermite4(const T offset, const T ym1, const T y0, const T y1, const T y2)
77  {
78  const T c = (y1 - ym1) * static_cast<T>(0.5);
79  const T v = y0 - y1;
80  const T w = c + v;
81  const T a = w + v + (y2 - y0) * static_cast<T>(0.5);
82  const T b_neg = w + a;
83 
84  return ((((a * offset) - b_neg) * offset + c) * offset + y0);
85  }
86 
95  template<typename T, typename Signal>
96  inline const T hermite4(const Signal& s, const T x)
97  {
98  long long x0 = std::floor<long long>(x);
99  return hermite4(x - x0, s(x0 - 1), s(x0), s(x0 + 1), s(x0 + 2));
100  }
101 
108  template<typename T>
109  inline const T linear(const T offset, const T y0, const T y1)
110  {
111  return y0 * (1 - offset) + y1 * offset;
112  }
113 
122  template<typename T, typename Signal>
123  inline const T linear(const Signal& s, const T x)
124  {
125  long long x0 = std::floor<long long>(x);
126  return linear(x - x0, s(x0), s(x0 + 1));
127  }
128 
129  namespace detail
130  {
131  // Code is a generalization of JUCE's lagrange interpolator class, which is this case is GPL v3
132  template <typename T, int k>
133  struct lagrange_basis
134  {
135  static void mul(T& a, T b) { a *= b * (static_cast<T>(1) / k); }
136  };
137 
138  template<typename T>
139  struct lagrange_basis <T, 0>
140  {
141  static void mul(T&, T) {}
142  };
143 
144  template <typename T, int k, int Order, std::size_t... I>
145  static float lagrange_polynomial(T input, const T offset, std::index_sequence<I...>) noexcept
146  {
147  auto const ks = -Order / 2;
148  (lagrange_basis <T, static_cast<int>(I) - k>::mul(input, static_cast<int>(I) + ks - offset), ...);
149  return input;
150  }
151 
152  template<typename T, std::size_t Order, typename Signal, std::size_t... I>
153  inline const T lagrange_unpack(const Signal& s, long long start, const T offset, std::index_sequence<I...> indices) noexcept
154  {
155  auto const ks = static_cast<long>(-static_cast<T>(Order) / static_cast<T>(2));
156  return (lagrange_polynomial<T, I, Order>(s(ks + start + I), offset, indices) + ...);
157  }
158  }
159 
169  template<typename T, std::size_t Order, typename Signal>
170  inline const T lagrange(const Signal& s, const T position) noexcept
171  {
172  using namespace detail;
173  const long long start = static_cast<long long>(position);
174  return lagrange_unpack<T, Order>(s, start, position - start, std::make_index_sequence<Order>());
175  }
176 
183  template<typename T>
184  inline const T lagrange5(const T offset, const T ym2, const T ym1, const T y0, const T y1, const T y2)
185  {
186  using namespace detail;
187 
188  const auto indices = std::make_index_sequence<5>();
189 
190  return lagrange_polynomial<T, 0, 5>(ym2, offset, indices)
191  + lagrange_polynomial<T, 1, 5>(ym1, offset, indices)
192  + lagrange_polynomial<T, 2, 5>(y0, offset, indices)
193  + lagrange_polynomial<T, 3, 5>(y1, offset, indices)
194  + lagrange_polynomial<T, 4, 5>(y2, offset, indices);
195  }
196 }
197 
198 #endif
dsp.h
ape::lanczosFilter
T lanczosFilter(const Signal &s, double x, long long wsize)
Do lanczos interpolation at a specific point in a signal.
Definition: interpolation.h:25
ape::lagrange5
const T lagrange5(const T offset, const T ym2, const T ym1, const T y0, const T y1, const T y2)
Do lagrange interpolation between the y parameters, with 5 terms.
Definition: interpolation.h:184
ape::detail::lagrange_unpack
const T lagrange_unpack(const Signal &s, long long start, const T offset, std::index_sequence< I... > indices) noexcept
Definition: interpolation.h:153
ape::lagrange
const T lagrange(const Signal &s, const T position) noexcept
Do lagrange interpolation at a specific point in a signal, of Order order. lagrange5
Definition: interpolation.h:170
ape::hermite4
const T hermite4(const T offset, const T ym1, const T y0, const T y1, const T y2)
Do hermite 4 interpolation given the four y-coordinates
Definition: interpolation.h:76
ape
Definition: audiofile.h:7
ape::sincFilter
T sincFilter(const Signal &s, double x, long long wsize)
Do sinc interpolation at a specific point in a signal.
Definition: interpolation.h:53
ape::linear
const T linear(const T offset, const T y0, const T y1)
Do simple linear interpolation between two points.
Definition: interpolation.h:109