function CS6640_Week6
%

BASE = 25;

% Set up an example function: 
%    sin of frequency 1

Fs = 8;
T = 1/Fs;
L = 8;
t = (0:L-1)*T;
f = sin(2*pi*t);
figure(1);
clf
plot([0:7],f);
hold on
plot([0:7],f,'ro');
axis equal

W = zeros(8,8);

% For u = 0, for x = 0:7
%   get [e^(-j*2*pi*0*x/8)
W(1,:) = CS6640_basis_FT1D(0,8);

% For u = 1, for x = 0:7
%   get [e^(-j*2*pi*1*x/8)
W(2,:) = CS6640_basis_FT1D(1,8);

W(3,:) = CS6640_basis_FT1D(2,8);
W(4,:) = CS6640_basis_FT1D(3,8);
W(5,:) = CS6640_basis_FT1D(4,8);
W(6,:) = CS6640_basis_FT1D(5,8);
W(7,:) = CS6640_basis_FT1D(6,8);
W(8,:) = CS6640_basis_FT1D(7,8);

figure(2);
clf
plot(0,0,'w.');
hold on
plot([0:7],BASE+W(1,:));
plot([0:7],BASE+W(1,:),'ko');

plot([0:7],BASE-2+real(W(2,:)));
plot([0:7],BASE-2+real(W(2,:)),'ko');
plot([0:7],BASE-2+imag(W(2,:)),'r');
plot([0:7],BASE-2+imag(W(2,:)),'ro');

plot([0:7],BASE-5+real(W(3,:)),'k');
plot([0:7],BASE-5+real(W(3,:)),'ko');
plot([0:7],BASE-5+imag(W(3,:)),'r');
plot([0:7],BASE-5+imag(W(3,:)),'ro');

plot([0:7],BASE-8+real(W(4,:)),'k');
plot([0:7],BASE-8+real(W(4,:)),'ko');
plot([0:7],BASE-8+imag(W(4,:)),'r');
plot([0:7],BASE-8+imag(W(4,:)),'ro');

plot([0:7],BASE-11+real(W(5,:)),'k');
plot([0:7],BASE-11+real(W(5,:)),'ko');
plot([0:7],BASE-11+imag(W(5,:)),'r');
plot([0:7],BASE-11+imag(W(5,:)),'ro');

plot([0:7],BASE-14+real(W(6,:)),'k');
plot([0:7],BASE-14+real(W(6,:)),'ko');
plot([0:7],BASE-14+imag(W(6,:)),'r');
plot([0:7],BASE-14+imag(W(6,:)),'ro');

plot([0:7],BASE-17+real(W(7,:)),'k');
plot([0:7],BASE-17+real(W(7,:)),'ko');
plot([0:7],BASE-17+imag(W(7,:)),'r');
plot([0:7],BASE-17+imag(W(7,:)),'ro');

plot([0:7],BASE-20+real(W(8,:)),'k');
plot([0:7],BASE-20+real(W(8,:)),'ko');
plot([0:7],BASE-20+imag(W(8,:)),'r');
plot([0:7],BASE-20+imag(W(8,:)),'ro');

figure(3);
clf
pv = (f.*W(2,:))';
v = zeros(8,1);
plot(-1,-1,'w.');
hold on
plot(1,5,'w.');
for s = 2:8
    v(s) = v(s-1) + pv(s);
    plot([real(v(s-1)),real(v(s))],[imag(v(s-1)),imag(v(s))],'k');
    tch = 0;
end
plot(real(v(8)),imag(v(8)),'ro');
text(real(v(8))-0.4,imag(v(8))+0.5,'0.0 + 4.0i','color','blue');
F = zeros(8,1);
for p = 1:8
    F(p) = sum((f.*W(p,:))');
end

Ffft = fft(f)'

figure(4);
clf
plot([0:7],sqrt(real(F).^2+imag(F).^2));
hold on
plot([0:7],sqrt(real(F).^2+imag(F).^2),'ko');

% 2D

figure(5);
clf

W = CS6640_basis_FT(0,1,8,8);
imshow(imresize(mat2gray(real(W)),[101,101]));
imshow(imresize(mat2gray(imag(W)),[101,101]));

W = CS6640_basis_FT(2,5,8,8);
imshow(imresize(mat2gray(real(W)),[101,101]));
imshow(imresize(mat2gray(imag(W)),[101,101]));

load img;
im1 = CS4640_pad(img,2*480,2*640);
im2 = CS4640_center(im1);
im3 = fft2(im2);
im4 = CS6640_chop(im3,100);
im5 = CS4640_center(real(ifft2(im4)));
figure(1);
clf
imshow(img);
figure(2);
clf
imshow(mat2gray(im5(1:480,1:640)));

% DCT

D = [8,16,24,32,40,48,56,64];
DF = fft(D);
DC = dct(D);
DFt = DF;
DFt(5:end) = 0;
DCt = DC;
DCt(5:end) = 0;
DFti = ifft(DFt);
DCti = idct(DCt);
figure(1);
clf
plot([1:8],D);
hold on
plot([1:8],D,'r*');
plot([1:8],real(DFti),'r');
plot([1:8],real(DFti),'ko');
text(6.5,40,'Truncated FFT Results');
plot([1:8],real(DCti),'g');
plot([1:8],real(DCti),'ko');
text(5,60,'Truncated DCT Results');
