Implementation of Gaussian filter

代码片段,留作日后用。

 1 vector<double> GaussianFilter(const vector<double> data, double standard_deviation, int filterlength)
2 { //length of data
3
4 int length = data.size();
5
6 //position array of filter elements
7 int* position = new int[filterlength];
8 int middle = filterlength/2;
9 for(int i = 0; i <filterlength; i++)
10 {
11 position[i] = i - middle;
12 }
13 long double* gaussianCoeffiecients = new long double[filterlength];
14 long double temp1 = 1/sqrt(2* PI * square(standard_deviation));
15 long double temp2 = 2 * square(standard_deviation);
16 for(int i = 0; i <filterlength ; i++)
17 {
18 gaussianCoeffiecients[i] = temp1* exp(-square(position[i])/temp2);
19 }
20 //initilise result vector to be the same length
21 vector<double> result(length);
22
23 for (int i = 0 ; i < length; i++)
24 {
25 double temp = 0.0;
26 if(i < middle)
27 {
28 for(int j = middle - i ; j < filterlength; j++)
29 {
30 temp += data[i - middle + j] * gaussianCoeffiecients[j];
31 }
32 }
33 else if(i >= middle; i <= length - 1 - middle)
34 {
35 for(int j = 0; j < filterlength; j++)
36 {
37 temp += data[i-middle+j]* gaussianCoeffiecients[j];
38 }
39 }
40 else if (i > length - middle - 1)
41 {
42 int lastfilterelement = middle + difference(length,i)- 1;
43 for(int j = 0; j < lastfilterelement; j++)
44 {
45 temp += data[i-middle+j]* gaussianCoeffiecients[j];
46 }
47 }
48 else
49 {//Not Possiable
50 }
51 result[i] = temp;
52 }
53 //memory clean up
54 delete[] gaussianCoeffiecients;
55 delete[] position;
56
57 //return result array
58 return result;
59 }

  

posted @ 2011-08-24 02:48  大有|元亨  阅读(364)  评论(0编辑  收藏  举报