%Lecture_050912.m


nPix = 200;  %number of pixels in each row and column
wDeg = 10;  %degrees of visual angle

[x,y] = meshgrid( linspace(-wDeg/2,wDeg/2,nPix));


img = zeros(size(x));
img(nPix/2,nPix/2) = 1;

figure(1)
clf
plotFFT2(img,x,y);


%% band-pass the spatial frequencies

wCenter = 3;  %c/deg
wSigma = .5; %c/deg

F  =myfft2(img,x,y);

F.amp = F.amp.* exp(-((F.sf-wCenter).^2/(2*wSigma^2)));

newImg = myifft2(F);

figure(1)
clf
plotFFT2(newImg);


% %%
% F  =myfft2(img,x,y);
% 
% sigma = 4;  %c/deg
% centerFreq = 10; %c/deg
% Gaussian = exp(-(F.sf-centerFreq).^2/sigma^2);
% F.amp = F.amp.*Gaussian;
% filt = myifft2(F);
% plotFFT2(filt,x,y,k,20);

%% 2-d Filtering (because ifft2 is not working...)

nPixFilt = 20;  
[xf,yf] = meshgrid(linspace(-1,1,nPixFilt));

%Gaussian filter

%GaussFilt = exp(-(xf.^2+yf.^2)/.5);

GaussFilt = exp(-(xf.^2+yf.^2)/.05)- .2*exp(-(xf.^2+yf.^2)/.25);
%GaussFilt = GaussFilt - mean(GaussFilt(:));

figure(2)
clf

imagesc(GaussFilt);
axis equal
axis tight

img = zeros(size(x));
%img(nPix/2,nPix/2) = 1;

img = floor(rand(size(x))+.01);

figure(1)
clf
subplot(1,2,1)
imagesc(img)
axis equal
axis off
colormap(gray)

filtImg = conv2(img,GaussFilt,'same');

subplot(1,2,2)
imagesc(filtImg)
axis equal
axis off
colormap(gray)


figure(3)
plotFFT2(img-mean(img(:)),x,y);


figure(4)
plotFFT2(filtImg-mean(filtImg(:)),x,y);

figure(2)
plotFFT2(GaussFilt,x,y);

%%

tmp = myifft2(myfft2(GaussFilt));

figure(5)
imagesc(tmp)
colormap(gray)
axis equal
axis off


%%

%'natural' image

img = imread('class_photo.JPG');
figure(1)
% clf
% image(img);
% axis equal
clf




img = mean(img,3);

sz = 1000;
img = img(400:(400+sz),200:(200+sz));
img = flipud(img);

imagesc(img);
colormap(gray)
axis equal
axis tight


figure(2)
clf
plotFFT2(img,[],[],3,.01);


