Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "OVR_SensorFilter.h"
00018
00019 namespace OVR {
00020
00021 Vector3f SensorFilter::Total() const
00022 {
00023 Vector3f total = Vector3f(0.0f, 0.0f, 0.0f);
00024 for (int i = 0; i < Size; i++)
00025 total += Elements[i];
00026 return total;
00027 }
00028
00029 Vector3f SensorFilter::Mean() const
00030 {
00031 Vector3f total = Vector3f(0.0f, 0.0f, 0.0f);
00032 for (int i = 0; i < Size; i++)
00033 total += Elements[i];
00034 return total / (float) Size;
00035 }
00036
00037 Vector3f SensorFilter::Median() const
00038 {
00039 int half_window = (int) Size / 2;
00040 float sortx[MaxFilterSize];
00041 float resultx = 0.0f;
00042
00043 float sorty[MaxFilterSize];
00044 float resulty = 0.0f;
00045
00046 float sortz[MaxFilterSize];
00047 float resultz = 0.0f;
00048
00049 for (int i = 0; i < Size; i++)
00050 {
00051 sortx[i] = Elements[i].x;
00052 sorty[i] = Elements[i].y;
00053 sortz[i] = Elements[i].z;
00054 }
00055 for (int j = 0; j <= half_window; j++)
00056 {
00057 int minx = j;
00058 int miny = j;
00059 int minz = j;
00060 for (int k = j + 1; k < Size; k++)
00061 {
00062 if (sortx[k] < sortx[minx]) minx = k;
00063 if (sorty[k] < sorty[miny]) miny = k;
00064 if (sortz[k] < sortz[minz]) minz = k;
00065 }
00066 const float tempx = sortx[j];
00067 const float tempy = sorty[j];
00068 const float tempz = sortz[j];
00069 sortx[j] = sortx[minx];
00070 sortx[minx] = tempx;
00071
00072 sorty[j] = sorty[miny];
00073 sorty[miny] = tempy;
00074
00075 sortz[j] = sortz[minz];
00076 sortz[minz] = tempz;
00077 }
00078 resultx = sortx[half_window];
00079 resulty = sorty[half_window];
00080 resultz = sortz[half_window];
00081
00082 return Vector3f(resultx, resulty, resultz);
00083 }
00084
00085
00086 Vector3f SensorFilter::Variance() const
00087 {
00088 Vector3f mean = Mean();
00089 Vector3f total = Vector3f(0.0f, 0.0f, 0.0f);
00090 for (int i = 0; i < Size; i++)
00091 {
00092 total.x += (Elements[i].x - mean.x) * (Elements[i].x - mean.x);
00093 total.y += (Elements[i].y - mean.y) * (Elements[i].y - mean.y);
00094 total.z += (Elements[i].z - mean.z) * (Elements[i].z - mean.z);
00095 }
00096 return total / (float) Size;
00097 }
00098
00099
00100 Matrix4f SensorFilter::Covariance() const
00101 {
00102 Vector3f mean = Mean();
00103 Matrix4f total = Matrix4f(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0);
00104 for (int i = 0; i < Size; i++)
00105 {
00106 total.M[0][0] += (Elements[i].x - mean.x) * (Elements[i].x - mean.x);
00107 total.M[1][0] += (Elements[i].y - mean.y) * (Elements[i].x - mean.x);
00108 total.M[2][0] += (Elements[i].z - mean.z) * (Elements[i].x - mean.x);
00109 total.M[1][1] += (Elements[i].y - mean.y) * (Elements[i].y - mean.y);
00110 total.M[2][1] += (Elements[i].z - mean.z) * (Elements[i].y - mean.y);
00111 total.M[2][2] += (Elements[i].z - mean.z) * (Elements[i].z - mean.z);
00112 }
00113 total.M[0][1] = total.M[1][0];
00114 total.M[0][2] = total.M[2][0];
00115 total.M[1][2] = total.M[2][1];
00116 for (int i = 0; i < 3; i++)
00117 for (int j = 0; j < 3; j++)
00118 total.M[i][j] *= 1.0f / Size;
00119 return total;
00120 }
00121
00122 Vector3f SensorFilter::PearsonCoefficient() const
00123 {
00124 Matrix4f cov = Covariance();
00125 Vector3f pearson = Vector3f();
00126 pearson.x = cov.M[0][1]/(sqrt(cov.M[0][0])*sqrt(cov.M[1][1]));
00127 pearson.y = cov.M[1][2]/(sqrt(cov.M[1][1])*sqrt(cov.M[2][2]));
00128 pearson.z = cov.M[2][0]/(sqrt(cov.M[2][2])*sqrt(cov.M[0][0]));
00129
00130 return pearson;
00131 }
00132
00133
00134 Vector3f SensorFilter::SavitzkyGolaySmooth8() const
00135 {
00136 OVR_ASSERT(Size >= 8);
00137 return GetPrev(0)*0.41667f +
00138 GetPrev(1)*0.33333f +
00139 GetPrev(2)*0.25f +
00140 GetPrev(3)*0.16667f +
00141 GetPrev(4)*0.08333f -
00142 GetPrev(6)*0.08333f -
00143 GetPrev(7)*0.16667f;
00144 }
00145
00146
00147 Vector3f SensorFilter::SavitzkyGolayDerivative4() const
00148 {
00149 OVR_ASSERT(Size >= 4);
00150 return GetPrev(0)*0.3f +
00151 GetPrev(1)*0.1f -
00152 GetPrev(2)*0.1f -
00153 GetPrev(3)*0.3f;
00154 }
00155
00156 Vector3f SensorFilter::SavitzkyGolayDerivative5() const
00157 {
00158 OVR_ASSERT(Size >= 5);
00159 return GetPrev(0)*0.2f +
00160 GetPrev(1)*0.1f -
00161 GetPrev(3)*0.1f -
00162 GetPrev(4)*0.2f;
00163 }
00164
00165 Vector3f SensorFilter::SavitzkyGolayDerivative12() const
00166 {
00167 OVR_ASSERT(Size >= 12);
00168 return GetPrev(0)*0.03846f +
00169 GetPrev(1)*0.03147f +
00170 GetPrev(2)*0.02448f +
00171 GetPrev(3)*0.01748f +
00172 GetPrev(4)*0.01049f +
00173 GetPrev(5)*0.0035f -
00174 GetPrev(6)*0.0035f -
00175 GetPrev(7)*0.01049f -
00176 GetPrev(8)*0.01748f -
00177 GetPrev(9)*0.02448f -
00178 GetPrev(10)*0.03147f -
00179 GetPrev(11)*0.03846f;
00180 }
00181
00182 Vector3f SensorFilter::SavitzkyGolayDerivativeN(int n) const
00183 {
00184 OVR_ASSERT(Size >= n);
00185 int m = (n-1)/2;
00186 Vector3f result = Vector3f();
00187 for (int k = 1; k <= m; k++)
00188 {
00189 int ind1 = m - k;
00190 int ind2 = n - m + k - 1;
00191 result += (GetPrev(ind1) - GetPrev(ind2)) * (float) k;
00192 }
00193 float coef = 3.0f/(m*(m+1.0f)*(2.0f*m+1.0f));
00194 result = result*coef;
00195 return result;
00196 }
00197
00198
00199
00200
00201 }