From 5991fa600dd6a3fb303ec494538f65182cfc6aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Bourdeauducq?= Date: Tue, 31 Dec 2024 17:04:37 +0800 Subject: [PATCH] phase_t --- dsp_lib.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/dsp_lib.h b/dsp_lib.h index b8d7553..10817ac 100644 --- a/dsp_lib.h +++ b/dsp_lib.h @@ -3,18 +3,21 @@ #include #include -static uint32_t frequency_to_ftw(double f) { - return f*(double)UINT32_MAX; +typedef uint32_t phase_t; +#define PHASE_MAX UINT32_MAX + +static phase_t frequency_to_ftw(double f) { + return f*(double)PHASE_MAX; } class DDS { private: - uint32_t phase = 0; + phase_t phase = 0; public: - uint32_t ftw = 0; + phase_t ftw = 0; double get() { phase += ftw; // wraps on overflow - return sin(phase*2.0*M_PI/(double)UINT32_MAX); + return sin(phase*2.0*M_PI/(double)PHASE_MAX); } }; @@ -41,10 +44,10 @@ template class Lockin { private: double scale; - uint32_t phase = 0; + phase_t phase = 0; Lowpass, order> lpf; public: - uint32_t ftw = 0; + phase_t ftw = 0; void set_scale(double s) { scale = s; } @@ -53,7 +56,7 @@ class Lockin { } std::complex update(double x) { std::complex rotated; - rotated = x*std::polar(scale, phase*2.0*M_PI/(double)UINT32_MAX); + rotated = x*std::polar(scale, phase*2.0*M_PI/(double)PHASE_MAX); phase -= ftw; // wraps on underflow return lpf.update(rotated); }