Serial monitor

Hi
How to fix the error ?

//https://github.com/Harvie/Programs/blob/master/c/goertzel/goertzel.c

#include <stdio.h>
#include <math.h>
//#include <getopt.h>
////////////////////////////////
int audioInPin = A0;
int audioOutPin = 8;
int ledPin = 13;
///////////////////////////////////////////
int i, k, n, q0, q1, q2, data;

int magnitude;
int floatnumSamples;
int omega = (2.0 * M_PI * k) / floatnumSamples;
int sine = sin(omega);
int cosine = cos(omega);
int coeff = 2.0 * cosine;
int imag;
int real ;
int scalingFactor;
int numSamples;

char code[20];

/////////////////////////////
float goertzel_mag(int numSamples, float TARGET_FREQUENCY, int SAMPLING_RATE, float* data)
{
  int     k, i;
  float   floatnumSamples;
  float   omega, sine, cosine, coeff, q0, q1, q2, magnitude, real, imag;

  float   scalingFactor = numSamples / 2.0;

  floatnumSamples = (float) numSamples;
  k = (int) (0.5 + ((floatnumSamples * TARGET_FREQUENCY) / (float)SAMPLING_RATE));
  omega = (2.0 * M_PI * k) / floatnumSamples;
  sine = sin(omega);
  cosine = cos(omega);
  coeff = 2.0 * cosine;
  q0 = 0;
  q1 = 0;
  q2 = 0;

}
void setup() {
  Serial.begin(115200);
}
//////////////////////////////////////////////
void loop() {

  for (i = 0; i < n; i++)
  {
    data[i] = analogRead(audioInPin);
    float q0;
    q0 = coeff * q1 - q2 + (float) data[i];
    q2 = q1;
    q1 = q0;
  }

  // calculate the real and imaginary results
  // scaling appropriately
  real = (q1 * cosine - q2) / scalingFactor;
  imag = (q1 * sine) / scalingFactor;

  magnitude = sqrtf(real * real + imag * imag);
  //phase = atan(imag/real)
  return magnitude;
  Serial.print(magnitude);
  Serial.println();
}

error
C:\Users\Galinka\Documents\Arduino\g1\sketch_jan02d\sketch_jan02d.ino: In function 'void loop()':

sketch_jan02d:54:11: error: invalid types 'int[int]' for array subscript

data = analogRead(audioInPin);

  • ^*
    sketch_jan02d:56:42: error: invalid types 'int[int]' for array subscript
    _ q0 = coeff * q1 - q2 + (float) data*;_
    _
    ^*_
    exit status 1
    invalid types 'int[int]' for array subscript

By using the correct type as the compiler suggests. Now data is an int but you try to use it as an array.

int i, k, n, q0, q1, q2, data;
 data[i] = analogRead(audioInPin);

oops.

Thanks , no errors but on serial monitor is nothing, blank screen.

#include <stdio.h>
#include <math.h>
//#include <getopt.h>

const float TARGET_FREQUENCY = 1700;
int audioInPin = A0;
int k, i;
int goertzel_mag(int numSamples, float TARGET_FREQUENCY, int SAMPLING_RATE, float* data);
// float   omega,sine,cosine,coeff,q0,q1,q2,magnitude,real,imag;
int  q0, q1, q2, omega, sine, cosine, coeff, magnitude, real, imag;
float   floatnumSamples;
int SAMPLING_RATE = 8900;
int numSamples;
float   scalingFactor = numSamples / 2.0;
float* data;

//int q0 = 0;
//int q1 = 0;
//int q2 = 0;

void setup() {
  k = (int) (0.5 + ((floatnumSamples * TARGET_FREQUENCY) / (float)SAMPLING_RATE));
  omega = (2.0 * M_PI * k) / floatnumSamples;
  sine = sin(omega);
  cosine = cos(omega);
  coeff = 2.0 * cosine;
  Serial.begin(115200);
}

void loop() {

  for (i = 0; i < numSamples; i++)
  {
    //testData[index] = analogRead(audioInPin);
    data[i] = analogRead(audioInPin);
    q0 = coeff * q1 - q2 + data[i];
    q2 = q1;
    q1 = q0;
  }

  // calculate the real and imaginary results
  // scaling appropriately
  real = (q1 * cosine - q2) / scalingFactor;
  imag = (q1 * sine) / scalingFactor;

  magnitude = sqrtf(real * real + imag * imag);
  //phase = atan(imag/real)
  return magnitude;
  Serial.print(real);
  Serial.print("\t");
  Serial.print(imag);
  Serial.print("\t");
  Serial.println();
}

'data' still isn't an array... So now you're writing stuff in memory that's not allocated for the task.

Read up on array's :slight_smile:

The printing code is after this:

 return magnitude;

so it never happens.

How can you expect to print AFTER a return statement? In fact, I am surprised that this doesn't generate a warning or an error.

I am away from my computer right now or I would try this myself.

vaj4088:
I am surprised that this doesn't generate a warning or an error.

Indeed. It doesn't though. I'll guess that whatever the default compiler flags the IDE uses supressed the warnings.

I would have expected something about unreachable code and value returned from void function, but I actually got no errors at all.

That was my expectation also. Interesting. Thanks !

I removed - return magnitude;
on serial monitor I have

serial 2.png

serial 2.png

septillion:
'data' still isn't an array... So now you're writing stuff in memory that's not allocated for the task.

Read up on array's :slight_smile:

not sure what to do ???

I got zeroes rather than nan when I ran it without the return. Please post the adjusted code.

3x nan
nan nan nan

#include <stdio.h>
#include <math.h>
//#include <getopt.h>

const float TARGET_FREQUENCY = 1700;
int audioInPin = A0;
int k, i;
int goertzel_mag(int numSamples, float TARGET_FREQUENCY, int SAMPLING_RATE, float* data);
float   omega, sine, cosine, coeff, q0, q1, q2, magnitude, real, imag;
//int  q0, q1, q2, omega, sine, cosine, coeff, magnitude, real, imag;
float   floatnumSamples;
int SAMPLING_RATE = 8900;
int numSamples;
float   scalingFactor = numSamples / 2.0;
float* data;

//int q0 = 0;
//int q1 = 0;
//int q2 = 0;

void setup() {
  k = (int) (0.5 + ((floatnumSamples * TARGET_FREQUENCY) / (float)SAMPLING_RATE));
  omega = (2.0 * M_PI * k) / floatnumSamples;
  sine = sin(omega);
  cosine = cos(omega);
  coeff = 2.0 * cosine;
  Serial.begin(115200);
}

void loop() {

  for (i = 0; i < numSamples; i++)
  {
    //testData[index] = analogRead(audioInPin);
    data[i] = analogRead(audioInPin);
    q0 = coeff * q1 - q2 + data[i];
    q2 = q1;
    q1 = q0;
  }

  // calculate the real and imaginary results
  // scaling appropriately
  real = (q1 * cosine - q2) / scalingFactor;
  imag = (q1 * sine) / scalingFactor;

  magnitude = sqrtf(real * real + imag * imag);
  //phase = atan(imag/real)

  Serial.print(real);
  Serial.print("\t");
  Serial.print(imag);
  Serial.print("\t");
  Serial.println(magnitude);

}

Scalefactor is zero. Division by zero is giving you nan.

tom321:
not sure what to do ???

Don't try to copy advanced code with advanced math without knowing what you do :wink:

But yeah, at least you need 'data' to be an array. Of what type and how large is up to you. Try digging deeper in the code you try to copy :slight_smile:

without scalingFactor results are the same

/////////////////////
  real = (q1 * cosine - q2) ;
  imag = (q1 * sine);
///////////////////////

You continue to do this:

float* data;

And then write to data[] like you actually allocated memory for an array. What good to you think will come of writing to memory that doesn't belong to you?

Read This: array - Arduino Reference

Same issue. floatnumsamples is zero. You use it as a divisor so omega is nan and so are anything that use them in a calc.

floatnumsamples is zero. = I changed to 20 and have 3 columns of: 0.00 instead of nan.

#include <stdio.h>
#include <math.h>
//#include <getopt.h>

//const float TARGET_FREQUENCY = 1700;
int audioInPin = A0;
int k, i;
//int goertzel_mag(int numSamples, float TARGET_FREQUENCY, int SAMPLING_RATE, float* data);
float   omega, sine, cosine, coeff, q0, q1, q2, magnitude, real, imag;
//int  q0, q1, q2, omega, sine, cosine, coeff, magnitude, real, imag;
float  floatnumSamples =20;
int SAMPLING_RATE = 8900;
float TARGET_FREQUENCY = 0.0;
int numSamples = 0;
int data[96];
float   scalingFactor = numSamples / 2.0;


//int q0 = 0;
//int q1 = 0;
//int q2 = 0;

void setup() {
  TARGET_FREQUENCY = 1700;
  int k;
  k = (int) (0.5 + ((floatnumSamples * TARGET_FREQUENCY) / (float)SAMPLING_RATE));
  omega = (2.0 * M_PI * k) / floatnumSamples;
  sine = sin(omega);
  cosine = cos(omega);
  coeff = 2.0 * cosine;
  Serial.begin(115200);
}

void loop() {

  for (i = 0; i < numSamples; i++)
  {
    //testData[index] = analogRead(audioInPin);
    data[i] = analogRead(audioInPin);
    q0 = coeff * q1 - q2 + data[i];
    q2 = q1;
    q1 = q0;
  }

  // calculate the real and imaginary results
  // scaling appropriately
  // real = (q1 * cosine - q2) / scalingFactor;
  //imag = (q1 * sine) / scalingFactor;
  /////////////////////
  real = (q1 * cosine - q2) ;
  imag = (q1 * sine);
  ///////////////////////
  magnitude = sqrtf(real * real + imag * imag);
  //phase = atan(imag/real)

  Serial.print(real);
  Serial.print("\t");
  Serial.print(imag);
  Serial.print("\t");
  Serial.println(magnitude);

}

q0, q1, and q2 all start out with a value of zero. What value do you think they have after this 'for' loop executes?

for (i = 0; i < numSamples; i++)
  {
    //testData[index] = analogRead(audioInPin);
    data[i] = analogRead(audioInPin);
    q0 = coeff * q1 - q2 + data[i];
    q2 = q1;
    q1 = q0;
  }

What are the resultant values for real, imag, and magnitude?