Stimfit @PACKAGE_VERSION@
Loading...
Searching...
No Matches
fit.h
Go to the documentation of this file.
1// Header file for the stimfit namespace
2// Routines for fitting functions to data
3// last revision: 08-08-2006
4// C. Schmidt-Hieber, christsc@gmx.de
5
6// This program is free software; you can redistribute it and/or
7// modify it under the terms of the GNU General Public License
8// as published by the Free Software Foundation; either version 2
9// of the License, or (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
25
26#ifndef _FITLIB_H
27#define _FITLIB_H
28
29#include "./stfnum.h"
30#include <deque>
31
32namespace stfnum {
33
37
39
45template <typename T>
46T linFit(
47 const std::vector<T>& x,
48 const std::vector<T>& y,
49 T& m,
50 T& c
51);
52
54
65double StfnumDll lmFit(const Vector_double& data, double dt,
66 const stfnum::storedFunc& fitFunc, const Vector_double& opts,
67 bool use_scaling, Vector_double& p, std::string& info, int& warning );
68
70
77double flin(double x, const Vector_double& p);
78
80void flin_init(const Vector_double& data, double base, double peak,
81 double RTLoHi, double HalfWidth, double dt, Vector_double& pInit );
82
84
87
89
98
100
103
104}
105
106template <typename T>
107T stfnum::linFit(const std::vector<T>& x,
108 const std::vector<T>& y,
109 T& m,
110 T& c)
111{
112 double sum_x=0.0;
113 double sum_y=0.0;
114 double sum_xx=0.0;
115 double sum_xy=0.0;
116 for (unsigned n=0;n<x.size();++n) {
117 sum_x+=x[n];
118 sum_y+=y[n];
119 sum_xx+=x[n]*x[n];
120 sum_xy+=x[n]*y[n];
121 }
122 m=(T)(((T)x.size()*sum_xy-sum_x*sum_y)/((T)x.size()*sum_xx-sum_x*sum_x));
123 c=(T)((sum_y-m*sum_x)/(T)x.size());
124 T error = 0.0;
125 for (unsigned n=0;n<x.size();++n) {
126 error += (y[n]-(m*x[n]+c)) * (y[n]-(m*x[n]+c));
127 }
128 return error;
129
131
132}
133
134#endif
WORD TpMarker int n
Definition Son.h:353
std::vector< double > Vector_double
Definition core.h:55
StfnumDll stfnum::storedFunc initLinFunc()
initializes a linear function
T linFit(const std::vector< T > &x, const std::vector< T > &y, T &m, T &c)
Performs a linear fit.
Definition fit.h:107
StfnumDll double base(enum stfnum::baseline_method method, double &var, const std::vector< double > &data, std::size_t llb, std::size_t ulb)
Calculate the average of all sampling points between and including llb and ulb.
StfnumDll double peak(const std::vector< double > &data, double base, std::size_t llp, std::size_t ulp, int pM, stfnum::direction, double &maxT)
Find the peak value of data between llp and ulp.
double flin(double x, const Vector_double &p)
Linear function.
StfnumDll Vector_double LM_default_opts()
Return default LM options.
Vector_double get_scale(Vector_double &data, double oldx)
Compute and perform normalisation.
double StfnumDll lmFit(const Vector_double &data, double dt, const stfnum::storedFunc &fitFunc, const Vector_double &opts, bool use_scaling, Vector_double &p, std::string &info, int &warning)
Uses the Levenberg-Marquardt algorithm to perform a non-linear least-squares fit.
void flin_init(const Vector_double &data, double base, double peak, double RTLoHi, double HalfWidth, double dt, Vector_double &pInit)
Dummy function to be passed to stfnum::storedFunc for linear functions.
Definition fit.h:32
Math functions.
#define StfnumDll
Defines dll export or import functions for libstfnum on Windows.
Definition stfnum.h:38
Function used for least-squares fitting.
Definition stfnum.h:270