Problems with for loop

Hi
I am facing problems with for loop in my code . The loop is supposed to run some 7400 times but it suddenly breaks after 5807 times. My first guess is that there is some where a overflow. Please find output in attachments.

#include <stdio.h>
#include <math.h>
#include <string.h>


#define SAMPLE_LENGTH 7500

float stddev(float data[], int len);
float mean(float data[], int len);
//void thresholding(float y[], int signals[], int lag, float threshold, float influence);
//void peak_detection();
 int signals[SAMPLE_LENGTH];

float y[SAMPLE_LENGTH]= {0};
  
void setup() 
{
    Serial.begin(115200);

          
}


  

void loop()
{
  
     int lag = 50;
    float threshold = 5;
    float influence = 0;
    float filteredY[SAMPLE_LENGTH];  
    float avgFilter[SAMPLE_LENGTH];
    float stdFilter[SAMPLE_LENGTH];

    memset(signals, 0, sizeof(float) * SAMPLE_LENGTH);  // set signals string to zero
    memcpy(filteredY, y, sizeof(float) * SAMPLE_LENGTH); // copying input array to filteredY array
   
    avgFilter[lag - 1] = mean(y, lag); // calling mean function 
    stdFilter[lag - 1] = stddev(y, lag); // calling standard deviation function
  
    for (int i = lag; i < SAMPLE_LENGTH; i++) // loop for detecting peaks in a signal
    {
      
      Serial.println("TRACE: 2nd loop");
      Serial.println(i);
       if (fabsf(y[i] - avgFilter[i-1]) > threshold * stdFilter[i-1]) 
       {
          if (y[i] > avgFilter[i-1]) 
          {

                signals[i] = 1;
          } 
          else 
          {
            signals[i] = -1;
          }
          filteredY[i] = influence * y[i] + (1 - influence) * filteredY[i-1];
      } 
      else
      {
         signals[i] = 0;
      }
     // Serial.println(signals[i]);
      avgFilter[i] = mean(filteredY + i-lag, lag);
      //Serial.println("TRACE: stdFilter");
      //Serial.println(filteredY[i]);
      stdFilter[i] = stddev(filteredY + i-lag, lag);  // Error : This loop should 7400 times but actually it runs  only 5807 times 
     // Serial.println(stdFilter[i]);
      //Serial.println("TRACE: Ready");

    }

   while(1);
          
}

     

 




float mean(float data[], int len) {
    float sum = 0.0, mean = 0.0;

    int i;
    for(i=0; i<len; ++i) {
        sum += data[i];
    }

    mean = sum/len;
    return mean;


}

float stddev(float data[], int len)
{
    float the_mean = mean(data, len);
    float standardDeviation = 0.0;
    float temp;
    int i;
    for(i=0; i<len; ++i)
    {
        standardDeviation += pow(data[i] - the_mean, 2);
    }
  //  Serial.println(standardDeviation);
    //Serial.println(len);

    temp= standardDeviation/len;
   // Serial.println(temp);
  //  Serial.println(sqrt(temp));
    
    return sqrt(temp);
    //return sqrt(standardDeviation/len);
}

Which processor, chip, or board are you using are you using? The 45k in the two arrays in RAM might be too much.

I am using arduino due board . I tried to run the C code on STM microcontroller it working there but i don't know what's wrong with arduino due board.

Could it have something to do with the fact that you are creating 135 Kbytes of arrays on a processor with only 96 Kbytes of RAM? And you're putting 90 Kbytes of arrays on the stack? And the Due RAM is "banked", with 64 Kbytes in one bank, and 32 Kbytes in the other? And you're using memset to "clear" the arrays, and memcpy to copy/move the arrays, which is likely wiping out most/all of your RAM?

Other than that, I see no problems at all...

Regards,
Ray L.

    float filteredY[SAMPLE_LENGTH]; 
    float avgFilter[SAMPLE_LENGTH];
    float stdFilter[SAMPLE_LENGTH];

On the stack?

Your going to have to offload this to.. Possibly files on an SD card?

-jim lee

Or, use the Wemos D1 mini which has 4mb of RAM.