Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Tools/Math/FourierCoefficient.h

Go to the documentation of this file.

    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_