diff --git a/README.md b/README.md index 16082acc..4bb2ea2f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Refer to the documentation to quickly integrate and utilize the library's signal | Category | Description | |--------------------------------------------------------------------|----------------------------------------------------------------------| -| [Analysis](doc/analysis/README.md) | FFT, Power Spectral Density, DCT, Window Functions, Signal Detectors, Convolution & Correlation | +| [Analysis](doc/analysis/README.md) | FFT, Power Spectral Density, DCT, Window Functions, Signal Detectors, Convolution & Correlation, Goertzel Algorithm | | [Control Analysis](doc/control_analysis/README.md) | Frequency Response, Root Locus | | [Controllers](doc/controllers/README.md) | Bang-Bang/Hysteresis, PID, LQR, MPC, Saturation, Rate Limiter, Slew-Limited Saturation, Feedforward/2-DOF, Gain-Scheduled Controller | | [Dynamics](doc/dynamics/README.md) | Euler-Lagrange, Newton-Euler, Recursive Newton-Euler, ABA | diff --git a/ROADMAP.md b/ROADMAP.md index 6b487f58..a5d7eeba 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -27,7 +27,6 @@ Difficulty legend: | # | Component | Target module | Difficulty | |----|------------------------------------------------------|---------------------------|------------| -| 13 | Goertzel algorithm | `analysis` | ★★☆☆☆ | | 15 | Biquad / Second-Order-Section cascade | `filters/passive` | ★★★☆☆ | | 16 | Notch / comb filter | `filters/passive` | ★★★☆☆ | | 17 | Lead-lag compensator | `controllers` | ★★★☆☆ | diff --git a/doc/analysis/GoertzelAlgorithm.md b/doc/analysis/GoertzelAlgorithm.md new file mode 100644 index 00000000..d8ab891d --- /dev/null +++ b/doc/analysis/GoertzelAlgorithm.md @@ -0,0 +1,111 @@ +# Goertzel Algorithm + +## Overview & Motivation + +When a system needs to detect or measure a single known frequency — a DTMF touch-tone, a pilot sync tone, mains-hum, or a narrowband alarm — computing a full FFT wastes every cycle spent on bins that will never be read. The Goertzel algorithm computes exactly one DFT bin using a second-order recurrence that processes samples one at a time, requiring no input buffer and no twiddle-factor table. On the smallest microcontrollers with a few hundred bytes of RAM, this distinction is decisive. + +## Mathematical Theory + +### Core Definitions + +Let $x[n]$, $n = 0, \ldots, N-1$, be a block of $N$ real samples. The DFT coefficient at bin $k$ is: + +$$X[k] = \sum_{n=0}^{N-1} x[n]\, e^{-j2\pi kn/N}$$ + +Define the resonator coefficient: + +$$c = 2\cos\!\left(\frac{2\pi k}{N}\right)$$ + +### Derivation + +The DFT sum can be reorganised as the output of a single-pole IIR filter evaluated at $n = N$. Introduce the auxiliary sequence: + +$$s[n] = x[n] + c\,s[n-1] - s[n-2], \quad s[-1] = s[-2] = 0$$ + +After $N$ steps the DFT bin follows from the final two state values: + +$$X[k] = s[N-1] - s[N-2]\,e^{-j2\pi k/N}$$ + +Separating real and imaginary parts: + +$$\operatorname{Re}\{X[k]\} = s[N-1] - s[N-2]\cos\!\left(\tfrac{2\pi k}{N}\right)$$ + +$$\operatorname{Im}\{X[k]\} = s[N-2]\sin\!\left(\tfrac{2\pi k}{N}\right)$$ + +The magnitude can be obtained without the final trigonometric products using the identity: + +$$|X[k]|^2 = s[N-1]^2 + s[N-2]^2 - c\,s[N-1]\,s[N-2]$$ + +so $|X[k]| = \sqrt{s[N-1]^2 + s[N-2]^2 - c\,s[N-1]\,s[N-2]}$, saving two multiplies on the hot path. + +### Frequency-to-Bin Mapping + +A physical frequency $f_t$ sampled at $f_s$ maps to bin: + +$$k = \operatorname{round}\!\left(\frac{f_t}{f_s}\,N\right)$$ + +Frequency resolution is $\Delta f = f_s / N$. + +## Complexity Analysis + +| Case | Time | Space | Notes | +|------------|--------|--------|-----------------------------------------| +| Per sample | $O(1)$ | $O(1)$ | One multiply, two adds, no table lookup | +| Full block | $O(N)$ | $O(1)$ | Two state words; trig only at block end | + +For $B$ bins of interest the total cost is $O(BN)$. An FFT computing all $N$ bins costs $O(N\log N)$, so the Goertzel approach is cheaper when $B \ll \log_2 N$ — typically $B \le 4$ for a 256-point transform. + +## Step-by-Step Walkthrough + +Consider $N = 4$, $k = 1$, $x = [1, 0, -1, 0]$. + +$c = 2\cos(\pi/2) = 0$ + +| $n$ | $x[n]$ | $s[n] = x[n] + 0\cdot s[n-1] - s[n-2]$ | +|-----|--------|----------------------------------------| +| 0 | 1 | $1 + 0 - 0 = 1$ | +| 1 | 0 | $0 + 0 - 1 = -1$ | +| 2 | -1 | $-1 + 0 - 0 = -1$ (note: $s[-1]=0$) | +| 3 | 0 | $0 + 0 - (-1) = 1$ | + +$\cos(2\pi/4) = 0$, $\sin(2\pi/4) = 1$ + +$\operatorname{Re}\{X[1]\} = 1 - (-1)\cdot 0 = 1$ + +$\operatorname{Im}\{X[1]\} = (-1)\cdot 1 = -1$ + +Direct DFT check: $X[1] = 1 - j$. Matches. + +## Pitfalls & Edge Cases + +The recurrence pole sits exactly on the unit circle, making the filter marginally stable. Error accumulates with each block if `Reset` is not called between blocks. The algorithm is designed for finite, bounded blocks of exactly $N$ samples. + +Choosing $N$ such that the target tone lands near an integer bin minimises spectral leakage. A mismatch of half a bin can reduce the apparent magnitude by several decibels. Window functions do not apply here because the Goertzel filter already selects one bin; applying a window changes the effective DFT kernel. + +The DC bin ($k = 0$) and the Nyquist bin ($k = N/2$) are purely real; their imaginary output is zero by symmetry. + +## Variants & Generalizations + +The magnitude-squared form $s_1^2 + s_2^2 - c\,s_1 s_2$ avoids the square root when only power detection (threshold comparison) is needed, saving a transcendental call on platforms without hardware `sqrt`. + +When several tones must be tracked simultaneously, one independent Goertzel instance per tone runs in $O(BN)$ total with $O(B)$ state — still far cheaper than an FFT for small $B$. + +A sliding Goertzel variant updates one sample at a time by subtracting the oldest sample, giving a continuous output at the cost of maintaining an input ring buffer; this trades $O(1)$ memory for $O(N)$ memory and is outside the scope of the no-heap constraint here. + +## Applications + +- DTMF decoding (telephony, industrial keypads) +- Sync-tone and pilot-tone detection in radio links +- Mains-frequency monitoring (50/60 Hz hum) +- Single-bin power spectral estimation in embedded monitors +- Lock-in detection for narrow-band sensor readout + +## Connections to Other Algorithms + +The Goertzel algorithm computes exactly the same result as one bin of the DFT computed by the Cooley-Tukey FFT (`FastFourierTransform`), but limits the computation to that single bin. When all $N$ bins are required simultaneously, the FFT is always faster. `PowerDensitySpectrum` builds on the FFT for broadband spectral estimation. `SignalDetectors` provides simpler, non-frequency-selective detectors (peak hold, RMS) that cost even less per sample. + +## References & Further Reading + +- G. Goertzel, "An Algorithm for the Evaluation of Finite Trigonometric Series," *American Mathematical Monthly*, 65(1), 34–35, 1958. +- A. V. Oppenheim and R. W. Schafer, *Discrete-Time Signal Processing*, 3rd ed., Prentice Hall, 2009, §8.1. +- P. Mock, "Add DTMF Generation and Decoding to DSP-µP Designs," *EDN*, March 1985 (practical embedded application of Goertzel). diff --git a/doc/analysis/README.md b/doc/analysis/README.md index dde16fbb..316b5a3e 100644 --- a/doc/analysis/README.md +++ b/doc/analysis/README.md @@ -10,6 +10,7 @@ Signal analysis algorithms for frequency-domain decomposition and spectral estim | [Power Spectral Density](PowerDensitySpectrum.md) | Estimation of signal power distribution across frequencies using Welch's method | | [Discrete Cosine Transform](DiscreteCosineTransform.md) | Real-valued frequency decomposition via cosine basis functions, computed through FFT | | [Signal Detectors](SignalDetectors.md) | Peak hold, zero-crossing counter, and RMS envelope detectors for real-time signal monitoring | +| [Goertzel Algorithm](GoertzelAlgorithm.md) | Single-bin DFT via a second-order recurrence for O(N) tone detection with O(1) memory | ## Sub-domains diff --git a/numerical/analysis/CMakeLists.txt b/numerical/analysis/CMakeLists.txt index f2ff7856..66f6da54 100644 --- a/numerical/analysis/CMakeLists.txt +++ b/numerical/analysis/CMakeLists.txt @@ -16,6 +16,7 @@ target_sources(numerical.analysis PRIVATE DiscreteCosineTransform.hpp FastFourierTransform.hpp FastFourierTransformRadix2Impl.hpp + GoertzelAlgorithm.hpp PowerDensitySpectrum.hpp SignalDetectors.hpp ) @@ -23,6 +24,7 @@ target_sources(numerical.analysis PRIVATE numerical_add_coverage_sources(numerical.analysis ConvolutionCorrelation.cpp FastFourierTransformRadix2Impl.cpp + GoertzelAlgorithm.cpp PowerDensitySpectrum.cpp SignalDetectors.cpp ) diff --git a/numerical/analysis/GoertzelAlgorithm.cpp b/numerical/analysis/GoertzelAlgorithm.cpp new file mode 100644 index 00000000..a3e6b1ee --- /dev/null +++ b/numerical/analysis/GoertzelAlgorithm.cpp @@ -0,0 +1,6 @@ +#include "numerical/analysis/GoertzelAlgorithm.hpp" + +namespace analysis +{ + template class GoertzelAlgorithm; +} diff --git a/numerical/analysis/GoertzelAlgorithm.hpp b/numerical/analysis/GoertzelAlgorithm.hpp new file mode 100644 index 00000000..0c43fbd5 --- /dev/null +++ b/numerical/analysis/GoertzelAlgorithm.hpp @@ -0,0 +1,110 @@ +#pragma once + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC optimize("O3", "fast-math") +#endif + +#include "numerical/math/CompilerOptimizations.hpp" +#include "numerical/math/ComplexNumber.hpp" +#include +#include +#include +#include + +namespace analysis +{ + template + class GoertzelAlgorithm + { + static_assert(std::is_floating_point_v, "GoertzelAlgorithm supports floating-point types only"); + + public: + GoertzelAlgorithm(std::size_t k, std::size_t blockLength); + GoertzelAlgorithm(T targetHz, T sampleHz, std::size_t blockLength); + + OPTIMIZE_FOR_SPEED void Push(T x); + bool Ready() const; + math::Complex Result() const; + T Magnitude() const; + void Reset(); + + static T Coefficient(std::size_t k, std::size_t blockLength); + + private: + T coeff; + T cosine; + T sine; + std::size_t blockSize; + T s1; + T s2; + std::size_t sampleCount; + }; + + template + GoertzelAlgorithm::GoertzelAlgorithm(std::size_t k, std::size_t blockLength) + : coeff{ T{ 2 } * std::cos(T{ 2 } * std::numbers::pi_v * static_cast(k) / static_cast(blockLength)) } + , cosine{ std::cos(T{ 2 } * std::numbers::pi_v * static_cast(k) / static_cast(blockLength)) } + , sine{ std::sin(T{ 2 } * std::numbers::pi_v * static_cast(k) / static_cast(blockLength)) } + , blockSize{ blockLength } + , s1{ T{ 0 } } + , s2{ T{ 0 } } + , sampleCount{ 0 } + {} + + template + GoertzelAlgorithm::GoertzelAlgorithm(T targetHz, T sampleHz, std::size_t blockLength) + : GoertzelAlgorithm( + static_cast(targetHz / sampleHz * static_cast(blockLength) + T{ 0.5 }), + blockLength) + {} + + template + OPTIMIZE_FOR_SPEED void GoertzelAlgorithm::Push(T x) + { + T s0{ x + coeff * s1 - s2 }; + s2 = s1; + s1 = s0; + ++sampleCount; + } + + template + bool GoertzelAlgorithm::Ready() const + { + return sampleCount >= blockSize; + } + + template + math::Complex GoertzelAlgorithm::Result() const + { + T real{ s1 - s2 * cosine }; + T imag{ s2 * sine }; + return math::Complex{ real, imag }; + } + + template + T GoertzelAlgorithm::Magnitude() const + { + return std::sqrt(s1 * s1 + s2 * s2 - coeff * s1 * s2); + } + + template + void GoertzelAlgorithm::Reset() + { + s1 = T{ 0 }; + s2 = T{ 0 }; + sampleCount = 0; + } + + template + T GoertzelAlgorithm::Coefficient(std::size_t k, std::size_t blockLength) + { + return T{ 2 } * std::cos(T{ 2 } * std::numbers::pi_v * static_cast(k) / static_cast(blockLength)); + } +} + +#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD +namespace analysis +{ + extern template class GoertzelAlgorithm; +} +#endif diff --git a/numerical/analysis/test/CMakeLists.txt b/numerical/analysis/test/CMakeLists.txt index e8eeba46..f8a87117 100644 --- a/numerical/analysis/test/CMakeLists.txt +++ b/numerical/analysis/test/CMakeLists.txt @@ -12,6 +12,7 @@ target_sources(numerical.analysis_test PRIVATE TestConvolutionCorrelation.cpp TestDiscreteCosineTransform.cpp TestFastFourierTransformRadix2Impl.cpp + TestGoertzelAlgorithm.cpp TestPowerDensitySpectrum.cpp TestSignalDetectors.cpp ) diff --git a/numerical/analysis/test/TestGoertzelAlgorithm.cpp b/numerical/analysis/test/TestGoertzelAlgorithm.cpp new file mode 100644 index 00000000..56c2aae9 --- /dev/null +++ b/numerical/analysis/test/TestGoertzelAlgorithm.cpp @@ -0,0 +1,134 @@ +#include "numerical/analysis/GoertzelAlgorithm.hpp" +#include "numerical/math/Tolerance.hpp" +#include "gmock/gmock.h" +#include +#include +#include + +namespace +{ + class TestGoertzelAlgorithm : public ::testing::Test + { + public: + static constexpr std::size_t N = 16; + }; +} + +TEST_F(TestGoertzelAlgorithm, detects_matching_tone) +{ + analysis::GoertzelAlgorithm goertzel{ std::size_t{ 2 }, N }; + + for (std::size_t n = 0; n < N; ++n) + { + float sample{ std::cos(2.0f * std::numbers::pi_v * 2.0f * static_cast(n) / static_cast(N)) }; + goertzel.Push(sample); + } + + EXPECT_TRUE(goertzel.Ready()); + EXPECT_NEAR(goertzel.Magnitude(), static_cast(N) / 2.0f, 1e-2f); +} + +TEST_F(TestGoertzelAlgorithm, rejects_off_bin_tone) +{ + analysis::GoertzelAlgorithm goertzel{ std::size_t{ 2 }, N }; + + for (std::size_t n = 0; n < N; ++n) + { + float sample{ std::cos(2.0f * std::numbers::pi_v * 5.0f * static_cast(n) / static_cast(N)) }; + goertzel.Push(sample); + } + + EXPECT_LT(goertzel.Magnitude(), 1.0f); +} + +TEST_F(TestGoertzelAlgorithm, magnitude_matches_dft_bin) +{ + constexpr std::size_t k{ 3 }; + analysis::GoertzelAlgorithm goertzel{ k, N }; + + std::array signal{}; + for (std::size_t n = 0; n < N; ++n) + signal[n] = std::cos(2.0f * std::numbers::pi_v * static_cast(n) / static_cast(N)) + 0.5f; + + for (std::size_t n = 0; n < N; ++n) + goertzel.Push(signal[n]); + + float refReal{ 0.0f }; + float refImag{ 0.0f }; + for (std::size_t n = 0; n < N; ++n) + { + float angle{ -2.0f * std::numbers::pi_v * static_cast(k) * static_cast(n) / static_cast(N) }; + refReal += signal[n] * std::cos(angle); + refImag += signal[n] * std::sin(angle); + } + + math::Complex result{ goertzel.Result() }; + EXPECT_NEAR(result.Real(), refReal, 1e-3f); + EXPECT_NEAR(result.Imaginary(), refImag, 1e-3f); +} + +TEST_F(TestGoertzelAlgorithm, coefficient_formula_correct) +{ + constexpr std::size_t k{ 3 }; + float expected{ 2.0f * std::cos(2.0f * std::numbers::pi_v * static_cast(k) / static_cast(N)) }; + EXPECT_NEAR(analysis::GoertzelAlgorithm::Coefficient(k, N), expected, math::Tolerance()); +} + +TEST_F(TestGoertzelAlgorithm, dc_bin_sums_input) +{ + constexpr float c{ 1.5f }; + analysis::GoertzelAlgorithm goertzel{ std::size_t{ 0 }, N }; + + for (std::size_t n = 0; n < N; ++n) + goertzel.Push(c); + + math::Complex result{ goertzel.Result() }; + EXPECT_NEAR(result.Real(), static_cast(N) * c, 1e-3f); +} + +TEST_F(TestGoertzelAlgorithm, magnitude_squared_matches_result) +{ + constexpr std::size_t k{ 2 }; + analysis::GoertzelAlgorithm goertzel{ k, N }; + + for (std::size_t n = 0; n < N; ++n) + { + float sample{ std::cos(2.0f * std::numbers::pi_v * static_cast(k) * static_cast(n) / static_cast(N)) }; + goertzel.Push(sample); + } + + float mag{ goertzel.Magnitude() }; + math::Complex result{ goertzel.Result() }; + float rSq{ result.Real() * result.Real() + result.Imaginary() * result.Imaginary() }; + EXPECT_NEAR(mag * mag, rSq, 1e-2f); +} + +TEST_F(TestGoertzelAlgorithm, reset_restarts_block) +{ + constexpr std::size_t k{ 2 }; + analysis::GoertzelAlgorithm goertzel{ k, N }; + + for (std::size_t n = 0; n < N; ++n) + goertzel.Push(1.0f); + + goertzel.Reset(); + EXPECT_FALSE(goertzel.Ready()); + + for (std::size_t n = 0; n < N; ++n) + { + float sample{ std::cos(2.0f * std::numbers::pi_v * static_cast(k) * static_cast(n) / static_cast(N)) }; + goertzel.Push(sample); + } + + EXPECT_NEAR(goertzel.Magnitude(), static_cast(N) / 2.0f, 1e-2f); +} + +TEST_F(TestGoertzelAlgorithm, not_ready_before_n_samples) +{ + analysis::GoertzelAlgorithm goertzel{ std::size_t{ 2 }, N }; + + for (std::size_t n = 0; n < N - 1; ++n) + goertzel.Push(0.0f); + + EXPECT_FALSE(goertzel.Ready()); +} diff --git a/roadmap/analysis/GoertzelAlgorithm/explanation.md b/roadmap/analysis/GoertzelAlgorithm/explanation.md deleted file mode 100644 index 36e5f0b7..00000000 --- a/roadmap/analysis/GoertzelAlgorithm/explanation.md +++ /dev/null @@ -1,31 +0,0 @@ -# Goertzel Algorithm — Overview - -## What it is -A way to compute **one** DFT bin `X[k]` with a tiny second-order IIR recurrence instead of a full -FFT. It streams samples in one at a time and produces the bin's real/imaginary value (or magnitude) -after exactly `N` samples. - -## Why it matters (embedded) -When you only care about a few known frequencies — DTMF touch-tones, a pilot/sync tone, mains-hum -monitoring — Goertzel is dramatically cheaper than an FFT: **O(1) memory** (two state words, no input -buffer, no twiddle table) and one multiply-add per sample. Ideal for the smallest MCUs. - -## How it works (intuition) -The DFT sum is reorganised into a resonator tuned to bin `k`. Each sample drives the recurrence -`s[n] = x[n] + 2cos(2πk/N)·s[n−1] − s[n−2]`; the resonator "rings" in proportion to how much energy -the input carries at that frequency. After `N` samples, two cheap combinations of the final states -give the exact DFT coefficient. Because the pole sits on the unit circle it acts as a perfect -(marginally stable) oscillator over one finite block. - -## Key parameters -- **k (target bin)** — which DFT frequency, `f = k·fs/N`; choose `N` so your tone lands near integer `k`. -- **N (block length)** — sets frequency resolution `fs/N` and detection latency. -- **coeff = 2·cos(2πk/N)** — the single precomputed constant that tunes the resonator. - -## Reference -G. Goertzel, "An Algorithm for the Evaluation of Finite Trigonometric Series," *American -Mathematical Monthly*, 65(1), 34–35, 1958. - -## See also -`FastFourierTransform` / `RealFft` (all bins at once), `SignalDetectors` (cheap non-selective -detection), `math::Complex`. diff --git a/roadmap/analysis/GoertzelAlgorithm/implementation.md b/roadmap/analysis/GoertzelAlgorithm/implementation.md deleted file mode 100644 index eaa28a81..00000000 --- a/roadmap/analysis/GoertzelAlgorithm/implementation.md +++ /dev/null @@ -1,76 +0,0 @@ -# Goertzel Algorithm — Implementation Pseudocode - -> Roadmap ref: #13 (Tier 2) · Target: `numerical/analysis` · Namespace `analysis` · Type: `float` (templated on `T`, instantiated for `float` only) - -## Data structures - -``` -template # static_assert(std::is_floating_point_v); instantiated for float -class Goertzel: - T coeff # 2·cos(2π·k/N), precomputed - T s1, s2 # recurrence state: s[n-1], s[n-2] - T cosine # cos(2π·k/N), for complex output - T sine # sin(2π·k/N) - size_t N # block length - size_t n # samples fed so far -``` - -## Interface - -``` -Goertzel(size_t k, size_t N) # target bin k of an N-point DFT -Goertzel(T targetHz, T sampleHz, size_t N) # convenience: pick the nearest bin -void Push(T x) # hot path: one sample into the recurrence -bool Ready() const # true after N samples -T Magnitude() # |X[k]| -math::Complex Result() # real + imaginary X[k] -void Reset() -static T Coefficient(size_t k, size_t N) # 2·cos(2π·k/N) -``` - -## Algorithm (pseudocode) - -``` -function Push(x): # OPTIMIZE_FOR_SPEED - s0 = x + coeff * s1 - s2 # second-order recurrence - s2 = s1 - s1 = s0 - n += 1 - -function Result(): # call once, after N samples - real = s1 - s2 * cosine - imag = s2 * sine - return Complex(real, imag) - -function Magnitude(): - # avoids the two trig mults: |X|^2 = s1^2 + s2^2 - coeff·s1·s2 - return sqrt(s1*s1 + s2*s2 - coeff*s1*s2) -``` - -## Complexity & memory - -- Time: `O(N)` total — **one multiply and two adds per sample**; trig only at the end. -- Memory: `O(1)` — two state words, no input buffer, no twiddle table. -- Roughly `N/log N` cheaper than an FFT when only a handful of bins are needed. - -## Numerical / embedded notes - -- `coeff = 2·cos(2π·k/N)` is `constexpr`/precomputed; only `k` and `N` are needed at runtime. -- The recurrence pole sits **on** the unit circle, so it is marginally stable — keep blocks finite - (`N` samples) and `Reset()` between blocks to stop error accumulation. -- Prefer the magnitude-squared form `s1²+s2²−coeff·s1·s2` to skip the final `sqrt` on the hot path. -- Bin resolution is `fs/N`; choose `N` so the tone of interest lands near an integer `k` for best SNR. -- Float-only: `static_assert(std::is_floating_point_v)`; the generic `T` signature keeps a - `Q15`/`Q31` specialisation cheap to add later. - -## Deployment - -- Header: `numerical/analysis/GoertzelAlgorithm.hpp` — `#pragma once` → - `#pragma GCC optimize("O3","fast-math")`, `OPTIMIZE_FOR_SPEED` on `Push`, and - `extern template class Goertzel;` under `#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD`. -- Coverage: `numerical/analysis/GoertzelAlgorithm.cpp` → `template class Goertzel;` -- Test: `numerical/analysis/test/TestGoertzelAlgorithm.cpp` -- Doc: `doc/analysis/GoertzelAlgorithm.md` (expand to follow `doc/TEMPLATE.md`) -- CMake: `.hpp` → `target_sources`; `.cpp` → `numerical_add_coverage_sources`; - `TestGoertzelAlgorithm.cpp` → the `_test` target. -- Generic pattern: see `roadmap/DEPLOYMENT.md`. diff --git a/roadmap/analysis/GoertzelAlgorithm/tests.md b/roadmap/analysis/GoertzelAlgorithm/tests.md deleted file mode 100644 index efb43e9b..00000000 --- a/roadmap/analysis/GoertzelAlgorithm/tests.md +++ /dev/null @@ -1,59 +0,0 @@ -# Goertzel Algorithm — Unit Test Plan (Pseudocode) - -> GoogleTest · `TEST_F` (`float`) · `StrictMock` only · no heap. - -## Fixture - -``` -class TestGoertzel : public ::testing::Test: - static constexpr size_t N = 16 - # each test constructs Goertzel with a chosen bin k -# each case below is a TEST_F(TestGoertzel, ) -``` - -## Test cases (Arrange / Act / Assert) - -``` -detects_matching_tone: - Arrange: k = 2, feed N samples of cos(2π·2·n/N) - Act: push all, read Magnitude() - Assert: Magnitude() large (≈ N/2), Ready() true - -rejects_off_bin_tone: - Arrange: k = 2, feed a tone at bin 5 - Assert: Magnitude() ≈ 0 (small leakage) - -magnitude_matches_dft_bin: - Arrange: arbitrary length-N signal - Assert: Result() ≈ reference DFT X[k] (±tol) - -coefficient_formula_correct: - Assert: Coefficient(k, N) ≈ 2·cos(2π·k/N) - -dc_bin_sums_input: - Arrange: k = 0, constant input c - Assert: Result().Real ≈ N·c - -magnitude_squared_matches_result: - Assert: Magnitude()^2 ≈ Real^2 + Imag^2 of Result() - -reset_restarts_block: - Arrange: push samples, Reset(), push a fresh block - Assert: result depends only on the second block - -not_ready_before_n_samples: - Arrange: push N-1 samples - Assert: Ready() == false -``` - -## Reference vectors - -- On-bin cosine of amplitude 1 ⇒ `|X[k]| ≈ N/2`. -- DC (`k = 0`), constant `c` ⇒ `X[0] = N·c`. -- Compare against a direct `Σ x[n]·e^{−j2πkn/N}` reference for a random block. - -## Edge cases - -- `k = 0` and `k = N/2` (real-only bins). -- Feeding more than `N` samples without `Reset` (documented drift). -- Single-sample block (`N = 1`).