%% function to show effect of eliminating low energy coefficients 
% usage clippic(pic,pct)
% pic = picture name for load command (picture must be stored as X)
% pct is the cutoff percent coefficient less than pct*maxcoeff/100 are discarded 

% S. Allen Broughton - 29 Oct 10

function z=clippic(pic,pct)
    cutoff_pct = pct

    load(pic);
    X=X-mean(X(:)); % eliminate the DC coefficient

    fX = fft2(X);
    [m,n] = size(X);
    picsize = m*n;

    afX = abs(fX);
    M = max(afX(:));
    J =  find(afX < M*(cutoff_pct/100));
    fXtr = fX;
    fXtr(J) = 0;
    Xtr = ifft2(fXtr);
    
    sizeJ = length(J);
    num_retained_coeffs = picsize-sizeJ
    percent_retained_coeffs =  100-100*sizeJ/picsize
    eX = energy(X);
    eXtr = energy(Xtr);
    percent_retained_energy = 100*eXtr/eX

    figure(1)
    imagesc(X)
    colormap(gray)
    figure(2)
    imagesc(real(Xtr))
    colormap(gray)

    figure(3)
    hist(log10(afX(:)+0.01),100)
