XLPack 7.0
XLPack Numerical Library (Excel VBA) Reference Manual
Loading...
Searching...
No Matches

◆ Cfft2f()

Sub Cfft2f ( L As  Long,
M As  Long,
C() As  Complex,
Wsave() As  Double,
Info As  Long 
)

Two-dimensional complex Fourier transform

Purpose
This routine computes the two-dimensional Fourier transform of a periodic sequence within a complex array. This is referred to as the forward transform or Fourier analysis, transforming the sequence from physical to spectral space.
C(j,k) = (1/lm)ΣΣC(l1,m1)exp(-2πi(j*l1/L+k*m1/M)) (1st Σ for l1 = 0 to L-1, 2nd Σ for m1 = 0 to M-1) (j = 0 to L-1, k = 0 to M-1) (i is imaginary unit)
This transform is normalized since a call to Cfft2f followed by a call to Cfft2b (or vice-versa) reproduces the original array subject to algorithmic constraints, roundoff error, etc.
Parameters
[in]LNumber of elements to be transformed in the first dimension of the two-dimensional array C(). (L >= 1) (The transform is most efficient when L is a product of small primes)
[in]MNumber of elements to be transformed in the second dimension of the two-dimensional array C(). (M >= 1) (The transform is most efficient when M is a product of small primes)
[in,out]C()Array C(LC1 - 1, LC2 - 1) (LC1 >= L, LC2 >= M)
[in] The two dimensional sequence dat to be transformed.
[out] Fourier forward transformed two dimensional sequence data.
[in]Wsave()Array Wsave(LWsave - 1) (LWsave >= 2*(L+M) + ln(L)/ln(2) + Ln(M)/Ln(2) + 8)
Work data. Its contents must be initialized with a call to Cfft2i before the first call to Cfft2f or Cfft2b for a given transform length L and M.
[out]Info= 0: Successful exit.
= -1: The argument L had an illegal value. (L < 1)
= -2: The argument M had an illegal value. (M < 1)
= -3: The argument C() is invalid. (Array C() is not big enough)
= -4: The argument Wsave() is invalid. (Array Wsave() is not big enough)
Reference
FFTPACK
Example Program
Compute the two-dimensional Fourier transform and backward transform of 4 rows x 4 columns random complex data sequence successively, and compare with the original data sequence.
Sub Ex_Cfft2()
Const L = 4, M = 4
Dim Wsave() As Double, C(L - 1, M - 1) As Complex, C0(L - 1, M - 1) As Complex
Dim LWsave As Long, Info As Long, I As Long, J As Long
'-- Initialization
LWsave = 2 * (L + M) + Log(L) / Log(2) + Log(M) / Log(2) + 8
ReDim Wsave(LWsave - 1)
Call Cfft2i(L, M, Wsave, Info)
If Info <> 0 Then GoTo Err
'-- Generate test data
For I = 0 To L - 1
For J = 0 To M - 1
C(I, J) = Cmplx(Rnd(), Rnd())
C0(I, J) = Cmplx(Creal(C(I, J)), Cimag(C(I, J)))
Next
Next
'-- Forward transform
Call Cfft2f(L, M, C(), Wsave(), Info)
If Info <> 0 Then GoTo Err
'-- Backward transform
Call Cfft2b(L, M, C(), Wsave(), Info)
If Info <> 0 Then GoTo Err
'-- Print result
For J = 0 To M - 1
For I = 0 To L - 1
Debug.Print "(" & CStr(Creal(C0(I, J))) & ", " & CStr(Cimag(C0(I, J))) & ")", "(" & CStr(Creal(C(I, J))) & ", " & CStr(Cimag(C(I, J))) & ")", Cabs(Csub(C(I, J), C0(I, J)))
Next
Debug.Print
Next
Exit Sub
Err:
Debug.Print "Error in Cfft2i/Cfft2f/Cfft2b: Info =", Info
End Sub
Function Cmplx(R As Double, Optional I As Double=0) As Complex
Building complex number
Function Cabs(A As Complex) As Double
Absolute value of complex number
Function Cimag(A As Complex) As Double
Imaginary part of complex number
Function Creal(A As Complex) As Double
Real part of complex number
Function Csub(A As Complex, B As Complex) As Complex
Subtraction of complex number from complex number
Sub Cfft2f(L As Long, M As Long, C() As Complex, Wsave() As Double, Info As Long)
Two-dimensional complex Fourier transform
Sub Cfft2b(L As Long, M As Long, C() As Complex, Wsave() As Double, Info As Long)
Two-dimensional complex Fourier backward transform
Sub Cfft2i(L As Long, M As Long, Wsave() As Double, Info As Long)
Initialization of work data for Cfft2f and Cfft2b
Example Results
(0.72104674577713, 2.41577625274658E-02) (0.72104674577713, 2.41577625274658E-02) 0
(0.603598415851593, 0.117225408554077) (0.603598415851593, 0.117225408554077) 0
(0.341512024402618, 0.784832239151001) (0.341512024402618, 0.784832239151001) 0
(0.734103977680206, 0.129029035568237) (0.734103977680206, 0.129029035568237) 0
(0.620623052120209, 0.25133216381073) (0.620623052120209, 0.25133216381073) 0
(0.619252145290375, 0.921948790550232) (0.619252145290375, 0.921948790550232) 0
(0.472388684749603, 0.294668078422546) (0.472388684749603, 0.294668078422546) 0
(0.373880326747894, 0.287947058677673) (0.373880326747894, 0.287947058677673) 0
(0.286389887332916, 0.837161540985107) (0.286389887332916, 0.837161540985107) 0
(0.77502828836441, 0.369781494140625) (0.77502828836441, 0.369781494140625) 0
(0.360483705997467, 2.57539749145508E-03) (0.360483705997467, 2.57539749145508E-03) 0
(0.787385046482086, 1.65653228759766E-03) (0.787385046482086, 1.65653228759766E-03) 0
(0.904512107372284, 0.480963826179504) (0.904512107372284, 0.480963826179504) 0
(0.811046779155731, 0.748176217079163) (0.811046779155731, 0.748176217079163) 0
(0.245184600353241, 0.948204159736633) (0.245184600353241, 0.948204159736633) 0
(0.908585727214813, 0.476067185401917) (0.908585727214813, 0.476067185401917) 0