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

◆ Cost1f()

Sub Cost1f ( N As  Long,
R() As  Double,
Wsave() As  Double,
Info As  Long,
Optional Inc As  Long = 1 
)

One-dimensional cosine transform

Purpose
This routine computes the one-dimensional Fourier transform of an even sequence within a real array. This is referred to as the forward transform or Fourier analysis, transforming the sequence from physical to spectral space.
R(0) = (1/2)R(0)/(N-1) + ΣR(j)/(N-1) + (1/2)R(N-1)/(N-1) (Σ for j = 1 to N-2)
R(k) = R(0)/(N-1) + Σ2R(j)cos(πjk/(N-1))/(N-1) + (-1)^k R(N-1)/(N-1) (Σ for j = 1 to N-2) (k = 1 to N-2)
R(N-1) = (1/2)R(0)/(N-1) + ΣR(j)(-1)^k/(N-1) + (1/2)(-1)^(N-1) R(N-1)/(N-1) (Σ for j = 1 to N-2)
This transform is normalized since a call to Cost1f followed by a call to Cost1b (or vice-versa) reproduces the original array subject to algorithmic constraints, roundoff error, etc.
Parameters
[in]NThe length 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 >= Inc*(N - 1) + 1)
[in] The sequence to be transformed.
[out] The Fourier forward transformed sequence 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 Cost1i before the first call to Cost1f or Cost1b for a given transform length N.
[out]Info= 0: Successful exit.
= -1: The argument N had an illegal value. (N < 1)
= -2: The argument R() is invalid. (Array R() is not big enough)
= -3: The argument Wsave() is invalid. (Array Wsave() is not big enough)
= -5: 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 5 random data sequence successively, and compare with the original data sequence.
Sub Ex_Cost1()
Const N = 5
Dim Wsave() As Double, R(N - 1) As Double, R0(N - 1) As Double
Dim LWsave As Long, Info As Long, I As Long
'-- Initialization
LWsave = 2 * N + Log(N) / Log(2) + 4
ReDim Wsave(LWsave - 1)
Call Cost1i(N, Wsave, Info)
If Info <> 0 Then GoTo Err
'-- Generate test data
For I = 0 To N - 1
R(I) = Rnd()
R0(I) = R(I)
Next
'-- Forward transform
Call Cost1f(N, R(), Wsave(), Info)
If Info <> 0 Then GoTo Err
'-- Backward transform
Call Cost1b(N, R(), Wsave(), Info)
If Info <> 0 Then GoTo Err
'-- Print result
For I = 0 To N - 1
Debug.Print R0(I), R(I), R(I) - R0(I)
Next
Exit Sub
Err:
Debug.Print "Error in Cost1i/Cost1f/Cost1b: Info =", Info
End Sub
Sub Cost1f(N As Long, R() As Double, Wsave() As Double, Info As Long, Optional Inc As Long=1)
One-dimensional cosine transform
Sub Cost1i(N As Long, Wsave() As Double, Info As Long)
Initialization of work data for Cost1f and Cost1b
Sub Cost1b(N As Long, R() As Double, Wsave() As Double, Info As Long, Optional Inc As Long=1)
One-dimensional cosine backward transform
Example Results
0.811601400375366 0.811601400375366 0
0.358389794826508 0.358389794826508 0
0.237738966941833 0.237738966941833 0
0.228048384189606 0.228048384189606 -5.55111512312578E-17
0.809549331665039 0.809549331665039 0