Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
interpolation.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Embeint Inc
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_ZEPHYR_MATH_INTERPOLATION_H_
8#define ZEPHYR_INCLUDE_ZEPHYR_MATH_INTERPOLATION_H_
9
10#include <stdint.h>
11#include <math.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
37static inline int32_t linear_interpolate(const int32_t *x_axis, const int32_t *y_axis, uint8_t len,
38 int32_t x)
39{
40 float rise, run, slope;
41 int32_t x_shifted;
42 uint8_t idx_low = 0;
43
44 /* Handle out of bounds values */
45 if (x <= x_axis[0]) {
46 return y_axis[0];
47 } else if (x >= x_axis[len - 1]) {
48 return y_axis[len - 1];
49 }
50
51 /* Find the lower x axis bucket */
52 while (x >= x_axis[idx_low + 1]) {
53 idx_low++;
54 }
55
56 /* Shift input to origin */
57 x_shifted = x - x_axis[idx_low];
58 if (x_shifted == 0) {
59 return y_axis[idx_low];
60 }
61
62 /* Local slope */
63 rise = y_axis[idx_low + 1] - y_axis[idx_low];
64 run = x_axis[idx_low + 1] - x_axis[idx_low];
65 slope = rise / run;
66
67 /* Apply slope, undo origin shift and round */
68 return roundf(y_axis[idx_low] + (slope * x_shifted));
69}
70
71#ifdef __cplusplus
72}
73#endif
74
75#endif /* ZEPHYR_INCLUDE_ZEPHYR_MATH_INTERPOLATION_H_ */
static int32_t linear_interpolate(const int32_t *x_axis, const int32_t *y_axis, uint8_t len, int32_t x)
Perform a linear interpolation across an arbitrary curve.
Definition interpolation.h:37
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT8_TYPE__ uint8_t
Definition stdint.h:88