fftCosine

Computes the discrete Fourier cosine transformation of an even sequence.

Synopsis

fftCosine (p)

Required Arguments

float p[] (Input)
Array of size n containing the sequence to be transformed.

Return Value

The transformed sequence. If no solution was computed, then None is returned.

Optional Arguments

params, float[] (Input)

Vector returned by a previous call to fftCosineInit. If fftCosine is used repeatedly with the same value of n, then it is more efficient to compute these parameters only once.

Default: Initializing parameters computed each time fftCosine is entered

Description

The function fftCosine computes the discrete Fourier cosine transform of a real vector of size N. It uses the system’s high performance library for the computation, if available. Otherwise, the method used is a variant of the Cooley-Tukey algorithm, which is most efficient when N - 1 is a product of small prime factors. If N satisfies this condition, then the computational effort is proportional to N logN. Specifically, given an N‑vector p, fftCosine returns in q

\[q_m = 2 \sum_{n=1}^{N-2} p_n \sin \left(\frac{mn \pi}{N-1}\right) + s_0 + s_{N-1}(-1)^m\]

Finally, note that the Fourier cosine transform is its own (unnormalized) inverse. The Cooley-Tukey algorithm is based on the cosine FFT in FFTPACK, which was developed by Paul Swarztrauber at the National Center for Atmospheric Research.

Example

This example inputs a pure cosine wave as a data vector and recovers its Fourier cosine series, which is a vector with all components zero, except n - 1 at the appropriate frequency.

from __future__ import print_function
from numpy import *
from pyimsl.math.constant import constant
from pyimsl.math.fftCosine import fftCosine

n = 7
pi = constant("pi")
p = empty(n)

# Fill p with a pure cosine wave
for k in range(0, n):
    p[k] = cos(k * pi / (n - 1))

q = fftCosine(p)

print(" index       p         q")
for i in range(0, n):
    print("   %1d      %5.2f      %5.2f" % (i, p[i], q[i]))

Output

 index       p         q
   0       1.00       0.00
   1       0.87       6.00
   2       0.50      -0.00
   3       0.00       0.00
   4      -0.50       0.00
   5      -0.87       0.00
   6      -1.00       0.00