From ed01827bcb8c83b437ccbd7c537910e8671564ee Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Wed, 1 Jan 2025 02:06:26 +0100 Subject: [PATCH] test --- lib/hilbert.c | 18 +++++++++++++++++- lib/hilbert.h | 31 ++----------------------------- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/lib/hilbert.c b/lib/hilbert.c index b31424e..48d587d 100644 --- a/lib/hilbert.c +++ b/lib/hilbert.c @@ -1,8 +1,24 @@ #include "hilbert.h" +static float generate_coefficient(int i, int nzeros) { + if (i == nzeros/2) return 0.0f; + + float n = i - nzeros/2; + // Basic Hilbert transform coefficient + float h = (i == nzeros/2) ? 0.0f : (2.0f/(PI * n)); + + // Apply Blackman window for better stopband attenuation + float w = 0.42f - 0.5f * cosf(2.0f * PI * i / nzeros) + + 0.08f * cosf(4.0f * PI * i / nzeros); + + return h * w; +} void init_hilbert(HilbertTransformer *hilbert) { hilbert->delay = calloc(D_SIZE, sizeof(float)); hilbert->dptr = 0; + for(int i = 0; i < NZEROS; i++) { + hilbert->coeffs[i] = generate_coefficient(i, NZEROS); + } } void apply_hilbert(HilbertTransformer *hilbert, float sample, float *output_0deg, float *output_90deg) { @@ -11,7 +27,7 @@ void apply_hilbert(HilbertTransformer *hilbert, float sample, float *output_0deg hilbert->delay[hilbert->dptr] = sample; hilb = 0.0f; for(int i = 0; i < NZEROS/2; i++) { - hilb += (xcoeffs[i] * hilbert->delay[(hilbert->dptr - i*2) & (D_SIZE - 1)]); + hilb += (hilbert->coeffs[i] * hilbert->delay[(hilbert->dptr - i*2) & (D_SIZE - 1)]); } *output_0deg = hilbert->delay[(hilbert->dptr - 99) & (D_SIZE - 1)]; diff --git a/lib/hilbert.h b/lib/hilbert.h index 920fb6d..b4ea07e 100644 --- a/lib/hilbert.h +++ b/lib/hilbert.h @@ -1,40 +1,13 @@ #include +#include "constants.h" #define D_SIZE 256 #define NZEROS 200 -/* The non-zero taps of the Hilbert transformer */ -static float xcoeffs[] = { - +0.0008103736f, +0.0008457886f, +0.0009017196f, +0.0009793364f, - +0.0010798341f, +0.0012044365f, +0.0013544008f, +0.0015310235f, - +0.0017356466f, +0.0019696659f, +0.0022345404f, +0.0025318040f, - +0.0028630784f, +0.0032300896f, +0.0036346867f, +0.0040788644f, - +0.0045647903f, +0.0050948365f, +0.0056716186f, +0.0062980419f, - +0.0069773575f, +0.0077132300f, +0.0085098208f, +0.0093718901f, - +0.0103049226f, +0.0113152847f, +0.0124104218f, +0.0135991079f, - +0.0148917649f, +0.0163008758f, +0.0178415242f, +0.0195321089f, - +0.0213953037f, +0.0234593652f, +0.0257599469f, +0.0283426636f, - +0.0312667947f, +0.0346107648f, +0.0384804823f, +0.0430224431f, - +0.0484451086f, +0.0550553725f, +0.0633242001f, +0.0740128560f, - +0.0884368322f, +0.1090816773f, +0.1412745301f, +0.1988673273f, - +0.3326528346f, +0.9997730178f, -0.9997730178f, -0.3326528346f, - -0.1988673273f, -0.1412745301f, -0.1090816773f, -0.0884368322f, - -0.0740128560f, -0.0633242001f, -0.0550553725f, -0.0484451086f, - -0.0430224431f, -0.0384804823f, -0.0346107648f, -0.0312667947f, - -0.0283426636f, -0.0257599469f, -0.0234593652f, -0.0213953037f, - -0.0195321089f, -0.0178415242f, -0.0163008758f, -0.0148917649f, - -0.0135991079f, -0.0124104218f, -0.0113152847f, -0.0103049226f, - -0.0093718901f, -0.0085098208f, -0.0077132300f, -0.0069773575f, - -0.0062980419f, -0.0056716186f, -0.0050948365f, -0.0045647903f, - -0.0040788644f, -0.0036346867f, -0.0032300896f, -0.0028630784f, - -0.0025318040f, -0.0022345404f, -0.0019696659f, -0.0017356466f, - -0.0015310235f, -0.0013544008f, -0.0012044365f, -0.0010798341f, - -0.0009793364f, -0.0009017196f, -0.0008457886f, -0.0008103736f, -}; - typedef struct { float* delay; int dptr; + float coeffs[NZEROS] } HilbertTransformer; void init_hilbert(HilbertTransformer *hilbert);