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

◆ Costmf()

Sub Costmf ( Lot As  Long,
Jump As  Long,
N As  Long,
R() As  Double,
Wsave() As  Double,
Info As  Long,
Optional Inc As  Long = 1 
)

One-dimensional cosine transform (multiple sequences)

Purpose
This routine computes the one-dimensional Fourier transform of multiple even sequences within a real array. This is referred to as the forward transform or Fourier analysis, transforming the sequence from physical to spectral space.
R(l*jump) = (1/2)R(l*jump)/(N-1) + ΣR(l*jump+j)/(N-1) + (1/2)R(l*jump+N-1)/(N-1) (Σ for j = 1 to N-2) (l = 0 to lot-1)
R(l*jump+k) = R(l*jump)/(N-1) + Σ2R(l*jump+j)cos(πjk/(N-1))/(N-1) + (-1)^k R(l*jump+N-1)/(N-1) (Σ for j = 1 to N-2) (l = 0 to lot-1, k = 1 to N-2)
R(l*jump+N-1) = (1/2)R(l*jump)/(N-1) + ΣR(l*jump+j)(-1)^k/(N-1) + (1/2)(-1)^(N-1) R(l*jump+N-1)/(N-1) (Σ for j = 1 to N-2) (l = 0 to lot-1)
This transform is normalized since a call to Costmf followed by a call to Costmb (or vice-versa) reproduces the original array subject to algorithmic constraints, roundoff error, etc.
Parameters
[in]LotNumber of sequences to be transformed. (Lot >= 1)
[in]JumpIncrement between the locations, in array R(), of the first elements of two consecutive sequences to be transformed. (Jump >= 1)
[in]NLength of the sequence to be transformed. (N >= 1) (The transform is most efficient when N-1 is a product of small primes)
[in,out]R()Array R(LR - 1) (LR >= (Lot - 1)*Jump + Inc*(N - 1) + 1)
[in] The sequences to be transformed.
[out] The Fourier forward transformed sequences of data.
[in]Wsave()Array Wsave(LWsave - 1) (LWsave >= 2*N + ln(N)/ln(2) + 4)
Work data. Its contents must be initialized with a call to Costmi before the first call to Costmf or Costmb for a given transform length N.
[out]Info= 0: Successful exit.
= -1: The argument Lot had an illegal value. (Lot < 1, or, Lot, Jump, N and Inc are inconsistent)
= -2: The argument Jump had an illegal value. (Jump < 1)
= -3: The argument N had an illegal value. (N < 1)
= -4: The argument R() had an illegal value. (Array R() is not big enough)
= -5: The argument Wsave() had an illegal value. (Array Wsave() is not big enough)
= -7: The argument Inc had an illegal value. (Inc < 1)
[in]Inc(Optional)
Integer increment between the locations, in array R(), of two consecutive elements within the sequence. (Inc >= 1) (default = 1)
Reference
FFTPACK
Example Program
Compute the cosine transform and backward transform of 2 sequences of 5 random data successively, and compare with the original data sequences.
Sub Ex_Costm()
Const N = 5, Lot = 2, Jump = N
Dim Wsave() As Double, R(Lot * N - 1) As Double, R0(Lot * N - 1) As Double
Dim LWsave As Long, Info As Long, I As Long, J As Long, K As Long
'-- Initialization
LWsave = 2 * N + Log(N) / Log(2) + 4
ReDim Wsave(LWsave - 1)
Call Costmi(N, Wsave, Info)
If Info <> 0 Then GoTo Err
'-- Generate test data
For I = 0 To Lot * N - 1
R(I) = Rnd()
R0(I) = R(I)
Next
'-- Forward transform
Call Costmf(Lot, Jump, N, R(), Wsave(), Info)
If Info <> 0 Then GoTo Err
'-- Backward transform
Call Costmb(Lot, Jump, N, R(), Wsave(), Info)
If Info <> 0 Then GoTo Err
'-- Print result
For J = 0 To Lot - 1
For I = 0 To N - 1
K = J * Jump + I
Debug.Print R0(K), R(K), R(K) - R0(K)
Next
Debug.Print
Next
Exit Sub
Err:
Debug.Print "Error in Costmi/Costmf/Costmb: Info =", Info
End Sub
Sub Costmf(Lot As Long, Jump As Long, N As Long, R() As Double, Wsave() As Double, Info As Long, Optional Inc As Long=1)
One-dimensional cosine transform (multiple sequences)
Sub Costmb(Lot As Long, Jump As Long, N As Long, R() As Double, Wsave() As Double, Info As Long, Optional Inc As Long=1)
One-dimensional cosine backward transform (multiple sequences)
Sub Costmi(N As Long, Wsave() As Double, Info As Long)
Initialization of work data for Costmf and Costmb
Example Results
0.116536915302277 0.116536915302277 0
0.999308347702026 0.999308347702026 1.11022302462516E-16
0.710308015346527 0.710308015346527 0
0.436867117881775 0.436867117881775 -1.11022302462516E-16
0.866015493869781 0.866015493869781 0
0.189593315124512 0.189593315124512 0
0.073291003704071 0.073291003704071 -5.55111512312578E-17
0.796404480934143 0.796404480934143 0
0.691944420337677 0.691944420337677 0
0.248183488845825 0.248183488845825 0