-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntrpl.c
55 lines (42 loc) · 1.04 KB
/
Intrpl.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Intrpl.h"
int linear_Intrpl(float *ip, int ipsz, float *op, int opsz){
int j;
float map;
int i;
float iI, iF;
op[0] = ip[0]; // Not in loop to avoid possible fp errors
for(j=1; j<opsz-1; j++){
map = ((float)j)*(ipsz-1)/(opsz-1);
iF = modff(map, &iI);
i = (int)iI;
op[j] = (1-iF)*ip[i] + iF*ip[i+1];
}
op[opsz-1] = ip[ipsz-1]; // Not in loop to avoid possible array out of bound
return 0;
}
int cubic_Intrpl(float *ip, int ipsz, float *op, int opsz){ // Cubic Hermite
int j;
float map;
int i;
float iI, iF;
op[0] = ip[0];
for(j=1; j<opsz-1; j++){
map = ((float)j)*(ipsz-1)/(opsz-1);
iF = modff(map, &iI);
i = (int)iI;
if(i==0 || i==ipsz-2) // Avoid index out of bound, switch to linear
op[j] = (1-iF)*ip[i] + iF*ip[i+1];
else
op[j] = 0.5*(
ip[i-1] *(-iF*iF*iF + 2*iF*iF -iF) +
ip[i] *(3*iF*iF*iF - 5*iF*iF + 2) +
ip[i+1] *(-3*iF*iF*iF + 4*iF*iF + iF) +
ip[i+2] *(iF*iF*iF - iF*iF)
);
}
op[opsz-1] = ip[ipsz-1];
return 0;
}