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