Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

matlab - Verify the convolution theorem

My main goal is to show that the convolution theorem works (just a reminder: the convolution theorem means that idft(dft(im) .* dft(mask)) = conv(im, mask)). I'm trying to program that.

Here is my code:

function displayTransform( im )
% This routine displays the Fourier spectrum of an image.
% 
% Input:      im - a grayscale image (values in [0,255]) 
% 
% Method:  Computes the Fourier transform of im and displays its spectrum,
%                    (if F(u,v) = a+ib, displays sqrt(a^2+b^2)).
%                    Uses display techniques for visualization: log, and stretch values to full range,
%                    cyclic shift DC to center (use fftshift).
%                    Use showImage to display and fft2 to apply transform.  

%displays the image in grayscale in the Frequency domain
imfft = fft2(im);
imagesc(log(abs(fftshift(imfft))+1)), colormap(gray);

% building mask and padding it with Zeros in order to create same size mask
b = 1/16*[1 1 1 1;1 1 1 1; 1 1 1 1; 1 1 1 1];
paddedB = padarray(b, [floor(size(im,1)/2)-2 floor(size(im,2)/2)-2]);
paddedB = fft2(paddedB);
C = imfft.*paddedB;
resIFFT = ifft2(C);

%reguler convolution
resConv = conv2(im,b);
showImage(resConv);

end

I want to compare resIFFT and resConv. I think I'm missing some casting because I am getting numbers in the matrix closer one to another if I'm using casting to double. Maybe I have some mistake in the place of the casting or the padding?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. In order to compute the linear convolution using DFT, you need to post-pad both signals with zeros, otherwise the result would be the circular convolution. You don't have to manually pad a signal though, fft2 can do it for you if you add additional parameters to the function call, like so:

    fft2(X, M, N)
    

    This pads (or truncates) signal X to create an M-by-N signal before doing the transform.
    Pad each signal in each dimension to a length that equals the sum of the lengths of both signals, that is:

    M = size(im, 1) + size(mask, 1);
    N = size(im, 2) + size(mask, 2);
    
  2. Just for good practice, instead of:

    b = 1 / 16 * [1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1];
    

    you can write:

    b = ones(4) / 16;
    

Anyway, here's the fixed code (I've generated a random image just for the sake of the example):

im = fix(255 * rand(500));            % # Generate a random image
mask = ones(4) / 16;                  % # Mask

% # Circular convolution
resConv = conv2(im, mask);

% # Discrete Fourier transform
M = size(im, 1) + size(mask, 1);
N = size(im, 2) + size(mask, 2);
resIFFT = ifft2(fft2(im, M, N) .* fft2(mask, M, N));
resIFFT = resIFFT(1:end-1, 1:end-1);  % # Adjust dimensions

% # Check the difference
max(abs(resConv(:) - resIFFT(:)))

The result you should get is supposed to be zero:

ans =

    8.5265e-014

Close enough.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...