%Chapter 6,  Fig 6.3
%Eric Dubois, updated 2018-12-12
%Plot the two-dimensional frequency response of the rectangular aperture
%associated with the green channel of a Bayer array
%Required functions: Lattice_Points_2d

clear all; close all

%compute the frequency response of the rect aperture for u and v between
%-1.2 and 1.2 c/px. Offset by eps to avoid divide by zero at (0,0)
eps = .00001;
u=-1.2+eps:.01:1.2+eps; v=u;      %u and v values between -1.2 and +1.2 spaced by .01
[U,V]=meshgrid(u,v);   % generate a 2D grid of uv values
Ha= sin(pi*U).*sin(pi*V)./(pi^2*U.*V);  % generate the sinc function on the grid
figure;
cv=0:.1:1.;             % contours will be from 0 to 1 in steps of 0.1
[C,h]=contour(U,V,Ha,cv); % generate the contour plot, including values to label contours
axis square
clabel(C,h,'FontSize',6)             %label the contours
xlabel('\itu \rm (c/px)'), ylabel('\itv \rm(c/px)');
set(gca,'ydir','reverse'); 
set(gca,'XAxisLocation','top');      %Xaxis labels on top
set(gca,'fontname','times')          %set the plot font to Times
colormap([0 0 0]);     % use black only
hold on

%Plot the reciprocal lattice points
%sampling matrix of reciprocal lattice and portion of R^2 to plot
VLs = [.5 0; -.5 1];
UL = [-1.2 -1.2]';
LR = [1.2 1.2]';
%get the points of the lattice in the desired region 
xoutLs = Lattice_Points_2d(VLs,UL,LR);
%plot the points
points = plot(xoutLs(:,1),xoutLs(:,2),'k.');
points(1).MarkerSize= 9;

%Plot the Voronoi unit cell
diamond = polyshape([0 .5 0 -.5],[-.5 0 .5 0]);
plot(diamond,'Facecolor','white')

set(gcf,'Color',[1 1 1]);