cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f411 sample frequency

LTut.226
Associate II

hi, im experimenting with a guitar string frequency detector,

on Arduino uno I get the correct values in serial monitor,

but the values are approx halved when I use stm32f411,

anyone know why, could it be the sample frequency

 
#define LENGTH 512
 
byte rawData[LENGTH];
int count;
 
// Sample Frequency in kHz
const float sample_freq = 8919;
 
int len = sizeof(rawData);
int i,k;
long sum, sum_old;
int thresh = 0;
float freq_per = 0;
byte pd_state = 0;
 
void setup(){
  //analogReference(EXTERNAL);   // Connect to 3.3V
  analogRead(A0);
  Serial.begin(115200);
  count = 0;
}
 
 
void loop(){
  
  if (count < LENGTH) {
    count++;
    rawData[count] = analogRead(A0)>>2;
  }
  else {
    sum = 0;
    pd_state = 0;
    int period = 0;
    for(i=0; i < len; i++)
    {
      // Autocorrelation
      sum_old = sum;
      sum = 0;
      for(k=0; k < len-i; k++) sum += (rawData[k]-128)*(rawData[k+i]-128)/256;
      // Serial.println(sum);
      
      // Peak Detect State Machine
      if (pd_state == 2 && (sum-sum_old) <=0) 
      {
        period = i;
        pd_state = 3;
      }
      if (pd_state == 1 && (sum > thresh) && (sum-sum_old) > 0) pd_state = 2;
      if (!i) {
        thresh = sum * 0.5;
        pd_state = 1;
      }
    }
    // for(i=0; i < len; i++) Serial.println(rawData[i]);
    // Frequency identified in Hz
    if (thresh >100) {
      freq_per = sample_freq/period;
      Serial.println(freq_per);
    }
    count = 0;
  }
}

2 REPLIES 2
TDK
Guru

You mean the frequency is halved? So it gives you 50 Hz instead of 100Hz (for example)?

I'd check your clock settings. Toggle a GPIO pin at every sample and look on a scope to see if it matches your expectations.

You don't show how your calls to loop() are done. Maybe you're calling it half/twice as often as you think you are. Most accurate way to do this would be to trigger the ADC from a timer and transfer values via DMA.

> // Sample Frequency in kHz

> const float sample_freq = 8919;

This is in Hz, not kHz right? Doubt you can sample at 8.9 Msps.

If you feel a post has answered your question, please click "Accept as Solution".
LTut.226
Associate II

sorry I don’t know what a timer or dma or sample frequency is, and I don’t have a scope, also didn’t write the code, the output value in serial monitor is approx halved, so a low E string using uno is correct at 82.41 hz but it’s 40 hz if I use a stm32f411 blackpill board, any ideas?