I want to convolve two large matrices and it is working fast - as long as one of those matrixes contains only separate points (point sources). If I replace the points by eg. Gaussians (line "HERE" below), it is getting terribly slow. My guess would be that Imfilter is swiching from FFT-based convolution to "normal" one.

Is there a way to avoid this? I need to use sizes up to 2^15.

% preapre image
len = 2^10; % size of both matrixes
mass = zeros (len);
mass = imnoise (mass, 'salt & pepper', 0.0001);
mass = mass.*(rand(len)).^2;
gauss = fspecial('gaussian', round(sqrt(len)), sqrt(sqrt(len)));
mass = imfilter (mass, gauss, 'replicate', 'conv'); % HERE

% prepare kernel
g = zeros(len);
lenMone = len-1;
for i = 1:len
    for j = 1:len
        g(i, j) = ((i-1)/lenMone - 0.5)^2 + ((j-1)/lenMone - 0.5)^2;
g = -log(sqrt(g));

% convolution
filteredFFT = imfilter (g, mass, 'replicate', 'conv');

% display results
figure('units', 'normalized', 'outerposition', [0 0.25 1 0.5])
subplot 131, imshow (mass, []); title ('Mass density')
subplot 132, imshow (g, []); title ('Green`s function')
subplot 133, imshow (filteredFFT, []); title ('Gravitational potential')

enter image description here

  • $\begingroup$ whoops! I was thinking of base 10, let me delete my comments then... $\endgroup$ – Fat32 Sep 29 '17 at 21:22
  • $\begingroup$ I don't know but I think you are asking for a lot more significant points. That sort of thing really chews up processing. Do you know the runtime versus the number of significant points? If not you might look at: scicomp.stackexchange.com/questions/10356/… which might have answers or hints. If not you could post there and likely get an answer. $\endgroup$ – rrogers Oct 3 '17 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.