Ejemplo n.º 1
0
 void savgol(double *c, int np, int nl, int nr, int ld, int m)  {
/*-------------------------------------------------------------------------------------------
 USES lubksb,ludcmp given below.
 Returns in c(np), in wrap-around order (see reference) consistent with the argument respns
 in routine convlv, a set of Savitzky-Golay filter coefficients. nl is the number of leftward
 (past) data points used, while nr is the number of rightward (future) data points, making
 the total number of data points used nl+nr+1. ld is the order of the derivative desired
 (e.g., ld = 0 for smoothed function). m is the order of the smoothing polynomial, also
 equal to the highest conserved moment; usual values are m = 2 or m = 4.
-------------------------------------------------------------------------------------------*/
int imj,ipj,j,k,kk,mm;
double d,fac,sum,**a,*b;

if (np < nl+nr+1 || nl < 0 || nr < 0 || ld > m || nl+nr < m)
	nrerror("bad args in savgol");

int *indx= intvector(1,m+1);
a=matrix(1,m+1,1,m+1);
b=vector(1,m+1);
for (ipj=0;ipj<=(m << 1);ipj++)
	{//Set up the normal equations of the desired least-squares fit
	sum=(ipj ? 0.0 : 1.0);
	for (k=1;k<=nr;k++)
		sum += pow((double)k,(double)ipj);
	for (k=1;k<=nl;k++)
		sum += pow((double)-k,(double)ipj);
	mm=IMIN(ipj,2*m-ipj);
	for (imj = -mm;imj<=mm;imj+=2)
		a[1+(ipj+imj)/2][1+(ipj-imj)/2]=sum;
	}

ludcmp(a, m+1, indx, &d); //Solve them: LU decomposition.

for (j=1;j<=m+1;j++)
	b[j]=0.0;

b[ld+1]=1.0; //Right-hand side vector is unit vector, depending on which derivative we want.

lubksb(a,m+1,indx,b); //Get one row of the inverse matrix.

for (kk=1;kk<=np;kk++)
	c[kk]=0.0; //Zero the output array (it may be bigger than number of coefficients).

for (k = -nl;k<=nr;k++)
	{
	sum=b[1];   //Each Savitzky-Golay coefficient is the dot product
				//of powers of an integer with the inverse matrix row.
	fac=1.0;
	for (mm=1;mm<=m;mm++)
		sum += b[mm+1]*(fac *= k);

	kk=((np-k) % np)+1; //Store in wrap-around order.
	c[kk]=sum;
	}

free_vector(b,1,m+1);
free_matrix(a,1,m+1,1,m+1);
free_intvector(indx,1,m+1);
}
Ejemplo n.º 2
0
void SmoothFilter::smoothSavGol(double *, double *y)
{
	double *s = new double[d_n];
    int nl = d_smooth_points;
    int nr = d_sav_gol_points;
	int np = nl+nr+1;
	double *c = vector(1, np);

	//seek shift index for given case nl, nr, m (see savgol).
	int *index = intvector(1, np);
	index[1]=0;
	int i, j=3;
	for (i=2; i<=nl+1; i++)
	{// index(2)=-1; index(3)=-2; index(4)=-3; index(5)=-4; index(6)=-5
		index[i]=i-j;
		j += 2;
	}
	j=2;
	for (i=nl+2; i<=np; i++)
	{// index(7)= 5; index(8)= 4; index(9)= 3; index(10)=2; index(11)=1
		index[i]=i-j;
		j += 2;
	}

	//calculate Savitzky-Golay filter coefficients.
	savgol(c, np, nl, nr, 0, d_polynom_order);

	for (i=0; i<d_n; i++){// Apply filter to input data.
		s[i]=0.0;
		for (j=1; j<=np; j++){
			int it = i+index[j];
			if (it >=0 && it < d_n)//skip left points that do not exist.
				s[i] += c[j]*y[i+index[j]];
		}
	}

    for (i = 0; i<d_n; i++)
        y[i] = s[i];

	delete[] s;
	free_vector(c, 1, np);
	free_intvector(index, 1, np);
}
Ejemplo n.º 3
0
int main()
{
    {
        counter_object counter;

        while (!counter.empty())
        {
            std::cout << *counter << " ";
            ++counter;
        }
        std::cout << std::endl;
    }

    {
        for (counter_object cnt; !cnt.empty(); ++cnt)
        {
            std::cout << *cnt << " ";
        }
        std::cout << std::endl;
    }

    {
        counter_object counter;
        squaring_object<counter_object> squares(counter);

        while (!squares.empty())
        {
            std::cout << *squares << " ";
            ++squares;
        }
        std::cout << std::endl;
    }

    {
        std::vector<int> intvector;
        // (fill intvector)

        // define stream class iterating over an integer vector
        using intstream_type = stxxl::stream::iterator2stream<std::vector<int>::const_iterator>;

        // instantiate the stream object, iterate from begin to end of intvector.
        intstream_type intstream(intvector.begin(), intvector.end());

        // plug in squaring object after vector iterator stream.
        squaring_object<intstream_type> squares(intstream);
    }

    {
        stxxl::vector<int> intvector;
        // (fill intvector)

        // define stream class iterating over an integer vector
        using intstream_type = stxxl::stream::vector_iterator2stream<stxxl::vector<int>::const_iterator>;

        // instantiate the stream object, iterate from begin to end of intvector.
        intstream_type intstream(intvector.begin(), intvector.end());

        // plug in squaring object after vector iterator stream.
        squaring_object<intstream_type> squares(intstream);
    }

    {
        // construct the squared counter stream
        counter_object counter;
        squaring_object<counter_object> squares(counter);

        // allocate vector of 100 integers
        std::vector<int> intvector(100);

        // materialize 100 integers from stream and put into vector
        stxxl::stream::materialize(squares, intvector.begin(), intvector.end());
    }

    {
        // construct the squared counter stream
        counter_object counter;
        squaring_object<counter_object> squares(counter);

        // allocate STXXL vector of 100 integers
        stxxl::vector<int> intvector(100);

        // materialize 100 integers from stream and put into STXXL vector
        stxxl::stream::materialize(squares, intvector.begin(), intvector.end());
    }

    {
        static const int ram_use = 10 * 1024 * 1024; // amount of memory to use in runs creation

        counter_object counter;                      // the counter stream from first examples

        // define a runs sorter for the counter stream which order by CompareMod10 object.
        using rc_counter_type = stxxl::stream::runs_creator<counter_object, CompareMod10>;

        // instance of CompareMod10 comparator class
        CompareMod10 comparemod10;

        // instance of runs_creator which reads the counter stream.
        rc_counter_type rc_counter(counter, comparemod10, ram_use);

        // define a runs merger for the sorted runs from rc_counter.
        using rm_counter_type = stxxl::stream::runs_merger<rc_counter_type::sorted_runs_type, CompareMod10>;

        // instance of runs_merger which merges sorted runs from rc_counter.
        rm_counter_type rm_counter(rc_counter.result(), comparemod10, ram_use);

        // read sorted stream: runs_merger also conforms to the stream interface.
        while (!rm_counter.empty())
        {
            std::cout << *rm_counter << " ";
            ++rm_counter;
        }
        std::cout << std::endl;
    }

    {
        static const int ram_use = 10 * 1024 * 1024;   // amount of memory to use in runs creation

        // define a runs sorter which accepts imperative push()s and orders by CompareMod10 object.
        using rc_counter_type = stxxl::stream::runs_creator<stxxl::stream::use_push<int>, CompareMod10>;

        // instance of CompareMod10 comparator class.
        CompareMod10 comparemod10;

        // instance of runs_creator which waits for input.
        rc_counter_type rc_counter(comparemod10, ram_use);

        // write sequence of integers into runs
        for (int i = 1; i <= 1000; ++i)
            rc_counter.push(i);

        // define a runs merger for the sorted runs from rc_counter.
        using rm_counter_type = stxxl::stream::runs_merger<rc_counter_type::sorted_runs_type, CompareMod10>;

        // instance of runs_merger which merges sorted runs from rc_counter.
        rm_counter_type rm_counter(rc_counter.result(), comparemod10, ram_use);

        // read sorted stream: runs_merger also conforms to the stream interface.
        while (!rm_counter.empty())
        {
            std::cout << *rm_counter << " ";
            ++rm_counter;
        }
        std::cout << std::endl;
    }

    {
        static const int ram_use = 10 * 1024 * 1024;   // amount of memory to use in runs creation

        // define a runs sorter which accepts imperative push()s and orders by CompareMod10 object.
        using sr_counter_type = stxxl::sorter<int, CompareMod10>;

        // instance of CompareMod10 comparator class.
        CompareMod10 comparemod10;

        // instance of sorter which waits for input.
        sr_counter_type sr_counter(comparemod10, ram_use);

        // write sequence of integers into sorter, which creates sorted runs
        for (int i = 1; i <= 1000; ++i)
            sr_counter.push(i);

        // signal sorter that the input stream is finished and switch to output mode.
        sr_counter.sort();

        // read sorted stream: sorter also conforms to the stream interface.
        while (!sr_counter.empty())
        {
            std::cout << *sr_counter << " ";
            ++sr_counter;
        }
        std::cout << std::endl;
    }
}
Ejemplo n.º 4
0
void main(int nar, char* ar[])
{
  int i,j,k,l,m, n=0, num, nnn[4];
  float*u; // ,*f;
  double*f;
  parlist par;
  FILE *in,*out,*col,*grs;
  char name[100];
  char buffer[48];
  float cpu[2]; 
  clock_t t;

  /* set default values */
  par.sigma=2; par.seed=200294;
  par.dim=intvector(1,3); par.dim[1]=64; par.dim[2]=32; par.dim[3]=16; 
  empty(&par.inname); empty(&par.outname);
  empty(&par.colname); empty(&par.grsname);
  par.length=1; par.nongauss=0; par.lo=-4; par.hi=4; par.bins=100; 
  par.med=0; par.pixel=1; par.normal=1; par.time=0;
  
  /* work through arguments */
  while(++n<nar){
    if(ar[n][0]=='-'&&ar[n][1]){
      switch(ar[n][1]) {
	case'0' : par.grsname =READSTR; break;
	case'1' : par.colname =READSTR; break;
	case'L' : par.length  =READINT; break;
	case'N' : par.normal  =0;       break;
	case'b' : par.bins    =READINT; break;
	case'g' : par.nongauss=READINT; break;
	case'h' : par.hi      =READFLT; break;
	case'i' : par.inname  =READSTR; break;
	case'l' : par.lo      =READFLT; break;
	case'm' : par.med     =READINT; break;
	case'o' : par.outname =READSTR; break;
	case'p' : par.pixel   =READINT; break;
	case'r' : par.seed    =READINT; break;
	case's' : par.sigma   =READFLT; break;
	case't' : par.time    =1;       break;
	case'x' : par.dim[1]  =READINT; break;
	case'y' : par.dim[2]  =READINT; break;
	case'z' : par.dim[3]  =READINT; break;
      default : Explain(stderr,ar[0],&par);
      }
    }
    else {
      Explain(stderr,ar[0],&par);
    }
  }
  par.a=1./(float)par.dim[1];	/* grid constant is used everywhere */

  /* allocate some memory */
  n=par.dim[1]*par.dim[2]*par.dim[3];
  u=vector(0,2*n);

  /* open input file and read data */
  fileopenr(&in,par.inname);
 /*   printf("%s"," here 1a \n");

    printf("size = %d\n",sizeof(in));   */
  if(in) {
    //f=vector(0,n);
    f=doublevector(0,n);

    fread((void*)buffer,sizeof(char),24,in);

    if(n!=fread((void*)f,sizeof(double),n,in)) {
      fprintf(stderr,"error when reading input!\n"); 
      exit(99);
    }  
/*      printf("f[0] %d\n",&f[0]);
      printf("f[0]wert %g\n", f[0]);
      printf("f[1]wert %g\n", f[1]);
      printf("f[1]wert %g\n", f[2]);
      printf("f[1]wert %g\n", f[3]);  */
      printf("---\n");    
 
      //for(i=0;i<n;i++) u[2*i]=f[i],u[2*i+1]=0; 
      for(i=0;i<n;i++) u[2*i]=(float)f[i],u[2*i+1]=0; 
 /*     printf("u[0] %d\n",&u[0]);
      printf("u[0]wert %g\n", u[0]);
      printf("f[0] %d\n",&f[0]);
      printf("f[0]wert %g\n", f[0]);
      printf("u[1] %d\n",&u[1]);
      printf("u[1]wert %g\n", u[1]);
      printf("f[1] %d\n",&f[1]);
      printf("f[1]wert %g\n", f[1]);
      printf("%s"," here 1c \n");    */

      //free_vector(f,0,n);
    free_doublevector(f,0,n);
 /*     printf("%s"," here 1d \n"); 
      printf("par.sigma = %g\n",par.sigma);   */

  if(par.sigma>0) fourn(u-1,par.dim,3,1); 
  }
   /*    printf("%s"," here 1e \n"); */
  fileclose(in);
  /*   printf("%s"," here 1 \n");   */
  /* open output files */
  fileopenw(&out,par.outname);
 /*   printf("%s"," here 2 \n");   */
  cpu[0]=cpu[1]=0;
  for(num=0; num<par.length; num++) {
      
    /* random field in Fourier space */
    if(par.time) t=clock();
    if(!in) randomfield(u,&par);
    if(par.time) cpu[0]+=(clock()-t)/(float)CLOCKS_PER_SEC;
    /* convolution and normalization */
    if(par.time) t=clock();
    if(par.sigma>0) convolution(u,&par);
    if(par.sigma>0) fourn(u-1,par.dim,3,-1);
    normalize(u,&par);
    if(par.time) cpu[0]+=(clock()-t)/(float)CLOCKS_PER_SEC;
  /*   printf("%s"," here 2c \n");*/
    /* perform statistics */
    if(par.time) t=clock();
    minkowski(out,u,&par);
    if(par.time) cpu[1]+=(clock()-t)/(float)CLOCKS_PER_SEC;
  }
/*      printf("%s"," here 3 \n"); */
  if(par.time) 
    fprintf(stderr,"CPU: %13s%13s\n"
	    "      %8.2f sec %8.2f sec\n",
	    "fields","minkowski",cpu[0],cpu[1]);

  /* output xpm bitmap data */
  fileopenw(&col,par.colname); if(col) picture(1,col,u,&par); fileclose(col);
  fileopenw(&grs,par.grsname); if(grs) picture(0,grs,u,&par); fileclose(grs);

  /* finish */
  fileclose(out);
  free_vector(u,0,2*n);
  exit(0);
}