# NAG Library Routine Document

## 1Purpose

c06fkf calculates the circular convolution or correlation of two real vectors of period $n$ (using a work array for extra speed).

## 2Specification

Fortran Interface
 Subroutine c06fkf ( job, x, y, n, work,
 Integer, Intent (In) :: job, n Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (Inout) :: x(n), y(n) Real (Kind=nag_wp), Intent (Out) :: work(n)
#include nagmk26.h
 void c06fkf_ (const Integer *job, double x[], double y[], const Integer *n, double work[], Integer *ifail)

## 3Description

c06fkf computes:
• if ${\mathbf{job}}=1$, the discrete convolution of $x$ and $y$, defined by
 $zk = ∑ j=0 n-1 xj y k-j = ∑ j=0 n-1 x k-j yj ;$
• if ${\mathbf{job}}=2$, the discrete correlation of $x$ and $y$ defined by
 $wk = ∑ j=0 n-1 xj y k+j .$
Here $x$ and $y$ are real vectors, assumed to be periodic, with period $n$, i.e., ${x}_{j}={x}_{j±n}={x}_{j±2n}=\dots \text{}$; $z$ and $w$ are then also periodic with period $n$.
Note:  this usage of the terms ‘convolution’ and ‘correlation’ is taken from Brigham (1974). The term ‘convolution’ is sometimes used to denote both these computations.
If $\stackrel{^}{x}$, $\stackrel{^}{y}$, $\stackrel{^}{z}$ and $\stackrel{^}{w}$ are the discrete Fourier transforms of these sequences, i.e.,
 $x^k = 1n ∑ j=0 n-1 xj × exp -i 2πjk n , etc.,$
then ${\stackrel{^}{z}}_{k}=\sqrt{n}.{\stackrel{^}{x}}_{k}{\stackrel{^}{y}}_{k}$ and ${\stackrel{^}{w}}_{k}=\sqrt{n}.{\stackrel{-}{\stackrel{^}{x}}}_{k}{\stackrel{^}{y}}_{k}$ (the bar denoting complex conjugate).
This routine calls the same auxiliary routines as c06paf to compute discrete Fourier transforms.
Brigham E O (1974) The Fast Fourier Transform Prentice–Hall

## 5Arguments

1:     $\mathbf{job}$ – IntegerInput
On entry: the computation to be performed.
${\mathbf{job}}=1$
${z}_{k}=\sum _{j=0}^{n-1}{x}_{j}{y}_{k-j}$ (convolution);
${\mathbf{job}}=2$
${w}_{k}=\sum _{j=0}^{n-1}{x}_{j}{y}_{k+j}$ (correlation).
Constraint: ${\mathbf{job}}=1$ or $2$.
2:     $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: the elements of one period of the vector $x$. If x is declared with bounds $\left(0:{\mathbf{n}}-1\right)$ in the subroutine from which c06fkf is called, ${\mathbf{x}}\left(\mathit{j}\right)$ must contain ${x}_{\mathit{j}}$, for $\mathit{j}=0,1,\dots ,n-1$.
On exit: the corresponding elements of the discrete convolution or correlation.
3:     $\mathbf{y}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: the elements of one period of the vector $y$. If y is declared with bounds $\left(0:{\mathbf{n}}-1\right)$ in the subroutine from which c06fkf is called, ${\mathbf{y}}\left(\mathit{j}\right)$ must contain ${y}_{\mathit{j}}$, for $\mathit{j}=0,1,\dots ,n-1$.
On exit: the discrete Fourier transform of the convolution or correlation returned in the array x; the transform is stored in Hermitian form; if the components of the transform ${z}_{k}$ are written as ${a}_{k}+i{b}_{k}$, then for $0\le k\le n/2$, ${a}_{k}$ is contained in ${\mathbf{y}}\left(k\right)$, and for $1\le k\le n/2-1$, ${b}_{k}$ is contained in ${\mathbf{y}}\left(n-k\right)$. (See also Section 2.1.2 in the C06 Chapter Introduction.)
4:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of values in one period of the vectors x and y.
Constraint: ${\mathbf{n}}>1$.
5:     $\mathbf{work}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
6:     $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{n}}\le 1$.
${\mathbf{ifail}}=4$
 On entry, ${\mathbf{job}}\ne 1$ or $2$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The results should be accurate to within a small multiple of the machine precision.

## 8Parallelism and Performance

c06fkf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
c06fkf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

The time taken is approximately proportional to $n×\mathrm{log}\left(n\right)$, but also depends on the factorization of $n$. c06fkf 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$.

## 10Example

This example reads in the elements of one period of two real vectors $x$ and $y$, and prints their discrete convolution and correlation (as computed by c06fkf). In realistic computations the number of data values would be much larger.

### 10.1Program Text

Program Text (c06fkfe.f90)

### 10.2Program Data

Program Data (c06fkfe.d)

### 10.3Program Results

Program Results (c06fkfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017