Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

lagrange.h

Go to the documentation of this file.
00001 #ifndef LAGRANGE
00002 #define LAGRANGE
00003 #include <iostream>
00004 #include <fstream>
00005 #include <complex.h>
00006 // 
00007 // Copyright(c) 1993-1996 Tony Kirke
00008 // author="Tony Kirke" *
00009 /*
00010  * SPUC - Signal processing using C++ - A DSP library
00011  * 
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2, or (at your option)
00015  * any later version.
00016  * 
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  * 
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025 */
00026 using namespace std;
00027 namespace SPUC {
00036 
00037 
00038 
00039 
00040 
00041 
00042 template <class Numeric> class lagrange
00043 {
00044       public: 
00045                 long num_taps;
00046         double* coeff;
00047       protected:
00048         Numeric* z; 
00049         Numeric output;
00050       
00051       public: 
00053           void reset() {        
00054                           for (int i=0;i<=num_taps;i++) z[i] = 0;
00055                           output = 0;
00056           }
00058       Numeric out() { return(output); }
00060       Numeric check(long i) { return(z[i]); }
00061         
00062           lagrange(void) { };
00064           lagrange(long n) : num_taps(n) {
00065                 int i;
00066                 coeff = new double[n+1];
00067                 z = new Numeric[n+1];
00068                 for (i=0;i<=n;i++) {
00069                         z[i] = 0;
00070                         coeff[i] = 0;  
00071                 }
00072           } 
00074                 ~lagrange(void) {
00075                           if (num_taps > 0) {
00076                                 delete [] coeff;
00077                                 delete [] z;
00078                           }
00079                 }
00081 void set_size(long n)  {
00082         int i;
00083         num_taps = n;
00084         coeff = new double[n+1];
00085         z = new Numeric[n+1];
00086         for (i=0;i<=n;i++) {
00087                 z[i] = 0;
00088                 coeff[i] = 0;  
00089         }
00090 } 
00092 void input(Numeric in) {
00093         int i; 
00094         // Update history of inputs
00095         for (i=num_taps;i>0;i--) z[i] = z[i-1];  
00096         // Add new input
00097         z[0] = in;   
00098 }
00103 Numeric update(Numeric in, double offset) {
00104         int i; 
00105         // Update history of inputs
00106         for (i=num_taps;i>0;i--) z[i] = z[i-1];  
00107         // Add new input
00108         z[0] = in;   
00109         // Calculate coefficients
00110         calculate_coeff(offset);
00111         // Perform FIR
00112         return(fir());
00113 }
00115 Numeric fir(void) {
00116         int i;                                                       
00117         // Perform FIR
00118         for (output=0,i=0;i<=num_taps;i++) output += coeff[i]*z[i];
00119         return(output);
00120 }
00122 Numeric rephase(double offset) {                                                  
00123         // Calculate coefficients
00124         calculate_coeff(offset);
00125         // Perform FIR
00126         return(fir());
00127 }
00129 void calculate_coeff(double offset) {
00130         int i,k;                                                       
00131         // Calculate coefficients
00132         double off = offset + num_taps/2;
00133         for (k=0;k<=num_taps;k++) {
00134                 coeff[num_taps-k] = 1;
00135                 for (i=0;i<=num_taps;i++) if (k!=i) coeff[num_taps-k] *= double(i-off)/double(i-k);
00136         }
00137 }
00139 void print() {
00140         cout << "Lagrange coefficients ";
00141         for (long i=0;i<=num_taps;i++) {
00142                 cout << coeff[i] << ' ';
00143                 if ((i+1)%6 == 0) cout << '\n';
00144         }
00145         cout << '\n';
00146         cout.flush();
00147 }    
00148       
00149 };
00150      
00151 } // namespace SPUC 
00152 #endif      

Generated on Fri Sep 16 11:02:27 2005 for spuc by  doxygen 1.4.4