Debian Codegraph @

comedi-calibrate/comedi_soft_calibrate/calibrator_misc.cpp

raw
1 /*
2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17 #include "calibrator_misc.hpp"
18
19 #include <cmath>
20 #include <gsl/gsl_matrix.h>
21 #include <gsl/gsl_multifit.h>
22 #include <gsl/gsl_statistics_double.h>
23 #include <gsl/gsl_vector.h>
24 #include <iostream>
25 #include <sstream>
26 #include <stdexcept>
27
28 double estimateMean(const std::vector<double> &samples)
29 {
30 return gsl_stats_mean(&samples.at(0), 1, samples.size());
31 }
32
33 double estimateStandardDeviation(const std::vector<double> &samples, double mean)
34 {
35 double value = gsl_stats_variance_m(&samples.at(0), 1, samples.size(), mean);
36 return std::sqrt(value);
37 }
38
39 double estimateStandardDeviationOfMean(const std::vector<double> &samples, double mean)
40 {
41 double value = gsl_stats_variance_m(&samples.at(0), 1, samples.size(), mean);
42 return std::sqrt(value / samples.size());
43 }
44
45 // returns polynomial coefficients for polynomial fit to data y = f(x)
46 std::vector<double> fitPolynomial(const std::vector<double> &x, const std::vector<double> &y,
47 double expansionOrigin, unsigned order)
48 {
49 const int NUM_COEFFICIENTS = order + 1;
50 if(x.size() != y.size())
51 {
52 std::ostringstream message;
53 message << __FUNCTION__ << ": x and y sizes are different.\n";
54 throw std::invalid_argument(message.str());
55 }
56 gsl_matrix *covariance = gsl_matrix_alloc(NUM_COEFFICIENTS, NUM_COEFFICIENTS);
57 gsl_multifit_linear_workspace *work = gsl_multifit_linear_alloc(x.size(), NUM_COEFFICIENTS);
58 double chisq;
59 gsl_matrix *m = gsl_matrix_alloc(x.size(), NUM_COEFFICIENTS);
60 unsigned i;
61 for(i = 0; i < x.size(); ++i)
62 {
63 gsl_matrix_set(m, i, 0, 1.);
64 int j;
65 for(j = 1; j < NUM_COEFFICIENTS; ++j)
66 {
67 gsl_matrix_set(m, i, j, gsl_matrix_get(m, i, j - 1) * (x.at(i) - expansionOrigin));
68 }
69 }
70 gsl_vector_const_view b = gsl_vector_const_view_array(&y.at(0), y.size());
71 std::vector<double> coefficients(NUM_COEFFICIENTS);
72 gsl_vector_view result = gsl_vector_view_array(&coefficients.at(0), NUM_COEFFICIENTS);
73 gsl_multifit_linear(m, &b.vector, &result.vector, covariance, &chisq, work);
74 gsl_matrix_free(m);
75 gsl_matrix_free(covariance);
76 gsl_multifit_linear_free(work);
77 return coefficients;
78 }
79
80 Polynomial::Polynomial(): expansionOrigin(0.)
81 {}
82
83 double Polynomial::operator()(double input) const
84 {
85 double value = 0.;
86 double term = 1.;
87 unsigned i;
88 for(i = 0; i < coefficients.size(); ++i)
89 {
90 value += coefficients.at(i) * term;
91 term *= input - expansionOrigin;
92 }
93 return value;
94 }
95
96 unsigned Polynomial::order() const
97 {
98 if(coefficients.size() < 1) throw std::invalid_argument(__FUNCTION__);
99 return coefficients.size() - 1;
100 }
101
102 void printPolynomial(const Polynomial &polynomial)
103 {
104 std::cout << "Polynomial:\n";
105 std::cout << "\torder = " << polynomial.order() << "\n";
106 std::cout << "\texpansion origin = " << polynomial.expansionOrigin << "\n";
107 unsigned j;
108 for(j = 0; j < polynomial.coefficients.size(); ++j)
109 std::cout << "\torder " << j << " coefficient = " << polynomial.coefficients.at(j) << "\n";
110 std::cout << std::flush;
111 }
112