Exemple #1
0
int main(void)
{
	long idum=(-1984);
	int i,mwt=1;
	float a,abdev,b,chi2,q,siga,sigb;
	float *x,*y,*sig;

	x=vector(1,NDATA);
	y=vector(1,NDATA);
	sig=vector(1,NDATA);
	for (i=1;i<=NPT;i++) {
		x[i]=0.1*i;
		y[i] = -2.0*x[i]+1.0+SPREAD*gasdev(&idum);
		sig[i]=SPREAD;
	}
	fit(x,y,NPT,sig,mwt,&a,&b,&siga,&sigb,&chi2,&q);
	printf("\nAccording to routine FIT the result is:\n");
	printf("   a =  %8.4f   uncertainty:  %8.4f\n",a,siga);
	printf("   b =  %8.4f   uncertainty:  %8.4f\n",b,sigb);
	printf("   chi-squared:  %8.4f  for  %4d  points\n",chi2,NPT);
	printf("   goodness-of-fit:  %8.4f\n",q);
	printf("\nAccording to routine MEDFIT the result is:\n");
	medfit(x,y,NPT,&a,&b,&abdev);
	printf("   a =  %8.4f\n",a);
	printf("   b =  %8.4f\n",b);
	printf("   absolute deviation (per data point): %8.4f\n",abdev);
	printf("   (note: gaussian SPREAD is %8.4f)\n",SPREAD);
	free_vector(sig,1,NDATA);
	free_vector(y,1,NDATA);
	free_vector(x,1,NDATA);
	return 0;
}
Exemple #2
0
void spectrum_wrapper(control *c, timeseries *ts, double *xdata, double *ydata, double *frequency, double *power)
{
    /* 
        wrapper func - to calculate periodgram for given time-series 
        also has the options to remove trends from the data and apply
        a window.
    */
    
    int     i, j, istart;
    double  intercept = 0., abdev = 0., slope = 0.;
    double *ftrx = NULL, *ftix = NULL, *xwk = NULL, *ywk = NULL, scal;
    
    ftrx = allocate_memory_double(ts->lfreq+1, __LINE__);
    ftix = allocate_memory_double(ts->lfreq+1, __LINE__);
    xwk  = allocate_memory_double(ts->nseg+1,  __LINE__); /* periodgram power */
    ywk  = allocate_memory_double(ts->nseg+1,  __LINE__); /* corresponding frequencies */
    
    /* scale autospectrum and setup frequency axis */
    scal = 2.0 / ((double)ts->n50 * (double)ts->nseg * ts->df * ts->ofac);
    ts->factor = scal;    /* store for coherency code */
             
    for (i = 1; i <= ts->n50; i++) {
        
        /* copy data of i'th segment into workspace */
        istart = (int)((i - 1) * (double)ts->nseg / 2.);
        for (j = 1; j <= ts->nseg; j++) {
            xwk[j] = xdata[istart + j];
              ywk[j] = ydata[istart + j];
            
        }
    
        /* detrend the data */
        if (c->DETREND == TRUE) {
            rmtrend(c, ts, ywk, xwk);
        } else if (c->ROBUSTDETREND == TRUE) {
            /* 
                fitting method that is more sensitive to outliers in 
                the time-series, I guess the way to go? 
            */
            medfit(ts->nseg, ywk, xwk, &intercept, &slope, &abdev);
    
            /* Subtract the fitted line from the time-series to obtain a linear detrend */
            for (j = 1; j <= ts->nseg; j++) {
                ywk[j] -= (slope * xwk[j]) + intercept;
            }
        }       
        
        /* apply window to data */
        if (c->WINDOW_TYPE != NO_WINDOW) {
            if (c->WINDOW_TYPE == COSINE_TAPER) {
                taper_timeseries(c, ts, ywk);    /* leads to some bizarre results if used in coherency */
            } else {
                window(c, ts, ywk, xwk);
            }
        }
    
        /* 
            calculate periodgram 
                - there are two options the original implementation in Scargle paper
                  or the Press et al method. They give identical results.
        */
        if (c->PERIODOGRAM_TYPE == SCARGLE) { 
            ft_uneven_data(xwk, ywk, ftrx, ftix, ts->nfreq, ts->nseg, ts->lfreq, ts->wz);
            
            /* sum raw spectra */
            for (j = 1; j <= ts->nout; j++) {
                power[j] += ftrx[j] * ftrx[j] + ftix[j] * ftix[j];
                
            }
        } else if (c->PERIODOGRAM_TYPE == PRESS) {
            period(xwk, ywk, ftrx, ftix, ts->nseg, ts->df, ts->nout);
            
            /* sum raw spectra */
            for (j = 1; j <= ts->nout; j++) {
                power[j] += ftrx[j] * ftrx[j] + ftix[j] * ftix[j];
            }
        }
        for (j = 1; j <= ts->nseg; j++) {
            xwk[j] = 0.0;
              ywk[j] = 0.0;
        }    
    }    
    
    /* rescale periodogram */
    if (c->PERIODOGRAM_TYPE == SCARGLE) { 
        for (i = 1; i <= ts->nout; i++) {
            power[i] *= scal;
            frequency[i] = (i - 1) * ts->df;
        }
    } else if (c->PERIODOGRAM_TYPE == PRESS) {
        for (i = 1; i <= ts->nout; i++) {
            power[i] *= scal;
            /* press algorithm starts at df, rather than 0.0 */
            frequency[i] = i * ts->df;
        }
    }
    
    /* tidy up */
    free_array_double(xwk);   
    free_array_double(ywk);
    free_array_double(ftrx);
    free_array_double(ftix);
    
    return;
}