1

I have an array which is taking values over the space of a second and calculating the average. The array is then permanently storing the values. I need them to remove the values after the average has been calculated. I have tried clearing the array at the end of the calculateAverage function using levelMeasurements = []; and levelMeasurements = 0; but that does not work.

Any help on this would be greatly appreciated, thanks!

My code:

var levelMeasurements = [];

function collectLevelMeasurements() {
    levelMeasurements.push(averagingLevel);
}
var collectInterval = window.setInterval(collectLevelMeasurements, 0);

function calculateAverage() {
    window.clearInterval(collectInterval);

    var avg = 0;

    for (counter = 0; counter < levelMeasurements.length; counter++) {
        avg += levelMeasurements[counter] / levelMeasurements.length;
    }

    averageAbsoluteLevel = Math.abs(avg);
    averageDbLevel = Tone.gainToDb(averageAbsoluteLevel) * scale + offset;

    console.log("Measure for 5 minutes: average level is:" + averageDbLevel);

}
window.setInterval(calculateAverage, 1000);
Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38
Alfie
  • 23
  • 6
  • You can use `array.length = 0;`, but I'm not sure what you mean by permanently storing values. You can also use [Array#splice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) – Xotic750 Apr 09 '18 at 18:53
  • @Xotic750 after the first time the function has run, and the contents of the array has been averaged, the array keeps the contents without replacing them with the next load of input samples – Alfie Apr 09 '18 at 18:56
  • 1
    You don't execute more than a single collection phase. – Xotic750 Apr 09 '18 at 19:03

2 Answers2

0

You can try following:

levelMeasurements.length = 0.

Or refer more solution at here: How do I empty an array in JavaScript?

Tomato32
  • 2,145
  • 1
  • 10
  • 10
  • This doesn't work unfortunately, it just sets the array to 0 and then does not refresh. I need it to store a whole new set of values in after each interval – Alfie Apr 09 '18 at 18:57
0

Your collectInterval timeout is never reinstated after the first time calculateAverage is called, unless the issue is something else not visible from the code.

In your code calculateAverage is called every 1000 milliseconds, but the values stored in levelMeasurements are not updated after the first window.clearInterval.

I made a simplified snippet without Tone lib:

var levelMeasurements = [];

//added this as a easy source of values
let count = 0;

function collectLevelMeasurements() {
    // levelMeasurements.push(averagingLevel);
  levelMeasurements.push(count++);
}
var collectInterval = window.setInterval(collectLevelMeasurements, 0);

function calculateAverage() {
    window.clearInterval(collectInterval);

    var avg = 0;

    for (counter = 0; counter < levelMeasurements.length; counter++) {
        avg += levelMeasurements[counter] / levelMeasurements.length;
    }

    averageAbsoluteLevel = Math.abs(avg);
    averageDbLevel = averageAbsoluteLevel;
  
    //prolly dont need tone lib for this example
    // averageDbLevel = Tone.gainToDb(averageAbsoluteLevel) * scale + offset;

    console.log("Measure for 5 minutes: average level is:" + averageDbLevel);
  
    //reset these variables
    levelMeasurements = [];
    count = 0;
    console.log(levelMeasurements)//empty array at this point
    //reset the collectInterval!
    collectInterval = window.setInterval(collectLevelMeasurements, 0);

}
window.setInterval(calculateAverage, 1000);
Roope
  • 291
  • 1
  • 8