Example #1
0
int main (int argc, char* argv[]) {
    int dim, i, j, n[SF_MAX_DIM], w[SF_MAX_DIM], k[SF_MAX_DIM], np, nw, n12;
    float *data, *wind;
    char *temp;
    FILE *tmp;
    sf_file in, out;

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

    dim = sf_filedims(in,n);
    if (!sf_getints("w",w,dim)) sf_error("Need w=");
    if (!sf_getints("k",k,dim)) sf_error("Need k=");

    n12 = 1; np = 1; nw = 1;
    for(j=0; j < dim; j++) {
	n12 *= n[j];
	np *= k[j];
	nw *= w[j];
    }
    nw *= np;

    data = sf_floatalloc (n12);
    wind = sf_floatalloc (nw);

    sf_floatread(data,n12,in);
    tmp = sf_tempfile(&temp,"w+b");

    ocparcel_init (dim, k, n, w);

    if (n12 != fwrite(data,sizeof(float),n12,tmp))
	sf_error("writing error:");
    
    ocparcel_lop (false, n12, nw, tmp, wind);

    oc_zero(n12*sizeof(float),tmp);

    ocparcel_lop ( true, n12, nw, tmp, wind);

    rewind(tmp);

    if (n12 != fread(data,sizeof(float),n12,tmp))
	sf_error("reading error");

    for (i=0; i < n12; i++) {
	sf_line2cart(dim, n, i, k);
	if (k[0] <= 1 || k[0] >= n[0] - 2) data[i] = 0.;
    }

    sf_floatwrite (data,n12,out);

    unlink(temp);

    exit (0);
}
Example #2
0
sf_filter steep(int dim    /* number of dimensions */,
                int *n     /* data size [dim] */,
                int *a     /* filter size [dim] */,
                float *d   /* axis sampling [dim] */,
                float vel  /* velocity */,
                float tgap /* time gap */)
/*< define PEF >*/
{
    int *lag, c[SF_MAX_DIM], i, h, na, j;
    float x, t0;
    sf_filter aa;

    na = 1;
    for (j=0; j < dim; j++) {
        na *= a[j];
    }

    lag = sf_intalloc(na);

    for (h=i=0; i < na; i++) {
        sf_line2cart(dim, a, i, c);

        for (j=0; j < dim-1; j++) {
            c[j] -= (a[j]-1)/2;
        }

        t0 = 0.;
        for (j=0; j < dim-1; j++) {
            x = d[j]*c[j];
            t0 += x*x;
        }
        t0 = sqrtf(t0)/vel;
        if (t0 < tgap) t0 = tgap;

        x = d[dim-1]*c[dim-1];
        if(x >= t0) {
            lag[h] = sf_cart2line(dim, n, c);
            h++;
        }
    }

    aa = sf_allocatehelix(h);

    for (i=0; i < h; i++) {
        aa->lag[i] = lag[i];
        aa->flt[i] = 0.;
    }

    free(lag);

    return aa;
}
Example #3
0
void sf_upgrad_set(sf_upgrad upg, const float *r0 /* reference */)
/*< supply reference >*/
{
    int i, m, it, jt, ii[3], a, b;
    unsigned char *up;
    float t, t2;

    t0 = r0;

    /* sort from small to large traveltime */
    for (it = 0; it < nt; it++) {
	upg->order[it] = it;
    }
    qsort(upg->order, nt, sizeof(int), fermat);
     
    for (it = 0; it < nt; it++) {
	jt = upg->order[it];

	sf_line2cart(ndim,nn,jt,ii);
	up = upg->update[it];
	up[0] = up[1] = 0;
	t = t0[jt];
	upg->ww[it][ndim] = 0.;
	for (i=0, m=1; i < ndim; i++, m <<= 1) {
	    a = jt-ss[i];
	    b = jt+ss[i];
	    if ((ii[i] == 0) || 
		(ii[i] != nn[i]-1 && 1==fermat(&a,&b))) {
		if (b < 0 || b >= nt) continue;
		up[1] |= m;
		t2 = t0[b];
	    } else {
		if (a < 0 || a >= nt) continue;
		t2 = t0[a];
	    }

	    if (t2 < t) {
		up[0] |= m;
		upg->ww[it][i] = (t-t2)*dd[i];
		upg->ww[it][ndim] += upg->ww[it][i];
	    }	    
	}
    }
}
Example #4
0
void print (int dim            /* number of dimensions */, 
	    const int *nd      /* data size [dim] */, 
	    const int *center  /* filter center [dim] */, 
	    const int *na      /* filter size [dim] */, 
	    const sf_filter aa /* filter to print */) 
/*< print a filter >*/ 
{
    float* filt; 
    int ii[SF_MAX_DIM], ii0[SF_MAX_DIM], ia, i, j, k, nf, br; 

    nf = 1;
    for (j=0; j < dim; j++) {
	nf *= na[j];
    }
    filt = sf_floatalloc(nf);

    box (dim, nd, center, na, aa, nf, filt); 
    fprintf(stderr,"-----------------------------------------\n");

    for (j=0; j < dim; j++) {
	ii0[j] = 0;
    }

    for (ia = 0; ia < nf - na[0]+1; ia += na[0]) {
	sf_line2cart(dim, na, ia, ii);
	br = 0;
	for (j=0; j < dim; j++) {
	    if (ii[j] != ii0[j]) br++;
	    ii0[j] = ii[j];
	}

        for (i=0; i < br-1; i++)
	    fprintf (stderr,"+++++++++++++++++++++++++++++++++++++++++\n");

	for (k=0; k < na[0]; k++) {
	    fprintf(stderr,"%7.3f",filt[k+ia]);
	}
	fprintf (stderr,"\n");
    }
    
    fprintf (stderr,"-----------------------------------------\n");

    free (filt);
}
Example #5
0
void upgrad_set(upgrad upg      /* upwind stencil */, 
		const float* t0 /* t0 */,
		const float* x0 /* x0 */,
		const int* f0   /* f0 */)
/*< supply reference >*/
{
    int i, j, m, it, jt, ii[3], a, b;
    unsigned char *up;
    float t, t2;
    float x, x2;

    t_0 = t0;
    x_0 = x0;
    f_0 = f0;

    /* sort from small to large traveltime */
    for (it = 0; it < nt; it++) {
	upg->order[it] = it;
    }
    qsort(upg->order, nt, sizeof(int), fermat);
     
    for (it = 0; it < nt; it++) {
	jt = upg->order[it];

	sf_line2cart(ndim,nn,jt,ii);
	up = upg->update[it];
	up[0] = up[1] = 0;
	t = t_0[jt];
	x = x_0[jt];
	upg->wt[it][ndim] = 0.;
	upg->wx[it][ndim] = 0.;

	/* special coded for one-sided update */
	if (f_0[jt] >= 0) {
	    i = f_0[jt];

	    for (j=0, m=1; j < i; j++) m <<= 1;

	    a = jt-ss[i];
	    b = jt+ss[i];
	    if ((ii[i] == 0) || 
		(ii[i] != nn[i]-1 && 1==fermat(&a,&b))) {
		up[1] |= m;
		t2 = t_0[b];
		x2 = x_0[b];
	    } else {
		t2 = t_0[a];
		x2 = x_0[a];
	    }

	    if (t2 < t) {
		up[0] |= m;
		upg->wt[it][i] = (t-t2)*dd[i];
		upg->wt[it][ndim] += upg->wt[it][i];
		upg->wx[it][i] = 0.;
		upg->wx[it][ndim] += upg->wx[it][i];
	    }
	    
	    continue;
	}

	/* x0 will also use the same upwind determined by t0 */
	for (i=0, m=1; i < ndim; i++, m <<= 1) {
	    a = jt-ss[i];
	    b = jt+ss[i];
	    if ((ii[i] == 0) || 
		(ii[i] != nn[i]-1 && 1==fermat(&a,&b))) {
		up[1] |= m;
		t2 = t_0[b];
		x2 = x_0[b];
	    } else {
		t2 = t_0[a];
		x2 = x_0[a];
	    }

	    if (t2 < t) {
		up[0] |= m;
		upg->wt[it][i] = (t-t2)*dd[i];
		upg->wt[it][ndim] += upg->wt[it][i];
		upg->wx[it][i] = (x-x2)*dd[i];
		upg->wx[it][ndim] += upg->wx[it][i];
	    }	    
	}
    }
}
Example #6
0
int main(int argc, char* argv[])
{ 
    int i, j, is, ip, dim, n[SF_MAX_DIM], ii[SF_MAX_DIM];
    int nsp, **k=NULL, **l=NULL, n1, n2, i1, i2, kk, ll;
    char key[7];
    const char *label, *unit;
    float f, *trace, *mag=NULL, **p=NULL, pp;
    sf_file in, spike;

    sf_init (argc,argv);

    if (!sf_stdin()) { /* no input file in stdin */
	in = NULL;
    } else {
	in = sf_input("in");
    }

    spike = sf_output("out");
    
    if (NULL == in) {
	sf_setformat(spike,"native_float");
    } else if (SF_FLOAT != sf_gettype(in)) {
	sf_error("Need float input");
    }
    
    /* dimensions */
    for (i=0; i < SF_MAX_DIM; i++) {
	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(spike,key,n[i]);
    }

    if (0==i) sf_error("Need n1=");
    dim=i;
    
    /* basic parameters */
    for (i=0; i < dim; i++) {
	snprintf(key,3,"o%d",i+1);
	if (!sf_getfloat(key,&f) && 
	    (NULL == in || !sf_histfloat(in,key,&f))) f=0.;
	/*( o#=[0,0,...] origin on #-th axis )*/  
	sf_putfloat(spike,key,f);

	snprintf(key,3,"d%d",i+1);
	if (!sf_getfloat(key,&f) &&
	    (NULL == in || !sf_histfloat(in,key,&f))) f = (i==0)? 0.004: 0.1;
	/*( d#=[0.004,0.1,0.1,...] sampling on #-th axis )*/  
	sf_putfloat(spike,key,f);

	snprintf(key,7,"label%d",i+1);
	if (NULL == (label = sf_getstring(key)) &&
	    (NULL == in || NULL == (label = sf_histstring(in,key))))
	    label = (i==0)? "Time":"Distance";
	/*( label#=[Time,Distance,Distance,...] label on #-th axis )*/  
	if (*label != '\0' && (*label != ' ' || *(label+1) != '\0')) 	
	    sf_putstring(spike,key,label);

	snprintf(key,6,"unit%d",i+1);
	if (NULL == (unit = sf_getstring(key)) &&
	    (NULL == in || NULL == (unit = sf_histstring(in,key))))
	    unit = (i==0)? "s":"km";
        /*( unit#=[s,km,km,...] unit on #-th axis )*/  
	if (*unit != '\0' && (*unit != ' ' || *(unit+1) != '\0')) 	
	    sf_putstring(spike,key,unit);
    }
	
    if (NULL != (label = sf_getstring("title")))
	sf_putstring(spike,"title",label);
    /* title for plots */

    if (!sf_getint("nsp",&nsp)) nsp=1;
    /* Number of spikes */

    if (nsp >= 1) { 
	mag = sf_floatalloc (nsp);
	k = sf_intalloc2 (nsp,dim);
	l = sf_intalloc2 (nsp,dim);
	p = sf_floatalloc2 (nsp,dim);
    
	for (i=0; i < dim; i++) {
	    snprintf(key,3,"k%d",i+1);
	    if ( !sf_getints(key,k[i],nsp)) {
		/*( k#=[0,...] spike starting position )*/
		for (is=0; is < nsp; is++) {
		    k[i][is]=-1;
		}
	    } else {
		for (is=0; is < nsp; is++) {
		    if (k[i][is] > n[i]) 
			sf_error("Invalid k%d[%d]=%d > n%d=%d",
				 i+1,is+1,k[i][is],i+1,n[i]);
		    k[i][is]--; /* C notation */
		}
	    }
	    snprintf(key,3,"l%d",i+1);
	    if (!sf_getints(key,l[i],nsp)) {
		/*( l#=[k1,k2,...] spike ending position )*/
		for (is=0; is < nsp; is++) {
		    l[i][is]=k[i][is];
		}
	    } else {
		for (is=0; is < nsp; is++) {
		    if (l[i][is] > n[i]) 
			sf_error("Invalid l%d[%d]=%d > n%d=%d",
				 i+1,is+1,l[i][is],i+1,n[i]);
		    l[i][is]--; /* C notation */
		}
	    }	
	    snprintf(key,3,"p%d",i+1);
	    if (!sf_getfloats(key,p[i],nsp)) {
		/*( p#=[0,...] spike inclination (in samples) )*/
		for (is=0; is < nsp; is++) {
		    p[i][is]=0.;
		}
	    }
	}
	
	if (!sf_getfloats("mag",mag,nsp)) {
	    /* spike magnitudes */
	    for (is=0; is < nsp; is++) {
		mag[is]=1.;
	    }
	}
    }

    n1 = n[0];
    n2 = sf_leftsize(spike,1);

    trace = sf_floatalloc (n[0]);

    for (i2=0; i2 < n2; i2++) { /* loop over traces */
	sf_line2cart(dim-1, n+1, i2, ii+1);
	/* zero trace */
	for (i1=0; i1 < n1; i1++) trace[i1]=0.;
	/* put spikes in it */
	for (is=0; is < nsp; is++) { /* loop over spikes */
	    pp = 0.;
	    for (i=1; i < dim; i++) {
		kk = k[i][is];
		ll = l[i][is];
		if ((kk < -1 && ll < -1) || 
		    (kk >= 0 && ll >= 0 && 
		     (kk > ii[i] || ll < ii[i]))) break;
		pp += p[i][is]*(ii[i]-k[i][is]-1);
	    }
	    if (i < dim) continue; /* skip this spike */

	    /* linear interpolation */
	    ip = floorf(pp);
	    pp = 1.-(pp-ip);

	    kk = k[0][is];
	    ll = l[0][is];
	    if (kk >= 0) { /* one segment per trace */
		kk = SF_MAX(kk+ip,0);
		ll = SF_MIN(ll+ip,n1-1);
	    } else {
		kk = SF_MAX(ip,0);
		ll = SF_MIN(n1-1+ip,n1-1);
	    }

	    for (j=kk; j <= ll; j++) {
		trace[j] += pp*mag[is];
		if (j+1 < n1) trace[j+1] += (1.-pp)*mag[is];
	    }
	}
	sf_floatwrite(trace,n1,spike);
    }

    exit (0);
}
Example #7
0
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);
}