%function teta = dTeta(dist)
% dTeta - calculates coefficient of "good data"
%	teta = dTeta(dist)
%	dist - matrix of distances between points
%	teta - matrix of coeficients, same size as dist
%	

global dist teta

tic;
DiffTol = 0.05;
MinStd  = 5;
DiffDiffStd= 2;
StdTol  = 4;
AsymCoef = 0.1;
MeanDist = mean(mean(dist));
WindLen  = 50;
n = size(dist,2);
k = size(dist,1)/n;
teta = ones(size(dist));
x1 = zeros(size(dist,1),1); x2 = zeros(size(dist,1),1);
 [num,den] = butter(3, 0.3);


for i = 1:n-1, 
disp(i);
    for j = i+1:n,
	x1 = dSpikeEliminate(dExtract( dist, i,j));	s1 = std(x1);
        x1 = filtfilt(num,den, x1 );
	ddx = diff(diff(x1)); 	sddx1 = std(dSpikeEliminate(ddx));
	if( s1 < MinStd  | sddx1 > DiffDiffStd ),
	   teta((0:k-1)*n+i, j ) = zeros(k,1);
        else
	   teta((0:k-1)*n+i, j ) =  ones(k,1);
        end;

	x2 = dSpikeEliminate(dExtract( dist, j,i));	s2 = std(x2);
        x2 = filtfilt(num,den, x2 );
	ddx = diff(diff(x2)); 	sddx2 = std(dSpikeEliminate(ddx));
	if( s2 < MinStd  | sddx2 > DiffDiffStd  ),
	   teta((0:k-1)*n+j, i ) = zeros(k,1);
        else
	   teta((0:k-1)*n+j, i ) =  ones(k,1);
        end;

	dist((0:k-1)*n+i, j ) = x1;
	dist((0:k-1)*n+j, i ) = x2;
end; end;
toc

clear AsymCoef MeanDist MinStd WindLen DiffTol StdTol DiffDiffStd DdxLimit         
clear   ans  i j   q2 ddx  x1 x2 n s1 s2  q sddx1 sddx2 q1 den num

%save distteta dist teta h w k 

%j=j+1; subplot(211); plot(dExtract( dist, i,j)); subplot(212); plot(dExtract( teta, i,j));

