www.gusucode.com > images 案例代码 matlab源码程序 > images/PerformFastConvolutionUsingTheFourierTransformExample.m

    %% Perform Fast Convolution Using the Fourier Transform
% This example shows how to perform fast convolution of two matrices using
% the Fourier transform. A key property of the Fourier transform is that
% the multiplication of two Fourier transforms corresponds to the
% convolution of the associated spatial functions. This property, together
% with the fast Fourier transform, forms the basis for a fast convolution
% algorithm.
%
% Note: The FFT-based convolution method is most often used for large
% inputs. For small inputs it is generally faster to use the |imfilter|
% function.
%%
% Create two simple matrices, |A| and |B|. |A| is an M-by-N matrix and |B| is a
% P-by-Q matrix.
A = magic(3);
B = ones(3);
%%
% Zero-pad |A| and |B| so that they are at least
% (M+P-1)-by-(N+Q-1). (Often |A| and |B| are zero-padded to a size
% that is a power of 2 because |fft2| is fastest for these sizes.) The
% example pads the matrices to be 8-by-8.
A(8,8) = 0;
B(8,8) = 0;
%%
% Compute the two-dimensional DFT of |A| and |B| using the |fft2| function.
% Multiply the two DFTs together and compute the inverse two-dimensional
% DFT of the result using the |ifft2| function.
C = ifft2(fft2(A).*fft2(B));
%%
% Extract the nonzero portion of the result and remove the imaginary part
% caused by roundoff error.
C = C(1:5,1:5);
C = real(C)