From b120a7c39762aeb5afb06b06f8972ebb53facf43 Mon Sep 17 00:00:00 2001 From: George Norton Date: Sat, 26 Aug 2023 16:59:37 +0100 Subject: [PATCH] Fix off by one error in fix_28_t --- firmware/code/fix16.inl | 9 +++++---- firmware/code/run.c | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/firmware/code/fix16.inl b/firmware/code/fix16.inl index ca45a2e..bca6f14 100644 --- a/firmware/code/fix16.inl +++ b/firmware/code/fix16.inl @@ -32,11 +32,12 @@ static inline fix3_28_t norm_fix3_28_from_s16sample(int16_t a) { /* So, we're using a Q3.28 fixed point system here, and we want the incoming audio signal to be represented as a number between -1 and 1. To do this, we need the 16-bit value to map to the 28-bit right-of-decimal field in - our fixed point number. 28-16 = 12, so we shift the incoming value by + our fixed point number. The 16 bit samples are siged, so bit 15 needs to + be positioned at bit 28. 28-15 = 13, so we shift the incoming value by that much to covert it to the desired Q3.28 format and do the normalization all in one go. */ - return (fix3_28_t)a << 12; + return (fix3_28_t)a << 13; } /// @brief Convert fixed point samples into signed integer. Used to convert @@ -69,9 +70,9 @@ static inline int16_t norm_fix3_28_to_s16sample(fix3_28_t a) { } } /* When we converted the USB audio sample to a fixed point number, we applied - a normalization, or a gain of 1/65536. To convert it back, we can undo that + a normalization, or a gain of 1/32767. To convert it back, we can undo that by shifting it back by the same amount we shifted it in the first place. */ - return (a >> 12); + return (a >> 13); } static inline fix3_28_t fix3_28_from_flt(float a) { diff --git a/firmware/code/run.c b/firmware/code/run.c index 3a33f89..50711c1 100644 --- a/firmware/code/run.c +++ b/firmware/code/run.c @@ -194,7 +194,7 @@ void __no_inline_not_in_flash_func(core1_entry)() { x_f16 = bqf_transform(x_f16, &bqf_filters_right[j], &bqf_filters_mem_right[j]); - out[i] = (int16_t) norm_fix3_28_to_s16sample(x_f16); + out[i] = (int32_t) norm_fix3_28_to_s16sample(x_f16); } }