ml-spectra-processing
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -0,1 +1,12 @@ | ||
# [0.3.0](https://github.com/cheminfo/spectra-processing/compare/v0.2.0...v0.3.0) (2019-08-30) | ||
### Features | ||
* **x:** add getTargetIndex ([389c3be](https://github.com/cheminfo/spectra-processing/commit/389c3be)) | ||
* **xy:** add maxClosestYPoint and minClosestYPoint ([85e36fe](https://github.com/cheminfo/spectra-processing/commit/85e36fe)) | ||
* **xy:** add realMinYPoint and realMaxYPoint ([0c628e3](https://github.com/cheminfo/spectra-processing/commit/0c628e3)) | ||
# [0.2.0](https://github.com/cheminfo/spectra-processing/compare/v0.1.5...v0.2.0) (2019-08-17) | ||
@@ -2,0 +13,0 @@ |
186
lib/index.js
@@ -115,3 +115,3 @@ 'use strict'; | ||
* @param {boolean} [options.reverse=false] - Integrate from the larger value to the smallest value | ||
* @return {{x:[],y:[]}} A object with the integration function | ||
* @return {{x:[],y:[]}} An object with the integration function | ||
*/ | ||
@@ -315,2 +315,179 @@ | ||
/** | ||
* Find the closest minimum going down hill | ||
* @param {object} [points={}] - Object of points contains property x (an ordered increasing array) and y (an array) | ||
* @param {object} [options={}] | ||
* @param {number} [options.target] | ||
* @param {number} [options.targetIndex=0] | ||
* @return {{x,y,xIndex}} An object with the x/y value | ||
*/ | ||
function minClosestYPoint(points, options = {}) { | ||
check(points); | ||
const { x, y } = points; | ||
let { target, targetIndex } = options; | ||
if (targetIndex === undefined) { | ||
if (target !== undefined) { | ||
targetIndex = findClosestIndex(x, target); | ||
} else { | ||
targetIndex = 0; | ||
} | ||
} | ||
let previousIndex = Number.MIN_SAFE_INTEGER; | ||
let currentIndex = targetIndex; | ||
let minY = y[targetIndex]; | ||
while (currentIndex !== previousIndex) { | ||
previousIndex = currentIndex; | ||
if (currentIndex > 0 && y[currentIndex - 1] < minY) { | ||
currentIndex--; | ||
} else if (currentIndex < x.length - 1 && y[currentIndex + 1] < minY) { | ||
currentIndex++; | ||
} | ||
minY = y[currentIndex]; | ||
} | ||
return { | ||
x: x[currentIndex], | ||
y: y[currentIndex], | ||
index: currentIndex, | ||
}; | ||
} | ||
/** | ||
* Find the closest maximum going up hill | ||
* @param {object} [points={}] - Object of points contains property x (an ordered increasing array) and y (an array) | ||
* @param {object} [options={}] | ||
* @param {number} [options.target] | ||
* @param {number} [options.targetIndex=0] | ||
* @return {{x,y,xIndex}} An object with the x/y value | ||
*/ | ||
function maxClosestYPoint(points, options = {}) { | ||
check(points); | ||
const { x, y } = points; | ||
let { target, targetIndex } = options; | ||
if (targetIndex === undefined) { | ||
if (target !== undefined) { | ||
targetIndex = findClosestIndex(x, target); | ||
} else { | ||
targetIndex = 0; | ||
} | ||
} | ||
let previousIndex = Number.MIN_SAFE_INTEGER; | ||
let currentIndex = targetIndex; | ||
let maxY = y[targetIndex]; | ||
while (currentIndex !== previousIndex) { | ||
previousIndex = currentIndex; | ||
if (currentIndex > 0 && y[currentIndex - 1] > maxY) { | ||
currentIndex--; | ||
} else if (currentIndex < x.length - 1 && y[currentIndex + 1] > maxY) { | ||
currentIndex++; | ||
} | ||
maxY = y[currentIndex]; | ||
} | ||
return { | ||
x: x[currentIndex], | ||
y: y[currentIndex], | ||
index: currentIndex, | ||
}; | ||
} | ||
/** | ||
* Returns the targetIndex | ||
* @param {array} [x] | ||
* @param {object} [options={}] | ||
* @param {number} [options.target] | ||
* @param {number} [options.targetIndex=0] | ||
* @param {number} | ||
*/ | ||
function getTargetIndex(x, options = {}) { | ||
let { target, targetIndex } = options; | ||
if (targetIndex === undefined) { | ||
if (target !== undefined) { | ||
return findClosestIndex(x, target); | ||
} else { | ||
return 0; | ||
} | ||
} | ||
return targetIndex; | ||
} | ||
function realMinYPoint(points, options = {}) { | ||
check(points); | ||
const { x, y } = points; | ||
const targetIndex = getTargetIndex(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] | ||
) { | ||
let alpha = 20 * Math.log10(-y[targetIndex - 1]); | ||
let beta = 20 * Math.log10(-y[targetIndex]); | ||
let gamma = 20 * Math.log10(-y[targetIndex + 1]); | ||
let 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, | ||
}; | ||
} | ||
} | ||
/** | ||
* Find the closest minimum going down hill | ||
* @param {object} [points={}] - Object of points contains property x (an ordered increasing array) and y (an array) | ||
* @param {object} [options={}] | ||
* @param {number} [options.target] | ||
* @param {number} [options.targetIndex=0] | ||
* @return {{x,y,xIndex}} An object with the x/y value | ||
*/ | ||
function realMaxYPoint(points, options = {}) { | ||
check(points); | ||
const { x, y } = points; | ||
const targetIndex = getTargetIndex(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] | ||
) { | ||
let alpha = 20 * Math.log10(y[targetIndex - 1]); | ||
let beta = 20 * Math.log10(y[targetIndex]); | ||
let gamma = 20 * Math.log10(y[targetIndex + 1]); | ||
let 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, | ||
}; | ||
} | ||
} | ||
const XY = { | ||
@@ -325,2 +502,6 @@ check, | ||
sortX, | ||
minClosestYPoint, | ||
maxClosestYPoint, | ||
realMaxYPoint, | ||
realMinYPoint, | ||
}; | ||
@@ -601,3 +782,4 @@ | ||
multiply, | ||
divide | ||
divide, | ||
getTargetIndex, | ||
}; | ||
@@ -604,0 +786,0 @@ |
{ | ||
"name": "ml-spectra-processing", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Various method to process spectra", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -7,2 +7,3 @@ import { findClosestIndex } from './findClosestIndex'; | ||
import { divide } from './divide'; | ||
import { getTargetIndex } from './getTargetIndex'; | ||
@@ -15,3 +16,4 @@ export const X = { | ||
multiply, | ||
divide | ||
divide, | ||
getTargetIndex, | ||
}; |
@@ -9,4 +9,7 @@ import { check } from './check'; | ||
import { sortX } from './sortX'; | ||
import { minClosestYPoint } from './minClosestYPoint'; | ||
import { maxClosestYPoint } from './maxClosestYPoint'; | ||
import { realMinYPoint } from './realMinYPoint'; | ||
import { realMaxYPoint } from './realMaxYPoint'; | ||
export const XY = { | ||
@@ -21,2 +24,6 @@ check, | ||
sortX, | ||
minClosestYPoint, | ||
maxClosestYPoint, | ||
realMaxYPoint, | ||
realMinYPoint, | ||
}; |
@@ -13,3 +13,3 @@ import { getFromToIndex } from '../x/getFromToIndex'; | ||
* @param {boolean} [options.reverse=false] - Integrate from the larger value to the smallest value | ||
* @return {{x:[],y:[]}} A object with the integration function | ||
* @return {{x:[],y:[]}} An object with the integration function | ||
*/ | ||
@@ -16,0 +16,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50345
34
1402