Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add full Arduino or STM32 mcu FFT frequency detection algorithm example #15

Open
ElectricRCAircraftGuy opened this issue Apr 16, 2023 · 0 comments

Comments

@ElectricRCAircraftGuy
Copy link
Owner

ElectricRCAircraftGuy commented Apr 16, 2023

Use the ADC in continuous sampling mode at a fixed frequency > 2x as fast as the fastest frequency I'd like to detect, per the Nyquist Frequency Theorem. We want continuous samples with zero missed time intervals. Use "ADC ready" interrupts to run an ISR each time a sample is ready.

In the ISR, store the latest sample into a ring buffer 2x as long as the number of samples we need for each FFT calculation. Once a full FFT sample count is stored, set a volatile, atomic-access-guard-protected (bare-metal-mutex-protected) flag to indicate "time to run FFT analysis."

In the main loop, read that flag and run the FFT once per data set, ensuring it runs fast enough that the full calculation can complete before that data set starts to get overwritten again in the ISR. In other words, the full calculation per data set should complete in the time it takes to store a single data set.

Once FFT analysis is complete and an array of peaks is stored, send out the FFT results data over serial for plotting and logging to a PC. Also, reset the flag that was set in the ISR.

Also, output a beep at a fixed tone through a speaker to indicate which frequency is detected. Ex: for a 1 KHz frequency detected, play a 1 KHz tone for a 1/4 sec beep. For a 1.5KHz frequency detected, play that freq for 1/4 sec beep. Dual drive the speaker by using an H-bridge and 2 pins at 50% PWM duty cycle, offset by 180 deg to dual drive the speaker in 2 directions, like Arduino's toneac() library does.

Here's the gist of the H-bridge wiring. The motor in the diagram would be a speaker in our case: https://www.google.com/amp/s/hackersgrid.com/2017/03/the-h-bridge.html

image

@ElectricRCAircraftGuy ElectricRCAircraftGuy changed the title Add full Arduino FFT frequency detection algorithm example Add full Arduino or STM32 mcu FFT frequency detection algorithm example Apr 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant