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

Signal1D.hpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
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 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * 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, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00021 #ifndef _Signal1D_hpp
00022 #define _Signal1D_hpp
00023 
00024 #include<ImLib3D/CppTools.hpp>
00025 #include<ImLib3D/ImLib3DFile.hpp>
00026 #include<math.h>
00027 
00029 
00034 class Signal1Df  : public Streamable
00035 {
00036     float x0,x1;
00037     vector<float> values;
00038 public:
00039     void ChangeSupport(float _x0,float _x1){x0=_x0;x1=_x1;}
00040     pair<float,float> GetSupport(){return pair<float,float>(x0,x1);}
00041     int NbSambles() const {return values.size();}
00042     int Size()      const {return values.size();}
00043     void GnuPlot()  const 
00044     {
00045         vector<float> coords(values.size());
00046         for(size_t i=0;i<values.size();i++){coords[i]=ISampleToX(i);}
00047         ::GnuPlot(coords,values);
00048     }
00049     float XToFSample(float x) const {return          (((x-x0)/(x1-x0))*values.size());}
00050     int   XToISample(float x) const {return (int)rint(((x-x0)/(x1-x0))*values.size());}
00051     float ISampleToX(int   i) const {return (i*(x1-x0)/values.size())+x0;}
00052     float &operator[](int i)        {return(values[i]);}
00053     float  operator[](int i) const  {return(values[i]);}
00054     float  operator()(float x) const{return InterpolatedValue(x);}
00055     float &SafeSample(size_t i)  
00056     {
00057         if(i<0){return values[0];}
00058         else
00059         if(i>=values.size()){return values.back();}
00060         else
00061         {return values[i];}
00062     }
00063     float SafeSample(size_t i)  const
00064     {
00065         if(i<0){return values[0];}
00066         else
00067         if(i>=values.size()){return values.back();}
00068         else
00069         {return values[i];}
00070     }
00072     float InterpolatedValue(float x) const
00073     {
00074         x=XToFSample(x);
00075         int i0=(int)x;
00076         float dx=x-i0;
00077         return(SafeSample(i0)*(1-dx)+SafeSample(i0+1)*dx);
00078     }
00079     float &Value(float rx){return SafeSample(XToISample(rx));}
00080     void Stats2ndOrder(float &avg,float &sigma)  const
00081     {
00082         float tot=0;
00083         float tot2=0;
00084         for(uint i=0;i<values.size();i++)
00085         {
00086             tot +=values[i];
00087             tot2+=values[i]*values[i];
00088         }
00089         avg=tot/values.size();
00090         sigma=sqrt(tot2/values.size()-avg*avg);
00091     }
00092     void Smooth(int fradius=3)  
00093     {
00094         // smooth this a bit
00095         vector<float> v0=values;
00096         for(size_t x=0;x<values.size();x++)
00097         {
00098             double s=0;
00099             int ct=0;
00100             for(int xx=(int)x-fradius;xx<=(int)x+fradius;xx++)
00101             {
00102                 if(xx<0 || xx>=(int)values.size()){continue;}
00103                 s+=v0[xx];
00104                 ct++;
00105             }
00106             values[x]=(ct>0 ? s/ct :0);
00107         }
00108     }
00109 
00110     void AddSampleAtBegining(float val)
00111     {
00112         x0-=(x1-x0)/values.size();
00113         values.insert(values.begin(),val);
00114     }
00115     void AddSampleAtEnd(float val)
00116     {
00117         x1+=(x1-x0)/values.size();
00118         values.push_back(val);
00119     }
00120 public:
00121     bool operator==(const Signal1Df &other)
00122     {
00123         return values==other.values && x0==other.x0 && x1==other.x1;
00124     }
00125     bool operator!=(const Signal1Df &other){return !operator==(other);}
00126         
00127 private:
00128     void Read( ImLib3DFile *file,xmlpp::Element *parentNode=NULL,xmlpp::Element *node=NULL);
00129     void Write(ImLib3DFile *file,xmlpp::Element *parentNode=NULL,xmlpp::Element *node=NULL) const;
00130 public:
00131     virtual ~Signal1Df(){}
00132     Signal1Df(int _size,float _x0=0,float _x1=1):
00133         x0(_x0),x1(_x1)
00134     {
00135         values.assign(_size,0);
00136     }
00137     Signal1Df(const string &filename)
00138     {
00139         ReadFromFile(filename);
00140     }
00141 
00142     Signal1Df():
00143         x0(0),x1(0)
00144     {
00145     }
00146 };
00148 void LinearCompleteLowConfidence(Signal1Df &fct,vector<double> &confidence,float minconf);
00149 
00150 #endif// _Signal1D_hpp

Generated on Fri Jun 17 13:36:07 2005 for ImLib3D by  doxygen 1.4.2