/
parameter_estimator.h
64 lines (57 loc) · 2.23 KB
/
parameter_estimator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef _PARAMETER_ESTIMATOR_H_
#define _PARAMETER_ESTIMATOR_H_
#ifdef __cplusplus
extern "C" void estimate_csi_parameters(double *test_data, double *parameters);
#else
void estimate_csi_parameters(double *test_data, double *parameters);
#endif
/**
* Error function that calculates residuals.
*/
#ifdef __cplusplus
struct ErrorFunction
{
typedef Eigen::Matrix<double, Eigen::Dynamic, 1> Vector;
ErrorFunction() : observed_x(0), observed_y(0), fitting_function(0) {}
ErrorFunction(const Vector& observed_x,
const Vector& observed_y,
double (*fitting_function)(double, const Eigen::VectorXd&)) : observed_x(observed_x), observed_y(observed_y), fitting_function(fitting_function) {}
void operator()(const Eigen::VectorXd& xval,
Eigen::VectorXd& fval,
Eigen::MatrixXd&) const
{
fval.resize(std::min(observed_x.size(), observed_y.size()));
for(lsq::Index i = 0; i < std::min(observed_x.size(), observed_y.size()); ++i) {
//~ fval(i) = pow(observed_y(i) - fitting_function(observed_x(i), xval), 2);
fval(i) = observed_y(i) - fitting_function(observed_x(i), xval);
}
}
/**
* Sets data points for non-linear least squares fitting.
*
* @param observed_x Reference to the vector containing data from independent variable.
* @param observed_y Reference to the vector containing data from dependent variable.
*/
void set_observation(const Vector& observed_x, const Vector& observed_y)
{
this->observed_x = observed_x;
this->observed_y = observed_y;
}
/**
* Set model function and number of parametes to find.
*
* @param fitting_function Function pointer to the model function whose parameters are to be found.
* The first argument is the x variable, the second is a Vector containing the current guess
* for the parameters.
*/
void set_fitting_function(double (*fitting_function)(double, const Eigen::VectorXd&))
{
this->fitting_function = fitting_function;
}
private:
Vector observed_x;
Vector observed_y;
double (*fitting_function)(double, const Eigen::VectorXd&);
};
#endif
#endif