Hello!
This is my first post here so apologies in advance if I am in the wrong place etc.
I have written a moving average algorithm that I would like to implement in JSFX.
I had it working in Google scripts (my coding abilities are pretty limited so I needed a more familiar platform to test and debug).
I tried to translate or migrate it to EEL2 using what I could glean from the online guides but unsurprisingly, it does not work.
I will post below both sets of code and I would really appreciate if someone could point me in the right direction!
Many thanks in advance,
GLD
GAS Code (working):
Code:
function WorkingAve(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); //get sheet
var values = ss.getDataRange().getValues(); //get data range
var buffcount = 0; // should stay at samplesize when buffer is full
var poscount = 0; // iterates through to samplesize and then resets
var samplesize = 3; // sample size will be drop down menu
var n = (samplesize-1)/2;//how much is either side of the middle
var buff = 0;// buffer
var pos = new Array(samplesize);// position array
var out = 0; // output
var prev = 0;// previous is the position of a sample to be subtracted from the buffer
var Output = new Array(0); // output array
var zeros = samplesize ;// counts consecutive zeros
var sample = 0 ; // counts consecutive non-zero samples
var logz = 0; //logger builder
var minusn = 0;// gets the sample 'N' turns ago position - N
for (i in values){//start loop through incoming values
if (values[i] == 0){//if incoming value is a zero
//incoming value is zero
// increment zeros counter up to samplesize
zeros = Math.min(((1*zeros) + 1),samplesize) ;
// nested if to test if the buffer is empty
// if the buffer is empty, reset the sample counter
if ((zeros*1) == (samplesize)){
//buffer is empty, reset sample counter
sample = 0;
//buffer is empty, reset pos counter
poscount = 0;
}else{
//if incoming value is zero but buffer is not yet empty
// increment sample counter up to sample size
sample = Math.min(((1*sample) +1),samplesize)
if (poscount*1 == samplesize*1){
// pos counter at maximum
// reset pos counter to first position
poscount = 1;
} else {
//pos counter not at maximum
//increment pos counter
poscount = +poscount+1;
} // end pos counter increment
} // end if buffer is empty
}else{
//incoming value is NOT zero
//reset zeros counter
zeros = 0 ;
// increment sample counter up to sample size
sample = Math.min(((1*sample) +1),samplesize)
//increment position counter for pos array
// test if pos counter needs to be reset
if (poscount*1 == samplesize*1){
// pos counter at maximum
// reset pos counter to first position
poscount = 1;
} else {
//pos counter not at maximum
//increment pos counter
poscount = +poscount+1;
} // end pos counter increment
}//end if incoming value is zero
// Previous counter for finding previous values to take away from the buffer
// if sample = samplesize then run the counter
if (sample*1 == samplesize*1){
//buffer is full
// if position counter is at the end then previous to take is the first sample in the position array
if (poscount*1 < samplesize*1){
// position counter is less than sample size and not zero
// set previous counter to the next position in the buffer
prev = (poscount*1)+1;
}else{
// position counter is not less than samplesize (and buffer is full)
// set prev to first position
prev = 1;
}//end if previous counter vs position
} else{
//Buffer is not full
// set previous to zero (no subtraction needed)
prev = 0;
}//end if for previous counter buffer test
// minus n counter for finding the minus N sample for beginning of calculations
// if sample is less than N+1
if (sample*1 < (n*1)+1){
//sample is less than N+1 middle value
//reset counter
minusn = 0;
}else if (sample*1 < samplesize*1){
// sample is less than sample size but bigger equal to or bigger than the middle value
minusn = sample*1 - n*1;
}else{
minusn = 0;
}//End if for minus n counter
//1. add current value to buffer or reset it to zero if sample is zero
if (sample == 0){
// sample counter is zero
//buffer and position array should be empty
//empty buffer
buff = 0 ;
//empty position array
pos = new Array(samplesize);// position array reset
}else {
// sample is NOT zero
//4. if pos not zero then add to pos buffer ready for next loop
// add incoming value to buffer
buff = (buff*1) +(values[i]*1);
// add incoming value to position array
pos[1*poscount] = + values[i]*1;
}// 1. end buffer add if sample not zero
//3. depending on sample number, find the moving average
// if sample number < N+1 (middle) output zero
//if sample number < SS output pos - n
// sample number = SS output moving average
if (1*sample < (1*n)+1){
// sample number is less than middle of sample size N+1
// output zero
out = 0;
}else if (1*sample < (1*samplesize)){
// sample number < SS
//output pos - n
out = pos[minusn];
}else { // sample size is sample size
out = (1*buff)/(samplesize*1);
// sample number = sample size
// output moving average
}//end if sample number < N+1
//2. if previous not zero then subtract from buffer
if (prev > 0){
//previous counter is greater than zero
//subtract previous position array value from buffer
buff = + (1*buff) - (1*pos[prev*1]);
}//end if previous not zero
ss.getRange("firstcell").offset(i,0).setValue(1*out);
//LOGGER
// clear log
logz = "";
// add incoming to log
//logz = "Incoming = "+parseInt(1*values[i]);
// add sample counter to log
logz = logz + " Sample counter = "+sample;
// add zeros counter to log
//logz = logz + " Zeros Counter = "+zeros;
// add position counter to log
logz = logz + " Pos Counter = "+poscount;
// add previous counter to log
logz = logz + " Prev Counter = "+prev;
// add minus n counter to log
logz = logz + " Minus N Counter = "+minusn;
// add buffer value to log
logz = logz + " Buff = "+buff;
// add output value to log
logz = logz + " output = "+out;
// print to log
Logger.log(logz) ;
}//end values loop
}// end function Working Average ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????