aboutsummaryrefslogtreecommitdiff
path: root/noao/rv/fftutil.x
blob: b69166ead4e24c31c05829da8b7eb1a01e4f0a72 (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
include <math.h>
include "rvpackage.h"
include "rvflags.h"
include "rvplots.h"

# GET_FFT - Take the input real array and return the absolute value of it's 
# DFT.  The array rfft must be sized to at least to the power of two greater 
# than npts.

procedure get_fft (rv, rinpt, npts, rfft, fnpts)

pointer	rv				#I RV struct pointer
real	rinpt[ARB]			#I Input real array
int	npts				#I No. pts in rinpt
real	rfft[ARB]			#O Output abs value of DFT
int	fnpts				#O No. pts in output array.

pointer	sp, tp, cpr, cpi, fft
int	i, last, ishift
real	xtmp, cx_abs()

begin
	fnpts = RV_FFTNPTS (rv)			# Get FFT size

	call smark (sp)
	call salloc (tp, fnpts, TY_REAL)	# Allocate temp vector
	call salloc (cpr, fnpts, TY_REAL)	# Allocate temp vector
	call salloc (cpi, fnpts, TY_REAL)
	call salloc (fft, 2*fnpts, TY_REAL)
	call aclrr (Memr[tp], fnpts)
	call aclrr (Memr[cpr], fnpts)
	call aclrr (Memr[cpi], fnpts)
	call amovr (rinpt, Memr[tp], npts)
	
	# Do forward transform
	ishift = 0
	if (RV_WHERE(rv) == TOP) {
	    call prep_spec (rv, RV_OSAMPLE(rv), npts, fnpts, RV_NPTS(rv), 
		tp, cpr, ishift, YES)
	} else {
	    call prep_spec (rv, RV_RSAMPLE(rv), npts, fnpts, RV_RNPTS(rv), 
		tp, cpr, ishift, YES)
	}
	call afftrr (Memr[cpr], Memr[cpi], Memr[cpr], Memr[cpi], fnpts)	
	if (RVP_WHEN(rv) == AFTER) {
	    if (RV_WHERE(rv) == TOP) {
		if (RV_FILTER(rv) == OBJ_ONLY || RV_FILTER(rv) == BOTH) {
	            call cx_pak (Memr[cpr], Memr[cpi], Memr[fft], fnpts/2)
	            call rv_filter (rv, Memr[fft], fnpts/2)
	            call cx_unpak (Memr[fft], Memr[cpr], Memr[cpi], fnpts)
		}
	    } else {
		if (RV_FILTER(rv) == TEMP_ONLY || RV_FILTER(rv) == BOTH) {
	            call cx_pak (Memr[cpr], Memr[cpi], Memr[fft], fnpts/2)
	            call rv_filter (rv, Memr[fft], fnpts/2)
	            call cx_unpak (Memr[fft], Memr[cpr], Memr[cpi], fnpts)
		}
	    }
	}

	# Get the absolute value of the transform
	last = fnpts / 2 + 1
	do i = 2, last {
	    xtmp = cx_abs (Memr[cpr+i-1], Memr[cpi+i-1])
	    if (RVP_LOG_SCALE(rv) == YES) {
	        if (xtmp != 0.0) 		# Divide by zero check in log
	            rfft[i-1] = log10 (xtmp)
		else 
		    rfft[i-1] = 0.0
	    } else 
		rfft[i-1] = xtmp
	}

	call sfree (sp)
end


# GET_PSPEC - Take the input real array and return the log of the power 
# spectrum.  The array pspec must be sized to at least to the power of two 
# greater than npts.

procedure get_pspec (rv, rinpt, npts, pspec, fnpts)

pointer	rv				#I RV struct pointer
real	rinpt[ARB]			#I Input real array
int	npts				#I No. pts in rinpt
real	pspec[ARB]			#O Output abs value of DFT
int	fnpts				#O No. pts in output array.

pointer	sp, tp, cpr, cpi, fft
int	i, j, ishift
real	xtmp

begin
	fnpts = RV_FFTNPTS (rv)			# Get FFT size

	call smark (sp)
	call salloc (tp, fnpts, TY_REAL)	# Allocate temp vector
	call salloc (cpr, fnpts, TY_REAL)	# Allocate temp vector
	call salloc (cpi, fnpts, TY_REAL)	# Allocate temp vector
	call salloc (fft, 2*fnpts, TY_REAL)	# Allocate temp vector
	call aclrr (Memr[tp], fnpts)
	call aclrr (Memr[cpr], fnpts)
	call aclrr (Memr[cpi], fnpts)
	call amovr (rinpt, Memr[tp], npts)
	
	# Do forward transform
	ishift = 0
	if (RV_WHERE(rv) == TOP) {
	    call prep_spec (rv, RV_OSAMPLE(rv), npts, fnpts, RV_NPTS(rv), 
		tp, cpr, ishift, YES)
	} else {
	    call prep_spec (rv, RV_RSAMPLE(rv), npts, fnpts, RV_RNPTS(rv), 
		tp, cpr, ishift, YES)
	}
	call afftrr (Memr[cpr], Memr[cpi], Memr[cpr], Memr[cpi], fnpts)	
	if (RVP_WHEN(rv) == AFTER) {
	    if (RV_WHERE(rv) == TOP) {
		if (RV_FILTER(rv) == OBJ_ONLY || RV_FILTER(rv) == BOTH) {
	            call cx_pak (Memr[cpr], Memr[cpi], Memr[fft], fnpts/2)
	            call rv_filter (rv, Memr[fft], fnpts/2)
	            call cx_unpak (Memr[fft], Memr[cpr], Memr[cpi], fnpts)
		}
	    } else {
		if (RV_FILTER(rv) == TEMP_ONLY || RV_FILTER(rv) == BOTH) {
	            call cx_pak (Memr[cpr], Memr[cpi], Memr[fft], fnpts/2)
	            call rv_filter (rv, Memr[fft], fnpts/2)
	            call cx_unpak (Memr[fft], Memr[cpr], Memr[cpi], fnpts)
		}
	    }
        } 

	# Get the power spectrum
	j = fnpts / 2 + 1
	do i = 2, j {
	    xtmp = (Memr[cpr+i-1]*Memr[cpr+i-1]) + (Memr[cpi+i-1]*Memr[cpi+i-1])
	    if (RVP_LOG_SCALE(rv) == YES) {
		if (xtmp != 0.0)
	            pspec[i-1] = log10 (xtmp)
		else
		    pspec[i-1] = 0.0 
	     } else 
		pspec[i-1] = xtmp
	}

	call sfree (sp)
end


# FFT_COSBEL - Apply a cosine bell to the data.

procedure fft_cosbel (v, npts, code, percent)

real	v[ARB]				#U Data vector
int	npts				#I Number of data points
int 	code				#I Direction code 
					     # <0 for forward,  >=0 for reverse
real	percent				#I percent of end to mask

int 	ndpercent, index, i
real	f

begin
	if (IS_INDEF(percent))
	    return

	ndpercent = int (npts * percent) 	# Compute no. of endpoints

	if (code < 0) {				# Forward application of window
	    do i = 1,ndpercent {
	        f = (1.0 - cos(PI*real(i-1)/real(ndpercent))) / 2.0
	        index = npts - i + 1
	        v[i] =  f * v[i]
	        v[index] =  f * v[index]
	    }
	} else if (code >= 0) {			# Reverse application of window
	    do i = 1,ndpercent {
	        f = (1.0 - cos(PI*real(i-1)/real(ndpercent))) / 2.0
	        if (abs(f) < 1.0e-3) 
		    f = 1.0e-3
	        index = npts - i + 1
	        v[i] = v[i] / f
	        v[index] = v[index] / f
	    }
	}
end


# FFT_FIXWRAP - Fix the wrap around ordering that results from the Numerical
# Recipes FFT routines.  Re-ordering is done such that points 1 to N/2 are
# switched in the array with points N/2+1 to N.  Re-ordering is done in-place.

procedure fft_fixwrap (v, npts)

real	v[ARB]				#U Data array in wrap around order
int	npts				#I length of data array

real 	temp
int 	i

begin
	do i = 1, npts/2 {
	    temp = v[i]
	    v[i] = v[i+npts/2]
	    v[i+npts/2] = temp
	}
end


# FFT_POW2 - Find the power of two that is greater than N.
# Returns INDEFI if a power can't be found less than 2^15.

int procedure fft_pow2 (N)
int	N	    	    		#I Number of data points

int 	i, nn

begin
	nn = 0
	do i = 1, 31 {
	    nn = 2 ** i
	    if (nn >= N) 	    	# return 2**i > N
		return (nn)
	}

	return (INDEFI)
end