% dip06_3.m          gr19mar15
% DCT, DFT and image reconstruction
%
clear all,%  close all

f = double(imread('.\img\cameraman.tif'));
F = fftshift(fft2(f));
G = dct2(f);  % note: G is real; in general, Gmin<0, Gmax>0

figure(1); imshow(log(abs(F)+1e-5),[]);
G1 = ( G-mean(G(:)) ) ./ std(G(:));  % scale to mean=0 std=1
G2 = 1 ./ (1+exp(-3*G1));	% and map with a steep sigmoid
figure(2); imshow(G2,[]);

return;

%%
for k=[2,5,10,20,30,60,100,128]; %  max. 128
  mask=zeros(256,256); 
  mask((128-k+1):(128+k),(128-k+1):(128+k))=1;
  F1 = F .* mask;       % keep 2k*2k coeff. (complex and symm.)
  f1 = real(ifft2(fftshift(F1)));

  mask=zeros(256,256); 
  mask(1:(2*k),1:(2*k))=1;
  G1 = G .* mask;       % keep 2k*2k coeff. (real)
  g1 = idct2(G1);
  figure(1); imshow([f1 g1],[0 255]); 
  figure(2); imshow(10*[f1-f  g1-f]+128, [0 255]);   pause();
end;

% note: truncation yields ringing in the data domain 
% note: DFT suffers from periodic repetition artifacts
%    (e.g. see first rows in cameraman)

return;
