Code covered by the BSD License  

Highlights from
FFT-based convolution

5.0

5.0 | 6 ratings Rate this file 68 Downloads (last 30 days) File Size: 4.99 KB File ID: #24504

FFT-based convolution

by Bruno Luong

 

21 Jun 2009 (Updated 16 Sep 2009)

Discrete convolution using FFT method

| Watch this File

File Information
Description

As opposed to Matlab CONV, CONV2, and CONVN implemented as straight forward sliding sums, CONVNFFT uses Fourier transform (FT) convolution theorem, i.e. FT of the convolution is equal to the product of the FTs of the input functions.

In 1-D, the complexity is O((na+nb)*log(na+nb)), where na/nb are respectively the lengths of A and B.

Optional arguments to control the dimension(s) along which convolution is carried out.

Slightly less accurate than sliding sum convolution.

Good usage recommendation:
        In 1D, this function is faster than CONV for nA, nB > 1000.
        In 2D, this function is faster than CONV2 for nA, nB > 20.
        In 3D, this function is faster than CONVN for nA, nB > 5.

Acknowledgements
This submission has inspired the following:
conv2fft_reuse
MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
23 Jul 2010 Eric  
02 Nov 2010 Alexander

Awesome function! My code runs 60x faster now (thanks to your GPU support).

06 Mar 2011 Felipe G. Nievinski

Well written (IMHO).

09 Aug 2011 Arun Shamanna  
12 Feb 2012 Romesh

I think this says it all...

>> tic;C = convn(Vs,Vs);toc;
Elapsed time is 473.103412 seconds.
>> tic;C2 = convnfft(Vs,Vs);toc;
Elapsed time is 1.351315 seconds.
>> max(max(max(abs(C-C2))))
ans =
   5.2208e-15

Thanks so much for this!

16 Mar 2012 Michael Völker

Hi Bruno, are you sure your inplaceprod() is (still) useful?

I'm pretty sure, MATLAB does A=A.*B in-place itself. I just compared my memory usage for very large A/B with both methods and there was no difference.
This post is from 2007: http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/

In a heterogeneous environment, it is useful to avoid mex code for such small tasks. If you are a non-privileged user on a compute server it is really a mess when compiling fails due to compiler version, libraries or whatever.

A minor notice is that (i)fftn is faster than for-loops around 1D (i)fft calls. At least as long as the input and output are of the same size. So I got at least a little speed gain by replacing
    for dim=dims
        A = ifft(A,[],dim);
    end
with
    A = ifftn( A );
for the MATLAB ifft case.

Thank you for the code!

18 Mar 2012 Bruno Luong

Hi Michael,
May be the inplace times is no longer necessary for recent Matlab.

I remember implement that from a user request.

Thanks for the useful feedback.

21 Apr 2012 Edwin

Moreover, when I checked the result form this code, there are some different between this and convn for two 3d matrix, the 2 input matrix are both positive.
the result from this code has negative values while convn does not.

Please login to add a comment or rating.
Updates
23 Jun 2009

correct bug when ndims(A)<ndims(B)

02 Sep 2009

GPU/Jacket capable

03 Sep 2009

GPU unable by default + changes in help section

16 Sep 2009

Option allows to disable padding to next power-two. Mex implement inplace product that saves about 1/3 memory. These two enhancement might be useful when perform convolution with very large arrays.

Tag Activity for this File
Tag Applied By Date/Time
convolution Bruno Luong 22 Jun 2009 11:19:43
conv Bruno Luong 22 Jun 2009 11:19:43
conv2 Bruno Luong 22 Jun 2009 11:19:43
convn Bruno Luong 22 Jun 2009 11:19:43
fft Bruno Luong 22 Jun 2009 11:19:43
gpu Bruno Luong 03 Sep 2009 13:21:59
jacket Bruno Luong 03 Sep 2009 13:21:59
fft Omri 07 Aug 2011 11:56:28

Contact us at files@mathworks.com