Esempio n. 1
0
int main(int argc, char* argv[])
{
    int n1, n2, i2, i, j;
    float *data, sigma, rand, thresh, thresh2;
    sf_file in, out;

    sf_init(argc,argv);
    in = sf_input("in");
    out = sf_output("out");

    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    n2 = sf_leftsize(in,1);

    if (!sf_getfloat("sigma",&sigma)) sigma=1.;
    /* noise magnitude */

    if (!sf_getfloat("thresh",&thresh)) thresh=0.93;
    /* noise threshold */

    if (!sf_getfloat("thresh2",&thresh2)) thresh2=0.4;
    /* noise threshold */

    data = sf_floatalloc(n1);

    for (i2=0; i2 < n2; i2++) {
	sf_floatread(data,n1,in);

	random_init (121794);
	
	for (i=0; i < n1; i++) {
	    data[i] += (random0()-0.5)*0.001;
	}
	
	random_init (121094);

	i=0;
	while (i < n1) {
	    rand = random0();
	    if (rand < thresh) { /* no noise */
		i++;
	    } else { 
		for (j=i; j < n1; j++) {
		    rand = random0();
		    data[j] += sigma*rand;
	
		    rand = random0();
		    if (rand < thresh2) break;
		}
		i = j + 1;
	    }
	}
	
	sf_floatwrite(data,n1,out);
    }


    exit(0);
}
Esempio n. 2
0
 /// Non-platform specific (pseudo) random number generator.
 double random1(double seed) {
   random_seed(seed);
   return random0();
 }
Esempio n. 3
0
int main(int argc, char* argv[])
{
    int n[SF_MAX_DIM], a[SF_MAX_DIM], center[SF_MAX_DIM], gap[SF_MAX_DIM];
    int *pch, *nh, dim, n123, nf, i, niter, nbf, nbp, id, ip, ig, np;
    int *kk, *pp;
    float *dd, eps, dabs, di;
    nfilter aa, bb;
    char varname[6], *lagfile;
    sf_file in, flt, lag, mask, patch, reg;

    sf_init(argc,argv);
    in = sf_input("in");
    flt = sf_output("out");

    dim = sf_filedims(in,n);

    if (NULL == (lagfile = sf_getstring("lag"))) sf_error("Need lag=");
    /* output file for filter lags */

    lag = sf_output(lagfile);
    sf_settype(lag,SF_INT);

    sf_putstring(flt,"lag",lagfile);

    sf_putints(lag,"n",n,dim);

    if (!sf_getints("a",a,dim)) sf_error("Need a=");

    if (!sf_getints("center",center,dim)) {
        for (i=0; i < dim; i++) {
            center[i] = (i+1 < dim && a[i+1] > 1)? a[i]/2: 0;
        }
    }

    if (!sf_getints("gap",gap,dim)) {
        for (i=0; i < dim; i++) {
            gap[i] = 0;
        }
    }

    n123 = 1;
    for (i=0; i < dim; i++) {
        n123 *= n[i];
    }

    dd = sf_floatalloc(n123);
    kk = sf_intalloc(n123);

    if (NULL != sf_getstring("maskin")) {
        /* optional input mask file */
        mask = sf_input("maskin");

        switch (sf_gettype(mask)) {
        case SF_INT:
            sf_intread (kk,n123,mask);
            break;
        case SF_FLOAT:
            sf_floatread (dd,n123,mask);
            for (i=0; i < n123; i++) {
                kk[i] = (dd[i] != 0.);
            }
            break;
        default:
            sf_error ("Wrong data type in maskin");
            break;
        }

        sf_fileclose (mask);
    } else {
        for (i=0; i < n123; i++) {
            kk[i] = 1;
        }
    }

    sf_floatread(dd,n123,in);

    dabs = fabsf(dd[0]);
    for (i=1; i < n123; i++) {
        di = fabsf(dd[i]);
        if (di > dabs) dabs=di;
    }

    random_init(2004);
    for (i=0; i < n123; i++) {
        dd[i] = dd[i]/dabs+ 100.*FLT_EPSILON*(random0()-0.5);;
    }

    pp = sf_intalloc(n123);
    if (NULL != sf_getstring("pch")) {
        patch = sf_input("pch");
        if (SF_INT != sf_gettype(patch)) sf_error("Need int pch");

        sf_intread(pp,n123,patch);

        np = pp[0];
        for (i=1; i < n123; i++) {
            if (pp[i] > np) np = pp[i];
        }

        sf_fileclose(patch);
    } else {
        np = n123;
        for (i=0; i < n123; i++) {
            pp[i] = i;
        }
    }

    aa = createnhelix(dim, n, center, gap, a, pp);
    free (pp);

    nf = aa->hlx[0]->nh;
    nfind_mask(n123, kk, aa);

    if(!sf_getint("niter",&niter)) niter=100;
    /* number of iterations */
    if (!sf_getfloat("epsilon",&eps)) eps=0.01;
    /* regularization parameter */

    sf_putint(flt,"n1",nf);
    sf_putint(flt,"n2",np);

    sf_putint(lag,"n1",nf);
    sf_putint(lag,"n2",np);

    for (i=2; i < dim; i++) {
        sprintf(varname,"n%d",i+1);
        sf_putint(flt,varname,1);
        sf_putint(lag,varname,1);
    }

    for (ip=0; ip < np; ip++) {
        sf_intwrite(aa->hlx[ip]->lag,nf,lag);
    }
    sf_fileclose(lag);

    if (NULL != sf_getstring("maskout")) {
        /* optional output mask file */
        mask = sf_output("maskout");

        for (i=0; i < n123; i++) {
            kk[i] = aa->mis[i]? 0.: 1.;
        }

        sf_settype(mask,SF_INT);
        sf_intwrite (kk,n123,mask);
    }

    reg = sf_input("filt");
    if (!sf_histint(reg,"n1",&nbf)) sf_error("No n1= in filt");
    if (!sf_histint(reg,"n2",&nbp)) sf_error("No n2= in filt");

    if (NULL != sf_getstring("filt_pch")) {
        patch = sf_input("filt_pch");
        if (SF_INT != sf_gettype(patch)) sf_error("Need int filt_pch");


        pp = sf_intalloc(np);
        sf_intread(pp,np,patch);
    } else {
        if (nbp != np) sf_error ("Wrong filter size: %d != %d",nbp,np);
        pp = NULL;
    }

    pch = sf_intalloc(nf*np);
    nh = sf_intalloc(nbp);

    for (i=0; i < nbp; i++) {
        nh[i] = nbf;
    }

    for (id=ig=0; ig < nf; ig++) {
        for (ip=0; ip < np; ip++, id++) {
            pch[id] = (NULL != pp)? pp[ip]: ip;
        }
    }

    bb = nallocate (nbp, nf*np, nh, pch);

    if (NULL == (lagfile = sf_getstring("filt_lag")) &&
            NULL == (lagfile = sf_histstring(reg,"lag")))
        sf_error("Need filt_lag=");
    /* input file for double-helix filter lags */

    lag = sf_input(lagfile);
    if (SF_INT != sf_gettype(lag)) sf_error("Need int filt_lag");

    for (ip=0; ip < nbp; ip++) {
        sf_intread (kk,nbf,lag);
        for (i=0; i < nbf; i++) {
            bb->hlx[ip]->lag[i] = kk[i]*nf;
        }
    }

    for (ip=0; ip < nbp; ip++) {
        sf_floatread (bb->hlx[ip]->flt,nbf,reg);
    }

    nfind_pef (n123, dd, aa, bb, niter, eps, nf);

    for (ip=0; ip < np; ip++) {
        sf_floatwrite (aa->hlx[ip]->flt,nf,flt);
    }


    exit(0);
}
Esempio n. 4
0
int main(int argc, char* argv[])
{
    int n[SF_MAX_DIM], w[3], k[3], a[3], gap[3], center[3];
    int n123, n1, dim, dim1, nk, i, j, ik, na;
    bool stp;
    float *data, d[3], o[3], *mask, vel, tgap, dabs, di;
    char varname[6], *lagfile;
    sf_filter aa, bb, ak;
    sf_file dat, pef, lag, known;

    sf_init(argc,argv);
    dat = sf_input("in");
    pef = sf_output("out");

    if (NULL == (lagfile = sf_getstring("lag"))) sf_error("Need lag=");
    /* output file for filter lags */
    lag = sf_output(lagfile);
    sf_settype(lag,SF_INT);

    sf_putstring(pef,"lag",lagfile);
    
    dim = sf_filedims(dat,n);
    if (dim > 3) sf_error("Need 3 dimensions or less");

    if (!sf_getint("dim",&dim1)) dim1=dim;
    /* PEF dimensionality */
    if (dim1 > dim) dim1=dim;
    sf_putint(pef,"dim",dim1);

    if (!sf_getints("w",w,dim1)) sf_error("Need w=");
    /* window size */
    for (j=0; j < dim1; j++) {
	if (w[j] > n[j]) w[j] = n[j];
    }
    sf_putints(pef,"w",w,dim1);

    if (!sf_getints("a",a,dim1)) sf_error("Need a=");
    /* filter size */
    sf_putints(pef,"a",a,dim1);

    if (!sf_getints("k",k,dim1)) {
	/* number of windows */
	for (j=0; j < dim1; j++) {
	    k[j] = 1.5 * n[j] / (w[j] - a[j] + 1.);
	}
    }
    sf_putints(pef,"k",k,dim1);

    if (!sf_getints("gap",gap,dim1)) {
	/* filter gap */
	for (j=0; j < dim1; j++) {
	    gap[j] = 0;
	}
    }
    sf_putints(pef,"gap",gap,dim1);

    if (!sf_getints("center",center,dim1)) {
	/* filter center */
	for (j=0; j < dim1-1; j++) {
	    center[j] = (a[j+1] > 1)? a[j]/2: 0;	    
	}
	center[dim1-1] = 0;
    }
    sf_putints(pef,"center",center,dim1);

    if (!sf_getbool("steepdip",&stp)) stp=false;
    /* if y, do steep-dip PEF estimation */

    for (j=0; j < dim1; j++) {
	sprintf(varname,"d%d",j+1);
	if (!sf_histfloat(dat,varname,d+j)) 
	    sf_error("No %s= in input",varname);
	sprintf(varname,"o%d",j+1);
	if (!sf_histfloat(dat,varname,o+j)) 
	    sf_error("No %s= in input",varname);
    }

    if (stp) {
	if (!sf_getfloat ("vel",&vel)) vel=1.7;
	/* velocity for steep-dip decon */

	if (!sf_getfloat("tgap",&tgap)) tgap=0.030;
	/* time gap for steep-dip decon */
 
	bb = steep(dim1, w, a, d, vel, tgap);
    } else {
	bb = createhelix(dim1, w, center, gap, a); 
    }

    sf_putints (lag,"n",w,dim1);

    bound (dim1, w, w, a, bb);
    for (i=0; i < bb->nh; i++) {
	bb->flt[i] = 2.;
    }
    print(dim1, w, center, a, bb);

    n123=n1=nk=1;
    for (j=0; j < dim; j++) {
	n123 *= n[j];
	if (j < dim1) {
	    n1 = n123;
	    nk *= k[j];
	}
    }
    na = bb->nh;

    sf_putint(pef,"n1",na);
    sf_putint(lag,"n1",na);

    for (j=0; j < dim; j++) {
	sprintf(varname,"n%d",j+2);
	sf_putint(lag,varname,1);
	if (j < dim1) {
	    sf_putint(pef,varname,k[j]);
	    sprintf(varname,"o%d",j+2);
	    sf_putfloat(pef,varname,o[j]+0.5*w[j]*d[j]);
	    sprintf(varname,"d%d",j+2);
	    sf_putfloat(pef,varname,(n[j]-w[j])/(k[j]-1.)*d[j]);
	} else if (j == dim1) {
	    sf_putint(pef,varname,n123/n1);
	} else {
	    sf_putint(pef,varname,1);
	}
    }

    sf_intwrite(bb->lag,na,lag);
    
    data = sf_floatalloc(n123);

    aa = (sf_filter) sf_alloc(nk,sizeof(*aa));

    if (NULL != sf_getstring ("mask")) {
	known = sf_input("mask");
	mask = sf_floatalloc(n123);
	sf_floatread(mask,n123,known);
	sf_fileclose(known);
    } else {
	mask = NULL;
    }

    sf_floatread(data,n123,dat);

    dabs = fabsf(data[0]);
    for (i=1; i < n123; i++) {
	di = fabsf(data[i]);
	if (di > dabs) dabs=di;
    }

    random_init (2004);
    for (i=0; i < n123; i++) {
	data[i] = data[i]/dabs + 100.*FLT_EPSILON*(random0()-0.5);
    }

    for (ik=0; ik < nk; ik++) {
	ak = aa+ik;
	ak->nh = na;
	ak->flt = sf_floatalloc(na);
	for (i=0; i < na; i++) {
	    ak->flt[i] = 0.;
	}
	ak->lag = bb->lag;
	ak->mis = bb->mis;
    }

    for (i=0; i < n123-n1+1; i += n1) {
	find_lopef (dim1, data+i, aa, k, n, w, mask);

	for (ik=0; ik < nk; ik++) {
	    sf_floatwrite ((aa+ik)->flt,na,pef);
	}
    }



    exit(0);
}