|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#if defined(__GNUC__) || defined(__clang__) |
| 4 | +#pragma GCC optimize("O3", "fast-math") |
| 5 | +#endif |
| 6 | + |
| 7 | +#include "infra/util/BoundedVector.hpp" |
| 8 | +#include "numerical/analysis/FastFourierTransform.hpp" |
| 9 | +#include "numerical/math/CompilerOptimizations.hpp" |
| 10 | +#include "numerical/math/ComplexNumber.hpp" |
| 11 | +#include <array> |
| 12 | +#include <cmath> |
| 13 | +#include <numbers> |
| 14 | +#include <type_traits> |
| 15 | + |
| 16 | +namespace analysis |
| 17 | +{ |
| 18 | + template<typename T, std::size_t N> |
| 19 | + class AnalyticSignalFft |
| 20 | + { |
| 21 | + static_assert(std::is_floating_point_v<T>, "AnalyticSignalFft supports floating-point types"); |
| 22 | + static_assert(N >= 4 && (N & (N - 1)) == 0, "AnalyticSignalFft length N must be a power of two >= 4"); |
| 23 | + |
| 24 | + public: |
| 25 | + using Complex = math::Complex<T>; |
| 26 | + using VectorComplex = infra::BoundedVector<Complex>; |
| 27 | + using VectorReal = infra::BoundedVector<T>; |
| 28 | + |
| 29 | + explicit AnalyticSignalFft(FastFourierTransform<T>& fft); |
| 30 | + |
| 31 | + OPTIMIZE_FOR_SPEED VectorComplex& Analytic(VectorReal& x); |
| 32 | + |
| 33 | + static T InstantaneousAmplitude(Complex a); |
| 34 | + static T InstantaneousPhase(Complex a); |
| 35 | + static T InstantaneousFrequency(T phaseNow, T phasePrev, T ts); |
| 36 | + |
| 37 | + private: |
| 38 | + FastFourierTransform<T>& fft; |
| 39 | + typename VectorComplex::template WithMaxSize<N> spectrum; |
| 40 | + typename VectorComplex::template WithMaxSize<N> analytic; |
| 41 | + }; |
| 42 | + |
| 43 | + template<typename T, std::size_t Taps> |
| 44 | + class HilbertFir |
| 45 | + { |
| 46 | + static_assert(std::is_floating_point_v<T>, "HilbertFir supports floating-point types"); |
| 47 | + static_assert(Taps >= 3 && (Taps % 2) == 1, "HilbertFir requires an odd number of taps >= 3"); |
| 48 | + |
| 49 | + public: |
| 50 | + using Complex = math::Complex<T>; |
| 51 | + |
| 52 | + HilbertFir(); |
| 53 | + |
| 54 | + OPTIMIZE_FOR_SPEED Complex Filter(T x); |
| 55 | + |
| 56 | + private: |
| 57 | + static constexpr std::size_t centerTap{ (Taps - 1) / 2 }; |
| 58 | + |
| 59 | + std::array<T, Taps> coeff{}; |
| 60 | + std::array<T, Taps> delay{}; |
| 61 | + std::size_t writeIndex{ 0 }; |
| 62 | + }; |
| 63 | + |
| 64 | + /// AnalyticSignalFft implementation /// |
| 65 | + |
| 66 | + template<typename T, std::size_t N> |
| 67 | + AnalyticSignalFft<T, N>::AnalyticSignalFft(FastFourierTransform<T>& fft) |
| 68 | + : fft{ fft } |
| 69 | + { |
| 70 | + spectrum.resize(N); |
| 71 | + analytic.resize(N); |
| 72 | + } |
| 73 | + |
| 74 | + template<typename T, std::size_t N> |
| 75 | + OPTIMIZE_FOR_SPEED typename AnalyticSignalFft<T, N>::VectorComplex& AnalyticSignalFft<T, N>::Analytic(VectorReal& x) |
| 76 | + { |
| 77 | + VectorComplex& X{ fft.Forward(x) }; |
| 78 | + |
| 79 | + spectrum[0] = X[0]; |
| 80 | + spectrum[N / 2] = X[N / 2]; |
| 81 | + |
| 82 | + for (std::size_t k{ 1 }; k < N / 2; ++k) |
| 83 | + spectrum[k] = Complex{ T(2) * X[k].Real(), T(2) * X[k].Imaginary() }; |
| 84 | + |
| 85 | + for (std::size_t k{ N / 2 + 1 }; k < N; ++k) |
| 86 | + spectrum[k] = Complex{ T(0), T(0) }; |
| 87 | + |
| 88 | + typename VectorComplex::template WithMaxSize<N> hilbertSpectrum{}; |
| 89 | + hilbertSpectrum.resize(N); |
| 90 | + for (std::size_t k{ 0 }; k < N; ++k) |
| 91 | + hilbertSpectrum[k] = Complex{ spectrum[k].Imaginary(), -spectrum[k].Real() }; |
| 92 | + |
| 93 | + VectorReal& hilbertTd{ fft.Inverse(hilbertSpectrum) }; |
| 94 | + |
| 95 | + for (std::size_t n{ 0 }; n < N; ++n) |
| 96 | + analytic[n] = Complex{ x[n], hilbertTd[n] }; |
| 97 | + |
| 98 | + return analytic; |
| 99 | + } |
| 100 | + |
| 101 | + template<typename T, std::size_t N> |
| 102 | + T AnalyticSignalFft<T, N>::InstantaneousAmplitude(Complex a) |
| 103 | + { |
| 104 | + return std::sqrt(a.Real() * a.Real() + a.Imaginary() * a.Imaginary()); |
| 105 | + } |
| 106 | + |
| 107 | + template<typename T, std::size_t N> |
| 108 | + T AnalyticSignalFft<T, N>::InstantaneousPhase(Complex a) |
| 109 | + { |
| 110 | + return std::atan2(a.Imaginary(), a.Real()); |
| 111 | + } |
| 112 | + |
| 113 | + template<typename T, std::size_t N> |
| 114 | + T AnalyticSignalFft<T, N>::InstantaneousFrequency(T phaseNow, T phasePrev, T ts) |
| 115 | + { |
| 116 | + T dphi{ phaseNow - phasePrev }; |
| 117 | + while (dphi > std::numbers::pi_v<T>) |
| 118 | + dphi -= T(2) * std::numbers::pi_v<T>; |
| 119 | + while (dphi < -std::numbers::pi_v<T>) |
| 120 | + dphi += T(2) * std::numbers::pi_v<T>; |
| 121 | + return dphi / (T(2) * std::numbers::pi_v<T> * ts); |
| 122 | + } |
| 123 | + |
| 124 | + /// HilbertFir implementation /// |
| 125 | + |
| 126 | + template<typename T, std::size_t Taps> |
| 127 | + HilbertFir<T, Taps>::HilbertFir() |
| 128 | + { |
| 129 | + coeff.fill(T(0)); |
| 130 | + delay.fill(T(0)); |
| 131 | + for (std::size_t i{ 0 }; i < Taps; ++i) |
| 132 | + { |
| 133 | + int k{ static_cast<int>(i) - static_cast<int>(centerTap) }; |
| 134 | + if (k != 0 && (k % 2) != 0) |
| 135 | + { |
| 136 | + T w{ T(0.54) - T(0.46) * std::cos(T(2) * std::numbers::pi_v<T> * static_cast<T>(i) / static_cast<T>(Taps - 1)) }; |
| 137 | + coeff[i] = (T(2) / (std::numbers::pi_v<T> * static_cast<T>(k))) * w; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + template<typename T, std::size_t Taps> |
| 143 | + OPTIMIZE_FOR_SPEED typename HilbertFir<T, Taps>::Complex HilbertFir<T, Taps>::Filter(T x) |
| 144 | + { |
| 145 | + delay[writeIndex] = x; |
| 146 | + |
| 147 | + T imag{ T(0) }; |
| 148 | + for (std::size_t i{ 0 }; i < Taps; ++i) |
| 149 | + { |
| 150 | + std::size_t idx{ (writeIndex + Taps - i) % Taps }; |
| 151 | + imag += coeff[i] * delay[idx]; |
| 152 | + } |
| 153 | + |
| 154 | + std::size_t realIdx{ (writeIndex + Taps - centerTap) % Taps }; |
| 155 | + T real{ delay[realIdx] }; |
| 156 | + |
| 157 | + writeIndex = (writeIndex + 1) % Taps; |
| 158 | + |
| 159 | + return Complex{ real, imag }; |
| 160 | + } |
| 161 | + |
| 162 | +#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD |
| 163 | + extern template class AnalyticSignalFft<float, 64>; |
| 164 | + extern template class HilbertFir<float, 31>; |
| 165 | +#endif |
| 166 | +} |
0 commit comments