From c8e7f0a48d1190464291d4d6407d0afb38a0c2b5 Mon Sep 17 00:00:00 2001 From: KSMehta11 Date: Mon, 26 Jan 2026 19:08:53 -0800 Subject: [PATCH 1/3] Add linear interpolation method to utils --- middleware/include/c_utils.h | 16 ++++++++++++++++ middleware/src/c_utils.c | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/middleware/include/c_utils.h b/middleware/include/c_utils.h index 1326bf23..10c62cd2 100644 --- a/middleware/include/c_utils.h +++ b/middleware/include/c_utils.h @@ -52,4 +52,20 @@ void endian_swap(void *ptr, int size); /// @return the same byte but wuth the bits reversed unsigned char reverse_bits(unsigned char b); +/** + * @brief Performs linear interpolation between two points. + * + * Computes the interpolated y-value corresponding to x, given two + * reference points (x1, y1) and (x2, y2). + * + * @param x Input value at which to interpolate. + * @param x1 First reference x-coordinate. + * @param x2 Second reference x-coordinate. + * @param y1 y-value at x1. + * @param y2 y-value at x2. + * + * @return Interpolated y-value. + */ +float linear_interpolate(float x, float x1, float x2, float y1, float y2); + #endif /* C_UTILS */ \ No newline at end of file diff --git a/middleware/src/c_utils.c b/middleware/src/c_utils.c index 7944d433..943676a7 100644 --- a/middleware/src/c_utils.c +++ b/middleware/src/c_utils.c @@ -17,4 +17,11 @@ unsigned char reverse_bits(unsigned char b) b = (b & 0xCC) >> 2 | (b & 0x33) << 2; b = (b & 0xAA) >> 1 | (b & 0x55) << 1; return b; +} + +float linear_interpolate(float x, float x1, float x2, float y1, float y2) +{ + assert(fabs(x2 - x1) > 0.0001f); + + return y1 + ((x - x1) * (y2 - y1) / (x2 - x1)); } \ No newline at end of file From 60eccb53ff5ee503b36eee7d26e8b1f29a498bf1 Mon Sep 17 00:00:00 2001 From: KSMehta11 Date: Mon, 26 Jan 2026 19:12:46 -0800 Subject: [PATCH 2/3] Add required header files --- middleware/src/c_utils.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/middleware/src/c_utils.c b/middleware/src/c_utils.c index 943676a7..6b44b7fe 100644 --- a/middleware/src/c_utils.c +++ b/middleware/src/c_utils.c @@ -1,4 +1,6 @@ #include "c_utils.h" +#include +#include void endian_swap(void *ptr, int size) { From e004cea9bae46ea06f998361da484e84d58cf76c Mon Sep 17 00:00:00 2001 From: KSMehta11 Date: Mon, 26 Jan 2026 19:18:27 -0800 Subject: [PATCH 3/3] Add required macro --- middleware/src/c_utils.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/middleware/src/c_utils.c b/middleware/src/c_utils.c index 6b44b7fe..1afcc5f0 100644 --- a/middleware/src/c_utils.c +++ b/middleware/src/c_utils.c @@ -2,6 +2,9 @@ #include #include +/* Epsilon for float comparisons */ +#define FLOAT_EPSILON (0.0001f) + void endian_swap(void *ptr, int size) { char *p = (char *)ptr; @@ -23,7 +26,7 @@ unsigned char reverse_bits(unsigned char b) float linear_interpolate(float x, float x1, float x2, float y1, float y2) { - assert(fabs(x2 - x1) > 0.0001f); + assert(fabs(x2 - x1) > FLOAT_EPSILON); return y1 + ((x - x1) * (y2 - y1) / (x2 - x1)); } \ No newline at end of file