-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFFTMod.f90
483 lines (325 loc) · 16.6 KB
/
FFTMod.f90
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
! NOTE: This MODULE is virtually a copy of MODULE FFT_Module() from TurbSim.
! A few unused things have been eliminated (indicated by "!remove")
! and a little bit of code has been added where noted (indicated by
! "NEW").
! BJJ: 02/22/2008: Updated to work with NWTC_Library v1.01.09
! all Abort() functions changed to ProgAbort()
!=======================================================================
MODULE FFT_Module
!-----------------------------------------------------------------------
! DESCRIPTION OF THE INVERSE FOURIER TRANSFORM ROUTINE:
!
! Given an array, H, of N complex numbers, calculate an array, h, of N real
! numbers:
! h(J) = the sum from K=1,...,N of [ H(K)*exp(i*(J-1)*(K-1)*2*pi/N) ]
! for J = 1,...,N
!
! where:
! i=sqrt(-1)
!
! In order for h to be real, the real components of H must be an even function
! of frequency and the imaginary components of H must be an odd function of
! frequency. Thus, only the first N/2 + 1 values of H are unique. (The first
! N/2 + 1 are the positive frequencies including zero; the last N/2 - 1 values
! are the negative frequencies.)
!
! We accomplish this by defining a real array, R, where:
! R(1) = REAL( H(1) ),
! R(2) = REAL( H(2) ), R(3) = IMAG( H(2) ),
! R(4) = REAL( H(3) ), R(5) = IMAG( H(3) ),...
! R(N) = REAL( H(N/2 + 1) ).
! Note that the values of IMAG( H(1) ) and IMAG( H(N/2 + 1) ) must be zero for
! the result to be real, else the routine will abort.
!
! We return the array, r = h, of real numbers as calculated by:
! h(J) = r(J) = R(1) + (-1)**(J-1)*R(N)
! + the sum from K=2,...,N/2 of
! [ 2*R(2*K-2)*COS((K-1)*(J-1)*2*PI/N)
! -2*R(2*K-1)*SIN((K-1)*(J-1)*2*PI/N) ]
! for J = 1,...,N, where N is an even number
!
! The routine is most effecient when N is a product of small primes.
!
! If the Normalization flag is set to "TRUE" in the initialization, we
! normalize the result by 1/N.
!------------------------------------------------------------------------
! DESCRIPTION OF THE COSINE TRANSFORM ROUTINE:
!
! Given an array, X, of N real numbers, calculate an array, x, of N real
! numbers:
! x(J) = X(1) + (-1)**(J-1)*X(N)
! + the sum from K=2,...,N-1 of [ 2*X(K)*COS((K-1)*(J-1)*PI/(N-1)) ]
! for J = 1,...,N, where N is an odd number
!
! The routine is most effecient when N-1 is a product of small primes.
!
! If the Normalization flag is set to "TRUE" in the initialization, we
! normalize the result by 1/(N-1).
!------------------------------------------------------------------------
! DESCRIPTION OF THE SINE TRANSFORM ROUTINE:
!
! Given an array, X, of N real numbers, calculate an array, x, of N real
! numbers:
! x(1) = X(1) = 0
! x(J) = the sum from K=2,...,N-1 of [ 2*X(K)*SIN((K-1)*(J-1)*PI/(N-1)) ]
! for J = 2,...,N-1, where N is an odd number
! x(N) = X(N) = 0
!
! Note that the values of X(1) and X(N) must be zero, else the routine will
! abort.
!
! The routine is most effecient when N-1 is a product of small primes.
!
! If the Normalization flag is set to "TRUE" in the initialization, we
! normalize the result by 1/(N-1).
!------------------------------------------------------------------------
!remove ! This module is NEW:
!remove USE Precision
! We need the Precision module and the Abort() and
! Int2LStr() functions from the NWTC_IO module.
USE NWTC_Library
IMPLICIT NONE
REAL(ReKi), PRIVATE, SAVE :: InvN ! Normization constant
REAL(ReKi), ALLOCATABLE, PRIVATE, SAVE :: wSave(:) ! Working array for performing transforms
INTEGER, PRIVATE, SAVE :: N ! Number of steps
LOGICAL, PRIVATE, SAVE :: Normalize ! Whether or not to normalize
!------------------------------------------------------------------------
CONTAINS
SUBROUTINE ApplyCOST( TRH )
! Perform cosine transform.
IMPLICIT NONE
REAL(ReKi), INTENT(INOUT) :: TRH(:)
! Make sure the array isn't too small
IF ( SIZE(TRH) < N ) &
CALL ProgAbort( 'Error in call to cosine transform. Array size is not large enough.' )
! Perform the cosine transform with a FFTpack routine
CALL COST(N, TRH, wSave) ! FFTpack routine
IF (Normalize) THEN
TRH(1:N) = InvN * TRH(1:N)
ENDIF
END SUBROUTINE ApplyCOST
!------------------------------------------------------------------------
SUBROUTINE ApplyFFT( TRH )
! Perform Backward FFT: given TRH, a REAL array representing complex numbers,
! return an array TRH, of real numbers.
! CALL FOURTH ( TRH, NumSteps, 1, WorkT, NumSteps+2 ) ! Sandia
IMPLICIT NONE
REAL(ReKi), INTENT(INOUT) :: TRH(:)
! Make sure the array isn't too small
IF ( SIZE(TRH) < N ) &
CALL ProgAbort( 'Error in call to FFT. Array size is not large enough.' )
! Perform the FFT with a FFTpack routine
CALL RFFTB(N, TRH, wSave) ! FFTpack routine
IF (Normalize) THEN
TRH(1:N) = InvN * TRH(1:N)
ENDIF
END SUBROUTINE ApplyFFT
!------------------------------------------------------------------------
SUBROUTINE ApplyFFT_cx( TRH, TRH_complex )
! Perform Backward FFT: given TRH, a REAL array representing complex numbers,
! return an array TRH, of real numbers.
IMPLICIT NONE
REAL(ReKi), INTENT(OUT) :: TRH(:)
COMPLEX(ReKi), INTENT(IN) :: TRH_complex(:)
INTEGER :: I
INTEGER :: Indx
! Make sure the arrays aren't too small
IF ( ( SIZE(TRH) < N ) .OR. ( SIZE(TRH_complex) < ( N/2 + 1 ) ) ) &
CALL ProgAbort( 'Error in call to FFT. Array size is not large enough.' )
! Make sure that the imaginary components at the zeroeth and largest
! positive frequency are zero, else abort.
IF ( AIMAG( TRH_complex(1 ) ) /= 0.0 ) &
CALL ProgAbort( 'Error in call to FFT. The imaginary component at the zeroeth frequency must be zero.' )
IF ( AIMAG( TRH_complex(N/2+1) ) /= 0.0 ) &
CALL ProgAbort( 'Error in call to FFT. The imaginary component at the largest positive frequency must be zero.' )
! Initialize the TRH array with Complex numbers
TRH(1) = REAL( TRH_complex(1 ) )
Indx = 1
DO I=2,N-2, 2
Indx = Indx + 1 ! I/2 + 1
TRH(I) = REAL( TRH_complex(Indx) )
TRH(I+1) = AIMAG( TRH_complex(Indx) )
ENDDO
TRH(N) = REAL( TRH_complex(N/2+1) )
! Perform the FFT with a FFTpack routine
CALL RFFTB(N, TRH, wSave)
IF (Normalize) THEN
TRH(1:N) = InvN * TRH(1:N)
ENDIF
END SUBROUTINE ApplyFFT_cx
!------------------------------------------------------------------------
SUBROUTINE ApplySINT( TRH )
! Perform sine transform.
IMPLICIT NONE
REAL(ReKi), INTENT(INOUT) :: TRH(:)
! Make sure the array isn't too small
IF ( SIZE(TRH) < N ) &
CALL ProgAbort( 'Error in call to sine transform. Array size is not large enough.' )
! Make sure that the value at the zeroeth and largest positive
! frequency are zero, else abort.
IF ( TRH(1) /= 0.0 ) &
CALL ProgAbort( 'Error in call to FFT. The value at the zeroeth frequency must be zero.' )
IF ( TRH(N) /= 0.0 ) &
CALL ProgAbort( 'Error in call to FFT. The value at the largest positive frequency must be zero.' )
! Perform the sine transform with a FFTpack routine
CALL SINT(N-2, TRH(2:N-1), wSave) ! FFTpack routine
IF (Normalize) THEN
TRH(1:N) = InvN * TRH(1:N)
ENDIF
END SUBROUTINE ApplySINT
!------------------------------------------------------------------------
SUBROUTINE ExitCOST
! This subroutine cleans up the cosine transform working space
IF ( ALLOCATED (wSave) ) DEALLOCATE( wSave )
END SUBROUTINE ExitCOST
!------------------------------------------------------------------------
SUBROUTINE ExitFFT
! This subroutine cleans up the backward FFT working space
IF ( ALLOCATED (wSave) ) DEALLOCATE( wSave )
END SUBROUTINE ExitFFT
!------------------------------------------------------------------------
SUBROUTINE ExitSINT
! This subroutine cleans up the sine transform working space
IF ( ALLOCATED (wSave) ) DEALLOCATE( wSave )
END SUBROUTINE ExitSINT
!------------------------------------------------------------------------
SUBROUTINE InitCOST( NumSteps, NormalizeIn )
! This subroutine initializes the cosine transform working space
IMPLICIT NONE
INTEGER, INTENT(IN) :: NumSteps ! Number of steps in the array
INTEGER :: Sttus ! Array allocation status
LOGICAL, INTENT(IN), OPTIONAL :: NormalizeIn ! Whether or not to normalize
! Number of timesteps in the time series returned from the cosine transform
! N should be odd:
N = NumSteps
IF ( MOD(N,2) /= 1 ) THEN
CALL ProgAbort ( 'The number of steps in the cosine transform must be odd' )
ENDIF
! Determine if we should normalize the cosine transform:
IF ( PRESENT( NormalizeIn ) ) THEN
Normalize = NormalizeIn
InvN = 1. / ( N - 1 )
ELSE
Normalize = .FALSE.
ENDIF
! According to FFTPACK documentation, the working array must be at
! least size 3N+15
ALLOCATE ( wSave(3*N + 15) , STAT=Sttus )
IF ( Sttus /= 0 ) THEN
CALL ProgAbort ( 'Error allocating memory for the cosine transform working array.' )
ENDIF
! Initialize the FFTPACK working space
CALL COSTI(N, wSave)
END SUBROUTINE InitCOST
!------------------------------------------------------------------------
SUBROUTINE InitFFT( NumSteps, NormalizeIn )
! This subroutine initializes the backward FFT working space
IMPLICIT NONE
INTEGER, INTENT(IN) :: NumSteps ! Number of steps in the array
INTEGER :: Sttus ! Array allocation status
LOGICAL, INTENT(IN), OPTIONAL :: NormalizeIn ! Whether or not to normalize the FFT
! Number of timesteps in the time series returned from the backward FFT
! N should be even:
N = NumSteps
IF ( MOD(N,2) /= 0 ) THEN
CALL ProgAbort ( 'The number of steps in the FFT must be even' ) ! For this Real FFT
ENDIF
! Determine if we should normalize the FFT
IF ( PRESENT( NormalizeIn ) ) THEN
Normalize = NormalizeIn
InvN = 1. / N
ELSE
Normalize = .FALSE.
ENDIF
! According to FFTPACK documentation, the working array must be at
! least size 2N+15
ALLOCATE ( wSave(2*N + 15) , STAT=Sttus )
IF ( Sttus /= 0 ) THEN
CALL ProgAbort ( 'Error allocating memory for the FFT working array.' )
ENDIF
! Initialize the FFTPACK working space
CALL RFFTI(N, wSave)
END SUBROUTINE InitFFT
!------------------------------------------------------------------------
SUBROUTINE InitSINT( NumSteps, NormalizeIn )
! This subroutine initializes the sine transform working space
IMPLICIT NONE
INTEGER, INTENT(IN) :: NumSteps ! Number of steps in the array
INTEGER :: Sttus ! Array allocation status
LOGICAL, INTENT(IN), OPTIONAL :: NormalizeIn ! Whether or not to normalize
! Number of timesteps in the time series returned from the sine transform
! N should be odd:
N = NumSteps
IF ( MOD(N,2) /= 1 ) THEN
CALL ProgAbort ( 'The number of steps in the sine transform must be odd' )
ENDIF
! Determine if we should normalize the sine transform:
IF ( PRESENT( NormalizeIn ) ) THEN
Normalize = NormalizeIn
InvN = 1. / ( N - 1 )
ELSE
Normalize = .FALSE.
ENDIF
! According to FFTPACK documentation, the working array must be at
! least size 2.5N+15; however, our N is +2 greater than their N
ALLOCATE ( wSave( CEILING( 2.5*(N-2) ) + 15 ) , STAT=Sttus )
IF ( Sttus /= 0 ) THEN
CALL ProgAbort ( 'Error allocating memory for the sine transform working array.' )
ENDIF
! Initialize the FFTPACK working space
CALL SINTI(N-2, wSave)
END SUBROUTINE InitSINT
!------------------------------------------------------------------------
FUNCTION PSF ( Npsf , NumPrimes )
! This routine factors the number N into its primes. If any of those
! prime factors is greater than the NumPrimes'th prime, a point is added to N
! and the new number is factored. This process is repeated until no
! prime factors are greater than the NumPrimes'th prime.
IMPLICIT NONE
!Passed variables
INTEGER, INTENT(IN) :: Npsf ! Initial number we're trying to factor.
INTEGER, INTENT(IN) :: NumPrimes ! Number of unique primes.
INTEGER :: PSF ! The smallest number at least as large as Npsf, that is the product of small factors when we return.
!Other variables
INTEGER :: IPR ! A counter for the NPrime array
INTEGER, PARAMETER :: NFact = 9 ! The number of prime numbers (the first NFact primes)
INTEGER :: NP ! A temp variable to determine if NPr divides NTR
INTEGER :: NPr ! A small prime number
INTEGER :: NT ! A temp variable to determine if NPr divides NTR: INT( NTR / NPr )
INTEGER :: NTR ! The number we're trying to factor in each iteration
INTEGER, PARAMETER :: NPrime(NFact) = (/ 2, 3, 5, 7, 11, 13, 17, 19, 23 /) ! The first 9 prime numbers
LOGICAL :: DividesN1(NFact) ! Does this factor divide NTR-1?
!remove ! This variable is NEW:
!remove CHARACTER(11), EXTERNAL :: Int2LStr ! A function to convert an interger to a left-justified string.
IF ( NumPrimes > NFact ) THEN
CALL ProgAbort ( 'In the call to PSF, NumPrimes must be less than '//TRIM( Int2LStr( NFact ) )//'.' )
ENDIF
DividesN1(:) = .FALSE. ! We need to check all of the primes the first time through
PSF = Npsf
DO
! 1.0 Factor NTR into its primes.
NTR = PSF
DO IPR=1,NumPrimes
IF ( DividesN1(IPR) ) THEN
! If P divides N-1, then P cannot divide N.
DividesN1(IPR) = .FALSE. ! We'll check it next time.
ELSE
NPr = NPrime(IPR) ! The small prime number we will try to find the the factorization of NTR
DO
NT = NTR/NPr ! Doing some modular arithmetic to see if
NP = NT*NPr ! MOD( NTR, NPr ) == 0, i.e. if NPr divides NTR
IF ( NP /= NTR ) EXIT ! There aren't any more of this prime number in the factorization
NTR = NT ! This is the new number we need to get factors for
DividesN1(IPR) = .TRUE. ! This prime number divides Npsf, so we won't check it next time (on Npsf+1).
ENDDO
IF ( NTR .EQ. 1 ) RETURN ! We've found all the prime factors, so we're finished
ENDIF ! DividesN1
ENDDO ! IPR
! 2.0 There is at least one prime larger than NPrime(NumPrimes). Add
! a point to NTR and factor again.
PSF = PSF + 1
ENDDO
RETURN
END FUNCTION PSF
END MODULE FFT_Module
!=======================================================================