hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include"interpolation.h"
#include "math.h"
#include <stdlib.h>
 
void interpolation(const float *x, const float *y, int Num, float x0, float*y0)
{
    int i, index;
    float k;
    if (x0 <= x[0])
    {
        k = y[0];
    }
    else if (x0 >= x[Num - 1])
    {
        k = y[Num - 1];
    }
    else
    {
        for (i = 0; i < Num; i++)
        {
            if (x0 < x[i])
                break;
        }
 
        index = i - 1;
        if ((float)x[index + 1] - (float)x[index] < 0.001)
            k = (float)y[index];
        else
            k = ((float)x0 - (float)x[index]) / ((float)x[index + 1] - (float)x[index])
                * ((float)y[index + 1] - (float)y[index])
                + (float)y[index];
    }
 
    *y0 = k;
}
 
void interpolation(const float *x, const unsigned short *y, int Num, float x0, unsigned short *y0)
{
    int i, index;
    float k;
    if (x0 <= x[0])
    {
        k = y[0];
    }
    else if (x0 >= x[Num - 1])
    {
        k = y[Num - 1];
    }
    else
    {
        for (i = 0; i < Num; i++)
        {
            if (x0 < x[i])
                break;
        }
 
        index = i - 1;
        if ((float)x[index + 1] - (float)x[index] < 0.001)
            k = (float)y[index];
        else
            k = ((float)x0 - (float)x[index]) / ((float)x[index + 1] - (float)x[index])
                * ((float)y[index + 1] - (float)y[index])
                + (float)y[index];
    }
 
    *y0 = (unsigned short)(k+0.5);
}
 
int interpolation(unsigned char *x, bool *y, int xNum, unsigned char x0, bool *y0)
{
    int i, index;
    bool k;
    if (x0 <= x[0] || x0 <= x[1])
    {
        k = y[0];
        index = 0;
    }
    else if (x0 >= x[xNum - 1])
    {
        k = y[xNum - 2];
        index = xNum - 2;
    }
    else {
        for (i = 0; i < xNum; i++)
        {
            if (x0 < x[i])
                break;
        }
 
        index = i - 1;
        if (abs(x[index + 1] - x0) > abs(x0 - x[index])) {
            k = y[index - 1];
        }
        else {
            k = y[index];
        }
    }
    *y0 = k;
    return(index);
}