Example #1
0
void line_search(int n, float *x, float *grad, float *direction, sf_gradient gradient, sf_optim opt, float *threshold, int *flag, int cpuid, int type)
/*< line search (Wolfe condition) >*/
{
	int i, j;
	float m1, m2, m3, fcost, alpha1=0., alpha2=0.;
	float *xk;

	xk=sf_floatalloc(n);
	copy(n, x, xk);
	dot_product(n, grad, direction, &m1);
	m2=m1*opt->c2;
	m1=m1*opt->c1;
	
	for(i=0; i<opt->nls; i++){
		
		opt->ils += 1;
		for(j=0; j<n; j++)
			x[j] =xk[j] + opt->alpha*direction[j];

		/* seislet regularization */
		if(seislet){
			seislet_lop(true, false, n, n, ss, x);
			soft_thresholding(ss, n, pclip);
			seislet_lop(false, false, n, n, ss, x);
		}

		/* model constraints */
		if(type==1){
			clip(x, n, threshold[0], threshold[1]);
		}else if(type==2){
			clip(x, n/2, threshold[0], threshold[1]);
			clip(x+n/2, n/2, threshold[2], threshold[3]);
		}

		gradient(x, &fcost, grad);
		opt->igrad += 1;
		dot_product(n, grad, direction, &m3);
		
		if(cpuid==0){
			sf_warning("line search i=%d",i+1);
			sf_warning("alpha1=%f alpha2=%f alpha=%f",alpha1, alpha2, opt->alpha);
			sf_warning("fcost=%f fk=%f fk+c1*alpha*m1=%f m3=%f c2*m1=%f ",fcost, opt->fk, opt->fk+opt->alpha*m1, m3, m2);
		}

		//if(counter1==4 && i==0) return;

		/* Wolfe condition */
		if(fcost <= opt->fk + opt->alpha*m1 && m3 >= m2){
			opt->fk=fcost;
			*flag=0;
			break;
		}else if (fcost > opt->fk + opt->alpha*m1){
			alpha2=opt->alpha;
			opt->alpha=0.5*(alpha1+alpha2);
		}else{
			alpha1=opt->alpha;
			if(alpha2 == 0.)
				opt->alpha *= opt->factor;
			else
				opt->alpha = 0.5*(alpha1+alpha2);
		}
		if(cpuid==0){
			sf_warning("After adjustment, alpha1=%f alpha2=%f alpha=%f",alpha1, alpha2, opt->alpha);
		}
	} // end of line search
	
	if(i==opt->nls){
		if(fcost <= opt->fk)
			*flag=1;
		else
			*flag=2;
	}

	free(xk);
}
Example #2
0
int main(int argc, char *argv[])
{
    int i, j, n1, n2, n3, n12; /*n1 is trace length, n2 is the number of traces, n3 is the number of 3th axis*/
    int order1, cut;
    float *data1, *data2, /* *adata, */ **dd;
    sf_file in, out, dip, slet, sletcut;
    float eps;
    char *type;
    bool unit=false, inv=true;

    sf_init(argc,argv);

    in = sf_input("in");
    dip=sf_input("dip");

    out = sf_output("out");
    slet=sf_output("slet");
    sletcut=sf_output("sletcut");

    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;
    n3 = sf_leftsize(in,2);
    /* get the trace length (n1) and the number of traces (n2) and n3*/

    if(!sf_getint("cut",&cut)) cut=n2/4;
    /* cut threshold value */

    if(!sf_getfloat("eps",&eps)) eps=0.01;
    /* regularization */

    if(!sf_getint("order1",&order1))order1=1;
    /* accuracy order for seislet transform*/

    if (NULL == (type=sf_getstring("type"))) type="linear";
    /* [haar,linear,biorthogonal] wavelet type, the default is linear  */

    if(!sf_getfloat("eps",&eps)) eps=0.01;
    /* regularization */
   

    data1 = sf_floatalloc(n1*n2);	/*allocate memory*/
    data2 = sf_floatalloc(n1*n2);	/*allocate memory*/
    /*   adata = sf_floatalloc(n1*n2);	allocate memory*/
    dd=sf_floatalloc2(n1,n2);

    seislet_init(n1,n2,inv,unit,eps,order1,type[0]);  /* unit=false inv=true */

    for(i=0;i<n3;i++)  {
	sf_floatread(data1,n1*n2,in);
	seislet_set(dd);
	sf_floatread(dd[0],n12,dip);

	seislet_lop(true,false,n12,n12,data2,data1); 
 	/*data1 is the input raw data and data2 is the unsorted seislet domain seismic data.*/

	if(NULL!=sf_getstring("slet"))	
	sf_floatwrite(data2,n1*n2,slet);
	/* seismic domain */

	/***********************************************************/
	/*applying mask operator*/
	for(i=cut; i<n2; i++)
		for(j=0;j<n1;j++)
			data2[i*n1+j]=0;
	/***********************************************************/

	if(NULL!=sf_getstring("sletcut"))	
	sf_floatwrite(data2,n1*n2,sletcut);
	/* cutted seislet domain */

	seislet_lop(false,false,n12,n12,data2,data1);	
	/*data2 is sorted seislet domain seismic data and data1 is denoised t-x domain seismic data*/

	sf_floatwrite(data1,n1*n2,out);
    }

    exit(0);
}
Example #3
0
int main(int argc, char* argv[])
{
    int i, niter, nw, n1, n2, n12, i1, i3, n3, iter, order; 
    float *mm, *dd, *dd2, *dd3, **pp, *m;
    float eps, perc;
    char *type, *oper;
    bool verb, *known;
    sf_file in, out, dip, mask=NULL;

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

    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;
    n3 = sf_leftsize(in,2);

    if (!sf_getint("niter",&niter)) niter=20;
    /* number of iterations */

    if (!sf_getint("order",&nw)) nw=1;
    /* [1,2,3] accuracy order */

    if (nw < 1 || nw > 3) 
	sf_error ("Unsupported nw=%d, choose between 1 and 3",nw);

    if (NULL == (type=sf_getstring("type"))) type="biorthogonal";
    /* [haar,linear,biorthogonal] wavelet type, the default is biorthogonal  */

    if (NULL == (oper=sf_getstring("oper"))) oper="bregman";
    /* [bregman,thresholding] method, the default is bregman  */

    if (!sf_getbool("verb",&verb)) verb = false;
    /* verbosity flag */

    if (!sf_getfloat("eps",&eps)) eps=0.01;
    /* regularization parameter */

    if (!sf_getfloat("perc",&perc)) perc=99.;
    /* percentage for soft thresholding */ 

    if (!sf_getint("order",&order)) order=1;
    /* accuracy order */
    
    pp = sf_floatalloc2(n1,n2);
    mm = sf_floatalloc(n12);
    dd = sf_floatalloc(n12);
    known = sf_boolalloc(n12);
    m = sf_floatalloc(n12);
    dd2 = sf_floatalloc(n12);
    dd3 = sf_floatalloc(n12);

    if (NULL != sf_getstring ("mask")) {
	mask = sf_input("mask");
    } else {
	mask = NULL;
    }

    sf_sharpen_init(n12,perc);
    seislet_init(n1,n2,true,true,eps,order,type[0]);
    seislet_set(pp);
    
    for (i3=0; i3 < n3; i3++) {
	if (verb) {
	    sf_warning("slice %d of %d",i3+1,n3);
	} else {
	    sf_warning("slice %d of %d;",i3+1,n3);
	}
	sf_floatread(dd,n12,in);
	sf_floatread(pp[0],n12,dip);
	
	if (NULL != mask) {
	    sf_floatread(m,n12,mask);
	    
	    for (i=0; i < n12; i++) {
		known[i] = (bool) (m[i] != 0.);
	    }
	} else {
	    for (i=0; i < n12; i++) {
		known[i] = (bool) (dd[i] != 0.);
	    }
	}

	for (i1=0; i1 < n12; i1++) {
	    dd2[i1] = 0.;
	    dd3[i1] = 0.;
	}
	for (iter=0; iter < niter; iter++) { /* Start iteration */
	    switch (oper[0]) {
		case 'b':
		    if (verb)
			sf_warning("Bregman iteration %d of %d;",iter+1,niter);
		    
		    for (i1=0; i1 < n12; i1++) {
			if (known[i1]) {
			    dd3[i1]= dd[i1]+dd3[i1]-dd2[i1]; 
			}
		    }
		    
		    for (i1=0; i1 < n12; i1++) {
			if (known[i1]) {
			    dd2[i1]= 0.; 
			}
		    }
		    for (i1=0; i1 < n12; i1++) {
			dd2[i1]+= dd3[i1]; 
		    }
		    
		    break;
		case 't':
		    if (verb)
			sf_warning("Thresholding iteration %d of %d;",
				   iter+1,niter);
		    
		    for (i1=0; i1 < n12; i1++) {
			if (known[i1]) {
			    dd3[i1]= dd[i1]+dd3[i1]-dd2[i1]; 
			}
		    }
		    
		    for (i1=0; i1 < n12; i1++) {
			dd2[i1] = dd3[i1]; 
		    }

		    break;
		default:
		    sf_error("Unknown wavelet type=%c",oper);
		    break;
	    }
	    seislet_lop(true,false,n12,n12,mm,dd2);

	    /* Thresholding */
	    sf_sharpen(mm);
	    sf_weight_apply(n12, mm);
	    
	    seislet_lop(false,false,n12,n12,mm,dd2);
	} /* End interation */

	for (i1=0; i1 < n12; i1++) {
	    dd[i1] = dd2[i1];
	}
	
	sf_floatwrite (dd,n12,out);
    }

    sf_sharpen_close();
    sf_warning(".");
    exit(0);
}
Example #4
0
int main(int argc, char *argv[])
{
    int n1, n2, i3, n3, n12, ip, np, n12p, ncycle, niter, order;
    bool inv, verb, decomp, twhole;
    char *type;
    float *pp, *qq, ***dd, ***mm, eps, perc, scale;
    sf_file in, out, dip, mask;

    sf_init(argc,argv);

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

    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_histint(dip,"n3",&np)) np=1;
    n12p = n12*np;
    scale = 1./np;

    pp = sf_floatalloc(n12);
    qq = sf_floatalloc(n12p);
    dd = sf_floatalloc3(n1,n2,np);

    if (!sf_getbool("inv",&inv)) inv=false;
    /* if y, do inverse transform */

    if (!sf_getfloat("eps",&eps)) eps=0.01;
    /* regularization */

    if (!sf_getbool("verb",&verb)) verb=true;
    /* verbosity flag */

    if (!sf_getbool("twhole",&twhole)) twhole=true;
    /* threshold flag, if y, whole model, otherwise, each component */

    if (!sf_getbool("decomp",&decomp)) decomp=false;
    /* do decomposition */

    if (!sf_getint("ncycle",&ncycle)) ncycle=0;
    /* number of iterations */

    if (!sf_getint("niter",&niter)) niter=1;
    /* number of Bregman iterations */

    if (!sf_getfloat("perc",&perc)) perc=50.0;
    /* percentage for sharpening */

    if (NULL != sf_getstring("mask")) { /* (optional) data mask file */
	mask = sf_input("mask");
	mm = sf_floatalloc3(n1,n2,np);
    } else {
	mask = NULL;
	mm = NULL;
    }

    if (inv) {
	n3 = sf_leftsize(in,3);
	if (!decomp) sf_unshiftdim(in, out, 3);
    } else {
	n3 = sf_leftsize(in,2);
	sf_putint(out,"n3",np);
	(void) sf_shiftdim(in, out, 3);
    } 

    if (!sf_getint("order",&order)) order=1;
    /* accuracy order */

    if (NULL == (type=sf_getstring("type"))) type="linear";
    /* wavelet type (haar,linear,biorthogonal), default is linear */
    
    diplet_init(n1,n2,decomp? 1: np, dd,mm,true,eps,order,type[0]);
 
    for (i3=0; i3 < n3; i3++) {
	sf_floatread(dd[0][0],n12p,dip);
	if (NULL != mask) sf_floatread(mm[0][0],n12p,mask);
	
	if (inv) {
	    sf_floatread(qq,n12p,in);

	    if (decomp) {
		for (ip=0; ip < np; ip++) {
		    seislet_set(dd[ip]);
		    seislet_lop(false,false, n12, n12,qq+ip*n12, pp);
		    datawrite(n12,1.,pp,out);
		}
	    } else {
		diplet_lop(false,false, n12p, n12,qq, pp);
		datawrite(n12,scale,pp,out);
	    }
	} else {
    	    sf_floatread(pp,n12,in);
	    sf_sharpinv(diplet_lop,
			scale,niter,ncycle,perc,verb,n12p,n12,qq,pp,twhole); 
	    sf_floatwrite(qq,n12p,out);
	} 
    }

    exit(0);
}