Cool mechanism this double buffering to take advantage of internal speed and 'external' coding simplicity!...
Lowering the sampling rate to give enough time to do the (filter/average) calculations would be the point. For example, a sampling rate of 128[samples] / 3[secs] = 43 [samples/sec] would deliver enough time - 3 seconds - to do the work. Depending how 'fast' temperature is changing though, only a segment of the 128 samples should then be taken... If this does not deliver enough samples, buffer size and sampling rate can to be increased proportionally.
Another solution - with high(er) sampling rate / samplings over a shorter period - for example, 0.5[secs], but allowing a longer period - for example, 5[secs] - can be achieved by option repeat:false. After processing the samples, issueing a .startInput() with a timeout will for sure prevent overruns. A third option is to put start with no-repeat into a function and invoke it on the desired interval. For example:
Every ... milliseconds, take 128 samples over a period of 0.5 seconds and average over 124 :
var samplingInterval = null;
var wf = new Waveform(128,{doubleBuffer:false, bits:16});
wf.on("buffer", function(arr) {
arr.sort();
var view = new Uint16Array(arr.buffer, 4, 128 - 4);
console.log((E.sum(view)/65536)/view.length);
});
var startSampling = function(interval) {
var si = isNaN(interval) ? 5000 : (interval < 1000) ? 1000 : interval;
samplingInterval = setInterval(function(){
wf.startInput(pin,256) ,{repeat:false});
},si);
};
var stopSampling = function() {
if (samplingInterval) { clearInterval(samplingInterval); }
samplingInterval = null;
};
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
#waveforms #sampling
Cool mechanism this double buffering to take advantage of internal speed and 'external' coding simplicity!...
Lowering the sampling rate to give enough time to do the (filter/average) calculations would be the point. For example, a sampling rate of 128[samples] / 3[secs] = 43 [samples/sec] would deliver enough time - 3 seconds - to do the work. Depending how 'fast' temperature is changing though, only a segment of the 128 samples should then be taken... If this does not deliver enough samples, buffer size and sampling rate can to be increased proportionally.
Another solution - with high(er) sampling rate / samplings over a shorter period - for example, 0.5[secs], but allowing a longer period - for example, 5[secs] - can be achieved by option repeat:false. After processing the samples, issueing a .startInput() with a timeout will for sure prevent overruns. A third option is to put start with no-repeat into a function and invoke it on the desired interval. For example:
Every ... milliseconds, take 128 samples over a period of 0.5 seconds and average over 124 :
To start the sampling, issue:
To stop the sampling, issue: