Wayverb
freqz.h
1 #pragma once
2 
3 #include "utilities/foldl.h"
4 #include "utilities/map.h"
5 
6 #include <cmath>
7 #include <complex>
8 
9 namespace wayverb {
10 namespace core {
11 
12 template <typename T>
13 struct item_and_index final {
14  T item;
15  size_t index;
16 };
17 
18 template <typename T>
19 constexpr auto make_item_and_index(T item, size_t index) {
20  return item_and_index<T>{std::move(item), index};
21 }
22 
23 template <typename T, size_t... Ix>
24 constexpr auto zip_with_indices(const std::array<T, sizeof...(Ix)>& arr,
25  std::index_sequence<Ix...>) {
26  return std::array<item_and_index<T>, sizeof...(Ix)>{
27  {make_item_and_index(std::get<Ix>(arr), Ix)...}};
28 }
29 
30 template <typename T, size_t N>
31 constexpr auto zip_with_indices(const std::array<T, N>& arr) {
32  return zip_with_indices(arr, std::make_index_sequence<N>{});
33 }
34 
36 
37 template <size_t N>
38 auto freqz(const std::array<double, N>& coeffs, double frequency) {
39  return util::foldl(
40  [&](const auto& accumulator, const auto& item) {
41  using namespace std::complex_literals;
42  return accumulator +
43  item.item * std::exp(-1i * frequency *
44  static_cast<double>(item.index));
45  },
46  0.0,
47  zip_with_indices(coeffs));
48 }
49 
50 template <size_t B, size_t A>
51 auto freqz(const std::array<double, B>& b_coeffs,
52  const std::array<double, A>& a_coeffs,
53  double frequency) {
54  return freqz(b_coeffs, frequency) / freqz(a_coeffs, frequency);
55 }
56 
57 template <size_t N, size_t O>
58 auto freqz(const std::array<double, N>& coeffs,
59  const std::array<double, O>& frequencies) {
60  return map([&](const auto& f) { return freqz(coeffs, f); }, frequencies);
61 }
62 
63 template <size_t B, size_t A, size_t O>
64 auto freqz(const std::array<double, B>& b_coeffs,
65  const std::array<double, A>& a_coeffs,
66  const std::array<double, O>& frequencies) {
67  return map([&](const auto& f) { return freqz(b_coeffs, a_coeffs, f); },
68  frequencies);
69 }
70 
71 } // namespace core
72 } // namespace wayverb
Definition: traits.cpp:2
Definition: capsule_base.h:9
Definition: freqz.h:13