Skip to content

Commit 5a217d0

Browse files
author
Rowan Easter
committed
Changed serial printf to more portable snprintf and serial print, changed int to double so can use floats in datapoints
1 parent 34102b0 commit 5a217d0

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=CurveFitting
2-
version=1.0.2
2+
version=1.0.3
33
author=Rotario <[email protected]>
44
maintainer=Rotario <[email protected]>
55
sentence=Fits polynomial curves to given datapoints
66
paragraph=Fit polynomial curves to given points using least squares regression. The max order of polynomial fitting is 20, this should be more than enough to fit most practical problems. All values are kept as double for precision, this works well on a Teensy due to its floating point unit and large (64 bit) double precision. the numbers required increase exponentially as the number of points or order increases.
77
url=https://github.com/Rotario/arduinoCurveFitting
88
includes=curveFitting.h
9-
category=Data Processing, Sensors
9+
category=Data Processing
1010
architectures=*

src/curveFitting.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@
1111

1212
void printMat(const char *s, double*m, int n){
1313
Serial.println(s);
14+
char buf[40];
1415
for (int i = 0; i < n; i++) {
1516
for (int j = 0; j < n; j++) {
16-
Serial.printf("%30.2f\t", m[i*n+j]);
17+
snprintf(buf, 40, "%30.4f\t", m[i*n+j]);
18+
Serial.print(buf);
1719
}
1820
Serial.println();
1921
}
2022
}
2123

2224
void showmat(const char *s, double **m, int n){
2325
Serial.println(s);
26+
char buf[40];
2427
for (int i = 0; i < n; i++) {
2528
for (int j = 0; j < n; j++){
26-
Serial.printf("%30.2f\t", m[i][j]);
29+
snprintf(buf, 40, "%30.4f\t", m[i][j]);
30+
Serial.print(buf);
2731
}
2832
Serial.println();
2933
}
@@ -101,14 +105,14 @@ double curveFitPower(double base, int exponent){
101105

102106
int fitCurve (int order, int nPoints, double py[], int nCoeffs, double *coeffs) {
103107
uint8_t maxOrder = MAX_ORDER;
104-
if (nCoeffs != order + 1) return ORDER_AND_NCOEFFS_DOES_NOT_MATCH; // no of coefficients is one larger than the order of the equation
108+
if (nCoeffs != order + 1) return ORDER_AND_NCOEFFS_DO_NOT_MATCH; // no of coefficients is one larger than the order of the equation
105109
if (nCoeffs > maxOrder || nCoeffs < 2) return ORDER_INCORRECT; //matrix memory hard coded for max of 20 order, which is huge
106110
if (nPoints < 1) return NPOINTS_INCORRECT; //Npoints needs to be positive and nonzero
107111
int i, j;
108112
double T[maxOrder] = {0}; //Values to generate RHS of linear equation
109113
double S[maxOrder*2+1] = {0}; //Values for LHS and RHS of linear equation
110114
double denom; //denominator for Cramer's rule, determinant of LHS linear equation
111-
int x, y;
115+
double x, y;
112116

113117
double px[nPoints]; //Generate X values, from 0 to n
114118
for (i=0; i<nPoints; i++){
@@ -135,28 +139,28 @@ int fitCurve (int order, int nPoints, double py[], int nCoeffs, double *coeffs)
135139

136140
double mat[nCoeffs*nCoeffs]; //Temp matrix as det() method alters the matrix given
137141
cpyArray(masterMat, mat, nCoeffs);
138-
denom = det(mat, nCoeffs, 0);
142+
denom = det(mat, nCoeffs, CURVE_FIT_DEBUG);
139143
cpyArray(masterMat, mat, nCoeffs);
140144

141145
//Generate cramers rule mats
142146
for (i = 0; i < nCoeffs; i++){ //Temporary matrix to substitute RHS of linear equation as per Cramer's rule
143147
subCol(mat, T, i, nCoeffs);
144-
coeffs[nCoeffs-i-1] = det(mat, nCoeffs, 0)/denom; //Coefficients are det(M_i)/det(Master)
148+
coeffs[nCoeffs-i-1] = det(mat, nCoeffs, CURVE_FIT_DEBUG)/denom; //Coefficients are det(M_i)/det(Master)
145149
cpyArray(masterMat, mat, nCoeffs);
146150
}
147151
return 0;
148152
}
149153

150154
int fitCurve (int order, int nPoints, double px[], double py[], int nCoeffs, double *coeffs) {
151155
uint8_t maxOrder = MAX_ORDER;
152-
if (nCoeffs != order + 1) return ORDER_AND_NCOEFFS_DOES_NOT_MATCH; //Number of coefficients is one larger than the order of the equation
156+
if (nCoeffs != order + 1) return ORDER_AND_NCOEFFS_DO_NOT_MATCH; //Number of coefficients is one larger than the order of the equation
153157
if(nCoeffs > maxOrder || nCoeffs < 2) return ORDER_INCORRECT; //Matrix memory hard coded for max of 20 order, which is huge
154158
if (nPoints < 1) return NPOINTS_INCORRECT; //Npoints needs to be positive and nonzero
155159
int i, j;
156160
double T[maxOrder] = {0}; //Values to generate RHS of linear equation
157161
double S[maxOrder*2+1] = {0}; //Values for LHS and RHS of linear equation
158162
double denom; //denominator for Cramer's rule, determinant of LHS linear equation
159-
int x, y;
163+
double x, y;
160164

161165
for (i=0; i<nPoints; i++) {//Generate matrix elements
162166
x = px[i];
@@ -178,14 +182,14 @@ int fitCurve (int order, int nPoints, double px[], double py[], int nCoeffs, dou
178182

179183
double mat[nCoeffs*nCoeffs]; //Temp matrix as det() method alters the matrix given
180184
cpyArray(masterMat, mat, nCoeffs);
181-
denom = det(mat, nCoeffs, 0);
185+
denom = det(mat, nCoeffs, CURVE_FIT_DEBUG);
182186
cpyArray(masterMat, mat, nCoeffs);
183187

184188
//Generate cramers rule mats
185189
for (i = 0; i < nCoeffs; i++){ //Temporary matrix to substitute RHS of linear equation as per Cramer's rule
186190
subCol(mat, T, i, nCoeffs);
187-
coeffs[nCoeffs-i-1] = det(mat, nCoeffs, 0)/denom; //Coefficients are det(M_i)/det(Master)
191+
coeffs[nCoeffs-i-1] = det(mat, nCoeffs, CURVE_FIT_DEBUG)/denom; //Coefficients are det(M_i)/det(Master)
188192
cpyArray(masterMat, mat, nCoeffs);
189193
}
190194
return 0;
191-
}
195+
}

src/curveFitting.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
#define curveFit_h
1111

1212
#include <Arduino.h>
13-
#define MAX_ORDER 20;
13+
#define MAX_ORDER 20
14+
15+
#ifndef CURVE_FIT_DEBUG
16+
#define CURVE_FIT_DEBUG 0
17+
#endif
1418

1519
/* Enum for error messages */
1620
enum curveFitERROR{
17-
ORDER_AND_NCOEFFS_DOES_NOT_MATCH = -1,
21+
ORDER_AND_NCOEFFS_DO_NOT_MATCH = -1,
1822
ORDER_INCORRECT = -2,
1923
NPOINTS_INCORRECT = -3
2024
};

0 commit comments

Comments
 (0)