2 * This file is part of Betaflight.
4 * Betaflight is free software. You can redistribute this software
5 * and/or modify this software under the terms of the GNU General
6 * Public License as published by the Free Software Foundation,
7 * either version 3 of the License, or (at your option) any later
10 * Betaflight is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this software.
19 * If not, see <http://www.gnu.org/licenses/>.
26 void pwlInitialize(pwl_t
*pwl
, float *yValues
, int numPoints
, float xMin
, float xMax
) {
27 pwl
->yValues
= yValues
;
28 pwl
->numPoints
= numPoints
;
31 pwl
->dx
= (xMax
- xMin
) / (numPoints
- 1);
34 void pwlFill(pwl_t
*pwl
, float (*function
)(float, void*), void *args
)
36 for (int i
= 0; i
< pwl
->numPoints
; ++i
) {
37 const float x
= pwl
->xMin
+ i
* pwl
->dx
;
38 pwl
->yValues
[i
] = function(x
, args
);
42 float pwlInterpolate(const pwl_t
*pwl
, float x
)
45 return pwl
->yValues
[0];
49 return pwl
->yValues
[pwl
->numPoints
- 1];
52 const int index
= (int)((x
- pwl
->xMin
) / pwl
->dx
);
53 if (index
>= pwl
->numPoints
- 1) {
54 return pwl
->yValues
[pwl
->numPoints
- 1];
57 const float x0
= pwl
->xMin
+ index
* pwl
->dx
;
58 const float y0
= pwl
->yValues
[index
];
59 const float y1
= pwl
->yValues
[index
+ 1];
61 return y0
+ (x
- x0
) * (y1
- y0
) / pwl
->dx
;