Stimfit  0.13.15
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
fit.h
Go to the documentation of this file.
00001 // Header file for the stimfit namespace
00002 // Routines for fitting functions to data
00003 // last revision: 08-08-2006
00004 // C. Schmidt-Hieber, christsc@gmx.de
00005 
00006 // This program is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU General Public License
00008 // as published by the Free Software Foundation; either version 2
00009 // of the License, or (at your option) any later version.
00010 
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 
00026 #ifndef _FITLIB_H
00027 #define _FITLIB_H
00028 
00029 #include "./stfmath.h"
00030 #include <deque>
00031 
00032 namespace stf {
00033 
00038 
00039 
00045 template <typename T>
00046 T linFit(
00047         const std::vector<T>& x,
00048         const std::vector<T>& y,
00049         T& m,
00050         T& c
00051 );
00052 
00054 
00065 double StfDll lmFit(const Vector_double& data, double dt,
00066                     const stf::storedFunc& fitFunc, const Vector_double& opts,
00067                     bool use_scaling, Vector_double& p, std::string& info, int& warning );
00068 
00070 
00077 double flin(double x, const Vector_double& p);
00078 
00080 void flin_init(const Vector_double& data, double base, double peak,
00081         double RTLoHi, double HalfWidth, double dt, Vector_double& pInit );
00082 
00084 
00085 stf::storedFunc initLinFunc();
00086 
00088 
00096  Vector_double get_scale(Vector_double& data, double oldx);
00097 
00099 
00101 Vector_double LM_default_opts();
00102 
00103 }
00104 
00105 template <typename T>
00106 T stf::linFit(const std::vector<T>& x,
00107         const std::vector<T>& y,
00108         T& m,
00109         T& c)
00110 {
00111     double sum_x=0.0;
00112     double sum_y=0.0;
00113     double sum_xx=0.0;
00114     double sum_xy=0.0;
00115     for (unsigned n=0;n<x.size();++n) {
00116         sum_x+=x[n];
00117         sum_y+=y[n];
00118         sum_xx+=x[n]*x[n];
00119         sum_xy+=x[n]*y[n];
00120     }
00121     m=(T)(((T)x.size()*sum_xy-sum_x*sum_y)/((T)x.size()*sum_xx-sum_x*sum_x));
00122     c=(T)((sum_y-m*sum_x)/(T)x.size());
00123     T error = 0.0;
00124     for (unsigned n=0;n<x.size();++n) {
00125         error += (y[n]-(m*x[n]+c)) * (y[n]-(m*x[n]+c));
00126     }
00127     return error;
00128 
00131 }
00132 
00133 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines