#define ARM_MATH_CM4
#include "arm_math.h"
#define TEST_LENGTH_SAMPLES 2048
#include "arm_fft_sine_data.h"
// NOTE: q15t is int16_t in arm_math.h
uint32_t fftSize = 512;
/* ------------------------------------------------------------------
* Global variables for FFT Bin Example
* ------------------------------------------------------------------- */
uint32_t ifftFlag = 0;
uint32_t doBitReverse = 1;
uint32_t testInputIndex = 0;
float32_t testInput[TEST_LENGTH_SAMPLES];
static float32_t testOutput[TEST_LENGTH_SAMPLES/2];
void setup() {
Serial.begin(19200);
pinMode(13, OUTPUT);
for (int i=0; i < 10; i++) {
Serial.println(" start program ");
delay(1000);
}
}
bool pit3Triggered = false;
extern "C" {
//! Audio input interrupt handler running at 15kHz
void pit3_isr(void)
{
pit3Triggered = true;
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
PIT_TFLG3 = 1;
}
void startup_late_hook(void) {
// This is called from mk20dx128.c
//Turn on interrupts:
SIM_SCGC6 |= SIM_SCGC6_PIT;
// turn on PIT
PIT_MCR = 0x00;
NVIC_ENABLE_IRQ(IRQ_PIT_CH3);
PIT_LDVAL3 = 3200 - 1; // setup timer 2 for frame timer period (15kHz) = 48MHz / 15kHz
PIT_TCTRL3 = 0x2; // enable Timer 3 interrupts
PIT_TCTRL3 |= 0x1; // start Timer 3
PIT_TFLG3 |= 1;
}
}
void loop() {
float32_t maxValue;
float32_t length = 256.0;
if (pit3Triggered) {
int sample;
sample = analogRead (14);
testInput[testInputIndex] = sample / 1024 * 10.0;
testInputIndex++;
if (testInputIndex >= TEST_LENGTH_SAMPLES) {
testInputIndex = 0;
}
}
if (testInputIndex == 0) {
arm_cfft_radix4_instance_f32 fft_inst; /* CFFT Structure instance */
arm_cfft_radix4_init_f32(&fft_inst, length, ifftFlag, doBitReverse);
uint32_t startTime, fftTime, magTime, maxTime;
Serial.println("Start");
startTime = millis();
/* Process the data through the CFFT/CIFFT module */
arm_cfft_radix4_f32(&fft_inst, testInput_f32_10khz);
fftTime = millis();
/* Process the data through the Complex Magnitude Module for
calculating the magnitude at each bin */
arm_cmplx_mag_f32(testInput_f32_10khz, testOutput, fftSize);
magTime = millis();
/* Calculates maxValue and returns corresponding BIN value */
arm_max_f32(testOutput, fftSize, &maxValue, &testIndex);
maxTime = millis();
Serial.println("End");
Serial.println(fftTime - startTime);
Serial.println(magTime - fftTime);
Serial.println(maxTime - magTime);
Serial.println("TOTAL: ");
Serial.println(maxTime - startTime);
Serial.print("MaxValue: ");
Serial.println(maxValue);
Serial.print("MaxIndex: ");
Serial.println(testIndex);
Serial.print("Magnitudes: ");
for (int j=0; j < length / 2; j++) {
Serial.print(j);
Serial.print(", ");
Serial.println(testOutput[j]);
}
}
}