function compress(fn, threshold, rate)

img = imread(fn);
img = im2double(img);
subplot(2,2,1), imshow(img), title 'Original Image';
T = 0.3; 
Ts = Inf;
img_fd = fft2(img);
magn_fd = abs(img_fd);
[r c] = size(magn_fd);
if rate > 1
    rate_r = 1:(rate-1)/(r-1):rate;
    rate_c = 1:(rate-1)/(c-1):rate;
    % extend to 2D
    rate2d = (repmat(rate_r',1,c)+repmat(rate_c,r,1))/2;
    magn_fd = magn_fd .* rate2d;    
end
while Ts > threshold
    insig = magn_fd ./ max(magn_fd(:)) < T;
    img_fd = fft2(img);
    img_fd(insig) = 0;
    img2 = real(ifft2(img_fd));
    Ts = sqrt(sum(sum(((img - img2).^2))))/(r*c);
    subplot(2,2,2), imshow(img2),...
        title(strcat('T=',num2str(T,'%0.2g')));
    T = T / 1.2;
    pause(0.01);
end
    