Gamma  0.9.5
Generic Synthesis Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
/Users/ljp/code/gamma/trunk/Gamma/TransferFunc.h
00001 #ifndef GAMMA_TRANSFER_FUNC_H_INC
00002 #define GAMMA_TRANSFER_FUNC_H_INC
00003 
00004 /*  Gamma - Generic processing library
00005     See COPYRIGHT file for authors and license information */
00006 
00007 #include <math.h>
00008 #include <vector>
00009 #include <complex>
00010 
00011 namespace gam{
00012 
00013 
00015 class TransferFunc {
00016 public:
00017     typedef std::complex<double> Complex;
00018 
00020     TransferFunc(double gain=1): mGain(gain){}
00021 
00023     TransferFunc& addX(double c, double d){ mX.push_back(DelayUnit(c,d)); return *this; }
00024 
00026     TransferFunc& addY(double c, double d){ mY.push_back(DelayUnit(c,d)); return *this; }
00027     
00029     TransferFunc& clear(){ mX.clear(); mY.clear(); return *this; }
00030     
00032     TransferFunc& gain(double v){ mGain=v; return *this; }
00033     
00035     Complex operator()(double f){
00036         Complex X(0,0), Y(1,0);
00037         f *= M_2PI;
00038         for(uint32_t i=0; i<mX.size(); ++i) X += mX[i].response(f);
00039         for(uint32_t i=0; i<mY.size(); ++i) Y -= mY[i].response(f);
00040         return X/Y * mGain; // H(z) = Y(z)/X(z)
00041     }
00042 
00043 protected:
00044 
00045     struct DelayUnit{
00048         DelayUnit(double c_, double d_): c(c_), d(d_){}
00049 
00050         Complex response(double f){
00051             double phs = f*d;
00052             return Complex(c*::cos(phs), c*::sin(phs));
00053         }
00054 
00055         double c, d;
00056     };
00057 
00058     std::vector<DelayUnit> mX, mY;
00059     double mGain;
00060 };
00061 
00062 } // gam::
00063 
00064 #endif