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

Merge branch 'interpolate' into 'main'

fix interpolate to handle any array

See merge request !235
parents e1b001ad 283e9de3
No related branches found
No related tags found
1 merge request!235fix interpolate to handle any array
Pipeline #491660 passed with warnings
...@@ -36,14 +36,12 @@ const interpolate = (xySequence: XySequence, value: number): number => { ...@@ -36,14 +36,12 @@ const interpolate = (xySequence: XySequence, value: number): number => {
const xValues = xySequence.xs; const xValues = xySequence.xs;
const yValues = xySequence.ys; const yValues = xySequence.ys;
const indexBelow = yValues.findIndex(y => { const index = closestIndex(yValues, value);
return y < value;
}); const x0 = xValues[index - 1];
const x1 = xValues[index];
const x0 = xValues[indexBelow - 1]; const y0 = yValues[index - 1];
const x1 = xValues[indexBelow]; const y1 = yValues[index];
const y0 = yValues[indexBelow - 1];
const y1 = yValues[indexBelow];
const x = interpolateValue(x0, x1, y0, y1, value); const x = interpolateValue(x0, x1, y0, y1, value);
return isNaN(x) ? x : round(x, 6); return isNaN(x) ? x : round(x, 6);
}; };
...@@ -90,6 +88,20 @@ const calculateResponseSpectrum = ( ...@@ -90,6 +88,20 @@ const calculateResponseSpectrum = (
return interpolate(xySequence, 1 / returnPeriod); return interpolate(xySequence, 1 / returnPeriod);
}; };
/**
* Return index of closest value.
*
* @param values Array of values
* @param value Value to find
*/
const closestIndex = (values: number[], value: number): number => {
const closestValue = values.reduce((prev, curr) =>
Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev
);
return values.findIndex(value => value === closestValue);
};
/** /**
* Interpolate. * Interpolate.
* *
......
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