Skip to content

Instantly share code, notes, and snippets.

@mattgaidica
Last active March 24, 2024 02:28
Show Gist options
  • Save mattgaidica/f5222d6167005da9277dff06ffc55421 to your computer and use it in GitHub Desktop.
Save mattgaidica/f5222d6167005da9277dff06ffc55421 to your computer and use it in GitHub Desktop.
// ... application includes here
// CMSIS Math includes
#include "arm_math.h"
#include "arm_const_structs.h"
// using a 1024 point signal
#define FFT_SAMPLES 1024
#define FFT_SAMPLES_HALF (FFT_SAMPLES / 2)
// see gist
#include "FFTsignal.h"
static float32_t complexFFT[FFT_SAMPLES], realFFT[FFT_SAMPLES_HALF],
imagFFT[FFT_SAMPLES_HALF], angleFFT[FFT_SAMPLES_HALF],
powerFFT[FFT_SAMPLES_HALF];
uint32_t fftSize = FFT_SAMPLES;
uint32_t ifftFlag = 0;
arm_rfft_fast_instance_f32 S;
uint32_t maxIndex = 0;
arm_status status;
float32_t maxValue;
int i;
void main() {
status = ARM_MATH_SUCCESS;
status = arm_rfft_fast_init_f32(&S, fftSize);
// input is real, output is interleaved real and complex
arm_rfft_fast_f32(&S, inputSignal, complexFFT, ifftFlag);
// first entry is all real DC offset
float32_t DCoffset = complexFFT[0];
// de-interleave real and complex values
for (i = 0; i < (FFT_SAMPLES / 2) - 1; i++) {
realFFT[i] = complexFFT[i * 2];
imagFFT[i] = complexFFT[(i * 2) + 1];
}
// find angle of FFT
for (i = 0; i < FFT_SAMPLES / 2; i++) {
angleFFT[i] = atan2f(imagFFT[i], realFFT[i]);
}
// compute power
arm_cmplx_mag_squared_f32(complexFFT, powerFFT, FFT_SAMPLES_HALF);
arm_max_f32(&powerFFT[1], FFT_SAMPLES_HALF - 1, &maxValue, &maxIndex);
// correct index
maxIndex += 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment