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_real_1d_rfmt (c06fa)

## Purpose

nag_sum_fft_real_1d_rfmt (c06fa) calculates the discrete Fourier transform of a sequence of $n$ real data values (using a work array for extra speed).

## Syntax

[x, ifail] = c06fa(x, 'n', n)
[x, ifail] = nag_sum_fft_real_1d_rfmt(x, 'n', n)

## Description

Given a sequence of $n$ real data values ${x}_{\mathit{j}}$, for $\mathit{j}=0,1,\dots ,n-1$, nag_sum_fft_real_1d_rfmt (c06fa) calculates their discrete Fourier transform defined by
 $z^k = 1n ∑ j=0 n-1 xj × exp -i 2πjk n , k= 0, 1, …, n-1 .$
(Note the scale factor of $\frac{1}{\sqrt{n}}$ in this definition.) 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 (see also the C06 Chapter Introduction).
To compute the inverse discrete Fourier transform defined by
 $w^k = 1n ∑ j=0 n-1 xj × exp +i 2πjk n ,$
this function should be followed by forming the complex conjugates of the ${\stackrel{^}{z}}_{k}$; that is, $x\left(\mathit{k}\right)=-x\left(\mathit{k}\right)$, for $\mathit{k}=n/2+2,\dots ,n$.
nag_sum_fft_real_1d_rfmt (c06fa) uses the fast Fourier transform (FFT) algorithm (see Brigham (1974)). There are some restrictions on the value of $n$ (see Arguments).

## References

Brigham E O (1974) The Fast Fourier Transform Prentice–Hall

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
${\mathbf{x}}\left(\mathit{j}+1\right)$ must contain ${x}_{\mathit{j}}$, for $\mathit{j}=0,1,\dots ,n-1$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array x.
$n$, the number of data values. The largest prime factor of n must not exceed $19$, and the total number of prime factors of n, counting repetitions, must not exceed $20$.
Constraint: ${\mathbf{n}}>1$.

### Output Parameters

1:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The discrete Fourier transform stored in Hermitian form. If the components of the transform ${\stackrel{^}{z}}_{k}$ are written as ${a}_{k}+i{b}_{k}$, and if x is declared with bounds $\left(0:{\mathbf{n}}-1\right)$ in the function from which nag_sum_fft_real_1d_rfmt (c06fa) is called, then for $0\le k\le n/2$, ${a}_{k}$ is contained in ${\mathbf{x}}\left(k\right)$, and for $1\le k\le \left(n-1\right)/2$, ${b}_{k}$ is contained in ${\mathbf{x}}\left(n-k\right)$. (See also Real transforms in the C06 Chapter Introduction and Example.)
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$
At least one of the prime factors of n is greater than $19$.
${\mathbf{ifail}}=2$
n has more than $20$ prime factors.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{n}}\le 1$.
${\mathbf{ifail}}=4$
An unexpected error has occurred in an internal call. Check all function calls and array dimensions. Seek expert help.
${\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_real_1d_rfmt (c06fa) 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_real_1d_rfmt (c06fa)), after expanding it from Hermitian form into a full complex sequence. It then performs an inverse transform using nag_sum_fft_hermitian_1d_rfmt (c06fb) and conjugation, and prints the sequence so obtained alongside the original data values.
```function c06fa_example

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

% Hermitian sequence x, stored in Hermitian form.
n = 7;
x = [0.34907;  0.5489;   0.74776;   0.94459;
1.1385;   1.3285;   1.5137];

% DFT of x
[xtrans, ifail] = c06fa(x);

% Display in full complex form
z = nag_herm2complex(xtrans);
disp('Discrete Fourier Transform of x:');
disp(transpose(z));

% Inverse DFT of xtrans
[xres] = nag_hermconj(xtrans);
[xres, ifail] = c06fb(xres);

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),xres(j));
end

function [z] = nag_herm2complex(x);
n = int64(size(x,1));
z(1) = complex(x(1));
for j = 2:floor((n-1)/2) + 1
z(j) = x(j) + i*x(n-j+2);
z(n-j+2) = x(j) - i*x(n-j+2);
end
if (mod(n,2)==0)
z(n/2+1) = complex(x(n/2+1));
end

function [xconj] = nag_hermconj(x);
n = size(x,1);
n2 = floor((n+4)/2);
xconj = x;
for j = n2:n
xconj(j) = -x(j);
end
```
```c06fa 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

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
```