Skip to content

Instantly share code, notes, and snippets.

@waldyrious
Created February 15, 2018 17:50
Show Gist options
  • Save waldyrious/47709a8c640c7011f7a4865f8f33a7ee to your computer and use it in GitHub Desktop.
Save waldyrious/47709a8c640c7011f7a4865f8f33a7ee to your computer and use it in GitHub Desktop.
Comparison of Matlab/Octave conv2() with FFT multiplication
a = rand(7);
b = rand(7);
% -------------------------------------------------------------------------
tic();
% CONV
c1p = conv2(a,b);
% UNPAD
c1 = c1p(1:round(size(c1p,1)/2), 1:round(size(c1p,2)/2));
toc()
% -------------------------------------------------------------------------
tic();
% PAD
ap = zeros(size(a)*2);
ap(1:size(a,1), 1:size(a,2)) = a;
bp = zeros(size(b)*2);
bp(1:size(b,1), 1:size(b,2)) = b;
% FFT MULT
apf = fft2(ap);
bpf = fft2(bp);
c2f = apf .* bpf;
c2p = ifft2(c2f);
% UNPAD
c2 = c2p(1:round(size(c2p,1)/2), 1:round(size(c2p,2)/2));
toc()
% -------------------------------------------------------------------------
% Observations:
% - the results match (c1 == c2)
% - time with fft_mult is consistently faster than using conv2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment