|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#if defined(__GNUC__) || defined(__clang__) |
| 4 | +#pragma GCC optimize("O3", "fast-math") |
| 5 | +#endif |
| 6 | + |
| 7 | +#include "numerical/analysis/FastFourierTransform.hpp" |
| 8 | +#include "numerical/math/CompilerOptimizations.hpp" |
| 9 | +#include "numerical/math/ComplexNumber.hpp" |
| 10 | +#include <cstddef> |
| 11 | +#include <infra/util/BoundedVector.hpp> |
| 12 | +#include <type_traits> |
| 13 | + |
| 14 | +namespace analysis |
| 15 | +{ |
| 16 | + template<typename T, std::size_t M, std::size_t K> |
| 17 | + OPTIMIZE_FOR_SPEED void LinearConvolution( |
| 18 | + const typename infra::BoundedVector<T>::template WithMaxSize<M>& x, |
| 19 | + const typename infra::BoundedVector<T>::template WithMaxSize<K>& h, |
| 20 | + typename infra::BoundedVector<T>::template WithMaxSize<M + K - 1>& y) |
| 21 | + { |
| 22 | + static_assert(std::is_floating_point_v<T>, "ConvolutionCorrelation supports floating-point types only"); |
| 23 | + y.clear(); |
| 24 | + y.resize(M + K - 1, T{ 0 }); |
| 25 | + for (std::size_t n = 0; n < M + K - 1; ++n) |
| 26 | + { |
| 27 | + T acc{ 0 }; |
| 28 | + std::size_t kStart{ n >= K - 1 ? n - (K - 1) : 0 }; |
| 29 | + std::size_t kEnd{ n < M - 1 ? n : M - 1 }; |
| 30 | + for (std::size_t k = kStart; k <= kEnd; ++k) |
| 31 | + acc += x[k] * h[n - k]; |
| 32 | + y[n] = acc; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + template<typename T, std::size_t N> |
| 37 | + OPTIMIZE_FOR_SPEED void CircularConvolution( |
| 38 | + const typename infra::BoundedVector<T>::template WithMaxSize<N>& x, |
| 39 | + const typename infra::BoundedVector<T>::template WithMaxSize<N>& h, |
| 40 | + typename infra::BoundedVector<T>::template WithMaxSize<N>& y) |
| 41 | + { |
| 42 | + static_assert(std::is_floating_point_v<T>, "ConvolutionCorrelation supports floating-point types only"); |
| 43 | + y.clear(); |
| 44 | + y.resize(N, T{ 0 }); |
| 45 | + for (std::size_t n = 0; n < N; ++n) |
| 46 | + { |
| 47 | + T acc{ 0 }; |
| 48 | + for (std::size_t k = 0; k < N; ++k) |
| 49 | + acc += x[k] * h[(n + N - k) % N]; |
| 50 | + y[n] = acc; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + template<typename T, std::size_t M, std::size_t K> |
| 55 | + OPTIMIZE_FOR_SPEED void CrossCorrelation( |
| 56 | + const typename infra::BoundedVector<T>::template WithMaxSize<M>& x, |
| 57 | + const typename infra::BoundedVector<T>::template WithMaxSize<K>& y, |
| 58 | + typename infra::BoundedVector<T>::template WithMaxSize<M + K - 1>& r) |
| 59 | + { |
| 60 | + static_assert(std::is_floating_point_v<T>, "ConvolutionCorrelation supports floating-point types only"); |
| 61 | + typename infra::BoundedVector<T>::template WithMaxSize<K> yRev; |
| 62 | + yRev.resize(K, T{ 0 }); |
| 63 | + for (std::size_t i = 0; i < K; ++i) |
| 64 | + yRev[i] = y[K - 1 - i]; |
| 65 | + LinearConvolution<T, M, K>(x, yRev, r); |
| 66 | + } |
| 67 | + |
| 68 | + template<typename T, std::size_t M> |
| 69 | + void AutoCorrelation( |
| 70 | + const typename infra::BoundedVector<T>::template WithMaxSize<M>& x, |
| 71 | + typename infra::BoundedVector<T>::template WithMaxSize<2 * M - 1>& r) |
| 72 | + { |
| 73 | + static_assert(std::is_floating_point_v<T>, "ConvolutionCorrelation supports floating-point types only"); |
| 74 | + CrossCorrelation<T, M, M>(x, x, r); |
| 75 | + } |
| 76 | + |
| 77 | + template<typename T, std::size_t N> |
| 78 | + std::size_t ArgMaxLag(const typename infra::BoundedVector<T>::template WithMaxSize<N>& r) |
| 79 | + { |
| 80 | + static_assert(std::is_floating_point_v<T>, "ConvolutionCorrelation supports floating-point types only"); |
| 81 | + std::size_t best{ 0 }; |
| 82 | + for (std::size_t i = 1; i < N; ++i) |
| 83 | + if (r[i] > r[best]) |
| 84 | + best = i; |
| 85 | + return best; |
| 86 | + } |
| 87 | + |
| 88 | + template<typename T, std::size_t M, std::size_t K, std::size_t L> |
| 89 | + void FastConvolution( |
| 90 | + const typename infra::BoundedVector<T>::template WithMaxSize<M>& x, |
| 91 | + const typename infra::BoundedVector<T>::template WithMaxSize<K>& h, |
| 92 | + typename infra::BoundedVector<T>::template WithMaxSize<M + K - 1>& y, |
| 93 | + FastFourierTransform<T>& fft) |
| 94 | + { |
| 95 | + static_assert(std::is_floating_point_v<T>, "ConvolutionCorrelation supports floating-point types only"); |
| 96 | + static_assert((L & (L - 1)) == 0, "L must be a power of two"); |
| 97 | + static_assert(L >= M + K - 1, "L must be at least M+K-1"); |
| 98 | + |
| 99 | + typename infra::BoundedVector<T>::template WithMaxSize<L> xPad; |
| 100 | + typename infra::BoundedVector<T>::template WithMaxSize<L> hPad; |
| 101 | + xPad.resize(L, T{ 0 }); |
| 102 | + hPad.resize(L, T{ 0 }); |
| 103 | + |
| 104 | + for (std::size_t i = 0; i < M; ++i) |
| 105 | + xPad[i] = x[i]; |
| 106 | + for (std::size_t i = 0; i < K; ++i) |
| 107 | + hPad[i] = h[i]; |
| 108 | + |
| 109 | + typename infra::BoundedVector<math::Complex<T>>::template WithMaxSize<L> X; |
| 110 | + X.resize(L, math::Complex<T>{ T{ 0 }, T{ 0 } }); |
| 111 | + { |
| 112 | + auto& rawX = fft.Forward(xPad); |
| 113 | + for (std::size_t i = 0; i < L; ++i) |
| 114 | + X[i] = rawX[i]; |
| 115 | + } |
| 116 | + auto& H = fft.Forward(hPad); |
| 117 | + |
| 118 | + typename infra::BoundedVector<math::Complex<T>>::template WithMaxSize<L> Y; |
| 119 | + Y.resize(L, math::Complex<T>{ T{ 0 }, T{ 0 } }); |
| 120 | + for (std::size_t i = 0; i < L; ++i) |
| 121 | + Y[i] = X[i] * H[i]; |
| 122 | + |
| 123 | + auto& yFull = fft.Inverse(Y); |
| 124 | + |
| 125 | + y.clear(); |
| 126 | + y.resize(M + K - 1, T{ 0 }); |
| 127 | + for (std::size_t i = 0; i < M + K - 1; ++i) |
| 128 | + y[i] = yFull[i]; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD |
| 133 | +namespace analysis |
| 134 | +{ |
| 135 | + extern template void LinearConvolution<float, 3, 3>( |
| 136 | + const infra::BoundedVector<float>::WithMaxSize<3>&, |
| 137 | + const infra::BoundedVector<float>::WithMaxSize<3>&, |
| 138 | + infra::BoundedVector<float>::WithMaxSize<5>&); |
| 139 | + |
| 140 | + extern template void CircularConvolution<float, 4>( |
| 141 | + const infra::BoundedVector<float>::WithMaxSize<4>&, |
| 142 | + const infra::BoundedVector<float>::WithMaxSize<4>&, |
| 143 | + infra::BoundedVector<float>::WithMaxSize<4>&); |
| 144 | + |
| 145 | + extern template void CrossCorrelation<float, 5, 5>( |
| 146 | + const infra::BoundedVector<float>::WithMaxSize<5>&, |
| 147 | + const infra::BoundedVector<float>::WithMaxSize<5>&, |
| 148 | + infra::BoundedVector<float>::WithMaxSize<9>&); |
| 149 | + |
| 150 | + extern template void AutoCorrelation<float, 4>( |
| 151 | + const infra::BoundedVector<float>::WithMaxSize<4>&, |
| 152 | + infra::BoundedVector<float>::WithMaxSize<7>&); |
| 153 | + |
| 154 | + extern template std::size_t ArgMaxLag<float, 9>( |
| 155 | + const infra::BoundedVector<float>::WithMaxSize<9>&); |
| 156 | + |
| 157 | + extern template void FastConvolution<float, 3, 3, 8>( |
| 158 | + const infra::BoundedVector<float>::WithMaxSize<3>&, |
| 159 | + const infra::BoundedVector<float>::WithMaxSize<3>&, |
| 160 | + infra::BoundedVector<float>::WithMaxSize<5>&, |
| 161 | + FastFourierTransform<float>&); |
| 162 | +} |
| 163 | +#endif |
0 commit comments