예제 #1
0
파일: tutest.c 프로젝트: gnovak/bin
void tutest(float data1[], unsigned long n1, float data2[], unsigned long n2,
	float *t, float *prob)
{
	void avevar(float data[], unsigned long n, float *ave, float *var);
	float betai(float a, float b, float x);
	float var1,var2,df,ave1,ave2;

	avevar(data1,n1,&ave1,&var1);
	avevar(data2,n2,&ave2,&var2);
	*t=(ave1-ave2)/sqrt(var1/n1+var2/n2);
	df=SQR(var1/n1+var2/n2)/(SQR(var1/n1)/(n1-1)+SQR(var2/n2)/(n2-1));
	*prob=betai(0.5*df,0.5,df/(df+SQR(*t)));
}
예제 #2
0
void ttest(float data1[], unsigned long n1, float data2[], unsigned long n2,
	float *t, float *prob)
{
	void avevar(float data[], unsigned long n, float *ave, float *var);
	float betai(float a, float b, float x);
	float var1,var2,svar,df,ave1,ave2;

	avevar(data1,n1,&ave1,&var1);
	avevar(data2,n2,&ave2,&var2);
	df=n1+n2-2;
	svar=((n1-1)*var1+(n2-1)*var2)/df;
	*t=(ave1-ave2)/sqrt(svar*(1.0/n1+1.0/n2));
	*prob=betai(0.5*df,0.5,df/(df+(*t)*(*t)));
}
예제 #3
0
파일: fasper.c 프로젝트: lnls-fac/code
void fasper(float x[], float y[], unsigned long n, float ofac, float hifac,
	float wk1[], float wk2[], unsigned long nwk, unsigned long *nout,
	unsigned long *jmax, float *prob)
{
	void avevar(float data[], unsigned long n, float *ave, float *var);
	void realft(float data[], unsigned long n, int isign);
	void spread(float y, float yy[], unsigned long n, float x, int m);
	unsigned long j,k,ndim,nfreq,nfreqt;
	float ave,ck,ckk,cterm,cwt,den,df,effm,expy,fac,fndim,hc2wt;
	float hs2wt,hypo,pmax,sterm,swt,var,xdif,xmax,xmin;

	*nout=0.5*ofac*hifac*n;
	nfreqt=ofac*hifac*n*MACC;
	nfreq=64;
	while (nfreq < nfreqt) nfreq <<= 1;
	ndim=nfreq << 1;
	if (ndim > nwk) nrerror("workspaces too small in fasper");
	avevar(y,n,&ave,&var);
	if (var == 0.0) nrerror("zero variance in fasper");
	xmin=x[1];
	xmax=xmin;
	for (j=2;j<=n;j++) {
		if (x[j] < xmin) xmin=x[j];
		if (x[j] > xmax) xmax=x[j];
	}
	xdif=xmax-xmin;
	for (j=1;j<=ndim;j++) wk1[j]=wk2[j]=0.0;
	fac=ndim/(xdif*ofac);
	fndim=ndim;
	for (j=1;j<=n;j++) {
		ck=(x[j]-xmin)*fac;
		MOD(ck,fndim)
		ckk=2.0*(ck++);
		MOD(ckk,fndim)
		++ckk;
		spread(y[j]-ave,wk1,ndim,ck,MACC);
		spread(1.0,wk2,ndim,ckk,MACC);
	}
	realft(wk1,ndim,1);
	realft(wk2,ndim,1);
	df=1.0/(xdif*ofac);
	pmax = -1.0;
	for (k=3,j=1;j<=(*nout);j++,k+=2) {
		hypo=sqrt(wk2[k]*wk2[k]+wk2[k+1]*wk2[k+1]);
		hc2wt=0.5*wk2[k]/hypo;
		hs2wt=0.5*wk2[k+1]/hypo;
		cwt=sqrt(0.5+hc2wt);
		swt=SIGN(sqrt(0.5-hc2wt),hs2wt);
		den=0.5*n+hc2wt*wk2[k]+hs2wt*wk2[k+1];
		cterm=SQR(cwt*wk1[k]+swt*wk1[k+1])/den;
		sterm=SQR(cwt*wk1[k+1]-swt*wk1[k])/(n-den);
		wk1[j]=j*df;
		wk2[j]=(cterm+sterm)/(2.0*var);
		if (wk2[j] > pmax) pmax=wk2[(*jmax=j)];
	}
	expy=exp(-pmax);
	effm=2.0*(*nout)/ofac;
	*prob=effm*expy;
	if (*prob > 0.01) *prob=1.0-pow(1.0-expy,effm);
}
예제 #4
0
void tptest(float data1[], float data2[], unsigned long n, float *t,
            float *prob)
{
    void avevar(float data[], unsigned long n, float *ave, float *var);
    float betai(float a, float b, float x);
    unsigned long j;
    float var1,var2,ave1,ave2,sd,df,cov=0.0;

    avevar(data1,n,&ave1,&var1);
    avevar(data2,n,&ave2,&var2);
    for (j=1; j<=n; j++)
        cov += (data1[j]-ave1)*(data2[j]-ave2);
    cov /= df=n-1;
    sd=sqrt((var1+var2-2.0*cov)/n);
    *t=(ave1-ave2)/sd;
    *prob=betai(0.5*df,0.5,df/(df+(*t)*(*t)));
}
예제 #5
0
파일: ftest.cpp 프로젝트: HunterAllman/kod
void NR::ftest(Vec_I_DP &data1, Vec_I_DP &data2, DP &f, DP &prob)
{
	DP var1,var2,ave1,ave2,df1,df2;

	int n1=data1.size();
	int n2=data2.size();
	avevar(data1,ave1,var1);
	avevar(data2,ave2,var2);
	if (var1 > var2) {
		f=var1/var2;
		df1=n1-1;
		df2=n2-1;
	} else {
		f=var2/var1;
		df1=n2-1;
		df2=n1-1;
	}
	prob = 2.0*betai(0.5*df2,0.5*df1,df2/(df2+df1*f));
	if (prob > 1.0) prob=2.0-prob;
}
예제 #6
0
void ftest(float data1[], unsigned long n1, float data2[], unsigned long n2,
	float *f, float *prob)
{
	void avevar(float data[], unsigned long n, float *ave, float *var);
	float beta1(float a, float b, float x);
	float var1, var2, ave1, ave2, df1, df2;

	avevar(data1, n1, &ave1, &var1);
	avevar(data2, n2, &ave2, &var2);
	if (var1 > var2) {
		*f = var1 / var2;
		df1 = n1 - 1;
		df2 = n2 - 1;
	}
	else {
		*f = var2 / var1;
		df1 = n2 - 1;
		df2 = n1 - 1;
	}
	*prob = 2.0*betai(0.5*df2, 0.5*df1, df2 / (df2 + df1*(*f)));
	if (*prob > 1.0) *prob = 2.0 - *prob;
}
예제 #7
0
void period(float x[], float y[], int n, float ofac, float hifac, float px[],
	float py[], int np, int *nout, int *jmax, float *prob)
{
	void avevar(float data[], unsigned long n, float *ave, float *var);
	int i,j;
	float ave,c,cc,cwtau,effm,expy,pnow,pymax,s,ss,sumc,sumcy,sums,sumsh,
		sumsy,swtau,var,wtau,xave,xdif,xmax,xmin,yy;
	double arg,wtemp,*wi,*wpi,*wpr,*wr;

	wi=dvector(1,n);
	wpi=dvector(1,n);
	wpr=dvector(1,n);
	wr=dvector(1,n);
	*nout=0.5*ofac*hifac*n;
	if (*nout > np) nrerror("output arrays too short in period");
	avevar(y,n,&ave,&var);
	xmax=xmin=x[1];
	for (j=1;j<=n;j++) {
		if (x[j] > xmax) xmax=x[j];
		if (x[j] < xmin) xmin=x[j];
	}
	xdif=xmax-xmin;
	xave=0.5*(xmax+xmin);
	pymax=0.0;
	pnow=1.0/(xdif*ofac);
	for (j=1;j<=n;j++) {
		arg=TWOPID*((x[j]-xave)*pnow);
		wpr[j] = -2.0*SQR(sin(0.5*arg));
		wpi[j]=sin(arg);
		wr[j]=cos(arg);
		wi[j]=wpi[j];
	}
	for (i=1;i<=(*nout);i++) {
		px[i]=pnow;
		sumsh=sumc=0.0;
		for (j=1;j<=n;j++) {
			c=wr[j];
			s=wi[j];
			sumsh += s*c;
			sumc += (c-s)*(c+s);
		}
		wtau=0.5*atan2(2.0*sumsh,sumc);
		swtau=sin(wtau);
		cwtau=cos(wtau);
		sums=sumc=sumsy=sumcy=0.0;
		for (j=1;j<=n;j++) {
			s=wi[j];
			c=wr[j];
			ss=s*cwtau-c*swtau;
			cc=c*cwtau+s*swtau;
			sums += ss*ss;
			sumc += cc*cc;
			yy=y[j]-ave;
			sumsy += yy*ss;
			sumcy += yy*cc;
			wr[j]=((wtemp=wr[j])*wpr[j]-wi[j]*wpi[j])+wr[j];
			wi[j]=(wi[j]*wpr[j]+wtemp*wpi[j])+wi[j];
		}
		py[i]=0.5*(sumcy*sumcy/sumc+sumsy*sumsy/sums)/var;
		if (py[i] >= pymax) pymax=py[(*jmax=i)];
		pnow += 1.0/(ofac*xdif);
	}
	expy=exp(-pymax);
	effm=2.0*(*nout)/ofac;
	*prob=effm*expy;
	if (*prob > 0.01) *prob=1.0-pow(1.0-expy,effm);
	free_dvector(wr,1,n);
	free_dvector(wpr,1,n);
	free_dvector(wpi,1,n);
	free_dvector(wi,1,n);
}
예제 #8
0
void period( double x[], double y[], int n, double ofac, double hifac, double px[],
						double py[], int np, int *nout, int *jmax, double *prob )
{
	// - raèuna Lombov periodogram za nejednoliko razmaknute toèke
	//   x - niz apscisa u kojima imamo vrijednosti
	//   y - niz vrijednosti promatrane varijable u danim toèkama apscise
	//   n - broj toèaka
	//   ofac - oversampling faktor (tipièno 4 ili više )
	//   hifac - do koje frekvencije idemo ( do hifac * Nyquistova frekvencija )
	//   np = ofac * hifac / 2 * N
	//   px[1..np] - niz frekvencija za koje je izraèunat periodogram
	//   py[1..np] - vrijednosti periodograma
	//   jmax - py[jmax] je maksimalni element u py[]
	//   prob -  procjena znaèenja tog maksimuma prema hipotetskom sluèajnm šumu 
	//           ( mala vrijednost indicira da postoji znaèajan periodièki signal)
	int			i,j;
	double		ave, c, cc, cwtau, effm, expy, pnow, pymax, s, ss, sumc, sumcy, sums, sumsh,
			sumsy, swtau, var, wtau, xave, xdif, xmax, xmin, yy;
	double	arg, wtemp, *wi, *wpi, *wpr, *wr;

	wi = new double[n+1];
	wpi = new double[n+1];
	wpr = new double[n+1];
	wr = new double[n+1];

	*nout = (int) (0.5 * ofac * hifac * n);

	if( *nout > np )
	{
		printf("output arrays too short in period ");
		assert(0);
	}
	avevar(y,n, &ave, &var);
	xmax = xmin = x[1];
	for( j=1; j<=n; j++ )
	{
		if( x[j] > xmax )
			xmax = x[j];
		if( x[j] < xmin )
			xmin = x[j];
	}
	xdif = xmax - xmin;
	xave = 0.5 * (xmax + xmin);
	pymax = 0.0;
	pnow = 1.0 / (xdif * ofac);
	for( j=1; j<=n; j++ )
	{
		arg = TWOPID * ((x[j] - xave)*pnow);
		wpr[j] = -2.0 * DSQR(sin(0.5*arg));
		wpi[j] = sin(arg);
		wr[j] = cos(arg);
		wi[j] = wpi[j];
	}
	for( i=1; i<=(*nout); i++ )
	{
		px[i] = pnow;
		sumsh = sumc = 0.0;
		for( j=1; j<=n; j++ )
		{
			c = wr[j];
			s = wi[j];
			sumsh += s * c;
			sumc += (c-s)*(c+s);
		}
		wtau = 0.5 * atan2(2.0*sumsh, sumc);
		swtau = sin(wtau);
		cwtau = cos(wtau);
		sums = sumc = sumsy = sumcy = 0.0;
		for( j=1; j<=n; j++ )
		{
			s = wi[j];
			c = wr[j];
			ss = s * cwtau - c * swtau;
			cc = c * cwtau + s * swtau;
			sums += ss*ss;
			sumc += cc*cc;
			yy = y[j] - ave;
			sumsy += yy*ss;
			sumcy += yy*cc;
			wr[j] = ((wtemp=wr[j])* wpr[j] - wi[j] * wpi[j]) + wr[j];
			wi[j] = (wi[j] * wpr[j] + wtemp * wpi[j]) + wi[j];
		}
		py[i] = 0.5*(sumcy * sumcy/ sumc + sumsy*sumsy/sums) / var;
		if( py[i] >= pymax )
			pymax = py[(*jmax=i)];
		pnow += 1.0 / (ofac * xdif);
	}
	expy = exp(-pymax);
	effm = 2.0*(*nout) / ofac;
	*prob = effm * expy;
	if( *prob > 0.01 ) 
		*prob = 1.0-pow(1.0-expy, effm);

	delete []wr;
	delete []wi;
	delete []wpi;
	delete []wpr;
}
예제 #9
0
VImage
PairedTest(VImage *src1, VImage *src2, VImage dest, int n, VShort type) {
    int i, j, k, b, r, c, nslices, nrows, ncols;
    float ave1, ave2, var1, var2, nx, u;
    float sum, smooth = 0;
    float t, z, df, sd, cov, *data1 = NULL, *data2 = NULL;
    float tiny = 1.0e-10;
    nslices = VImageNBands(src1[0]);
    nrows   = VImageNRows(src1[0]);
    ncols   = VImageNColumns(src1[0]);
    gsl_set_error_handler_off();
    dest = VCopyImage(src1[0], NULL, VAllBands);
    VFillImage(dest, VAllBands, 0);
    VSetAttr(VImageAttrList(dest), "num_images", NULL, VShortRepn, (VShort)n);
    VSetAttr(VImageAttrList(dest), "patient", NULL, VStringRepn, "paired_ttest");
    if(type == 0)
        VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, "tmap");
    else
        VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, "zmap");
    VSetAttr(VImageAttrList(dest), "df", NULL, VShortRepn, (VShort)(n - 1));
    /* get smoothness estimates */
    sum = nx = 0;
    for(i = 0; i < n; i++) {
        if(VGetAttr(VImageAttrList(src1[i]), "smoothness", NULL, VFloatRepn, &smooth) == VAttrFound) {
            sum += smooth;
            nx++;
        }
    }
    for(i = 0; i < n; i++) {
        if(VGetAttr(VImageAttrList(src2[i]), "smoothness", NULL, VFloatRepn, &smooth) == VAttrFound) {
            sum += smooth;
            nx++;
        }
    }
    if(nx > 1) {
        VSetAttr(VImageAttrList(dest), "smoothness", NULL, VFloatRepn, sum / nx);
    }
    data1 = (float *) VMalloc(n * sizeof(float));
    data2 = (float *) VMalloc(n * sizeof(float));
    df = n - 1;
    nx = (float)n;
    for(b = 0; b < nslices; b++) {
        for(r = 0; r < nrows; r++) {
            for(c = 0; c < ncols; c++) {
                k = 0;
                for(i = 0; i < n; i++) {
                    data1[i] = VPixel(src1[i], b, r, c, VFloat);
                    data2[i] = VPixel(src2[i], b, r, c, VFloat);
                    if(ABS(data1[i]) > tiny && ABS(data2[i]) > tiny)
                        k++;
                }
                if(k < n - 3)
                    continue;
                avevar(data1, n, &ave1, &var1);
                avevar(data2, n, &ave2, &var2);
                if(var1 < tiny || var2 < tiny)
                    continue;
                z = t = 0;
                cov = 0;
                for(j = 0; j < n; j++)
                    cov += (data1[j] - ave1) * (data2[j] - ave2);
                cov /= df;
                u = (var1 + var2 - 2.0 * cov);
                if(u < tiny)
                    continue;
                sd = sqrt(u / nx);
                if(sd < tiny)
                    continue;
                t = (ave1 - ave2) / sd;
                if(isnan(t) || isinf(t))
                    continue;
                switch(type) {
                case 0:
                    VPixel(dest, b, r, c, VFloat) = t;
                    break;
                case 1:
                    /* z = t2z_approx(t,df); */
                    z = t2z((double)t, (double)df);
                    if(t < 0)
                        z = -z;
                    VPixel(dest, b, r, c, VFloat) = z;
                    break;
                default:
                    VError(" illegal type");
                }
            }
        }
    }
    return dest;
}
예제 #10
0
파일: fitexy.c 프로젝트: bamford/astrobamf
void fitexy(float x[], float y[], int ndat, float sigx[], float sigy[],
	float *a, float *b, float *siga, float *sigb, float *chi2, float *q)
{
	void avevar(float data[], unsigned long n, float *ave, float *var);
	float brent(float ax, float bx, float cx,
		float (*f)(float), float tol, float *xmin);
	float chixy(float bang);
	void fit(float x[], float y[], int ndata, float sig[], int mwt,
		float *a, float *b, float *siga, float *sigb, float *chi2, float *q);
	float gammq(float a, float x);
	void mnbrak(float *ax, float *bx, float *cx, float *fa, float *fb,
		float *fc, float (*func)(float));
	float zbrent(float (*func)(float), float x1, float x2, float tol);
	int j;
	float swap,amx,amn,varx,vary,ang[7],ch[7],scale,bmn,bmx,d1,d2,r2,
		dum1,dum2,dum3,dum4,dum5;

	xx=vector(1,ndat);
	yy=vector(1,ndat);
	sx=vector(1,ndat);
	sy=vector(1,ndat);
	ww=vector(1,ndat);
	avevar(x,ndat,&dum1,&varx);
	avevar(y,ndat,&dum1,&vary);
	scale=sqrt(varx/vary);
	nn=ndat;
	for (j=1;j<=ndat;j++) {
		xx[j]=x[j];
		yy[j]=y[j]*scale;
		sx[j]=sigx[j];
		sy[j]=sigy[j]*scale;
		ww[j]=sqrt(SQR(sx[j])+SQR(sy[j]));
	}
	fit(xx,yy,nn,ww,1,&dum1,b,&dum2,&dum3,&dum4,&dum5);
	offs=ang[1]=0.0;
	ang[2]=atan(*b);
	ang[4]=0.0;
	ang[5]=ang[2];
	ang[6]=POTN;
	for (j=4;j<=6;j++) ch[j]=chixy(ang[j]);
	mnbrak(&ang[1],&ang[2],&ang[3],&ch[1],&ch[2],&ch[3],chixy);
	*chi2=brent(ang[1],ang[2],ang[3],chixy,ACC,b);
	*chi2=chixy(*b);
	*a=aa;
	*q=gammq(0.5*(nn-2),*chi2*0.5);
	for (r2=0.0,j=1;j<=nn;j++) r2 += ww[j];
	r2=1.0/r2;
	bmx=BIG;
	bmn=BIG;
	offs=(*chi2)+1.0;
	for (j=1;j<=6;j++) {
		if (ch[j] > offs) {
			d1=fabs(ang[j]-(*b));
			while (d1 >= PI) d1 -= PI;
			d2=PI-d1;
			if (ang[j] < *b) {
				swap=d1;
				d1=d2;
				d2=swap;
			}
			if (d1 < bmx) bmx=d1;
			if (d2 < bmn) bmn=d2;
		}
	}
	if (bmx < BIG) {
		bmx=zbrent(chixy,*b,*b+bmx,ACC)-(*b);
		amx=aa-(*a);
		bmn=zbrent(chixy,*b,*b-bmn,ACC)-(*b);
		amn=aa-(*a);
		*sigb=sqrt(0.5*(bmx*bmx+bmn*bmn))/(scale*SQR(cos(*b)));
		*siga=sqrt(0.5*(amx*amx+amn*amn)+r2)/scale;
	} else (*sigb)=(*siga)=BIG;
	*a /= scale;
	*b=tan(*b)/scale;
	free_vector(ww,1,ndat);
	free_vector(sy,1,ndat);
	free_vector(sx,1,ndat);
	free_vector(yy,1,ndat);
	free_vector(xx,1,ndat);
}
예제 #11
0
void
VROIpaired_ttest(VImage *src1, VImage *src2, VImage *mask, int n, int nmask, FILE *fp) {
    VString str;
    int i, j, id, b, r, c, c0, c1, nROI = 0;
    float ave1 = 0, ave2 = 0, var1 = 0, var2 = 0;
    float sum1 = 0, sum2 = 0, u, mx;
    float t, z, p, df, sd, cov, *data1 = NULL, *data2 = NULL;
    float tiny = 1.0e-8;
    float xa, ya, za, xx, yy, zz, voxelsize = 1;
    double mean[3];
    Volumes *volumes;
    Volume vol;
    VTrack tc;
    VBoolean found = FALSE;
    gsl_set_error_handler_off();
    /*
    ** get ROIs from mask
    */
    fprintf(stderr, "\n List of ROIs:\n");
    fprintf(fp, "\n List of ROIs:\n");
    volumes = (Volumes *) VCalloc(nmask, sizeof(Volumes));
    nROI = 0;
    for(i = 0; i < nmask; i++) {
        fprintf(stderr, "\n Mask %2d:\n", i + 1);
        fprintf(fp, "\n Mask %2d:\n", i + 1);
        volumes[i] = VImage2Volumes(mask[i]);
        voxelsize = 1;
        if(VGetAttr(VImageAttrList(mask[i]), "voxel", NULL,
                    VStringRepn, (VPointer) & str) == VAttrFound) {
            sscanf(str, "%f %f %f", &xa, &ya, &za);
            voxelsize = xa * ya * za;
        }
        fprintf(stderr, " ROI              addr               size(mm^3)\n");
        fprintf(stderr, "-----------------------------------------------\n");
        fprintf(fp, " ROI              addr               size(mm^3)\n");
        fprintf(fp, "-----------------------------------------------\n");
        for(vol = volumes[i]->first; vol != NULL; vol = vol->next) {
            VolumeCentroid(vol, mean);
            if(nROI < vol->label)
                nROI = vol->label;
            b = mean[0];
            r = mean[1];
            c = mean[2];
            xx = mean[2];
            yy = mean[1];
            zz = mean[0];
            VGetTalCoord(src1[0], zz, yy, xx, &xa, &ya, &za);
            id = vol->label;
            fprintf(stderr, " %2d    %7.2f  %7.2f  %7.2f    %7.0f\n",
                    id, xa, ya, za, voxelsize *(double)VolumeSize(vol));
            fprintf(fp, " %2d    %7.2f  %7.2f  %7.2f    %7.0f\n",
                    id, xa, ya, za, voxelsize *(double)VolumeSize(vol));
        }
    }
    fprintf(stderr, "\n\n");
    fprintf(fp, "\n\n");
    /* check consistency */
    if(nROI < 1)
        VError(" no ROIs found");
    CheckROI(volumes, nmask, nROI);
    /*
    ** process each ROI
    */
    fprintf(stderr, "\n");
    fprintf(stderr, "  ROI          mean          t        z       p   \n");
    fprintf(stderr, " --------------------------------------------------\n");
    if(fp) {
        fprintf(fp, "\n");
        fprintf(fp, "  ROI          mean          t        z       p   \n");
        fprintf(fp, " --------------------------------------------------\n");
    }
    df = n - 1;
    data1 = (float *) VCalloc(n, sizeof(float));
    data2 = (float *) VCalloc(n, sizeof(float));
    for(id = 1; id <= nROI; id++) {
        for(i = 0; i < n; i++) {
            j = 0;
            if(nmask > 1)
                j = i;
            found = FALSE;
            for(vol = volumes[j]->first; vol != NULL; vol = vol->next) {
                if(vol->label != id)
                    continue;
                found = TRUE;
                sum1 = sum2 = mx = 0;
                for(j = 0; j < VolumeNBuckets(vol); j++) {
                    for(tc = VFirstTrack(vol, j); VTrackExists(tc); tc = VNextTrack(tc)) {
                        b  = tc->band;
                        r  = tc->row;
                        c0 = tc->col;
                        c1 = c0 + tc->length;
                        for(c = c0; c < c1; c++) {
                            u = VPixel(src1[i], b, r, c, VFloat);
                            if(ABS(u) < tiny)
                                continue;
                            sum1 += u;
                            u = VPixel(src2[i], b, r, c, VFloat);
                            if(ABS(u) < tiny)
                                continue;
                            sum2 += u;
                            mx++;
                        }
                    }
                }
                if(mx < 1) {
                    VWarning(" no voxels in ROI %d of mask %d", id, i);
                    data1[i] = data2[i] = 0;
                    continue;
                }
                data1[i] = sum1 / mx;
                data2[i] = sum2 / mx;
            }
            if(!found)
                goto next;
        }
        avevar(data1, n, &ave1, &var1);
        avevar(data2, n, &ave2, &var2);
        if(var1 < tiny || var2 < tiny) {
            VWarning(" no variance in ROI %d", id);
            continue;
        }
        z = t = p = 0;
        cov = 0;
        for(j = 0; j < n; j++)
            cov += (data1[j] - ave1) * (data2[j] - ave2);
        cov /= df;
        sd = sqrt((var1 + var2 - 2.0 * cov) / (df + 1.0));
        if(sd < tiny)
            continue;
        t = (ave1 - ave2) / sd;
        if(ABS(t) < tiny)
            p = z = 0;
        else {
            p = t2p((double)t, (double)df);
            z = t2z((double)t, (double)df);
            if(t < 0)
                z = -z;
        }
        fprintf(stderr, " %3d   %8.4f (%.3f)  %7.3f  %7.3f  %7.4f\n",
                id, (ave1 - ave2), sd, t, z, p);
        if(fp)
            fprintf(fp, " %3d  %8.4f (%.3f)  %7.3f  %7.3f  %7.4f\n",
                    id, (ave1 - ave2), sd, t, z, p);
next:
        ;
    }
    fprintf(stderr, "\n");
    if(fp) {
        fprintf(fp, "\n");
        fclose(fp);
    }
}
예제 #12
0
double ls_oneperiod(double *x, double *y, int n, double period)
{
  /* This routine is based on the "period" function in Numerical Recipes in C by Press et al., it skips the loop over periods though */
  void avevar(double *data, int n, double *ave, double *var);
  int j;
  double ave, c, cc, cwtau, pnow, pymax, s, ss, sumc, sumcy, sums, sumsh, sumsy, swtau, var, wtau, xave, xdif, xmax, xmin, yy;
  double arg, *wi, *wpi, *wpr, *wr;

  wi = (double *) malloc(n*sizeof(double));
  wpi = (double *) malloc(n * sizeof(double));
  wpr = (double *) malloc(n * sizeof(double));
  wr = (double *) malloc(n * sizeof(double));
  avevar(y,n,&ave,&var);
  xmax = xmin=x[0];
  for (j=0;j<n;j++) {
    if(x[j] > xmax) xmax = x[j];
    if(x[j] < xmin) xmin = x[j];
  }
  xdif = xmax - xmin;
  xave = 0.5*(xmax + xmin);
  pymax = 0.0;
  pnow = 1.0/(period);
  for (j=0;j<n;j++) {
    arg = TWOPID*((x[j]-xave)*pnow);
    wpr[j] = -2.0*SQR(sin(0.5*arg));
    wpi[j] = sin(arg);
    wr[j] = cos(arg);
    wi[j] = wpi[j];
  }

  pnow = 1./period;
  sumsh = sumc = 0.0;
  for(j=0;j<n;j++) {
    c = wr[j];
    s = wi[j];
    sumsh += s*c;
    sumc += (c-s)*(c+s);
  }
  wtau = 0.5*atan2(2.0*sumsh,sumc);
  swtau = sin(wtau);
  cwtau = cos(wtau);
  sums=sumc=sumsy=sumcy=0.0;
  for(j=0;j<n;j++) {
    s = wi[j];
    c = wr[j];
    ss = s*cwtau-c*swtau;
    cc = c*cwtau+s*swtau;
    sums += ss*ss;
    sumc += cc*cc;
    yy = y[j] - ave;
    sumsy += yy*ss;
    sumcy += yy*cc;
  }
  pymax=0.5*(sumcy*sumcy/sumc+sumsy*sumsy/sums)/var;

  free(wi);
  free(wpi);
  free(wpr);
  free(wr);

  return pymax;
}