示例#1
0
文件: medbin.c 项目: 1014511134/src
void medbin (const float *ord /* data */, 
	     float *model     /* binned */)
/*< apply median binning >*/
{
    int im, id, p, np;

    for (id = 0; id < nd; id++) { 
	if (m[id]) {
	    im = k[id];
	    bd[pt[im]] = ord[id]; 
	    pt[im]++;
	}
    }

    for (im=0; im < nm; im++) {
	np = ct[im];
	if (np > 0) {
	    p = pt[im]-np;
	    model[im] = 
		(sf_quantile(np/2,    np,bd+p)+
		 sf_quantile((np-1)/2,np,bd+p))*0.5;
	} else {
	    model[im] = 0.;
	}
    }
}
示例#2
0
文件: slow3.c 项目: 1014511134/src
/*------------------------------------------------------------*/
int slow3(int nr           /* maximum number of references */, 
	  float ds         /* minimum slowness separation */, 
	  int ns           /* number of slownesses */, 
	  const float* ss  /* [ns] slowness array */, 
	  float* sr        /* [nr] reference slownesses squared */) 
/*< compute reference slownesses, return their number >*/
{
    int is,jr,ir;
    float smin, smax, s, s2=0., qr, *ss2;

    ss2 = sf_floatalloc(ns);
    for (is=0; is<ns; is++) {
	ss2[is]=ss[is];
    }
    
    smax = sf_quantile(ns-1,ns,ss2);
    smin = sf_quantile(   0,ns,ss2);
    nr = SF_MIN(nr,1+(smax-smin)/ds);
    
    jr=0;
    for (ir=0; ir<nr; ir++) {
	qr = (ir+1.0)/nr - 0.5 * 1./nr;
	s = sf_quantile(qr*ns,ns,ss2);
	if (0==ir || SF_ABS(s-s2) > ds) {
	    sr [jr] = s*s;
	    s2 = s;
	    jr++;
	}
    }
    
    free(ss2);
    return jr;
}
示例#3
0
文件: graph.c 项目: housian0724/src
static void getminmax(const float* f, float* min, float* max)
{
    int i, m, nc;
    float fmin, fmax, fi, fbig, fsml, fdif;

    m=0;
    fmin=+FLT_MAX;
    fmax=-FLT_MAX;
    for (i=0; i < n; i++) {
	fi = f[i];
	if (isfinite(fi)) {
	    t[m] = fi;
	    m++;
	    if (fmin > fi) fmin=fi;
	    if (fmax < fi) fmax=fi;
	}
    }

    nc = (1.-0.01*pclip)*m;
    if (nc < 0) nc=0;
    if (nc >= m) nc=m-1;
    
    if (nc > 0 && nc < m-1) {
	fsml = sf_quantile(nc,m,t);
	fbig = sf_quantile(m-nc-1, m,t);
	fdif = fbig - fsml;

	*min = SF_MAX(fmin-fdif/8,fsml-fdif);
	*max = SF_MIN(fmax+fdif/8,fbig+fdif);
    } else {
	*min = fmin;
	*max = fmax;
    }
}
示例#4
0
文件: impl3.c 项目: 1014511134/src
void impl3_set(float *** x)
/*< compute weighting function >*/
{
    int i;
    float a, xsum, wsum, *tmp;

    sf_sobel32(n1,n2,n3,x,w);
    tmp = ww[0][0];
    
    for (i=0; i < n; i++) {
	tmp[i] = w[0][0][i];
    }

    a = sf_quantile(nclip,n,tmp);
    if (a==0.) sf_error("%s: clip at nclip=%d is zero, use a higher pclip",
			__FILE__,nclip);

    for (i=0; i < n; i++) {
	w[0][0][i] = sqrtf(1.+w[0][0][i]/a);
	if (NULL != dist) w[0][0][i] *= dist[i];	
	tmp[i] = 1./w[0][0][i];
	if (up) w[0][0][i] = tmp[i];
    }

    if (verb) {
	wsum = xsum = 0.;
	for (i=0; i < n; i++) {
	    wsum += w[0][0][i];
	    xsum += x[0][0][i]*x[0][0][i];
	}

	sf_warning("xsum=%g, wsum=%g, a=%g", xsum, wsum, a);
    }
}
示例#5
0
文件: fixbad.c 项目: 1014511134/src
void fixbad (int niter    /* number of iterations */, 
	     sf_filter aa /* PEF */, 
	     int ny       /* data size */,
	     float *yy    /* in - data, out - deburst */) 
/*< find bad data and restore it >*/
{
    int iy;
    float *rr, *rabs, rbar;
    bool *known;

    rr = sf_floatalloc(ny);
    rabs = sf_floatalloc(ny);
    known = sf_boolalloc(ny);

    sf_helicon_init(aa);
    sf_helicon_lop (false,false,ny,ny,yy,rr); 
    for (iy=0; iy < ny; iy++) 
	rabs[iy] = fabsf (rr[iy]);
    rbar = sf_quantile(ny/2,ny,rabs);
    for (iy=0; iy < ny; iy++) 
	known[iy] = (bool) ((yy[iy] > 0.) && (fabsf(rr[iy]) < 4. * rbar));
    mis2 (niter, ny, yy, aa, known, 0., true);

    free(rr);
    free(rabs);
    free(known);
}
示例#6
0
void soft_thresholding(float *x, int n, float pclip)
/*< soft thresholding >*/
{
	int i, m;
	float *tmp, t, d;

	m=0.5+n*(1-0.01*pclip);
	if(m<0) m=0;
	if(m>=n) m=n-1;

	tmp=sf_floatalloc(n);
	for(i=0; i<n; i++)
		tmp[i]=fabsf(x[i]);

	t=sf_quantile(m, n, tmp);

	for (i=0; i<n; i++){
		d=x[i];
		if(d<-t){
			x[i]=d+t;
		}else if (d>t){
			x[i]=d-t;
		}else{
			x[i]=0.;
		}
	}

	free(tmp);
}
示例#7
0
文件: impl1.c 项目: 1014511134/src
void impl1_apply (float *x)
/*< apply diffusion >*/
{
    int istep, i;
    float a, xsum, wsum;

    /******** 
	      dx/dt = 1/w D' 1/w D u
	      x_{t+1} = (I + t 1/w D' 1/w D)^{-1} x_t
	      = (w + D' t/w D)^{-1} w x_t 
    *********/


    for (istep=0; istep < nstep; istep++) {
	sf_grad2(n,x,w);

	for (i=0; i < n; i++) {
	    w1[i] = w[i];
	}

	a = sf_quantile(nclip,n,w1);
	
	if (a==0.) sf_error("%s: clip at nclip=%d is zero, use a higher pclip",
			    __FILE__,nclip);

	wsum = xsum = 0.;
	for (i=0; i < n; i++) {
	    w[i] = sqrtf(1.+w[i]/a);
	    wsum += w[i];
	    xsum += x[i]*x[i];
	}

	sf_warning("step %d of %d, xsum=%g, wsum=%g, a=%g",  
		   istep, nstep, xsum, wsum, a);

	for (i=0; i < n; i++) {
	    if (up) w[i] = 1./w[i];
	    x[i] *= w[i];
	}

	for (i=0; i < n; i++) {
	    d1[i] = w[i];
	    if (up) {
		w1[i] = -t*d1[i];
	    } else {
		w1[i] = -t/d1[i];
	    }
	}

	d1[0] -= w1[0];
	for (i=1; i < n-1; i++) {
	    d1[i] -= w1[i] + w1[i-1];
	}
	d1[n-1] -= w1[n-2];

	sf_tridiagonal_define (slv, d1, w1); 
	sf_tridiagonal_solve (slv, x);
    }
}
示例#8
0
int main (int argc, char* argv[])
{
    int i, j;
    float xx[] = {1.,3.,4.,5.,2.};
    float yy[NX];
    
    for (i=0; i < NX; i++) {
	for (j=0; j < NX; j++) {
	    yy[j] = xx[j];
	}
	printf("%d %f\n", i, sf_quantile (i, NX, yy));
    }
    
    exit (0);
}
示例#9
0
文件: rweone.c 项目: 1014511134/src
void rweone_psc_coef(
    float **aa,
    float **bb,
    float **a0,
    float **b0)
/*< PSC coefficients >*/
{
    float tt,tt2, *tb;
    int ii,ir,it,ig;
    float boa, boa2;
    float aoa;
    float bob;

    if(verb) sf_warning("compute PSC coef");

    /* find max(b0) */
    tb=sf_floatalloc (ar.n*at.n);
    ii=0;
    for(it=0;it<at.n;it++) {
	for(ir=0;ir<ar.n;ir++) {
	    tb[ii] = b0[it][ir];
	    ii++;
	}
    }
    tt = SF_ABS(sf_quantile(ar.n*at.n-1,ar.n*at.n,tb));
    tt2= tt*tt;
    free(tb);

    for(it=0;it<at.n;it++) {
	for(ir=0;ir<ar.n;ir++) {
	    boa = ( b0[it][ir]/tt ) / a0[it][ir];
	    boa2=boa*boa;

	    for(ig=0;ig<ag.n;ig++) {
		aoa = aa[it][ig]/a0[it][ir];
		bob = bb[it][ig]/b0[it][ir];

		m0[it][ir][ig] = 1 / tt2;
		n0[it][ir][ig] = a0[it][ir] * boa2 * (c1*(aoa-1.) - (bob-1.));
		r0[it][ir][ig] =    3 * c2 * boa2;

		m0[it][ir][ig] *= kmu;
		n0[it][ir][ig] *= knu;
		r0[it][ir][ig] *= kro;
	    }
	}
    }
}
示例#10
0
int main(int argc, char* argv[]) 
{
    int wide1,wide2,wide3, wide, shift1, shift2, shift3, i, j, k, i1, n1, i2, n2, i3, n3, i4, n4;
    float ***data, ***signal, ***win;
    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");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    if (!sf_histint(in,"n3",&n3)) sf_error("No n3= in input");
    n4 = sf_leftsize(in,3);

    data = sf_floatalloc3(n1,n2,n3);
    signal = sf_floatalloc3(n1,n2,n3);

    if (!sf_getint("wide1",&wide1)) wide1=5;
    if (!sf_getint("wide2",&wide2)) wide2=5;
    if (!sf_getint("wide3",&wide3)) wide3=5;
    /* sliding window width */
    wide = wide1*wide2*wide3;

    win = sf_floatalloc3(wide1,wide2,wide3);

    for (i4=0; i4 < n4; i4++) {
	sf_floatread(data[0][0],n1*n2*n3,in);	
	for (i3=0; i3 < n3; i3++) { shift3 = SF_MAX (0, SF_MIN (n3-wide3, i3-wide3/2));
	    for (i2=0; i2 < n2; i2++) { shift2 = SF_MAX (0, SF_MIN (n2-wide2, i2-wide2/2));
		for (i1=0; i1 < n1; i1++) { shift1 = SF_MAX (0, SF_MIN (n1-wide1, i1-wide1/2));
		    for (i=0; i < wide; i++) {
			for (j=0; j < wide; j++) {
			    for (k=0; k < wide; k++) {
				win[i][j][k] = data[shift3+k][shift2+i][shift1+j];
			    }
			}
		    }
		    signal[i3][i2][i1] = sf_quantile(wide/2,wide,win[0][0]);
		}
	    }
	}
	
	sf_floatwrite(signal[0][0],n1*n2*n3,out);
    }	

    exit(0);
}
示例#11
0
文件: irls.c 项目: 1014511134/src
void sf_cauchy (int n, const float *res, float *weight)  
/*< weighting for Cauchy norm >*/
{
    float rbar;
    int i;

    /* take absolute value */
    for (i=0; i < n; i++) {
	abs1[i] = fabsf(res[i]);
    }

    /* find median (rbar) */
    rbar = sf_quantile(n/2,n,abs1);

    /* weight = 1/sqrt(1+(res/rbar)^2) */
    for (i=0; i < n; i++) {
	weight[i] = 1./hypotf(1.,res[i]/rbar);
    }
}
示例#12
0
文件: scale.c 项目: 1014511134/src
static float getscale(int ntest, int n1, float *abuf)
{
    int i;
    float dscale, f;

    if (ntest == n1) { /* 100% clip - take maximum */
	dscale = 0.;
	for (i=0; i < n1; i++) {
	    f = abuf[i];
	    if (f > dscale) dscale=f;
	}
    } else {
	dscale = sf_quantile(ntest,n1,abuf);
    }

    if (dscale > 0.) dscale=1./dscale;

    return dscale;
}
示例#13
0
int main(int argc, char* argv[])
{
    int i, n, n1;
    float *dat=NULL, *adat=NULL, t, pclip, d;
    sf_complex *cdat=NULL;
    sf_file in=NULL, out=NULL;

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

    n = sf_filesize(in);
    adat = sf_floatalloc(n);

    if (!sf_getfloat("pclip",&pclip)) sf_error("Need pclip=");
    /* percentage to clip */
    n1 = 0.5+n*(1.-0.01*pclip);
    if (n1 < 0) n1=0;
    if (n1 >= n) n1=n-1;

    if (SF_FLOAT == sf_gettype(in)) {
	dat = sf_floatalloc(n);
	sf_floatread(dat,n,in);
	for (i=0; i < n; i++) {
	    adat[i] = fabsf(dat[i]);
	}
    } else if (SF_COMPLEX == sf_gettype(in)) {
	cdat = sf_complexalloc(n);
	sf_complexread(cdat,n,in);
	for (i=0; i < n; i++) {
	    adat[i] = cabsf(cdat[i]);
	}
    } else {
	sf_error("Need float or complex input");
    }

    t = sf_quantile(n1,n,adat);

    if (NULL != dat) {
	for (i=0; i < n; i++) {
	    d = dat[i];
	    if (d < -t) {
		dat[i] = d+t;
	    } else if (d > t) {
		dat[i] = d-t;
	    } else {
		dat[i] = 0.;
	    }
	}
	sf_floatwrite(dat,n,out);
    } else {
	for (i=0; i < n; i++) {
	    d = cabsf(cdat[i]);
	    if (d < -t) {
#ifdef SF_HAS_COMPLEX_H
		cdat[i] *= (d+t)/d;
#else
		cdat[i] = sf_crmul(cdat[i],(d+t)/d);
#endif
	    } else if (d > t) {		
#ifdef SF_HAS_COMPLEX_H
		cdat[i] *= (d-t)/d;
#else
		cdat[i] = sf_crmul(cdat[i],(d-t)/d);
#endif
	    } else {
		cdat[i] = sf_cmplx(0.,0.);
	    }
	}
	sf_complexwrite(cdat,n,out);
    }

    exit(0);
}
示例#14
0
文件: running.c 项目: nicklinyi/src
int main(int argc, char* argv[])
{
    int w1, w2, nw, s1,s2, j1,j2, i1,i2,i3, n1,n2,n3;
    char *how;
    float **data, **signal, **win;
    sf_file in, out;

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

    /* get data dimensions */
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1=");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2=");
    n3 = sf_leftsize(in,2);

    /* input and output */
    data = sf_floatalloc2(n1,n2);
    signal = sf_floatalloc2(n1,n2);

    if (!sf_getint("w1",&w1)) w1=5;
    if (!sf_getint("w2",&w2)) w2=5;
    /* sliding window width */

    nw = w1*w2;
    win = sf_floatalloc2(w1,w2);

    how = sf_getstring("how");
    /* what to compute
       (fast median, slow median, mean) */
    if (NULL == how) how="fast";

    for (i3=0; i3 < n3; i3++) {

        /* read data plane */
        sf_floatread(data[0],n1*n2,in);

        for (i2=0; i2 < n2; i2++) {
            s2 = SF_MAX(0,SF_MIN(n2-w2,i2-w2/2-1));
            for (i1=0; i1 < n1; i1++) {
                s1 = SF_MAX(0,SF_MIN(n1-w1,i1-w1/2-1));

                /* copy window */
                for (j2=0; j2 < w2; j2++) {
                    for (j1=0; j1 < w1; j1++) {
                        win[j2][j1] = data[s2+j2][s1+j1];
                    }
                }

                switch (how[0]) {
                case 'f': /* fast median */
                    signal[i2][i1] =
                        sf_quantile(nw/2,nw,win[0]);
                    break;
                case 's': /* slow median */
                    signal[i2][i1] =
                        slow_median(nw,win[0]);
                    break;
                case 'm': /* mean */
                default:
                    /* !!! ADD CODE !!! */
                    break;
                }
            }
        }

        /* write out */
        sf_floatwrite(signal[0],n1*n2,out);
    }

    exit(0);
}
示例#15
0
文件: Mtvmf.c 项目: 1014511134/src
int main (int argc, char* argv[]) 
{
    int n1,n2,n3; /*n1 is trace length, n2 is the number of traces, n3 is the number of 3th axis*/
    int i,j,k,kk,ii;
    int nfw;    /*nfw is the reference filter-window length*/
    int tempnfw;  /*temporary variable*/
    int m;
    float medianv; /*temporary median variable*/
    bool boundary;
    int alpha,beta,gamma,delta; /*time-varying window coefficients*/
    
    float *trace;
    float *tempt; /*temporary array*/
    float *result; /*output array*/
    float *extendt;
    float *medianarray;   /*1D median filtered array*/
    float *temp1,*temp2,*temp3; /*temporary array*/
    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");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    n3 = sf_leftsize(in,2);
    /* get the trace length (n1) and the number of traces (n2) and n3*/
    
    if (!sf_getbool("boundary",&boundary)) boundary=false;
    /* if y, boundary is data, whereas zero*/
    
    if (!sf_getint("nfw",&nfw)) sf_error("Need integer input");
    /* reference filter-window length (>delta, positive and odd integer)*/
    
    if (!sf_getint("alpha",&alpha)) alpha=2;
    /* time-varying window parameter "alpha" (default=2)*/
    
    if (!sf_getint("beta",&beta)) beta=0;
    /* time-varying window parameter "beta" (default=0)*/
    
    if (!sf_getint("gamma",&gamma)) gamma=2;
    /* time-varying window parameter "gamma" (default=2)*/
    
    if (!sf_getint("delta",&delta)) delta=4;
    /* time-varying window parameter "delta" (default=4)*/
    
    if (alpha<beta || delta<gamma) sf_error("Need alpha>=beta && delta>=gamma"); 
    if ((alpha%2)!=0) alpha = alpha+1;
    if ((beta%2)!=0) beta = beta+1;
    if ((gamma%2)!=0) gamma = gamma+1;
    if ((delta%2)!=0) delta = delta+1;
    
    if (nfw <=delta)  sf_error("Need nfw > delta"); 
    if (nfw%2 == 0)  nfw++;
    m=(nfw-1)/2;
    tempnfw=nfw;
    
    trace = sf_floatalloc(n1*n2);
    tempt = sf_floatalloc(n1*n2);
    result = sf_floatalloc(n1*n2);
    extendt = sf_floatalloc((n1+2*m)*n2);
    medianarray =sf_floatalloc(n1*n2);
    temp1 = sf_floatalloc(nfw);
    temp2 = sf_floatalloc(n1);
    /*set the data space*/
    
    for(ii=0;ii<n3;ii++){
	sf_floatread(trace,n1*n2,in);
	for(i=0;i<n1*n2;i++){
	    tempt[i]=trace[i];
	}
	
	bound1(tempt,extendt,nfw,n1,n2,boundary);
	
	/************1D reference median filtering****************/
	
	for(i=0;i<n2;i++){
	    for(j=0;j<n1;j++){
		for(k=0;k<nfw;k++){
		    temp1[k]=extendt[(n1+2*m)*i+j+k];
		}
		medianarray[n1*i+j]=sf_quantile(m,nfw,temp1); 
	    }
	}
	medianv=0.0;
	for(i=0;i<n1*n2;i++){
	    medianv=medianv+fabs(medianarray[i]);
	}
	medianv=medianv/(1.0*n1*n2);
	
	/************1D time-varying median filter****************/
	for(i=0;i<n2;i++){
	    for(kk=0;kk<n1;kk++){
		temp2[kk]=trace[n1*i+kk];
	    }
	    for(j=0;j<n1;j++){
		if(fabs(medianarray[n1*i+j])<medianv){
		    if(fabs(medianarray[n1*i+j])<medianv/2.0){
			tempnfw=nfw+alpha;
		    }
		    else{
			tempnfw=nfw+beta;
		    }
		}
		else{
		    if(fabs(medianarray[n1*i+j])>=(medianv*2.0)){
			tempnfw=nfw-delta;
		    }
		    else{
			tempnfw=nfw-gamma;
		    }
		}
		temp3 = sf_floatalloc(tempnfw);
		bound2(temp2,temp3,n1,tempnfw,j,boundary);
		result[n1*i+j]=sf_quantile((tempnfw-1)/2,tempnfw,temp3); 
		tempnfw=nfw;
	    }
	}
	sf_floatwrite(result,n1*n2,out);
    }

    exit (0);
}
示例#16
0
文件: Mcanny.c 项目: 1014511134/src
int main(int argc, char* argv[])
{
    int n1, n2, n3, i1, i2, i3, j1, j2, k1, k2, nedge, nold, nmin, nmax, n12;
    int **edge;
    float **pp, **ww, **w1, **w2, g1, g2, w, min, max;
    sf_file in=NULL, out=NULL;

    sf_init(argc,argv);

    in = sf_input("in");
    out = sf_output("out");

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

    if (!sf_getfloat("min",&min)) min=5.0;
    /* minimum threshold */
    if (!sf_getfloat("max",&max)) max=95.0;
    /* maximum threshold */

    n12 = n1*n2;
    nmin = min*0.01*n12;
    if (nmin < 0) nmin=0;
    if (nmin >= n12) nmin=n12-1;
    nmax = max*0.01*n12;
    if (nmax < 0) nmax=0;
    if (nmax >= n12) nmax=n12-1;

    pp = sf_floatalloc2(n1,n2);
    w1 = sf_floatalloc2(n1,n2);
    w2 = sf_floatalloc2(n1,n2);
    ww = sf_floatalloc2(n1,n2);
    edge = sf_intalloc2(n1,n2);

    sf_settype(out,SF_INT);

    for (i3=0; i3 < n3; i3++) {
	sf_floatread(pp[0],n12,in);
	/* gradient computation */
	sf_sobel(n1,n2,pp,w1,w2);

	for (i2=0; i2 < n2; i2++) {
	    for (i1=0; i1 < n1; i1++) {
		/* gradient norm */
		g1 = w1[i2][i1];
		g2 = w2[i2][i1];
		ww[i2][i1] = g1*g1+g2*g2;
	    }
	}
	/* edge thinning */
	for (i2=0; i2 < n2; i2++) {
	    for (i1=0; i1 < n1; i1++) {
		g1 = w1[i2][i1];
		g2 = w2[i2][i1];
		if (fabsf(g1) > fabsf(g2)) {
		    j1=1;
		    if (g2/g1 > 0.5) { 
			j2=1;
		    } else if (g2/g1 < - 0.5) {
			j2=-1; 
		    } else {
			j2=0;
		    }
		} else if (fabsf(g2) > fabsf(g1)) {
		    j2=1;
		    if (g1/g2 > 0.5) { 
			j1=1;
		    } else if (g1/g2 < - 0.5) {
			j1=-1; 
		    } else {
			j1=0;
		    }
		} else {
		    j1=0;
		    j2=0;
		}
		k1 = i1+j1; if (j1 && (k1 < 0 || k1 >= n1)) k1=i1;
		k2 = i2+j2; if (j2 && (k2 < 0 || k2 >= n2)) k2=i2;
		if (ww[i2][i1] <= ww[k2][k1]) {
		    pp[i2][i1] = 0.;
		    continue;
		} 
		k1 = i1-j1; if (k1 < 0 || k1 >= n1) k1=i1;
		k2 = i2-j2; if (k2 < 0 || k2 >= n2) k2=i2;
		if (ww[i2][i1] <= ww[k2][k1]) {
		    pp[i2][i1] = 0.;
		    continue;
		} 
		pp[i2][i1] = ww[i2][i1];
	    }
	}
	/* edge selection */
	max = sf_quantile(nmax,n12,ww[0]);
	min = sf_quantile(nmin,n12,ww[0]);

	nedge=0;
	for (i2=0; i2 < n2; i2++) {
	    for (i1=0; i1 < n1; i1++) {
		w = pp[i2][i1];
		if (w > max) {
		    edge[i2][i1] = SF_IN;
		    nedge++;
		} else if (w < min) {
		    edge[i2][i1] = SF_OUT;
		} else {
		    edge[i2][i1] = SF_FRONT;
		}
	    }
	}

	nold=0;
	while (nedge != nold) {
	    nold = nedge;
	    for (i2=0; i2 < n2; i2++) {
		for (i1=0; i1 < n1; i1++) {
		    if (SF_FRONT == edge[i2][i1]) {
			if (i2 > 0) {
			    if (SF_IN == edge[i2-1][i1] || 
				(i1 > 0 && SF_IN == edge[i2-1][i1-1]) ||
				(i1 < n1-1 && SF_IN == edge[i2-1][i1+1])) {
				edge[i2][i1] = SF_IN;
				nedge++;
				continue;
			    }
			}
			if (i2 < n2-1) {
			    if (SF_IN == edge[i2+1][i1] || 
				(i1 > 0 && SF_IN == edge[i2+1][i1-1]) ||
				(i1 < n1-1 && SF_IN == edge[i2+1][i1+1])) {
				edge[i2][i1] = SF_IN;
				nedge++;
				continue;
			    }
			}
			if ((i1 > 0 && SF_IN == edge[i2][i1-1]) ||
			    (i1 < n1-1 && SF_IN == edge[i2][i1+1])) {
			    edge[i2][i1] = SF_IN;
			    nedge++;
			    continue;
			}
		    }
		}
	    }
	}
	for (i2=0; i2 < n2; i2++) {
	    for (i1=0; i1 < n1; i1++) {
		if (SF_FRONT == edge[i2][i1]) edge[i2][i1] = SF_OUT;
	    }
	}
	
	sf_intwrite(edge[0],n12,out);
    }


    exit(0);
}
示例#17
0
文件: rweone.c 项目: 1014511134/src
void rweone_ffd_coef(
    float **aa,
    float **bb,
    float **a0,
    float **b0)
/*< FFD coefficients >*/
{
    float ***d1, ***d2;
    int ii,ir,it,ig;
    float tt,tt2, *tb;
    float boa, boa2, boa4;
    float bob, bob2, bob4;
    float aoa, aoa3;

    if(verb) sf_warning("compute FFD coef");

    d1=sf_floatalloc3(ag.n,ar.n,at.n);
    d2=sf_floatalloc3(ag.n,ar.n,at.n);

    /* find max(b0) */
    tb=sf_floatalloc (ar.n*at.n); 
    ii=0;
    for(it=0;it<at.n;it++) {
	for(ir=0;ir<ar.n;ir++) {
	    tb[ii] = b0[it][ir];
	    ii++;
	}
    }
    tt = SF_ABS(sf_quantile(ar.n*at.n-1,ar.n*at.n,tb));
    tt2= tt*tt;
    free(tb);

    for(it=0;it<at.n;it++) {
	for(ir=0;ir<ar.n;ir++) {
	    boa = ( b0[it][ir]/tt ) / a0[it][ir];
	    boa2= boa *boa ;
	    boa4= boa2*boa2;

	    for(ig=0;ig<ag.n;ig++) {
		bob = bb[it][ig]/b0[it][ir];
		bob2= bob *bob;
		bob4= bob2*bob2;

		aoa = a0[it][ir]/aa[it][ig];
		aoa3= aoa*aoa*aoa;

		d1[it][ir][ig] = a0[it][ir] * boa2 * ( bob2 * aoa  - 1.);
		d2[it][ir][ig] = a0[it][ir] * boa4 * ( bob4 * aoa3 - 1.);
	    }
	}
    }

    /* regularize d1 */
    for(it=0;it<at.n;it++) {
	for(ir=0;ir<ar.n;ir++) {
	    for(ig=0;ig<ag.n;ig++) {
		if( SF_ABS(d1[it][ir][ig]) < 1e-6) d1[it][ir][ig]=1e-6;
	    }
	}
    }
    
    for(it=0;it<at.n;it++) {
	for(ir=0;ir<ar.n;ir++) {
	    for(ig=0;ig<ag.n;ig++) {

		m0[it][ir][ig] =       d1[it][ir][ig] / tt2;
		n0[it][ir][ig] = -c1 * d1[it][ir][ig] * d1[it][ir][ig];
		r0[it][ir][ig] =  c2 * d2[it][ir][ig];

		m0[it][ir][ig] *= kmu;
		n0[it][ir][ig] *= knu;
		r0[it][ir][ig] *= kro;
	    }
	}
    }
    
    free(**d1); free(*d1); free(d1);
    free(**d2); free(*d2); free(d2);
}
示例#18
0
文件: Mmf.c 项目: 1014511134/src
int main (int argc, char* argv[]) 
{
    int n1,n2,n3; /*n1 is trace length, n2 is the number of traces, n3 is the number of 3th axis*/
    int nfw;    /*nfw is the filter-window length*/
    int m;
    int i,j,k,ii;
    bool boundary;
    
    float *trace;
    float *tempt;
    float *temp1;
    float *extendt;
    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");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    n3 = sf_leftsize(in,2);
    /* get the trace length (n1) and the number of traces (n2) and n3*/
    
    if (!sf_getint("nfw",&nfw)) sf_error("Need integer input");
    /* filter-window length (positive and odd integer)*/
    
    if (!sf_getbool("boundary",&boundary)) boundary=false;
    /* if y, boundary is data, whereas zero*/
    
    if (nfw < 1)  sf_error("Need positive integer input"); 
    if (nfw%2 == 0) nfw++;
    m=(nfw-1)/2;
    
    trace = sf_floatalloc(n1*n2);
    tempt = sf_floatalloc(n1*n2);
    temp1 = sf_floatalloc(nfw);
    extendt = sf_floatalloc((n1+2*m)*n2);
    
    for(ii=0;ii<n3;ii++){
	sf_floatread(trace,n1*n2,in);
	
	for(i=0;i<n1*n2;i++){
	    tempt[i]=trace[i];
	}
	
	bound1(tempt,extendt,nfw,n1,n2,boundary);
	
	/************1D median filter****************/
	
	for(i=0;i<n2;i++){
	    for(j=0;j<n1;j++){
		for(k=0;k<nfw;k++){
		    temp1[k]=extendt[(n1+2*m)*i+j+k];
		}
		trace[n1*i+j]=sf_quantile(m,nfw,temp1); 
	    }
	}
	
	sf_floatwrite(trace,n1*n2,out);
    }

    exit (0);
}
示例#19
0
文件: wexlsr.c 项目: 1014511134/src
             wexlsr3d lsr,
             float **bs,
             float   wo  /* frequency */
)
/*< k-domain weight >*/
{
    int ix,iy,ii,nn;
    float *ss, smin, ko;

    /* find s min */
    nn = cub->amx.n*cub->amy.n;
    ss = sf_floatalloc(nn);
    ii=0;
    LOOP( ss[ii] = bs[iy][ix];
          ii++; );
    smin = sf_quantile(0,nn,ss);
    free(ss);

    ko  = abs(wo) * smin;
    ko *= ko;

    KOOP(
        if( lsr->kk[iy][ix] < ko ) {
            lsr->kw[iy][ix] = 1.;
        } else {
            lsr->kw[iy][ix] = 0.;
        } );
}

/*------------------------------------------------------------*/
void wexlsr_s2w(
示例#20
0
文件: Mistpad.c 项目: 1014511134/src
int main(int argc, char* argv[])
{
    	bool verb, pow2;
    	char key[7], *mode;;
    	int n1, n2, n1padded, n2padded, num, dim, n[SF_MAX_DIM], npadded[SF_MAX_DIM], ii[SF_MAX_DIM];
	int i, j, i1, i2, index, nw, iter, niter, nthr, *pad;
    	float thr, pclip, normp;
    	float *dobs_t, *thresh, *mask;
    	fftwf_complex *mm, *dd, *dobs;
    	fftwf_plan fft1, ifft1, fftrem, ifftrem;/* execute plan for FFT and IFFT */
    	sf_file in, out, Fmask;	/* mask and I/O files*/ 

    	sf_init(argc,argv);	/* Madagascar initialization */
    	in=sf_input("in");	/* read the data to be interpolated */
    	out=sf_output("out"); 	/* output the reconstructed data */
    	Fmask=sf_input("mask");	/* read the (n-1)-D mask for n-D data */
 
    	if(!sf_getbool("verb",&verb))    	verb=false;
    	/* verbosity */
    	if(!sf_getbool("pow2",&pow2))    	pow2=false;
    	/* round up the length of each axis to be power of 2 */
    	if (!sf_getint("niter",&niter)) 	niter=100;
    	/* total number of iterations */
    	if (!sf_getfloat("pclip",&pclip)) 	pclip=10.;
    	/* starting data clip percentile (default is 10)*/
    	if ( !(mode=sf_getstring("mode")) ) 	mode = "exp";
    	/* thresholding mode: 'hard', 'soft','pthresh','exp';
	  'hard', hard thresholding;	   'soft', soft thresholding; 
	  'pthresh', generalized quasi-p;  'exp', exponential shrinkage */
    	if (pclip <=0. || pclip > 100.)	sf_error("pclip=%g should be > 0 and <= 100",pclip);
    	if (!sf_getfloat("normp",&normp)) 	normp=1.;
    	/* quasi-norm: normp<2 */
   	for (i=0; i < SF_MAX_DIM; i++) {/* dimensions */
		snprintf(key,3,"n%d",i+1);
		if (!sf_getint(key,n+i) && 
		    (NULL == in || !sf_histint(in,key,n+i))) break;
		/*( n# size of #-th axis )*/  
		sf_putint(out,key,n[i]);
    	}
    	if (0==i) sf_error("Need n1=");
    	dim=i;
    	pad=sf_intalloc (dim);
	for (i=0; i<dim; i++) pad[i]=0;
	sf_getints("pad",pad,dim); /* number of zeros to be padded for each axis */

    	n1=n[0];
    	n2=sf_leftsize(in,1);
	for (i=0; i<SF_MAX_DIM; i++) npadded[i]=1;
	npadded[0]=n1+pad[0];
	n1padded=npadded[0];
	n2padded=1;
	for (i=1; i<dim; i++){
	  npadded[i]=n[i]+pad[i];
	  if (pow2) {/* zero-padding to be power of 2 */
	    npadded[i]=nextpower2(n[i]);
	    fprintf(stderr,"n%d=%d n%dpadded=%d\n",i,n[i],i,npadded[i]);
	  }
	  n2padded*=npadded[i];
	}
	nw=npadded[0]/2+1;
	num=nw*n2padded;/* data: total number of elements in frequency domain */

    	/* allocate data and mask arrays */
	thresh=(float*)            malloc(nw*n2padded*sizeof(float));
    	dobs_t=(float*)      fftwf_malloc(n1padded*n2padded*sizeof(float));  /* time domain observation */
    	dobs=(fftwf_complex*)fftwf_malloc(nw*n2padded*sizeof(fftwf_complex));/* freq-domain observation */
    	dd=(fftwf_complex*)  fftwf_malloc(nw*n2padded*sizeof(fftwf_complex));
    	mm=(fftwf_complex*)  fftwf_malloc(nw*n2padded*sizeof(fftwf_complex));
 
    	if (NULL != sf_getstring("mask")){
		mask=sf_floatalloc(n2padded);
    	} else sf_error("mask needed!");

	/* initialize the input data and mask arrays */
	memset(dobs_t,0,n1padded*n2padded*sizeof(float));
	memset(mask,0,n2padded*sizeof(float));
	for (i=0; i<n1*n2; i+=n1){
	  sf_line2cart(dim,n,i,ii);
	  j=sf_cart2line(dim,npadded,ii);
	  sf_floatread(&dobs_t[j], n1, in);
	  sf_floatread(&mask[j/n1padded], 1, Fmask);
	}

	/* FFT for the 1st dimension and the remaining dimensions */
     	fft1=fftwf_plan_many_dft_r2c(1, &n1padded, n2padded, dobs_t, &n1padded, 1, n1padded, dobs, &n1padded, 1, nw, FFTW_MEASURE);
   	ifft1=fftwf_plan_many_dft_c2r(1, &n1padded, n2padded, dobs, &n1padded, 1, nw, dobs_t, &n1padded, 1, n1padded, FFTW_MEASURE);
	fftrem=fftwf_plan_many_dft(dim-1, &npadded[1], nw, dd, &npadded[1], nw, 1, dd, &npadded[1], nw, 1, FFTW_FORWARD, FFTW_MEASURE);
	ifftrem=fftwf_plan_many_dft(dim-1, &npadded[1], nw, dd, &npadded[1], nw, 1, dd, &npadded[1], nw, 1, FFTW_BACKWARD, FFTW_MEASURE);

	/* transform the data from time domain to frequency domain: dobs_t-->dobs */
	fftwf_execute(fft1);
	for(i=0; i<num; i++) dobs[i]/=sqrtf(n1padded);
	memset(mm,0,num*sizeof(fftwf_complex));

	/* Iterative Shrinkage-Thresholding (IST) Algorithm:
	   	mm^{k+1}=T[mm^k+A^* M^* (dobs-M A mm^k)] (M^*=M; Mdobs=dobs)
		   	=T[mm^k+A^*(dobs-M A mm^k)]; (k=0,1,...niter-1)
	   	dd^=A mm^; 
	*/
    	for(iter=0; iter<niter; iter++)
    	{
		/* dd<-- A mm^k */
		memcpy(dd, mm, num*sizeof(fftwf_complex));
		fftwf_execute(ifftrem);
		for(i=0; i<num; i++) dd[i]/=sqrtf(n2padded);

		/* apply mask: dd<--dobs-M A mm^k=dobs-M dd */
		for(i2=0; i2<n2padded; i2++)
		for(i1=0; i1<nw; i1++)
		{ 
			index=i1+nw*i2;
			dd[index]=dobs[index]-mask[i2]*dd[index];
		}

		/* mm^k += A^*(dobs-M A mm^k); dd=dobs-M A mm^k */
		fftwf_execute(fftrem);
		for(i=0; i<num; i++) mm[i]+=dd[i]/sqrtf(n2padded);		

		/* perform thresholding */
		for(i=0; i<num; i++)	thresh[i]=cabsf(mm[i]);
	   	nthr = 0.5+num*(1.-0.01*pclip);  /* round off */
	    	if (nthr < 0) nthr=0;
	    	if (nthr >= num) nthr=num-1;
		thr=sf_quantile(nthr, num, thresh);
		sf_cpthresh(mm, num, thr, normp, mode);

		if (verb) sf_warning("iteration %d;",iter+1);
    	}

	/* frequency--> time domain: dobs-->dobs_t */
	memcpy(dd, mm, num*sizeof(fftwf_complex));
	fftwf_execute(ifftrem);
	for(i=0; i<num; i++) dd[i]/=sqrtf(n2padded);
	memcpy(dobs, dd, num*sizeof(fftwf_complex));
	fftwf_execute(ifft1);
	for(i=0; i<n1padded*n2padded; i++) dobs_t[i]/=sqrtf(n1padded);
	
	for (i=0; i<n1*n2; i+=n1){
	  sf_line2cart(dim,n,i,ii);
	  j=sf_cart2line(dim,npadded,ii);
	  sf_floatwrite(&dobs_t[j],n1,out);
	}

	free(thresh);
	fftwf_free(dobs_t);
	fftwf_free(dobs);
	fftwf_free(dd);
	fftwf_free(mm);

    	exit(0);
}
示例#21
0
文件: Mcflow.c 项目: 1014511134/src
int main(int argc, char* argv[])
{
    int n12, i, i1, i2, rect, order, niter;
    float **img, **grad, **grad1, **grad2, *y, **x;
    float med, t, t2, tol[2], band;
    sf_eno2 g[2];
    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");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    n12 = n1*n2;

    if (!sf_getint("rect",&rect)) rect=3;
    /* smoothing radius */
    t = (rect*rect-1)/6.0;
    sf_runge_init(2,1,t/2);

    if (!sf_getint("order",&order)) order=3;
    /* interpolation order */
    if (!sf_getfloat("tol",&tol[0])) tol[0]=0.1;
    /* error tolerance */
    tol[1]=tol[0];
    if (!sf_getint("niter",&niter)) niter=100;
    /* number of iterations */
    if (!sf_getfloat("band",&band)) band=1.;
    /* narrow band */

    img = sf_floatalloc2(n1,n2);
    grad = sf_floatalloc2(n1,n2);
    grad1 = sf_floatalloc2(n1,n2);
    grad2 = sf_floatalloc2(n1,n2);
    y = sf_floatalloc(n12);
    x = sf_floatalloc2(2,n12);

    g[0] = sf_eno2_init (order, n1, n2);
    g[1] = sf_eno2_init (order, n1, n2);

    sf_floatread(img[0],n12,in);

    /* compute gradient squared */
    sf_sobel2 (n1,n2,img,grad);

    /* normalize by median */
    for (i=0; i < n12; i++) {
	y[i] = grad[0][i];
    }

    med = sf_quantile(n12/2,n12,y);

    for (i=0; i < n12; i++) {
	grad[0][i] = -0.5*logf(1.+grad[0][i]/med);
    }

    /* compute gradient */
    sf_sobel (n1,n2,grad,grad1,grad2);
    sf_eno2_set (g[0], grad1);
    sf_eno2_set (g[1], grad2);

    /* convection */
    i=0;
    for (i2=0; i2 < n2; i2++) {
	for (i1=0; i1 < n1; i1++) {
	    if (fabsf(img[i2][i1]) < band) {
		x[i][0] = i1;
		x[i][1] = i2;
		t2 = sf_ode23 (t,tol, x[i], g, rhs, term);
		if (t2 >= t) {
		    y[i] = img[i2][i1];
		    i++;
		}
	    }
	}
    }

    /* diffusion */
    sf_int2_init (x, 0,0,1,1,n1,n2, sf_lin_int, 2, i);
    sf_triangle2_init(rect,rect, n1, n2, 1);
    sf_conjgrad_init(n12, n12, i, i, 1.0/i, 1.e-8, true, false);
    sf_conjgrad(NULL, 
		sf_int2_lop, sf_triangle2_lop, grad[0], img[0], y, niter);

    sf_floatwrite(img[0],n12,out);

    exit(0);
}