Zephyr API Documentation 4.2.0-rc3
A Scalable Open Source RTOS
 4.2.0-rc3
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
21
28
44static inline int32_t linear_interpolate(const int32_t *x_axis, const int32_t *y_axis, uint8_t len,
45 int32_t x)
46{
47 float rise, run, slope;
48 int32_t x_shifted;
49 uint8_t idx_low = 0;
50
51 /* Handle out of bounds values */
52 if (x <= x_axis[0]) {
53 return y_axis[0];
54 } else if (x >= x_axis[len - 1]) {
55 return y_axis[len - 1];
56 }
57
58 /* Find the lower x axis bucket */
59 while (x >= x_axis[idx_low + 1]) {
60 idx_low++;
61 }
62
63 /* Shift input to origin */
64 x_shifted = x - x_axis[idx_low];
65 if (x_shifted == 0) {
66 return y_axis[idx_low];
67 }
68
69 /* Local slope */
70 rise = y_axis[idx_low + 1] - y_axis[idx_low];
71 run = x_axis[idx_low + 1] - x_axis[idx_low];
72 slope = rise / run;
73
74 /* Apply slope, undo origin shift and round */
75 return roundf(y_axis[idx_low] + (slope * x_shifted));
76}
77
81
82#ifdef __cplusplus
83}
84#endif
85
86#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:44
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT8_TYPE__ uint8_t
Definition stdint.h:88