function [y,met]=filter4x4(x)

m=[18 22];  %threshold for switching among filters
diff=comp_diff(x);  %compute sum of difference between adjacent pixel values
sd=std(x(:));       %compute standard deviation

%met = 0 corresponds to filter1  (just subsample)
%met = 128 corresponds to filter2 (average and then find median)
%met = 255 corresponds to linear filter

%which filter is used based on diff/sd and threshold m

if sd == 0
   y=filter1(x);
   met=0;
elseif diff/sd < m(1)
   y=filter1(x);
   met=0;
elseif diff/sd < m(2)
   y=filter2(x);
   met=128;
else
   y=filter3(x);
   met=255;
end

%filter1
function y=filter1(x)
y=x(3,3);

%filter2
function y=filter2(x)
temp1=mean([x(2,2) x(3,2) x(3,3)]);
temp2=mean([x(2,2) x(3,2) x(2,3)]);
temp3=mean([x(2,2) x(2,3) x(3,3)]);
temp4=mean([x(2,3) x(3,2) x(3,3)]);
y=median([temp1 temp2 temp3 temp4]);

%filter3
function y=filter3(x)
%w=[1/10 2/5 2/5 1/10];
w=[.18 .32 .32 .18];
w=w'*w;
y=sum(sum(w.*x));

