0

I have to plot a graph based on the data values v/s time {x-axis= time[] ,y-axis = data1[]}. I want to extrapolate this array and then plot the graph . I used the following code is this the correct way to do so?

    function interpolateArray(data, fitCount) {

    var linearInterpolate = function (before, after, atPoint) {
        return before + (after - before) * atPoint;
    };

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

data1 = interpolateArray(data1,(data1.length*2))

data2 = interpolateArray(data2,(data2.length*2))

and then I converted date in epoch time and then did the same and converted back to utc time , like this

Timestamp11=interpolateArray(Timestamp11,(Timestamp11.length*2))


for(var i=0;i<Timestamp11.length;i++)
{
console.log(Timestamp11[i]);
var edgeTime11=Timestamp11[i]/1000000
var myDate11 = new Date(Time11)


var utcString = myDate11.toString();
var time = utcString.slice(16, 24);

Timestamp11[i] = time;}

window.myLine3 = new Chart(ctx3, charInit3(data1, data2, Timestamp11));

Is it the correct way to extrapolate a time series data?? if not please correct me.

minu
  • 1
  • 2
  • for function() linearInterpolate Make sure you verify the inputs otherwise it will fail, unless, the inputs are always sanitized. And as for the empty array newData[0] = data[0]; Do an empty array and array.push... it would clean out the code. – Sergio Rodriguez Jun 07 '21 at 08:15
  • but it is working very well here [link](http://jsfiddle.net/sbv2jj9m/4/) – minu Jun 07 '21 at 08:32
  • 1
    If the goal is to make it work, instead of improving the code... Then yes. Even bad code runs and gets the job done. – Sergio Rodriguez Jun 07 '21 at 10:40
  • is it correct way to extrapolate a time series data?? – minu Jun 07 '21 at 10:42
  • If this is a math question. This is the wrong place. That said, find a method and apply the formula in code. https://en.wikipedia.org/wiki/Extrapolation – Sergio Rodriguez Jun 07 '21 at 20:10

1 Answers1

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);
Sergio Rodriguez
  • 8,258
  • 3
  • 18
  • 25