aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/lib/rgfft.x
blob: b986a9d74ba09141288428e30eac3dd100ccce23 (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

# RG_SZFFT -- Compute the size of the required FFT given the dimension of the
# image the window size and the fact that the FFT must be a power of 2.

int procedure rg_szfft (npts, window)

int	npts			#I the number of points in the data
int	window			#I the width of the valid cross correlation

int	nfft, pow2

begin
	nfft = npts + window / 2

	pow2 = 2
	while (pow2 < nfft)
	    pow2 = pow2 * 2

	return (pow2)
end


# RG_RLOAD -- Procedure to load a real array into the real part of a complex
# array.

procedure rg_rload (buf, ncols, nlines, fft, nxfft, nyfft)

real    buf[ARB]        	#I the input data buffer
int     ncols, nlines   	#I the size of the input buffer
real    fft[ARB]        	#O the out array to be fft'd
int     nxfft, nyfft    	#I the dimensions of the fft

int     i, dindex, findex

begin
        # Load the reference and image data.
        dindex = 1
        findex = 1
        do i = 1, nlines {
            call rg_rweave (buf[dindex], fft[findex], ncols)
            dindex = dindex + ncols
            findex = findex + 2 * nxfft
        }
end


# RG_ILOAD -- Procedure to load a real array into the complex part of a complex
# array.

procedure rg_iload (buf, ncols, nlines, fft, nxfft, nyfft)

real    buf[ARB]        	#I the input data buffer
int     ncols, nlines   	#I the size of the input buffer
real    fft[ARB]        	#O the output array to be fft'd
int     nxfft, nyfft    	#I the dimensions of the fft

int     i, dindex, findex

begin
        # Load the reference and image data.
        dindex = 1
        findex = 1
        do i = 1, nlines {
            call rg_iweave (buf[dindex], fft[findex], ncols)
            dindex = dindex + ncols
            findex = findex + 2 * nxfft
        }
end


# RG_RWEAVE -- Weave a real array into the real part of a complex array.
# The output array must be twice as long as the input array.

procedure rg_rweave (a, b, npts)

real    a[ARB]          	#I input array
real    b[ARB]          	#O output array
int     npts            	#I the number of data points

int     i

begin
        do i = 1, npts
            b[2*i-1] = a[i]
end


# RG_IWEAVE -- Weave a real array into the complex part of a complex array.
# The output array must be twice as long as the input array.

procedure rg_iweave (a, b, npts)

real    a[ARB]          	#I the input array
real    b[ARB]          	#O the output array
int     npts            	#I the number of data points

int     i

begin
        do i = 1, npts
            b[2*i] = a[i]
end


# RG_FOURN -- Replaces datas by its n-dimensional discreter Fourier transform,
# if isign is input as 1. NN is an integer array of length ndim containing
# the lengths of each dimension (number of complex values), which must all
# be powers of 2. Data is a real array of length twice the product of these
# lengths, in which the data are stored as in a multidimensional complex
# Fortran array. If isign is input as -1, data is replaced by its inverse
# transform times the product of the lengths of all dimensions.

procedure rg_fourn (data, nn, ndim, isign)

real    data[ARB]               #I/O input data and output fft
int     nn[ndim]                #I array of dimension lengths
int     ndim                    #I number of dimensions
int     isign                   #I forward or inverse transform

int     idim, i1, i2, i3, ip1, ip2, ip3, ifp1, ifp2, i2rev, i3rev, k1, k2
int     ntot, nprev, n, nrem, pibit
double  wr, wi, wpr, wpi, wtemp, theta
real    tempr, tempi

begin
        ntot = 1
        do idim = 1, ndim
            ntot = ntot * nn[idim]

        nprev = 1
        do idim = 1, ndim {

            n = nn[idim]
            nrem = ntot / (n * nprev)
            ip1 = 2 * nprev
            ip2 = ip1 * n
            ip3 = ip2 * nrem
            i2rev = 1

	    do i2 = 1, ip2, ip1 {

                if (i2 < i2rev) {
                    do i1 = i2, i2 + ip1 - 2, 2 {
                        do i3 = i1, ip3, ip2 {
                            i3rev = i2rev + i3 - i2
                            tempr = data [i3]
                            tempi = data[i3+1]
                            data[i3] = data[i3rev]
                            data[i3+1] = data[i3rev+1]
                            data[i3rev] = tempr
                            data[i3rev+1] = tempi
                        }
                    }
                }

                pibit = ip2 / 2
                while ((pibit >= ip1) && (i2rev > pibit)) {
                    i2rev = i2rev - pibit
                    pibit = pibit / 2
                }

                i2rev = i2rev + pibit
            }

    	    ifp1 = ip1
            while (ifp1 < ip2) {

                ifp2 = 2 * ifp1
                theta = isign * 6.28318530717959d0 / (ifp2 / ip1)
                wpr = - 2.0d0 * dsin (0.5d0 * theta) ** 2
                wpi = dsin (theta)
                wr = 1.0d0
                wi = 0.0d0

                do i3 = 1, ifp1, ip1 {
                    do i1 = i3, i3 + ip1 - 2, 2 {
                        do i2 = i1, ip3, ifp2 {
                            k1 = i2
                            k2 = k1 + ifp1
                            tempr = sngl (wr) * data[k2] - sngl (wi) *
                                data[k2+1]
                            tempi = sngl (wr) * data[k2+1] + sngl (wi) *
                                data[k2]
                            data[k2] = data[k1] - tempr
                            data[k2+1] = data[k1+1] - tempi
                            data[k1] = data[k1] + tempr
                            data[k1+1] = data[k1+1] + tempi
                        }
                    }
                    wtemp = wr
                    wr = wr * wpr - wi * wpi + wr
                    wi = wi * wpr + wtemp * wpi + wi
                }

                ifp1 = ifp2
            }
            nprev = n * nprev
        }
end


# RG_FSHIFT -- Center the array after doing the FFT.

procedure rg_fshift (fft1, fft2, nx, ny)

real    fft1[nx,ARB]            #I input fft array
real    fft2[nx,ARB]            #O output fft array
int     nx, ny                  #I fft array dimensions

int     i, j
real    fac

begin
        fac = 1.0
        do j = 1, ny {
            do i = 1, nx, 2 {
                fft2[i,j] = fac * fft1[i,j]
                fft2[i+1,j] = fac * fft1[i+1,j]
                fac = -fac
            }
            fac = -fac
        }
end


# RG_MOVEXR -- Extract the portion of the FFT for which the computed lags
# are valid. The dimensions of the the FFT are a  power of two
# and the 0 frequency is in the position nxfft / 2 + 1, nyfft / 2 + 1.

procedure rg_movexr (fft, nxfft, nyfft, xcor, xwindow, ywindow)

real    fft[ARB]                #I the input fft
int     nxfft, nyfft            #I the dimensions of the input fft
real    xcor[ARB]   	        #O the output cross-correlation function
int     xwindow, ywindow        #I the cross-correlation function window

int     j, ix, iy, findex, xindex

begin
        # Compute the starting index of the extraction array.
        ix = 1 + nxfft  - 2 * (xwindow / 2)
        iy = 1 + nyfft / 2 - ywindow / 2

        # Copy the real part of the Fourier transform into the
        # cross-correlation array.
        findex = ix + 2 * nxfft * (iy - 1)
	xindex = 1
        do j = 1, ywindow {
            call rg_extract (fft[findex], xcor[xindex], xwindow)
            findex = findex + 2 * nxfft
	    xindex = xindex + xwindow
        }
end


# RG_EXTRACT -- Extract the real part of a complex array.

procedure rg_extract (a, b, npts)

real    a[ARB]          #I the input array
real    b[ARB]          #O the output array
int     npts            #I the number of data points

int     i

begin
        do i = 1, npts
            b[i] = a[2*i-1]
end