Skip to content
Snippets Groups Projects
Commit e1b001ad authored by Clayton, Brandon Scott's avatar Clayton, Brandon Scott
Browse files

Merge branch 'interpolate' into 'main'

add interpolate function

See merge request !234
parents 45d4776b c22be3d4
No related branches found
Tags 3.8.0
1 merge request!234add interpolate function
Pipeline #491655 passed with warnings
......@@ -26,6 +26,28 @@ const erf = (x: number) => {
return x < 0.0 ? -erfBase(-x) : erfBase(x);
};
/**
* Interpolate.
*
* @param xySequence The Xy sequence
* @param value The value to interpolate at.
*/
const interpolate = (xySequence: XySequence, value: number): number => {
const xValues = xySequence.xs;
const yValues = xySequence.ys;
const indexBelow = yValues.findIndex(y => {
return y < value;
});
const x0 = xValues[indexBelow - 1];
const x1 = xValues[indexBelow];
const y0 = yValues[indexBelow - 1];
const y1 = yValues[indexBelow];
const x = interpolateValue(x0, x1, y0, y1, value);
return isNaN(x) ? x : round(x, 6);
};
/**
* Round a number to specific format
*
......@@ -65,30 +87,26 @@ const calculateResponseSpectrum = (
xySequence: XySequence,
returnPeriod: number
): number => {
returnPeriod = 1 / returnPeriod;
const xValues = xySequence.xs;
const yValues = xySequence.ys;
const afeIndexBelowReturnPeriod = yValues.findIndex(y => {
return y < returnPeriod;
});
const x0 = xValues[afeIndexBelowReturnPeriod - 1];
const x1 = xValues[afeIndexBelowReturnPeriod];
const y0 = yValues[afeIndexBelowReturnPeriod - 1];
const y1 = yValues[afeIndexBelowReturnPeriod];
const x = returnPeriodInterpolation(x0, x1, y0, y1, returnPeriod);
return isNaN(x) ? x : round(x, 6);
return interpolate(xySequence, 1 / returnPeriod);
};
const returnPeriodInterpolation = (
/**
* Interpolate.
*
* @param x0 X value before value
* @param x1 X value after value
* @param y0 Y value before value
* @param y1 Y value after value
* @param value Value to interpolate at
*/
const interpolateValue = (
x0: number,
x1: number,
y0: number,
y1: number,
returnPeriod: number
value: number
): number => {
return x0 + (Math.log10(returnPeriod / y0) * (x1 - x0)) / Math.log10(y1 / y0);
return x0 + (Math.log10(value / y0) * (x1 - x0)) / Math.log10(y1 / y0);
};
const erfBase = (x: number) => {
......@@ -115,6 +133,7 @@ const erfBase = (x: number) => {
export const Maths = {
normalCcdf,
erf,
interpolate,
round,
responseSpectrum,
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment