from FFT2 optimization by Luigi Rosa
Speed up FFT2.

tempofft2.m
% FFT2 optimization.
% Many image processing applications require an extensive usage
% of FFT2 routine (or, in the most general case, a N-dimensional
% FFT) of matrices having the same dimensions. In these cases Matlab
% FFT2 can result extremely inefficient. In general the execution time
% can be significantly reduced by splitting the N-dimensional FFT into 
% several unidimensional FFT. A good trick is to apply the fft operator
% varying the minimum number of times the length of the unidimensional
% vectors which have to be FFT-transformed.
% You might be able to increase the speed of fft using the utility 
% function fftw, which controls how MATLAB optimizes the algorithm 
% used to compute an FFT of a particular size and dimension.
% In the following examples the planner is always 'hybrid'. 
% The best vectorization strongly depends on the dimensions of
% input matrices. Chose the optimal solution comparing the execution
% times of the methods proposed.
%
% Luigi ROSA
% Via Centrale 35
% 67042 Civita Di Bagno
% L'Aquila --- ITALY
% mobile +39 3207214179
% email  luigi.rosa@tiscali.it
% website http://utenti.lycos.it/matlab

clc;clear;close all;

volte      = 4000;
% dimensions
dimx       = 64;
dimy       = 64;
matrice = rand(dimx,dimy);
%---------------------------------------
fftw('planner', 'hybrid');
% fftw('planner', 'exhaustive');
%---------------------------------------
tic;
for conta=1:volte
    y0 = fft2(matrice);
end
t0=toc;
%---------------------------------------
tic;
y1   = zeros(dimx,dimy);
for conta=1:volte
    for ii=1:dimx
        y1(ii,:)=fft(matrice(ii,:));
    end
    for jj=1:dimy
        y1(:,jj)=fft(y1(:,jj));
    end
end
t1=toc;
%---------------------------------------
tic;
for conta=1:volte
    y2=fft(matrice,[],1);
    y2=fft(y2,[],2);
end
t2=toc;
%---------------------------------------
tic;
for conta=1:volte
    y3=fft(fft(matrice).').';
end
t3=toc;
%---------------------------------------
disp('Method FFT2 Elapsed time');
disp(t0);
disp('Method #1   Elapsed time');
disp(t1);
disp('Method #2   Elapsed time');
disp(t2);
disp('Method #3   Elapsed time');
disp(t3);
disp('************************************');
disp('Speed improvement % vs. fft2');
if t0>0
    disp('Method #1');
    disp((t0-t1)/t0*100);
    disp('Method #2');
    disp((t0-t2)/t0*100);
    disp('Method #3');
    disp((t0-t3)/t0*100);
else
    disp('t0 nullo')
end
disp('************************************');

Contact us at files@mathworks.com