Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_sum_fft_realherm_1d (c06pa)

## Purpose

nag_sum_fft_realherm_1d (c06pa) calculates the discrete Fourier transform of a sequence of $n$ real data values or of a Hermitian sequence of $n$ complex data values stored in compact form in a double array.

## Syntax

[x, ifail] = c06pa(direct, x, n)
[x, ifail] = nag_sum_fft_realherm_1d(direct, x, n)

## Description

Given a sequence of $n$ real data values ${x}_{\mathit{j}}$, for $\mathit{j}=0,1,\dots ,n-1$, nag_sum_fft_realherm_1d (c06pa) calculates their discrete Fourier transform (in the forward direction) defined by
 $z^k = 1n ∑ j=0 n-1 xj × exp -i 2πjk n , k= 0, 1, …, n-1 .$
The transformed values ${\stackrel{^}{z}}_{k}$ are complex, but they form a Hermitian sequence (i.e., ${\stackrel{^}{z}}_{n-k}$ is the complex conjugate of ${\stackrel{^}{z}}_{k}$), so they are completely determined by $n$ real numbers (since ${\stackrel{^}{z}}_{0}$ is real, as is ${\stackrel{^}{z}}_{n/2}$ for $n$ even).
Alternatively, given a Hermitian sequence of $n$ complex data values ${z}_{j}$, this function calculates their inverse (backward) discrete Fourier transform defined by
 $x^k = 1n ∑ j=0 n-1 zj × exp i 2πjk n , k= 0, 1, …, n-1 .$
The transformed values ${\stackrel{^}{x}}_{k}$ are real.
(Note the scale factor of $\frac{1}{\sqrt{n}}$ in the above definitions.)
A call of nag_sum_fft_realherm_1d (c06pa) with ${\mathbf{direct}}=\text{'F'}$ followed by a call with ${\mathbf{direct}}=\text{'B'}$ will restore the original data.
nag_sum_fft_realherm_1d (c06pa) uses a variant of the fast Fourier transform (FFT) algorithm (see Brigham (1974)) known as the Stockham self-sorting algorithm, which is described in Temperton (1983).
The same functionality is available using the forward and backward transform function pair: nag_sum_fft_real_2d (c06pv) and nag_sum_fft_hermitian_2d (c06pw) on setting ${\mathbf{n}}=1$. This pair use a different storage solution; real data is stored in a double array, while Hermitian data (the first unconjugated half) is stored in a complex array.

## References

Brigham E O (1974) The Fast Fourier Transform Prentice–Hall
Temperton C (1983) Self-sorting mixed-radix fast Fourier transforms J. Comput. Phys. 52 1–23

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{direct}$ – string (length ≥ 1)
If the forward transform as defined in Description is to be computed, then direct must be set equal to 'F'.
If the backward transform is to be computed then direct must be set equal to 'B'.
Constraint: ${\mathbf{direct}}=\text{'F'}$ or $\text{'B'}$.
2:     $\mathrm{x}\left({\mathbf{n}}+2\right)$ – double array
If x is declared with bounds $\left(0:{\mathbf{n}}+1\right)$ in the function from which nag_sum_fft_realherm_1d (c06pa) is called, then:
• if ${\mathbf{direct}}=\text{'F'}$, ${\mathbf{x}}\left(\mathit{j}\right)$ must contain ${x}_{\mathit{j}}$, for $\mathit{j}=0,1,\dots ,n-1$;
• if ${\mathbf{direct}}=\text{'B'}$, ${\mathbf{x}}\left(2×\mathit{k}\right)$ and ${\mathbf{x}}\left(2×\mathit{k}+1\right)$ must contain the real and imaginary parts respectively of ${z}_{\mathit{k}}$, for $\mathit{k}=0,1,\dots ,n/2$. (Note that for the sequence ${z}_{k}$ to be Hermitian, the imaginary part of ${z}_{0}$, and of ${z}_{n/2}$ for $n$ even, must be zero.)
3:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of data values.
Constraint: ${\mathbf{n}}\ge 1$.

None.

### Output Parameters

1:     $\mathrm{x}\left({\mathbf{n}}+2\right)$ – double array
• if ${\mathbf{direct}}=\text{'F'}$ and x is declared with bounds $\left(0:{\mathbf{n}}+1\right)$, ${\mathbf{x}}\left(2×\mathit{k}\right)$ and ${\mathbf{x}}\left(2×\mathit{k}+1\right)$ will contain the real and imaginary parts respectively of ${\stackrel{^}{z}}_{\mathit{k}}$, for $\mathit{k}=0,1,\dots ,n/2$;
• if ${\mathbf{direct}}=\text{'B'}$ and x is declared with bounds $\left(0:{\mathbf{n}}+1\right)$, ${\mathbf{x}}\left(\mathit{j}\right)$ will contain ${\stackrel{^}{x}}_{\mathit{j}}$, for $\mathit{j}=0,1,\dots ,n-1$.
2:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=2$
$_$ is an invalid value of direct.
${\mathbf{ifail}}=3$
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

Some indication of accuracy can be obtained by performing a subsequent inverse transform and comparing the results with the original sequence (in exact arithmetic they would be identical).

The time taken is approximately proportional to $n×\mathrm{log}\left(n\right)$, but also depends on the factorization of $n$. nag_sum_fft_realherm_1d (c06pa) is faster if the only prime factors of $n$ are $2$, $3$ or $5$; and fastest of all if $n$ is a power of $2$.

## Example

This example reads in a sequence of real data values and prints their discrete Fourier transform (as computed by nag_sum_fft_realherm_1d (c06pa) with ${\mathbf{direct}}=\text{'F'}$), after expanding it from complex Hermitian form into a full complex sequence. It then performs an inverse transform using nag_sum_fft_realherm_1d (c06pa) with ${\mathbf{direct}}=\text{'B'}$, and prints the sequence so obtained alongside the original data values.
```function c06pa_example

fprintf('c06pa example results\n\n');

% Real data x
n = int64(7);
x = zeros(n+2,1);
x(1:n) = [0.34907;  0.5489;   0.74776;   0.94459;
1.13850;  1.3285;   1.51370];

% Transform x to get Hermitian data in compact form
direct = 'F';
[xt, ifail] = c06pa(direct, x, n);
zt = nag_herm2complex(n,xt);
disp('Discrete Fourier Transform of x:');
disp(transpose(zt));

% Restore x by inverse transform
direct = 'B';
[xr, ifail] = c06pa(direct, xt, n);

fprintf('Original sequence as restored by inverse transform\n\n');
fprintf('       Original   Restored\n');
for j = 1:n
fprintf('%3d   %7.4f    %7.4f\n',j, x(j),xr(j));
end

function [z] = nag_herm2complex(n,x);
z(1) = complex(x(1));
for j = 1:floor(double(n)/2) + 1
z(j) = x(2*j-1) + i*x(2*j);
z(n-j+2) = x(2*j-1) - i*x(2*j);
end
```
```c06pa example results

Discrete Fourier Transform of x:
2.4836 + 0.0000i
-0.2660 + 0.5309i
-0.2577 + 0.2030i
-0.2564 + 0.0581i
-0.2564 - 0.0581i
-0.2577 - 0.2030i
-0.2660 - 0.5309i
2.4836 + 0.0000i

Original sequence as restored by inverse transform

Original   Restored
1    0.3491     0.3491
2    0.5489     0.5489
3    0.7478     0.7478
4    0.9446     0.9446
5    1.1385     1.1385
6    1.3285     1.3285
7    1.5137     1.5137
```