00001 /* 00002 Copyright (c) 2010 Les Newell. All rights reserved 00003 00004 This program is free software: you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation, either version 3 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef MULTIFILTER_H_INCLUDED 00019 #define MULTIFILTER_H_INCLUDED 00020 00021 //This class will filter any number of floating point axes. It also has built in zero referencing 00022 00023 template <int N> 00024 class MultiFilterRef 00025 { 00026 public: 00027 MultiFilterRef() 00028 { 00029 for (int ct=0; ct < N; ct++) 00030 { 00031 filtered[ct] = 0; 00032 zeros[ct] = 0; 00033 amounts[ct] = 0; 00034 } 00035 m_referencing = 0; 00036 } 00037 00038 void ParamChanged(Socket * param) 00039 { 00040 for(int ct=0; ct< N; ct++) 00041 { 00042 if(param == &amounts[ct]) 00043 { 00044 if(amounts[ct] > 1) //greater than 1 is BAD 00045 { 00046 amounts[ct] = 1; 00047 } 00048 } 00049 } 00050 } 00051 00052 00053 void Update(float values[N]) 00054 { 00055 if (m_referencing) 00056 { 00057 m_referencing--; 00058 for (int ct=0; ct< N; ct++) 00059 { 00060 m_averages[ct] += values[ct]; 00061 } 00062 if (m_referencing == 0) 00063 { 00064 for (int ct=0; ct< N; ct++) 00065 { 00066 zeros[ct] = -m_averages[ct] / m_refCounts; 00067 filtered[ct] = 0; 00068 } 00069 } 00070 } 00071 00072 for (int ct=0; ct< N; ct++) 00073 { 00074 filtered[ct] += ((values[ct] + zeros[ct]) - filtered[ct]) * amounts[ct]; 00075 } 00076 } 00077 00078 void Reference(unsigned char count) 00079 { 00080 m_refCounts = count; 00081 m_referencing = count; 00082 for (int ct=0; ct < N; ct++) 00083 { 00084 m_averages[ct] = 0; 00085 } 00086 } 00087 00088 inline bool Referencing() 00089 { 00090 return(m_referencing); 00091 } 00092 00093 SocketF filtered[N]; 00094 ParameterF amounts[N]; 00095 float zeros[N]; 00096 00097 protected: 00098 unsigned char m_refCounts; 00099 float m_averages[N]; 00100 unsigned char m_referencing; 00101 }; 00102 00103 00104 //This class will filter any number of floating point axes. 00105 00106 template <int N> 00107 class MultiFilter 00108 { 00109 public: 00110 MultiFilter() 00111 { 00112 for (int ct=0; ct < N; ct++) 00113 { 00114 filtered[ct] = 0; 00115 amounts[ct] = 0; 00116 } 00117 } 00118 00119 void Update(float values[N]) 00120 { 00121 for (int ct=0; ct< N; ct++) 00122 { 00123 filtered[ct] += (values[ct] - filtered[ct]) * amounts[ct]; 00124 } 00125 } 00126 00127 SocketF filtered[N]; 00128 ParameterF amounts[N]; 00129 00130 }; 00131 00132 00133 class FIRFloat 00134 { 00135 public: 00136 00137 FIRFloat() 00138 { 00139 m_data = NULL; 00140 m_sizeMultiplier = 0; 00141 m_acc = 0; 00142 Init(0); 00143 } 00144 00145 ~FIRFloat() 00146 { 00147 free(m_data); 00148 } 00149 00150 void Init(unsigned char size) 00151 { 00152 m_size = size; 00153 m_index = 0; 00154 m_data = (float *)realloc(m_data, sizeof(float) * m_size); 00155 m_acc = 0; 00156 memset(m_data,0,sizeof(float) * m_size); 00157 m_sizeMultiplier = 1 / (float)m_size; 00158 } 00159 00160 float Update(const float& nextValue) 00161 { 00162 if (m_size <= 1) 00163 { 00164 return(nextValue); 00165 } 00166 m_index ++; 00167 if (m_index >= m_size) 00168 { 00169 m_index =0; 00170 } 00171 m_acc -= m_data[m_index]; 00172 m_data[m_index] = nextValue; 00173 m_acc += nextValue; 00174 return(m_acc * m_sizeMultiplier); 00175 } 00176 00177 00178 private: 00179 int m_index; 00180 int m_size; 00181 float * m_data; 00182 float m_acc; 00183 float m_sizeMultiplier; 00184 }; 00185 00186 00187 00188 template <int N> 00189 class MultiFirFilterRef 00190 { 00191 public: 00192 MultiFirFilterRef() 00193 { 00194 for (int ct=0; ct < N; ct++) 00195 { 00196 filtered[ct] = 0; 00197 zeros[ct] = 0; 00198 } 00199 m_referencing = 0; 00200 } 00201 00202 void ParamChanged(Socket * param) 00203 { 00204 for(int ct=0; ct< N; ct++) 00205 { 00206 if(param == &amounts[ct]) 00207 { 00208 m_firs[ct].Init(amounts[ct]); 00209 } 00210 } 00211 } 00212 00213 void Update(float values[N]) 00214 { 00215 if (m_referencing) 00216 { 00217 m_referencing--; 00218 for (int ct=0; ct< N; ct++) 00219 { 00220 m_averages[ct] += values[ct]; 00221 } 00222 if (m_referencing == 0) 00223 { 00224 for (int ct=0; ct< N; ct++) 00225 { 00226 zeros[ct] = -m_averages[ct] / m_refCounts; 00227 filtered[ct] = 0; 00228 } 00229 } 00230 } 00231 00232 for (int ct=0; ct< N; ct++) 00233 { 00234 filtered[ct] = m_firs[ct].Update(values[ct]) + zeros[ct]; 00235 } 00236 } 00237 00238 void Reference(unsigned char count) 00239 { 00240 m_refCounts = count; 00241 m_referencing = count; 00242 for (int ct=0; ct < N; ct++) 00243 { 00244 m_averages[ct] = 0; 00245 } 00246 } 00247 00248 inline bool Referencing() 00249 { 00250 return(m_referencing); 00251 } 00252 00253 SocketF filtered[N]; 00254 Parameter8 amounts[N]; 00255 float zeros[N]; 00256 00257 protected: 00258 unsigned char m_refCounts; 00259 unsigned char m_referencing; 00260 float m_averages[N]; 00261 FIRFloat m_firs[N]; 00262 }; 00263 00264 00265 #endif// #ifndef MULTIFILTER_H_INCLUDED 00266
1.7.0