Пример #1
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);
}
Пример #2
0
int main(void)
{
	int i,j,m,nl,np,nr;
	float *c,sum;
	static int mtest[NTEST+1]={0,2,2,2,2,4,4};
	static int nltest[NTEST+1]={0,2,3,4,5,4,5};
	static int nrtest[NTEST+1]={0,2,1,0,5,4,5};
	static char *ans[NTEST+1]={"",
"                      -0.086  0.343  0.486  0.343 -0.086",
"               -0.143  0.171  0.343  0.371  0.257",
"         0.086 -0.143 -0.086  0.257  0.886",
" -0.084  0.021  0.103  0.161  0.196  0.207  0.196  0.161  0.103  0.021 -0.084",
"         0.035 -0.128  0.070  0.315  0.417  0.315  0.070 -0.128  0.035",
"  0.042 -0.105 -0.023  0.140  0.280  0.333  0.280  0.140 -0.023 -0.105  0.042"};

	c=vector(1,NMAX);
	printf("M nl nr\n");
	printf("\t\t\tSample Savitzky-Golay Coefficients\n");
	for (i=1;i<=NTEST;i++) {
		m=mtest[i];
		nl=nltest[i];
		nr=nrtest[i];
		np=nl+nr+1;
		savgol(c,np,nl,nr,0,m);
		for (sum=0.0,j=1;j<=np;j++) sum += c[j];
		printf("%1d %1d %1d\n",m,nl,nr);
		for (j=nl;j<5;j++) printf("%7s"," ");
		for (j=nl+1;j>=1;j--) printf("%7.3f",c[j]);
		for (j=0;j<nr;j++) printf("%7.3f",c[np-j]);
		printf("\n");
		printf("Sum = %7.3f\n",sum);
		printf("Compare answer:\n%s\n",ans[i]);
	}
	free_vector(c,1,NMAX);
	return 0;
}