double CEggStatistics::FractalDimensionDisplay(const char *varName,double *sigvar,long length)
{
	int ndim= 0;
	double dimension= 0.0;

	char tmp[200];
	sprintf(tmp,"Get fractal dimension using variable %s",varName);
	Log(tmp);

	int i,j,k,m,n;
	double maxval= sigvar[0];
	double minval= sigvar[0];
	for(i=1;i < m_nrecords;i++)
	{
		if(sigvar[i] > maxval)
			maxval= sigvar[i];
		if(sigvar[i] < minval)
			minval= sigvar[i];
	}
	// normalize magnitude
	if(minval != 0.0)
	{
		for(i=0;i < length;i++)
		{
			sigvar[i] -= minval;
		}
		maxval -= minval;
	}
	// adjust so that each axis has the same range
	for(i=0;i < length;i++)
	{
		sigvar[i] /= maxval;
		sigvar[i] *= (double)length;
	}
	maxval= (double)length;

	
	long xsize;
	double t,ysize;
	int box[101][101];

	char path[200];
	sprintf(path,"fractal_%s.dat",varName);
	FILE *fp= fopen(path,"wt");
	if(fp)
	{
		for(int s=100;s >= 20;s-=10) // decrement number of boxes per axis
		{
			xsize= length/s;
			ysize= maxval/(double)s;
			for(i=0,m=0;i < length;i+=xsize,m++)
			{
				for(t=0,n=0;t < (long)maxval;t+=ysize,n++)
				{
					box[m][n]= 0;
					for(k=i;k t && sigvar[k] < t+ysize)
						{
							box[m][n]++;
							break;
						}
					}
				}
			}
			long sum= 0;
			for(i=0;i < s;i++)
			{
				for(j=0;j < s;j++)
					if(box[i][j] > 0) sum++;
			}
			if(sum > 0)
			{
				double Ns= log(sum);
				double Rs= log((double)(s));
				double d= Ns/Rs;
				dimension += d; ndim++;
				fprintf(fp,"%d\t%d\t%f\t%f\t%f\n",s,sum,Rs,Ns,d);
			}
			else
				fprintf(fp,"%d\t0");
		}
		dimension /= (double)ndim;
		fprintf(fp,"\nMean= %f\n",dimension);
		fclose(fp);
	}
	return(dimension);
}