Chartjs-Javascript : extrapolate an array of numbers

0👍

Top section

 const interpolateArray = (data, fitCount) => {
  let newData = [];
  let springFactor = Number((data.length - 1) / (fitCount - 1));
  let linearInterpolate = (before, after, atPoint) => {
    return before + (after - before) * atPoint;
  };

  for (var i = 0; i < fitCount - 1; i++) {
    let tmp = i * springFactor;
    let before = Number(Math.floor(tmp)).toFixed();
    let after = Number(Math.ceil(tmp)).toFixed();
    let atPoint = tmp - before;
    newData.push(linearInterpolate(data[before], data[after], atPoint));
  }
  
  // for new allocation
  newData[fitCount - 1] = data[data.length - 1];
  
  return newData;
};

let originalArry = [1, 5, 3];
let newArry = interpolateArray([1, 5, 3], 5);
let p1 = document.createElement('div');
let p2 = document.createElement('div');
document.body.appendChild(p1);
document.body.appendChild(p2);
p1.textContent = 'Original Array : ' + JSON.stringify(originalArry);
p2.textContent = 'After Intrapolation( value = 5) : ' + JSON.stringify(newArry);

Leave a comment