Created
February 15, 2018 17:50
-
-
Save waldyrious/47709a8c640c7011f7a4865f8f33a7ee to your computer and use it in GitHub Desktop.
Comparison of Matlab/Octave conv2() with FFT multiplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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