-
Notifications
You must be signed in to change notification settings - Fork 0
/
roots.cpp
74 lines (70 loc) · 2.16 KB
/
roots.cpp
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
65
66
67
68
69
70
71
72
73
74
//
// roots.cpp
// nmr
//
// Created by Eduardo Rubio on 10/21/19.
// Copyright © 2019 Eduardo Rubio. All rights reserved.
//
#include <stdio.h>
#include "globals.h"
/**
* Bisection Method:
* a: initial endpoint
* b: ending endpoint
* tol: tolerance level for finding roots
* n: max number of iterations
* idx: integer to determine which spline interpolant to use
*/
double bisection( double a, double b, double tol, int n, const Spline spline, int idx ) {
int i = 1;
double p, FP;
double FA = f( a -_data.x[ idx ],
_data.y[ idx ] - options.baseline,
spline.B[ idx ],
spline.C[ idx ],
spline.D[ idx ] );
while( i <= n ){
p = a + ((b-a)/2);
FP = f(p - _data.x[ idx ],
_data.y[ idx ] - options.baseline,
spline.B[ idx ],
spline.C[ idx ],
spline.D[ idx ] );
if( FP == 0 || fabs((b-a)/2) < tol ) return p;
i++;
if( FA * FP > 0 ){
a = p;
FA = FP;
} else {
b = p;
}
}
printf("Error: No root found in index %d\n", idx );
return 0;
}
/**
* Runs through the filtered data and find all the spline intersections with the user designated baseline
*/
void findIntersections( const Spline spline ){
std::ofstream file( "roots.dat" );
double adjustedA, adjustedB;
bool newPeak = true;
Peak peak;
for( int i = 0; i < _data.n-1; i++ ){
adjustedA = _data.y[ i ] - options.baseline;
adjustedB = _data.y[ i + 1 ] - options.baseline;
if( adjustedA * adjustedB < 0 ){
if( newPeak ){
peak.rootA = bisection(_data.x[ i ], _data.x[ i + 1 ], options.tol, 100, spline, i );
peak.indexA = i;
} else {
peak.rootB = bisection(_data.x[ i ], _data.x[ i + 1 ], options.tol, 100, spline, i );
peak.indexB = i;
peak.isComplete = true;
peak.midpoint = (peak.rootB + peak.rootA)/2;
peaks.push_back( peak );
}
newPeak = !newPeak;
}
}
}