Change language to:
English - Français - 日本語 -

See the recommended documentation of this function

# fft

fast Fourier transform.

# ifft

fast Fourier transform.

### Calling Sequence

```x=fft(a ,-1) or x=fft(a)
x=fft(a,1) or x=ifft(a)
x=fft(a,-1,dim,incr)
x=fft(a,1,dim,incr)```

### Arguments

x

real or complex vector or real or complex matrix (2-dim fft)

a

real or complex vector or real or complex matrix (2-dim fft).

dim

positive integer

incr

positive integer

### Description

Short syntax
direct

`x=fft(a,-1)` or `x=fft(a)` gives a direct transform.

single variate

If `a` is a vector a single variate direct FFT is computed that is:

(the `-1` argument refers to the sign of the exponent..., NOT to "inverse"),

multivariate

If `a` is a matrix or or a multidimensionnal array a multivariate direct FFT is performed.

inverse

`a=fft(x,1)` or `a=ifft(x)`performs the inverse transform normalized by `1/n`.

single variate

If `a` is a vector a single variate inverse FFT is computed

multivariate

If `a` is a matrix or or a multidimensionnal array a multivariate inverse FFT is performed.

Long syntax for multidimensional FFT

`x=fft(a,-1,dim,incr)` allows to perform an multidimensional fft.

If a is a real or complex vector implicitly indexed by `j1,j2,..,jp` i.e. `a(j1,j2,..,jp)` where `j1` lies in `1:dim(1),` `j2` in `1:dim(2),...` one gets a p-variate FFT by calling p times `fft` as follows

```incrk=1;
x=a;
for k=1:p
x=fft(x ,-1,dim(k),incrk)
incrk=incrk*dim(k)
end```

where `dimk` is the dimension of the current variable w.r.t which one is integrating and `incrk` is the increment which separates two successive `jk` elements in `a`.

In particular,if `a` is an mxn matrix, `x=fft(a,-1)` is equivalent to the two instructions:

```a1=fft(a,-1,m,1);
x=fft(a1,-1,n,m);```

### Algorithms

If the `fftw` module has been loaded into Scilab this function uses that library (http://www.fftw.org/). On the other case the fft function is based on the Fortran routines fft842.f (Cooley-Tukey algorithm for vectors of size n=2^m) and dfftbi.f (for other sizes) .

### Examples

```//Frequency components of a signal
//----------------------------------
// build a noides signal sampled at 1000hz  containing to pure frequencies
// at 50 and 70 Hz
sample_rate=1000;
t = 0:1/sample_rate:0.6;
N=size(t,'*'); //number of samples
s=sin(2*%pi*50*t)+sin(2*%pi*70*t+%pi/4)+grand(1,N,'nor',0,1);

y=fft(s);
//the fft response is symmetric we retain only the first N/2 points
f=sample_rate*(0:(N/2))/N; //associated frequency vector
n=size(f,'*')
clf()
plot(f,abs(y(1:n)))```
```//Using explicit formula for  discrete Fourier transform
//------------------------------------------------
function xf=DFT(x, flag);
n=size(x,'*');
//Compute the n by n Fourier matrix
if flag==1 then,//backward transformation
am=exp(2*%pi*%i*(0:n-1)'*(0:n-1)/n);
else //forward transformation
am=exp(-2*%pi*%i*(0:n-1)'*(0:n-1)/n);
end
xf=am*matrix(x,n,1);//dft
xf=matrix(xf,size(x));//reshape
if flag==1 then,xf=xf/n;end
endfunction

//Comparison with the fast Fourier algorithm
a=rand(1,1000);
norm(DFT(a,1) - fft(a,1))
norm(DFT(a,-1) - fft(a,-1))

timer();DFT(a,-1);timer()
timer();fft(a,-1);timer()```