aboutsummaryrefslogtreecommitdiff
path: root/math/iminterp/ii_pc1deval.x
blob: 7eca530432f57ea82ee7a1307bdef174fe1d0a8b (plain) (blame)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<math.h>
include "im1interpdef.h"

# IA_PCPOLY3 -- Calculate the coefficients of a 3rd order polynomial.

procedure ia_pcpoly3 (x, datain, npts, pcoeff)

real	x		# x value
real	datain[ARB]	# array of input data
int	npts		# number of data points
real	pcoeff[ARB]	# array of polynomial coefficients

int 	i, k, nearx, nterms
real	temp[POLY3_ORDER]

begin
	nearx = x

	# Check for edge problems.
	k = 0
	for(i = nearx - 1; i <= nearx + 2; i = i + 1) {
	    k = k + 1

	    # project data points into temporary array
	    if (i < 1)
		temp[k] = 2. * datain[1] - datain[2-i]
	    else if (i > npts)
		temp[k] = 2. * datain[npts] - datain[2*npts-i]
	    else
		temp[k] = datain[i]
	}

	nterms = 4

	# Generate the difference table for Newton's form.
	do k = 1, nterms - 1
	    do i = 1, nterms - k
		temp[i] = (temp[i+1] - temp[i]) / k
	
	# Shift to generate polynomial coefficients.
	do k = nterms, 2, -1
	    do i = 2, k
		temp[i] = temp[i] + temp[i-1] * (k- i - nterms/2)
	do i = 1, nterms
	    pcoeff[i] = temp[nterms+1-i]
end


# IA_PCPOLY5 -- Calculate the coefficients of a fifth order polynomial.

procedure ia_pcpoly5 (x, datain, npts, pcoeff)

real	x		# x value
real	datain[ARB]	# array of input data
int	npts		# number of data points
real	pcoeff[ARB]	# array of polynomial coefficients

int 	i, k, nearx, nterms
real	temp[POLY5_ORDER]

begin
	nearx = x

	# Check for edge effects.
	k = 0
	for (i = nearx - 2; i <= nearx + 3; i = i + 1) {
	    k = k + 1
	    # project data points into temporary array
	    if (i < 1)
		temp[k] = 2. * datain[1] - datain[2-i]
	    else if (i > npts)
		temp[k] = 2. * datain[npts] - datain[2*npts-i]
	    else
		temp[k] = datain[i]
	}

	nterms = 6

	# Generate difference table for Newton's form.
	do k = 1, nterms - 1
	    do i = 1, nterms - k
		temp[i] = (temp[i+1] - temp[i]) / k
	
	# Shift to generate polynomial coefficients.
	do k = nterms, 2, -1
	    do i = 2, k
		temp[i] = temp[i] + temp[i-1] * (k - i - nterms/2)
	do i = 1, nterms
	    pcoeff[i] = temp[nterms+1-i]
end


# IA_PCSPLINE3 -- Calculate the derivatives of a cubic spline.

procedure ia_pcspline3 (x, datain, npts, pcoeff)

real	x		# x value
real	datain[ARB]	# data array
int	npts		# number of data points
real	pcoeff[ARB]	# array of polynomial coefficients

int 	i, k, nearx, px
real	temp[SPLPTS+3], bcoeff[SPLPTS+3]

begin
	nearx = x
	k = 0

	# Check for edge effects.
	for (i = nearx - SPLPTS/2 + 1; i <= nearx + SPLPTS/2; i = i + 1) {
	    if(i < 1 || i > npts)
		;
	    else {
		k = k + 1
		if (k == 1)
		    px = nearx - i + 1
		bcoeff[k+1] = datain[i]
	    }
	}

	bcoeff[1] = 0.
	bcoeff[k+2] = 0.

	# Use special routine for cardinal splines.
	call ii_spline (bcoeff, temp, k)

	px = px + 1
	bcoeff[k+3] = 0.

	# Calculate polynomial coefficients.
	pcoeff[1] = bcoeff[px-1] + 4. * bcoeff[px] + bcoeff[px+1]
	pcoeff[2] = 3. * (bcoeff[px+1] - bcoeff[px-1])
	pcoeff[3] = 3. * (bcoeff[px-1] - 2. * bcoeff[px] + bcoeff[px+1])
	pcoeff[4] = -bcoeff[px-1] + 3. * bcoeff[px] - 3. * bcoeff[px+1] +
				    bcoeff[px+2]
end


# II_SINCDER -- Evaluate derivatives of the sinc interpolator. If the
# function value only is needed call ii_sinc. This routine computes only
# the first two derivatives. The second  derivative is computed even if only
# the first derivative is needed. The sinc truncation length is nsinc.
# The taper is a cosbell function approximated by a quartic polynomial.
# The data value is returned if x is closer to x[i] than mindx.

procedure ii_sincder (x, der, nder, data, npix, nsinc, mindx)

real	x		# x value
real	der[ARB]	# derivatives to return
int	nder		# number of derivatives
real	data[npix]	# data to be interpolated
int	npix		# number of pixels
int	nsinc		# sinc truncation length
real	mindx		# interpolation minimum

int	i, j, xc
real	dx, w, a, d, z, sconst, a2, a4, dx2, taper
real	w1, w2, w3, u1, u2, u3, v1, v2, v3

begin
	# Return if no derivatives.
	if (nder == 0)
	    return

	# Set derivatives intially to zero.
	do i = 1, nder
	    der[i] = 0.

	# Return if outside data range.
	xc = nint (x)
	if (xc < 1 || xc > npix)
	    return

	# Call ii_sinc if only the function value is needed.
	if (nder == 1) {
	    call ii_sinc (x, der, 1, data, npix, nsinc, mindx)
	    return
	}

        # Compute the constants for the cosine bell taper approximation.
        sconst = (HALFPI / nsinc) ** 2
        a2 = -0.49670
        a4 = 0.03705

	# Compute the derivatives by doing the required convolutions.
	dx = x - xc
	if (abs (dx) < mindx) {

	    w = 1.
	    d = data[xc]
	    w1 = 1.; u1 = d * w1; v1 = w1
	    w2 = 0.; u2 = 0.; v2 = 0.
	    w3 = -1./3.; u3 = d * w3; v3 = w3

	    # Derivative at the center of a pixel.
	    do i = 1, nsinc {

		w = -w
		dx2 = sconst * i * i
		taper = (1.0 + a2 * dx2 + a4 * dx2 * dx2) ** 2

		j = xc - i
		z = 1. / i
		if (j >= 1)
		    d = data[j]
		else
		    d = data[1]
		w2 = w * z * taper
		u2 = u2 + d * w2
		v2 = v2 + w2
		w3 = -2 * w2 * z
		u3 = u3 + d * w3
		v3 = v3 + w3

		j = xc + i
		if (j <= npix)
		    d = data[j]
		else
		    d = data[npix]
		w2 = -w * z * taper
		u2 = u2 + d * w2
		v2 = v2 + w2
		w3 = 2 * w2 * z
		u3 = u3 + d * w3
		v3 = v3 + w3
	    }

	} else {

	    w = 1.0
	    a = 1 / tan (PI * dx)

	    d = data[xc]
	    z = 1. / dx
	    w1 = w * z; u1 = d * w1; v1 = w1
	    w2 = w1 * (a - z); u2 = d * w2; v2 = w2
	    w3 = -w1 * (1 + 2 * z * (a - z)); u3 = d * w3; v3 = w3

	    # Derivative off center of a pixel.
	    do i = 1, nsinc {

		w = -w
		dx2 = sconst * i * i
		taper = (1.0 + a2 * dx2 + a4 * dx2 * dx2) ** 2

		j = xc - i
		if (j >= 1)
		    d = data[j]
		else
		    d = data[1]
		z = 1. / (dx + i)
		w1 = w * z * taper
		u1 = u1 + d * w1
		v1 = v1 + w1
		w2 = w1 * (a - z)
		u2 = u2 + d * w2
		v2 = v2 + w2
		w3 = -w1 * (1 + 2*z*(a-z))
		u3 = u3 + d * w3
		v3 = v3 + w3

		j = xc + i
		if (j <= npix)
		    d = data[j]
		else
		    d = data[npix]
		z = 1. / (dx - i)
		w1 = w * z * taper
		u1 = u1 + d * w1
		v1 = v1 + w1
		w2 = w1 * (a - z)
		u2 = u2 + d * w2
		v2 = v2 + w2
		w3 = -w1 * (1 + 2*z*(a-z))
		u3 = u3 + d * w3
		v3 = v3 + w3
	    }
	}

	# Compute the derivatives.
	w1 = v1
	w2 = v1 * w1
	w3 = v1 * w2
	der[1] = u1 / w1
	if (nder > 1)
	    der[2] = (u2 * v1 - u1 * v2) / w2
	if (nder > 2)
	    der[3] = u3 / w1 - 2*u2*v2 / w2 + 2*u1*v2*v2 / w3 - u1*v3 / w2
end