-
Notifications
You must be signed in to change notification settings - Fork 0
/
spline2.c
196 lines (164 loc) · 3.79 KB
/
spline2.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Author: Francois Rigaut
*
* Copyright (c) 2003-2007, Francois Rigaut
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details (to receive a copy of the GNU
* General Public License, write to the Free Software Foundation, Inc., 675
* Mass Ave, Cambridge, MA 02139, USA).
*
*
*/
#include <stdio.h>
#include "ydata.h"
#include "pstdlib.h"
void _splint(float *xa,
float *ya,
float *y2a,
long n,
float x,
float *y)
{
long klo,khi,k;
float h,b,a;
klo=0;
khi=n-1;
while (khi-klo > 1) {
k=(khi+klo) >> 1;
if (xa[k] > x) khi=k;
else klo=k;
}
h=xa[khi]-xa[klo];
if (h == 0.0) YError("Bad xa input to routine _splint");
a=(xa[khi]-x)/h;
b=(x-xa[klo])/h;
*y=a*ya[klo]+b*ya[khi]+((a*a*a-a)*y2a[klo]+(b*b*b-b)*y2a[khi])*(h*h)/6.0;
}
void _splinf(float *x,
float *y,
long n,
float *y2)
{
long i,k;
float p,qn,sig,un,*u;
u = p_malloc(sizeof(float)*(n-1));
y2[0]=u[0]=qn=un=0.0;
for (i=1;i<=n-2;i++) {
sig=(x[i]-x[i-1])/(x[i+1]-x[i-1]);
p=sig*y2[i-1]+2.0;
y2[i]=(sig-1.0)/p;
u[i]=(y[i+1]-y[i])/(x[i+1]-x[i]) - (y[i]-y[i-1])/(x[i]-x[i-1]);
u[i]=(6.0*u[i]/(x[i+1]-x[i-1])-sig*u[i-1])/p;
}
y2[n-1]=(un-qn*u[n-2])/(qn*y2[n-2]+1.0);
for (k=n-2;k>=0;k--) y2[k]=y2[k]*y2[k+1]+u[k];
p_free(u);
}
void
_splin2(float *xin,
float *yin,
float *image,
float *deriv,
long nx,
long ny,
long *nvalidx,
float xout,
float yout,
float *res)
{
long j,m;
long n=0;
float *y2tmp,*yytmp;
y2tmp = p_malloc(sizeof(float)*ny);
yytmp = p_malloc(sizeof(float)*ny);
for (j=0;j<=ny-1;j++) {
m = nvalidx[j];
_splint(&xin[n],&image[n],&deriv[n],m,xout,&yytmp[j]);
n += m;
}
_splinf(yin,yytmp,ny,y2tmp);
_splint(yin,yytmp,y2tmp,ny,yout,res);
p_free(y2tmp);
p_free(yytmp);
}
void
_splie2( float *xin,
float *im,
long nx,
long ny, \
float *deriv,
long *nvalidx)
/* updated for XY faster indice */
{
long j,m;
long n=0;
for (j=0;j<=ny-1;j++) {
m = nvalidx[j];
_splinf(&xin[n],&im[n],m,&deriv[n]);
n += m;
}
}
void
_spline2( float *xin,
float *yin,
float *im,
float *deriv,
long nx,
long ny, \
float *xout,
float *yout,
long npt,
long *nvalidx,
float *res)
{
long i;
for (i=0;i<=npt;i++) _splin2(xin,yin,im,deriv,nx,ny,nvalidx, \
xout[i],yout[i],&res[i]);
}
void
_spline2grid( float *xin,
float *yin,
float *im,
float *deriv,
long nx,
long ny,
float *xout,
float *yout,
long nxout,
long nyout,
long *nvalidx,
float *res)
/* checked indices. runs good and fast. */
{
long j,ii,jj;
float *y2tmp,*yytmp;
long n;
long m;
y2tmp = p_malloc(sizeof(float)*ny);
yytmp = p_malloc(sizeof(float)*ny);
for (ii=0;ii<=nxout-1;ii++) {/* loop on out x */
n=0;
/* fill Y vector for xout(ii) */
for (j=0;j<=ny-1;j++) {
m = nvalidx[j];
_splint(&xin[n],&im[n],&deriv[n],m,xout[ii],&yytmp[j]);
n += m;
}
/* find second derivative */
_splinf(yin,yytmp,ny,y2tmp);
/* find and fill interpolated out Y vector for this xout(ii) */
for (jj=0;jj<=nyout-1;jj++) {
_splint(yin,yytmp,y2tmp,ny,yout[jj],&res[jj*nxout+ii]);
}
}
p_free(y2tmp);
p_free(yytmp);
}