XLPack 7.0
Python API Reference Manual
Loading...
Searching...
No Matches

◆ rfft1f()

def rfft1f ( ,
,
wsave  ,
inc  = 1 
)

One-dimensional real Fourier transform

Purpose
rfft1f computes the one-dimensional Fourier transform of a periodic 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/n)Σr[j] (Σ for j = 0 to n-1)
r[2k-1] = (2/n)Σr[j]cos(2πjk/n) (Σ for j = 0 to n-1) (k = 1 to nh)
r[2k] = (2/n)Σr[j]sin(2πjk/n) (Σ for j = 0 to n-1) (k = 1 to nh)
r[n-1] = (1/n)Σ(-1)^j r[j] (Σ for j = 0 to n-1) (only if n is even)
(If n is even, nh = n/2-1. If n is odd, nh = (n-1)/2)
This transform is normalized since a call to rfft1f followed by a call to rfft1b (or vice-versa) reproduces the original array subject to algorithmic constraints, roundoff error, etc.
Returns
info (int)
= 0: Successful exit
= -1: The argument n had an illegal value (n < 1)
= -2: The argument r is invalid
= -3: The argument wsave is invalid
= -4: The argument inc had an illegal value (inc < 1)
Parameters
[in]nThe length of the sequence to be transformed. (n >= 1)
The transform is most efficient when n is a product of small primes.
[in,out]rNumpy ndarray (1-dimensional, float, length inc*(n - 1) + 1)
[in] The sequence to be transformed.
[out] The Fourier forward transformed sequence of data.
[in]wsaveNumpy ndarray (1-dimensional, float, length n + ln(n)/ln(2) + 4)
Work data. Its contents must be initialized with a call to rfft1i before the first call to rfft1f or rfft1b for a given transform length n.
[in]inc(Optional)
Integer increment between the locations, in array r, of two consecutive elements within the sequence. (inc >= 1) (default = 1)
Reference
FFTPACK 5.1
Example Program
Compute the Fourier transform and backward transform of 5 random data sequence successively, and compare with the original data sequence.
def TestRfft1():
# Initialization
seed = 13
n = 5
lwsave = n + int(log(n)/log(2)) + 4
wsave = np.empty(lwsave)
info = rfft1i(n, wsave)
if info != 0:
print('Error during initialization: info =', info)
return 1
# Generate test data
inc = 1
lr = inc*(n - 1) + 1
r = np.empty(lr)
rcopy = np.empty(lr)
for i in range(n):
k = inc*i
r[k] = genrand_res53()
rcopy[k] = r[k]
# Forward transform
info = rfft1f(n, r, wsave)
if info != 0:
print('Error in Rfft1f: info =', info)
return 2
# Backward transform
info = rfft1b(n, r, wsave)
if info != 0:
print('Error in Rfft1b: info =', info)
return 3
# Check results
diff = 0.0
for i in range(n):
k = inc*i
if abs(r[k] - rcopy[k]) > diff:
diff = abs(r[k] - rcopy[k])
print(rcopy[k], r[k], abs(r[k] - rcopy[k]))
print('diff(max) =', diff)
def rfft1b(n, r, wsave, inc=1)
One-dimensional real fast Fourier backward transform
def rfft1f(n, r, wsave, inc=1)
One-dimensional real Fourier transform
def rfft1i(n, wsave)
Initialization of work data for rfft1f and rfft1b
def init_genrand(s)
Initialization with seed for random number generator (Mersenne Twister)
def genrand_res53()
53 bit real random number in [0, 1) (Mersenne Twister)
Example Results
>>> TestRfft1()
0.7777024105738202 0.7777024105738204 2.220446049250313e-16
0.2375412200349123 0.23754122003491207 2.220446049250313e-16
0.8242785326613685 0.8242785326613685 0.0
0.9657491980429997 0.9657491980429999 1.1102230246251565e-16
0.9726011139048933 0.9726011139048933 0.0
diff(max) = 2.220446049250313e-16