% conv_via_fft_template.m
% template for image filtering using 2D FFT

rect = @(x) abs(x) < 1/2; % convenient function

nx = ?;	ny = ?; % sampling parameters
dx = ?;	dy = ?;
x = [-nx/2:nx/2-1] * dx;
y = ?
? = ndgrid ? % sampling grid
fxy = ? % (samples of) the image f(x,y)
clf, subplot(331), imagesc(x,y,fxy'), axis image, axis xy
xlabel x, ylabel y, title 'f(x,y)', colormap(1-gray(256)), colorbar

% find the 2D FFT of the image
% hint: fftshift() will be important somewhere near here!

F = ?
u = ? % frequency sample positions (\nu_X) 
v = ? % frequency sample positions (\nu_Y) 


% display spectrum F of f(x,y)
subplot(334), imagesc(u, v, abs(F)'), axis image, axis xy
xlabel '\nu_X', ylabel '\nu_Y', title '|F(\nu_X,\nu_Y)|', colorbar
subplot(335), imagesc(u, v, real(F)'), axis image, axis xy
xlabel '\nu_X', ylabel '\nu_Y', title 'F_R(\nu_X,\nu_Y)', colorbar
subplot(336), imagesc(u, v, imag(F)'), axis image, axis xy
xlabel '\nu_X', ylabel '\nu_Y', title 'F_I(\nu_X,\nu_Y)', colorbar

% Create (samples of) filter frequency response, and display
H = ?

subplot(333), imagesc(u, v, H'), axis image, axis xy
xlabel '\nu_X', ylabel '\nu_Y', title 'H(\nu_X,\nu_Y)', colorbar

% Find spectrum of output image using convolution property of 2D FT
G = ?

% Find final image by returning to image domain
% Watch out for fftshift() again!
g = ?

% display real and imaginary parts of g(x,y)
% Hint: does your imaginary part make sense?
subplot(337), imagesc(x, y, real(g)'), axis image, axis xy
xlabel x, ylabel y, title 'Re[g(x,y)]', colorbar
subplot(338), imagesc(x, y, imag(g)'), axis image, axis xy
xlabel x, ylabel y, title 'Im[g(x,y)]', colorbar
%subplot(339), imagesc(x, y, abs(g)'), axis image, axis xy
%xlabel x, ylabel y, title '|g(x,y)|', colorbar

frac = max(abs(imag(g(:)))) / max(abs(g(:))) % check imaginary part
% orient tall, print('conv_via_fft', '-deps')
