PROGMEM problem with my project - dynamic allocation error

My project is and RTISR (Real Time Instant Signal Recognizer). I’m sampling a signal from PIN A0 in the Arduino, and I have 6 types of Signals which I compare with (using squared cross correlation to avoid using sqrt() function).

Any way, to avoid killing my SRAM I wanted to burn the data of the 6 known signals in the Flash memory. I wanted to put there the signal, its Mean value, and its variance, which I calculate in advanced (with a function).

there is the code:

#include <avr/pgmspace.h>
#include <math.h>

#define T1_PS_L 5 //Timer1_Prescalers_Length
#define MAIN_CLK_FREQ 16000000
#define TIMER1_REG_CAPACITY 65536

#define NUM_OF_SAMPLES 64
#define SAMPLING_FREQUENCY 100

#define NUM_OF_ETALONS 6

double Variance(double arr[], int len);
double Mean(double arr[], int len);

/*-------------- ETALON SIGNALS ---------------------*/
const double sinWave1[NUM_OF_SAMPLES] PROGMEM = {0.0, 0.0980171403295606, 0.19509032201612825, 0.29028467725446233,
                           0.3826834323650898, 0.47139673682599764, 0.5555702330196022, 0.6343932841636455,
                           0.7071067811865475, 0.773010453362737, 0.8314696123025452, 0.8819212643483549,
                           0.9238795325112867, 0.9569403357322089, 0.9807852804032304, 0.9951847266721968, 1.0,
                           0.9951847266721969, 0.9807852804032304, 0.9569403357322089, 0.9238795325112867,
                           0.881921264348355, 0.8314696123025455, 0.7730104533627371, 0.7071067811865476,
                          };
const double sinWave2[NUM_OF_SAMPLES] PROGMEM = {0.0, 0.19509032201612825, 0.3826834323650898, 0.5555702330196022,
                           0.7071067811865475, 0.8314696123025452, 0.9238795325112867, 0.9807852804032304,
                           1.0, 0.9807852804032304, 0.9238795325112867, 0.8314696123025455,
                           0.7071067811865476, 0.5555702330196022, 0.3826834323650899, 0.1950903220161286,
                           };
const double sinWave3[NUM_OF_SAMPLES] PROGMEM = {0.0, 0.29028467725446233, 0.5555702330196022, 0.773010453362737,
                           0.9238795325112867, 0.9951847266721968, 0.9807852804032304, 0.881921264348355,
                           };
const double rectWave4[NUM_OF_SAMPLES] PROGMEM = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
const double rectWave5[NUM_OF_SAMPLES] PROGMEM = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
const double triWave6[NUM_OF_SAMPLES] PROGMEM = {0.0, 0.0625, 0.125, 0.1875, 0.25, 0.3125, 0.375, 0.4375, 0.5,
                                  0.5625, 0.625, 0.6875, 0.75, 0.8125, 0.875, 0.9375, 1.0, 0.9375,
                                  0.875, 0.8125, 0.75, 0.6875, 0.625, 0.5625, 0.5, 0.4375, 0.375,
                                  0.3125, 0.25, 0.1875, 0.125, 0.0625, 0.0, -0.0625, -0.125, -0.1875,};

const double sinWaveMean1 PROGMEM = (const double)Mean((double*)sinWave1, NUM_OF_SAMPLES);
const double sinWaveMean2 PROGMEM = (const double)Mean((double*)sinWave2, NUM_OF_SAMPLES);
const double sinWaveMean3 PROGMEM = (const double)Mean((double*)sinWave3, NUM_OF_SAMPLES);
const double rectWaveMean4 PROGMEM = (const double)Mean((double*)rectWave4, NUM_OF_SAMPLES);
const double rectWaveMean5 PROGMEM = (const double)Mean((double*)rectWave5, NUM_OF_SAMPLES);
const double triWaveMean6 PROGMEM = (const double)Mean((double*)triWave6, NUM_OF_SAMPLES);

const double sinWaveVariance1 PROGMEM = Variance((double*)sinWave1, NUM_OF_SAMPLES);
const double sinWaveVariance2 PROGMEM = Variance((double*)sinWave2, NUM_OF_SAMPLES);
const double sinWaveVariance3 PROGMEM = Variance((double*)sinWave3, NUM_OF_SAMPLES);
const double rectWaveVariance4 PROGMEM = Variance((double*)rectWave4, NUM_OF_SAMPLES);
const double rectWaveVariance5 PROGMEM = Variance((double*)rectWave5, NUM_OF_SAMPLES);
const double triWaveVariance6 PROGMEM = Variance((double*)triWave6, NUM_OF_SAMPLES);

const double* const Etalon_Signals[NUM_OF_ETALONS] PROGMEM = {sinWave1, sinWave2, sinWave3, rectWave4, rectWave5, triWave6};
const double Etalon_Signals_Mean[NUM_OF_ETALONS] PROGMEM = {sinWaveMean1, sinWaveMean2, sinWaveMean3, rectWaveMean4, rectWaveMean5, triWaveMean6};
const double Etalon_Signals_Variance[NUM_OF_ETALONS] PROGMEM = {sinWaveVariance1, sinWaveVariance2, sinWaveVariance3, rectWaveVariance4, rectWaveVariance5, triWaveVariance6};
/*-------------- SAMPLED SIGNAL ---------------------*/
volatile double signal_samples[NUM_OF_SAMPLES];
volatile int idx = 0;
/*-------------- CORRELATION VARS -------------------*/
volatile int counter = 0;
volatile double correlation = 0, MostSimillarCorrVal = 0, LocalMaxCorr = 0, maxCorrWave = 0;
volatile double SignalSamplesVar = 0, yMean = 0;
double EtalonSignalVal, EtalonMeanVal, EtalonVarianceVal, SignalSamplesVarTemp;
.
.
.

I’m getting the Error: “variable ‘Etalon_Signals_Mean’ with dynamic initialization put into program memory area” and it marks the line:

const double Etalon_Signals_Mean[NUM_OF_ETALONS] PROGMEM = {sinWaveMean1, sinWaveMean2, sinWaveMean3, rectWaveMean4, rectWaveMean5, triWaveMean6};

which are somewhere at the beggining (line 116). I dont understand why. My guess is that the compiler sees the “Mean” values which are a result of a function, and functions are “dynamic”. But those values are getting into a const variable, so I don’t get it. Please help.

When you put a constant into PROGMEM, its value has to be known at compile time, so you cannot use a function to calculate the value.

Why have you set NUM_OF_SAMPLES to 64 when none of your arrays are anywhere near that large? That is a huge waste of memory.