Function File: fftconvn (A, B)
Function File: fftconvn (A, B, shape)

Convolve N dimensional signals using the FFT for computation.

This function is equivalent to convn but using the FFT. It convolves the two N dimensional A and B. The size of output is controlled by the option shape which removes the borders where boundary effects may be seen:

"full" (default)

Return the full convolution.

"same"

Return central part of the convolution with the same size as A.

"valid"

Return only the parts which do not include zero-padded edges.

Using the FFT may be faster but this is not always the case and can be a lot worse, specially for smalls A and B. This performance increase also comes at the cost of increased memory usage, as well as a loss of precision.

a = randi (255, 1024, 1024);
b = randi (255, 10, 10);
t = cputime (); convn (a, b); cputime () -t
   ⇒ 0.096000
t = cputime (); fftconvn (a, b); cputime () -t
   ⇒ 1.2560

b = randi (255, 50, 50);
t = cputime (); convn (a, b); cputime () -t
   ⇒ 2.3400
t = cputime (); fftconvn (a, b); cputime () -t
   ⇒ 1.2560

Note how computation time for convn increased with the size of B but remained constant when using fftconvn. When performing the convolution, fftconvn zero pads both A and B so their lengths are a power of two on all dimensions. This may further increase memory usage but will also increase performance. In this example, the computation time will remain constant until size (A) + size (B) -1 is greater than 2048 after which it will remain constant again until it reaches 4096.

a = randi (255, 1024, 1024);
b = randi (255, 50, 50);
t = cputime (); fftconvn (a, b); cputime () -t
   ⇒ 1.2760
a = randi (255, 2048-50+1, 2048-50+1);
t = cputime (); fftconvn (a, b); cputime () -t
   ⇒ 1.2120
a = randi (255, 2049-50+1, 2049-50+1);
t = cputime (); fftconvn (a, b); cputime () -t
   ⇒ 6.1520
a = randi (255, 4096-50+1, 4096-50+1);
t = cputime (); fftconvn (a, b); cputime () -t
   ⇒ 6.2360
a = randi (255, 4097-50+1, 4097-50+1);
t = cputime (); fftconvn (a, b); cputime () -t
   ⇒ 38.120

See also: convn, fftconv2, fftconv, padarray.

Package: image