double CEggStatistics::LenCalc(double *y,int n)
{
	if(n==0) return(0);
	double len= 0;
	double yant=0.0;
	for(int i=0;i < n;i++)
	{
		if(i > 0)
		{
			len += sqrt(((y[i] - yant)*(y[i] - yant) + (1.0 / (double)(n - 1))*(1.0 / (double)(n - 1))));
		}
		yant= y[i];
	}
	return(len);
}

double CEggStatistics::FractalCarlosSevcik(const char *varName,double *sigvar,long length)
{
	char tmp[200];
	sprintf(tmp,"Get fractal dimension using variable %s",varName);
	Log(tmp);

	char path[200];
	sprintf(path,"csfractal_%s.dat",varName);
	FILE *fp= fopen(path,"wt");

	double d=0.0;
	if(fp)
	{
		double maxval= sigvar[0];
		double minval= sigvar[0];
		for(int i=1;i < length;i++)
		{
			if(sigvar[i] > maxval)
				maxval= sigvar[i];
			if(sigvar[i] < minval)
				minval= sigvar[i];
		}
		for(i=0;i < length;i++)
		{
			sigvar[i]= (sigvar[i]-minval)/(maxval-minval);
		}
		double len= LenCalc (sigvar, length);
		d = 1.0 + log(len) / log(2.0*(double)(length-1));
		fprintf(fp,"%lf\n",d);
		fclose(fp);
	}
	return(d);
}