Goertzel for stm32f103 LCD

What are you developing?
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Goertzel for stm32f103 LCD

Post by ozcar »

stan wrote: Fri Dec 18, 2020 4:42 pm it is compiling but not working I added LCD, in uno LCD it is alive in stm32 it is dead just displays 0.00 and do on respond to signal on PA6.
How are you generating the signal on PA6, and are you sure there is really something to see there?

This is the sampling routine in Goertzel.cpp:

Code: Select all

/* Sample some test data. */
void Goertzel::sample(int sensorPin)
{
  for (int index = 0; index < _N; index++)
  {
    testData[index] = analogRead(sensorPin);
  }
}
You could insert some debugging code there to see what is being returned by the analogRead(), or put an analogRead() in your loop() to see if the value ever changes.

If you can see the value changing, perhaps the sampling rate you have specified (8900) is just too different to what you are actually getting. This is the issue as mentioned already by Feluga and Fredfox. The sampling code simply spins around as fast as it can, and the rate is likely to be very different to what you get on a 16MHz Uno.

You could insert some code into Goertzel::sample() to see how long it takes, and from that work out the rate. It is going to also depend on the compiler and optimisation. Hopefully the actual rate you get is at least 10kHz to detect a 5kHz signal.

There is probably a better way to sample at a known rate, but may require drastic surgery to the code.
stan
Posts: 70
Joined: Wed Nov 11, 2020 7:40 pm

Re: Goertzel for stm32f103 LCD

Post by stan »

Here is fft bin 12 = 3.6kHz, and SAMPLING FREQUENCY 40000, so I put 40000 to Goertzel but nothing changes. In fft LCD is responding to PA6, numbers are jumping without signal from noise in Goertzel on LCD is 28.3 steady.

Code: Select all

#include "arduinoFFT.h"
 #include <LiquidCrystal.h>
LiquidCrystal lcd(PB15, PB5, PA2, PB6, PB8, PB9);

#define SAMPLES1 128
#define SAMPLING_FREQUENCY 40000
 
arduinoFFT FFT = arduinoFFT();
 
unsigned int sampling_period_us;
unsigned long microseconds;
 
double vReal[SAMPLES1];
double vImag[SAMPLES1];
 
void setup() {
  Serial.begin(115200);
  lcd.begin( 16, 2 );
 
  sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
}
 
void loop() {
 
  /*SAMPLING*/
  for (int i = 0; i < SAMPLES1; i++)
  {
    microseconds = micros();    //Overflows after around 70 minutes!
 
    vReal[i] = analogRead(PA6);
    vImag[i] = 0;
 
    while (micros() < (microseconds + sampling_period_us)) {
    }
  }
 
  /*FFT*/
  FFT.Windowing(vReal, SAMPLES1, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
  FFT.Compute(vReal, vImag, SAMPLES1, FFT_FORWARD);
  FFT.ComplexToMagnitude(vReal, vImag, SAMPLES1);
  double peak = FFT.MajorPeak(vReal, SAMPLES1, SAMPLING_FREQUENCY);
 
  /*PRINT RESULTS*/
 
  //Serial.print(" ");
  Serial.println(vReal[40], 1);
    lcd.setCursor(0, 0);
  lcd.print(vReal[12], 1);// 10 - 3kHz, 12 = 3.63kHz
  
}
stan
Posts: 70
Joined: Wed Nov 11, 2020 7:40 pm

Re: Goertzel for stm32f103 LCD

Post by stan »

ozcar wrote: Fri Dec 18, 2020 6:44 pm
stan wrote: Fri Dec 18, 2020 4:42 pm it is compiling but not working I added LCD, in uno LCD it is alive in stm32 it is dead just displays 0.00 and do on respond to signal on PA6.
How are you generating the signal on PA6, and are you sure there is really something to see there?

I have signal generator and oscilloscope.
stan
Posts: 70
Joined: Wed Nov 11, 2020 7:40 pm

Re: Goertzel for stm32f103 LCD

Post by stan »

I added sensorValue and numbers are changing when generator is turned on /off

Code: Select all

// Set things up
//http://play.fallows.ca/wp/radio/ham-radio/signal-analysis-morse-decoder/
#include <Goertzel.h>
int sensorPin = PA6;
//int sensorPin =A0;
int led = PC13;
//int led = 13;
//const float TARGET_FREQUENCY = 500;
const float TARGET_FREQUENCY = 5000;
const int N = 100;
const float THRESHOLD = 4000;
const float SAMPLING_FREQUENCY = 40000;
// Create the filter
//Goertzel goertzel = Goertzel(TARGET_FREQUENCY, N, SAMPLING_FREQUENCY);
Goertzel goertzel = Goertzel(TARGET_FREQUENCY, N, SAMPLING_FREQUENCY);

#include <LiquidCrystal.h>
LiquidCrystal lcd(PB15, PB5, PA2, PB6, PB8, PB9);
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sensorValue = 0;
void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(115200);
    lcd.begin( 16, 2 );
}

void loop()
{
  ///////////
  sensorValue = analogRead(sensorPin);
  ////////////
  goertzel.sample(sensorPin);
  // Check samples for tone at target
  float magnitude = goertzel.detect();
  // if the tone is present, light LED
  if (magnitude > THRESHOLD)
    digitalWrite(led, HIGH);
  else
    digitalWrite(led, LOW);
  // Serial.println(magnitude);
  Serial.println(magnitude);

  lcd.setCursor(0, 0);
  lcd.print(magnitude);
    lcd.setCursor(0, 1);
  lcd.print( sensorValue );
 
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: Goertzel for stm32f103 LCD

Post by fredbox »

You might find some useful info here: https://github.com/jmharvey1/STM32_CWDecoder. The tone detection has been solved using a timer interrupt.
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Goertzel for stm32f103 LCD

Post by ozcar »

stan wrote: Fri Dec 18, 2020 7:18 pm Here is fft bin 12 = 3.6kHz, and SAMPLING FREQUENCY 40000, so I put 40000 to Goertzel but nothing changes.
The fft sampling at least tries to do it at a known rate. Why would you expect the Goertzel rate to be the same?
stan
Posts: 70
Joined: Wed Nov 11, 2020 7:40 pm

Re: Goertzel for stm32f103 LCD

Post by stan »

fredbox wrote: Fri Dec 18, 2020 7:58 pm You might find some useful info here: https://github.com/jmharvey1/STM32_CWDecoder. The tone detection has been solved using a timer interrupt.
Thanks for link. now I have to make it shorter , it has 1760 lines, I think just tone detect part will be about 100 lines.
stan
Posts: 70
Joined: Wed Nov 11, 2020 7:40 pm

Re: Goertzel for stm32f103 LCD

Post by stan »

I am trying different frequencies, i think this is not issue, with 40000 the highest input frequency = 2000 Hz, 1 bin is 312Hz wide..... So sampling frequency is changing the numbers but this doesn't make the Goertzel work or not.
Post Reply

Return to “Projects”