Signal Averaging P5JS

So I was writing the signal averaging code on a random generated data.

Steps I did -

  1. Generating a signal to simulated Real time data
  2. Signal = random data (-10,10) + periodic data (50,100 ) after every 100 tx
  3. Taking sample of 100 points and averaging with live data

What I think I am doing incorrectly is -

  1. Averaging sample incorrectly

Can you please look into the code?

Here is the live version - https://editor.p5js.org/black/sketches/CKhVPepqn

let signal = [];
let prev = [];
let curr = [];
let sum = [];
let counter = 0;
let tx = 0;
let sampleSize = 200;

function setup() {
  createCanvas(400, 400);
  for(let i=0;i<sampleSize;i++){
    prev.push(0);
  }
}

function draw() {
  background(220);

  translate(0,height);
  scale(1,-1);
  
  // signal
  if (signal.length < 400) signal.push(random(-10, 10)+(tx<100?random(50,100):0));
  else signal.shift();
  
  if(tx<100)tx++;
  else tx =0;

  // signal plot
  stroke(0);
  noFill();
  beginShape();
  for (let i = 0; i < signal.length; i++) {
    vertex(i, signal[i]);
  }
  endShape();
  
  prev[counter]=(prev[counter]+signal[counter])/2;
   
  if(counter<sampleSize)counter++;
  else counter=0;
  
  stroke(255,0,0);
  beginShape();
  for(let i=0;i<prev.length;i++){
      vertex(i, prev[i]+200);
  } 
  endShape();
}

Were you able to resolve this?

As there are many ways to do averaging, and many potential results, what I’m not understanding is what you mean by “incorrectly”: What is your code currently doing, and what do you want it to be doing instead? How do you know it is wrong, and how will you know when it is working?