-
Notifications
You must be signed in to change notification settings - Fork 12
/
xyRealMaxYPoint.ts
42 lines (39 loc) · 1.26 KB
/
xyRealMaxYPoint.ts
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
import { DataXY } from 'cheminfo-types';
import { PointWithIndex } from '../types';
import { xGetTargetIndex } from '../x';
import { xyCheck } from './xyCheck';
/**
* Find the closest minimum going down hill
*
* @param data - Object that contains property x (an ordered increasing array) and y (an array)
* @param options - options
* @returns - An object with the x/y value
*/
export function xyRealMaxYPoint(data: DataXY, options = {}): PointWithIndex {
xyCheck(data);
const { x, y } = data;
const targetIndex = xGetTargetIndex(x, options);
// interpolation to a sin() function
if (
y[targetIndex - 1] > 0 &&
y[targetIndex + 1] > 0 &&
y[targetIndex] >= y[targetIndex - 1] &&
y[targetIndex] >= y[targetIndex + 1]
) {
const alpha = 20 * Math.log10(y[targetIndex - 1]);
const beta = 20 * Math.log10(y[targetIndex]);
const gamma = 20 * Math.log10(y[targetIndex + 1]);
const p = (0.5 * (alpha - gamma)) / (alpha - 2 * beta + gamma);
return {
x: x[targetIndex] + (x[targetIndex] - x[targetIndex - 1]) * p,
y: y[targetIndex] - 0.25 * (y[targetIndex - 1] - y[targetIndex + 1]) * p,
index: targetIndex,
};
} else {
return {
x: x[targetIndex],
y: y[targetIndex],
index: targetIndex,
};
}
}