00001 /** 00002 * @file FourierCoefficient.h 00003 * 00004 * Declaration of class FourierCoefficient 00005 * 00006 * @author <a href="mailto:jhoffman@informatik.hu-berlin.de">Jan Hoffmann</a>, Matthias Jüngel 00007 */ 00008 00009 #ifndef __FourierCoefficient_h_ 00010 #define __FourierCoefficient_h_ 00011 00012 #include "Tools/Streams/InOut.h" 00013 #include "Representations/Motion/JointData.h" 00014 00015 00016 /** 00017 * This class contains a set of fourier coefficients as well as 00018 * methods transform them from and to a period of function values 00019 */ 00020 class FourierCoefficient 00021 { 00022 public: 00023 enum {numOfCoeffs = 80}; 00024 00025 /** Constructor */ 00026 FourierCoefficient(); 00027 00028 /** Destructor */ 00029 ~FourierCoefficient(); 00030 00031 /** 00032 * Synthesizes the value of the original function for a given joint 00033 * using the fourier coefficients. A scaling factor is added. This allows 00034 * the amplitude of the curve to be changed while keeping the origin/offset constant. 00035 * This factor is also used for inverting the direction of the function (in essence 00036 * playing the function backward if the factor is smaller than 0) 00037 * @param joint The joint for which the synthesis is calculated. 00038 * @param time Time (in the format returned by "getSystemTime"). This time 00039 * is used to generate the curves as a function of that time. 00040 * @param cMax Number of coefficients used. Small numbers result in a low 00041 * pass behavior, numbers larger than numOfCoeffs/2 result in 00042 * oversampling effects. (This is due to the fact that a continous Fourier 00043 * (back) transformation is calculated from descrete FT coefficients.) 00044 * @param scalingFactor is used to vary the amplitude of the wave without 00045 * touching the offset. 1.0 = 100%, values greater than 00046 * 1 possible. Negative numbers will be interpreted as running the function 00047 * backwards (negative time line) 00048 * @return The synthesized function value. 00049 */ 00050 long fourierSynth( 00051 JointData::JointID joint, 00052 unsigned long time, 00053 int cMax, 00054 double scalingFactor=1.0); 00055 00056 /** 00057 * Like the other fourierSynth, a change to the joint parameter 00058 * and no return value. In contrast to the above, a reference to the 00059 * entire joint data array is passed in order to minimize function 00060 * calls. All joint values are computed in one go. 00061 * 00062 * Also scalingfactor is no longer used. don't need it, really. 00063 * 00064 * @param jointData Array of joint datas to be calculated. 00065 * @param time This time is used to generate the curves as a function of that time. 00066 * @param cMax Number of coefficients used. 00067 * @return nothing 00068 */ 00069 long fourierSynth( 00070 JointData* jointData, 00071 unsigned long time, 00072 int cMax); 00073 00074 /** 00075 * Calculate LUT for the function values so they don't have 00076 * to be computed every time the synth. is used 00077 * @param joint The joint that is described by the function 00078 * @param cMax Number of coefficients used. 00079 * @return true if successful, false if failed 00080 */ 00081 bool calcLUT(JointData::JointID joint, int cMax); 00082 00083 /** 00084 * Calculate all LUTs 00085 * @param cMax Number of coefficients used. 00086 * @return true if successful, false if failed 00087 */ 00088 bool calcAllLUTs(int cMax); 00089 00090 00091 /** 00092 * Calculate coefficients from the function values over 00093 * one period. 00094 * @param functionValues Pointer to an array containing the 00095 * function values 00096 * @param joint The joint that is described by the function 00097 * @return true if successful, false if failed 00098 */ 00099 bool calculate(long* functionValues, JointData::JointID joint); 00100 00101 /** 00102 * Calculates phi and r for the first numOfC coefficients only 00103 * Calculates the coefficients in the exponential form, i.e. 00104 * z = R * exp (i*phi), from the "vector" representation 00105 * z = x + iy 00106 * 00107 * @param numOfC number of coefficients 00108 * @return true if successful, false if failed 00109 */ 00110 bool calcPhiAndR(int numOfC=numOfCoeffs); 00111 00112 /** 00113 * Calculates the coefficients in the vector form, i.e. 00114 * z = x + iy, from the exponential representation 00115 * z = R * exp (i*phi) 00116 * Calculates the real and imaginary parts for the first 00117 * numOfC coefficients 00118 * 00119 * @param numOfC 00120 * @return true if successful, false if failed 00121 */ 00122 bool calcReAndIm(int numOfC=numOfCoeffs); 00123 00124 /** 00125 * Takes 1+3=4 sets of Fourier coefficients and averages them 00126 * according to the weights. negative weights are interpreted 00127 * relativly to fc0, a weight=1 means: go from fc0 100% into 00128 * direction fcn, so fc=fc0+w1*(fc1-fc0)+w2*(fc2-fc0)+... 00129 * Takes an parameter that determines the accuracy of 00130 * the merge, i.e. the number of coefficients that are merged 00131 * @param fc0 Fourier Coefficient set 0 ("stand" would be useful) 00132 * @param fc1 Fourier Coefficient set 1 00133 * @param w1 the weigth of set 1 00134 * @param fc2 Fourier Coefficient set 2 00135 * @param w2 the weigth of set 2 00136 * @param fc3 Fourier Coefficient set 3 00137 * @param w3 the weigth of set 3 00138 * @param numOfC the number of coefficients (starting from the first) 00139 * that should be merged 00140 * @return true if successful, false if failed 00141 */ 00142 bool merge(FourierCoefficient *fc0, FourierCoefficient *fc1, double w1, FourierCoefficient *fc2, double w2, FourierCoefficient *fc3, double w3, int numOfC); 00143 00144 /** 00145 * Similar to merge(many parameters) but only merges two sets! 00146 * @param fc1 Fourier Coefficient set 1 00147 * @param w1 the weigth of set 1 00148 * @param fc2 Fourier Coefficient set 2 00149 * @param w2 the weigth of set 2 00150 * @param numOfC the number of coefficients (starting from the first) 00151 * that should be merged 00152 * @return true if successful, false if failed 00153 */ 00154 bool merge(FourierCoefficient *fc1, double w1, FourierCoefficient *fc2, double w2, int numOfC); 00155 00156 /** 00157 * Function shifts all phases so that the 1 Hz frequency 00158 * of the joint is a sine function starting at t=0 (i.e. 00159 * there is no cosine part).This is necessary when using 00160 * different function since they usually are not 00161 * coherent. Coherence may be desirable in order to merge 00162 * or superimpose two given functions. 00163 * 00164 * @param joint reference joint (it is examined 00165 * @return phaseShift how much the current function is 00166 * out of phase with respect to a 1Hz sine 00167 */ 00168 double phaseAlign(JointData::JointID joint); 00169 00170 /** length of period these coefficients are designed for */ 00171 int lengthOfPeriod; 00172 00173 /** real and imaginary parts of the coefficients*/ 00174 double real[JointData::numOfJoint][numOfCoeffs]; 00175 double imaginary[JointData::numOfJoint][numOfCoeffs]; 00176 00177 /** alternative representation: 00178 * length r and phase phi of the coefficients*/ 00179 double phi[JointData::numOfJoint][numOfCoeffs]; 00180 double r[JointData::numOfJoint][numOfCoeffs]; 00181 00182 /** LUT that stores the function calculated from the coeffs */ 00183 double functionLUT[JointData::numOfJoint][numOfCoeffs]; 00184 00185 bool useLookUp[numOfCoeffs]; 00186 }; 00187 00188 /** 00189 * Streaming operator that reads FourierCoefficients from a stream. 00190 * @param stream The stream from which is read. 00191 * @param fc The GT2001WalkingParameters object. 00192 * @return The stream. 00193 */ 00194 In& operator>>(In& stream,FourierCoefficient& fc); 00195 00196 /** 00197 * Streaming operator that writes FourierCoefficients to a stream. 00198 * @param stream The stream to write on. 00199 * @param fc The FourierCoefficients object. 00200 * @return The stream. 00201 */ 00202 Out& operator<<(Out& stream, FourierCoefficient& fc); 00203 00204 #endif //__FourierCoefficient_h_ 00205 00206 /* 00207 * Change log : 00208 * 00209 * $Log: FourierCoefficient.h,v $ 00210 * Revision 1.1.1.1 2004/05/22 17:37:02 cvsadm 00211 * created new repository GT2004_WM 00212 * 00213 * Revision 1.1 2003/10/07 10:13:24 cvsadm 00214 * Created GT2004 (M.J.) 00215 * 00216 * Revision 1.2 2003/09/26 15:28:10 juengel 00217 * Renamed DataTypes to representations. 00218 * 00219 * Revision 1.1 2003/09/26 11:40:40 juengel 00220 * - sorted tools 00221 * - clean-up in DataTypes 00222 * 00223 * Revision 1.1.1.1 2003/07/02 09:40:22 cvsadm 00224 * created new repository for the competitions in Padova from the 00225 * tamara CVS (Tuesday 2:00 pm) 00226 * 00227 * removed unused solutions 00228 * 00229 * Revision 1.13 2003/02/02 23:30:30 jhoffman 00230 * added merge for only two types of motion and more debugging 00231 * and control stuff (mostly needed for performance testing) 00232 * 00233 * Revision 1.12 2003/01/22 12:16:21 dueffert 00234 * doxygen docu corrected 00235 * 00236 * Revision 1.11 2003/01/21 14:58:39 jhoffman 00237 * increased performance by precalculating sin/cos 00238 * 00239 * Revision 1.10 2003/01/18 08:26:35 jhoffman 00240 * changes in order to improve performance 00241 * 00242 * Revision 1.9 2002/12/10 16:07:05 dueffert 00243 * Fourier working now 00244 * 00245 * Revision 1.8 2002/12/09 15:24:06 dueffert 00246 * some optimisation 00247 * 00248 * Revision 1.7 2002/11/26 12:25:57 dueffert 00249 * doxygen docu added 00250 * 00251 * Revision 1.6 2002/11/22 13:42:08 dueffert 00252 * cleanup 00253 * 00254 * Revision 1.5 2002/11/22 13:41:21 loetzsch 00255 * - removed the FourierCoefficient::loadLegs and ::saveLegs functions 00256 * (streaming operators are now used) 00257 * - .fcb files have text format now 00258 * . moved the .fcb files from /Config to /Config/Fourier 00259 * 00260 * Revision 1.4 2002/11/19 17:14:14 risler 00261 * coding conventions: renamed JointData::joint to JointID, GetName to getName 00262 * 00263 * Revision 1.3 2002/10/14 13:14:24 dueffert 00264 * doxygen comments corrected 00265 * 00266 * Revision 1.2 2002/09/22 18:40:54 risler 00267 * added new math functions, removed GTMath library 00268 * 00269 * Revision 1.1 2002/09/10 15:26:40 cvsadm 00270 * Created new project GT2003 (M.L.) 00271 * - Cleaned up the /Src/DataTypes directory 00272 * - Removed Challenge Code 00273 * - Removed processing of incoming audio data 00274 * - Renamed AcousticMessage to SoundRequest 00275 * 00276 * Revision 1.2 2002/07/23 13:32:57 loetzsch 00277 * new streaming classes 00278 * 00279 * removed many #include statements 00280 * 00281 * Revision 1.1.1.1 2002/05/10 12:40:13 cvsadm 00282 * Moved GT2002 Project from ute to tamara. 00283 * 00284 * Revision 1.8 2002/04/25 14:50:37 kallnik 00285 * changed double/float to double 00286 * added several #include GTMath 00287 * 00288 * PLEASE use double 00289 * 00290 * Revision 1.7 2002/04/23 15:00:06 jhoffman 00291 * changes and additions 00292 * 00293 * Revision 1.6 2002/04/03 16:44:31 jhoffman 00294 * added "stabilizeRobot" to motionControl (which is turned off as a default) 00295 * 00296 * Revision 1.5 2002/03/19 16:45:43 jhoffman 00297 * no message 00298 * 00299 * Revision 1.4 2002/03/19 13:23:50 jhoffman 00300 * no message 00301 * 00302 * Revision 1.3 2002/03/19 12:11:14 jhoffman 00303 * extended functionality (synth, load, save, ...) 00304 * 00305 * Revision 1.2 2002/03/12 14:10:48 jhoffman 00306 * added fourier synthesis functionality which calculates the FT from the coefficients 00307 * 00308 * Revision 1.1 2002/01/26 20:22:52 juengel 00309 * no message 00310 * 00311 * 00312 */