initial commit

This commit is contained in:
2026-09-25 10:48:51 +10:00
commit b90beea8ae
1020 changed files with 131665 additions and 0 deletions
@@ -0,0 +1,90 @@
/* This example shows how to use the FFT library with a circuit playground express.
*
* The LEDs will map around the circle when frequencies between FREQ_MIN and FREQ_MAX are detected
*/
#include <Adafruit_CircuitPlayground.h>
#include "Adafruit_ZeroFFT.h"
//this must be a power of 2
#define DATA_SIZE 256
#define NUM_PIXELS 12
//the sample rate
#define FS 22000
//the lowest frequency that will register on the meter
#define FREQ_MIN 600
//the highest frequency that will register on the meter
#define FREQ_MAX 3000
#define MIN_INDEX FFT_INDEX(FREQ_MIN, FS, DATA_SIZE)
#define MAX_INDEX FFT_INDEX(FREQ_MAX, FS, DATA_SIZE)
#define SCALE_FACTOR 32
int16_t pixelData[NUM_PIXELS];
int16_t inputData[DATA_SIZE];
// the setup routine runs once when you press reset:
void setup() {
CircuitPlayground.begin();
}
void loop() {
CircuitPlayground.mic.capture(inputData, DATA_SIZE);
/*******************************
* REMOVE DC OFFSET
******************************/
int32_t avg = 0;
int16_t *ptr = inputData;
for(int i=0; i<DATA_SIZE; i++) avg += *ptr++;
avg = avg/DATA_SIZE;
ptr = inputData;
for(int i=0; i<DATA_SIZE; i++){
*ptr -= avg;
*ptr++ = *ptr*SCALE_FACTOR;
}
//run the FFT
ZeroFFT(inputData, DATA_SIZE);
//set all to 0
memset(pixelData, 0, NUM_PIXELS*sizeof(int16_t));
//downsample into NUM_PIXELS bins
for(int i=MIN_INDEX; i<MAX_INDEX; i++){
int ix = map(i, MIN_INDEX, MAX_INDEX, 0, NUM_PIXELS);
pixelData[ix] += inputData[i];
}
//display the data
for(int i=0; i<NUM_PIXELS; i++)
CircuitPlayground.strip.setPixelColor(i, Wheel(pixelData[i]));
CircuitPlayground.strip.show();
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
// values below 20 will not register
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 20) {
return CircuitPlayground.strip.Color(0, 0, 0);
}
else if(WheelPos < 85) {
return CircuitPlayground.strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if(WheelPos < 170) {
WheelPos -= 85;
return CircuitPlayground.strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return CircuitPlayground.strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
@@ -0,0 +1,43 @@
/* This example shows the most basic usage of the Adafruit ZeroFFT library.
* it calculates the FFT and prints out the results along with their corresponding frequency
*
* The signal.h file constains a 200hz sine wave mixed with a weaker 800hz sine wave.
* The signal was generated at a sample rate of 8000hz.
*
* Note that you can print only the value (coment out the other two print statements) and use
* the serial plotter tool to see a graph.
*/
#include "Adafruit_ZeroFFT.h"
#include "signal.h"
//the signal in signal.h has 2048 samples. Set this to a value between 16 and 2048 inclusive.
//this must be a power of 2
#define DATA_SIZE 256
//the sample rate
#define FS 8000
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(115200);
while(!Serial); //wait for serial to be ready
//run the FFT
ZeroFFT(signal, DATA_SIZE);
//data is only meaningful up to sample rate/2, discard the other half
for(int i=0; i<DATA_SIZE/2; i++){
//print the frequency
Serial.print(FFT_BIN(i, FS, DATA_SIZE));
Serial.print(" Hz: ");
//print the corresponding FFT output
Serial.println(signal[i]);
}
}
void loop() {
//don't even do anything
}
@@ -0,0 +1,214 @@
#ifndef _SIGNAL_H
#define _SIGNAL_H
#include "Adafruit_ZeroFFT.h"
q15_t signal[] = {
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560,
};
#endif
@@ -0,0 +1,118 @@
#include "Adafruit_ZeroFFT.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define STMPE_CS 6
#define TFT_CS 9
#define TFT_DC 10
#define SD_CS 5
//set this to 0 to disable autoranging and scale to FFT_MAX
#define AUTOSCALE 1
#if AUTOSCALE == 0
#define FFT_MAX 512
#endif
/*set this to a power of 2.
* Lower = faster, lower resolution
* Higher = slower, higher resolution
*/
#define DATA_SIZE 1024
//the sample rate.
#define FS 2360
#define NUM_REFERENCE_LINES 6
#define GRAPH_OFFSET 30
#define GRAPH_WIDTH (tft.width() - 3)
#define GRAPH_HEIGHT (tft.height() - GRAPH_OFFSET)
#define GRAPH_MIN (tft.height() - 2)
#define GRAPH_MAX (tft.height() - GRAPH_HEIGHT)
static float xScale;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int16_t data[DATA_SIZE];
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(115200);
delay(10);
//while(!Serial);
Serial.println("FFT with TFT featherwing!");
pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH);
pinMode(A1, OUTPUT);
digitalWrite(A1, LOW);
tft.begin();
tft.setRotation(1);
}
void drawReference(){
//draw some reference lines
uint16_t refStep = DATA_SIZE / 2 / NUM_REFERENCE_LINES;
tft.setTextSize(2);
tft.setTextColor(ILI9341_RED);
for(int i=0; i<DATA_SIZE/2 - refStep; i+=refStep){
uint16_t fc = FFT_BIN(i, FS, DATA_SIZE);
uint16_t xpos = map(i, 0, DATA_SIZE/2, 0, GRAPH_WIDTH);
tft.setCursor(xpos, 0);
tft.drawLine(xpos, GRAPH_MIN, xpos, GRAPH_MAX, ILI9341_RED);
tft.print(fc);
}
}
void loop() {
int32_t avg = 0;
for(int i=0; i<DATA_SIZE; i++){
int16_t val = analogRead(A2);
avg += val;
data[i] = val;
}
//remove DC offset and gain up to 16 bits
avg = avg/DATA_SIZE;
for(int i=0; i<DATA_SIZE; i++) data[i] = (data[i] - avg) * 64;
//run the FFT
ZeroFFT(data, DATA_SIZE);
#if AUTOSCALE
//get the maximum value
float maxVal = 0;
//data is only meaningful up to sample rate/2, discard the other half
for(int i=0; i<DATA_SIZE/2; i++) if(data[i] > maxVal) maxVal = data[i];
//normalize to the maximum returned value
for(int i=0; i<DATA_SIZE/2; i++)
data[i] =(float)data[i] / maxVal * GRAPH_HEIGHT;
#endif
tft.fillScreen(ILI9341_BLACK);
drawReference();
//draw all points
int16_t lasty, thisy;
thisy = GRAPH_MIN;
for(int i=1; i<GRAPH_WIDTH; i++){
uint16_t ix = map(i, 0, GRAPH_WIDTH, 0, DATA_SIZE/2);
#if AUTOSCALE
thisy = constrain(map(data[ix], 0, GRAPH_HEIGHT, GRAPH_MIN, GRAPH_MAX), GRAPH_OFFSET, GRAPH_MIN);
#else
thisy = constrain(map(data[ix], 0, FFT_MAX, GRAPH_MIN, GRAPH_MAX), GRAPH_OFFSET, GRAPH_MIN);
#endif
tft.drawLine(i - 1, lasty, i, thisy, ILI9341_GREEN);
lasty = thisy;
}
}
@@ -0,0 +1,50 @@
/* This example shows how to calculate an FFT an normalize it.
*
* The signal.h file constains a 200hz sine wave mixed with a weaker 800hz sine wave.
* The signal was generated at a sample rate of 8000hz.
*/
#include "Adafruit_ZeroFFT.h"
#include "signal.h"
//the signal in signal.h has 2048 values. Set this to a value from 16 to 2048.
//this must be a power of 2
#define DATA_SIZE 1024
//the sample rate
#define FS 8000
//this array will hold the normalized data
float normalized[DATA_SIZE/2];
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(115200);
while(!Serial); //wait for serial to be ready
//run the FFT
ZeroFFT(signal, DATA_SIZE);
//get the maximum value
float maxVal = 0;
//data is only meaningful up to sample rate/2, discard the other half
for(int i=0; i<DATA_SIZE/2; i++) if(signal[i] > maxVal) maxVal = signal[i];
//normalize to the maximum returned value
for(int i=0; i<DATA_SIZE/2; i++)
normalized[i] = (float)signal[i] / maxVal;
for(int i=0; i<DATA_SIZE/2; i++){
//print the frequency
Serial.print(FFT_BIN(i, FS, DATA_SIZE));
Serial.print(" Hz: ");
//print the corresponding FFT output
Serial.println(normalized[i]);
}
}
void loop() {
//don't even do anything
}
@@ -0,0 +1,214 @@
#ifndef _SIGNAL_H
#define _SIGNAL_H
#include "Adafruit_ZeroFFT.h"
q15_t signal[] = {
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560, 18619, 24134,
26213, 24134, 18619, 11560, 5301, 1754, 1677, 4400, 8100, 10515,
9830, 5389, -2025, -10475, -17582, -21414, -21207, -17635, -12543, -8229,
-6553, -8229, -12543, -17635, -21207, -21414, -17582, -10475, -2025, 5389,
9830, 10515, 8100, 4400, 1677, 1754, 5301, 11560,
};
#endif
@@ -0,0 +1,155 @@
#include "Adafruit_ZeroFFT.h"
#include "Adafruit_ZeroPDM.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define SAMPLERATE_HZ 22000
#define DECIMATION 64
#define STMPE_CS 6
#define TFT_CS 9
#define TFT_DC 10
#define SD_CS 5
//set this to 0 to disable autoranging and scale to FFT_MAX
#define AUTOSCALE 1
#if AUTOSCALE == 0
#define FFT_MAX 512
#endif
/*set this to a power of 2.
* Lower = faster, lower resolution
* Higher = slower, higher resolution
*/
#define DATA_SIZE 1024
#define NUM_REFERENCE_LINES 6
#define GRAPH_OFFSET 30
#define GRAPH_WIDTH (tft.width() - 3)
#define GRAPH_HEIGHT (tft.height() - GRAPH_OFFSET)
#define GRAPH_MIN (tft.height() - 2)
#define GRAPH_MAX (tft.height() - GRAPH_HEIGHT)
static float xScale;
Adafruit_ZeroPDM pdm = Adafruit_ZeroPDM(1, 4);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int16_t data[DATA_SIZE];
// a windowed sinc filter for 44 khz, 64 samples
uint16_t sincfilter[DECIMATION] = {0, 2, 9, 21, 39, 63, 94, 132, 179, 236, 302, 379, 467, 565, 674, 792, 920, 1055, 1196, 1341, 1487, 1633, 1776, 1913, 2042, 2159, 2263, 2352, 2422, 2474, 2506, 2516, 2506, 2474, 2422, 2352, 2263, 2159, 2042, 1913, 1776, 1633, 1487, 1341, 1196, 1055, 920, 792, 674, 565, 467, 379, 302, 236, 179, 132, 94, 63, 39, 21, 9, 2, 0, 0};
// a manual loop-unroller!
#define ADAPDM_REPEAT_LOOP_16(X) X X X X X X X X X X X X X X X X
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(115200);
delay(10);
//while(!Serial);
Serial.println("FFT with TFT featherwing!");
// Initialize the PDM/I2S receiver
if (!pdm.begin()) {
Serial.println("Failed to initialize I2S/PDM!");
while (1);
}
// Configure PDM receiver, sample rate
if (!pdm.configure(SAMPLERATE_HZ * DECIMATION / 16, true)) {
Serial.println("Failed to configure PDM");
while (1);
}
Serial.println("PDM configured");
tft.begin();
tft.setRotation(1);
}
void drawReference(){
//draw some reference lines
uint16_t refStep = DATA_SIZE / 2 / NUM_REFERENCE_LINES;
tft.setTextSize(2);
tft.setTextColor(ILI9341_RED);
for(int i=0; i<DATA_SIZE/2 - refStep; i+=refStep){
uint16_t fc = FFT_BIN(i, SAMPLERATE_HZ, DATA_SIZE);
uint16_t xpos = map(i, 0, DATA_SIZE/2, 0, GRAPH_WIDTH);
tft.setCursor(xpos, 0);
tft.drawLine(xpos, GRAPH_MIN, xpos, GRAPH_MAX, ILI9341_RED);
tft.print(fc);
}
}
void loop() {
int32_t avg = 0;
for(int i=0; i<DATA_SIZE; i++){
uint16_t runningsum = 0;
uint16_t *sinc_ptr = sincfilter;
for (uint8_t samplenum=0; samplenum < (DECIMATION/16) ; samplenum++) {
uint16_t sample = pdm.read() & 0xFFFF; // we read 16 bits at a time, by default the low half
ADAPDM_REPEAT_LOOP_16( // manually unroll loop: for (int8_t b=0; b<16; b++)
{
// start at the LSB which is the 'first' bit to come down the line, chronologically
// (Note we had to set I2S_SERCTRL_BITREV to get this to work, but saves us time!)
if (sample & 0x1) {
runningsum += *sinc_ptr; // do the convolution
}
sinc_ptr++;
sample >>= 1;
}
)
}
avg += runningsum;
data[i] = runningsum;
}
//remove DC offset
avg = avg/DATA_SIZE;
for(int i=0; i<DATA_SIZE; i++) data[i] = (data[i] - avg);
//run the FFT
ZeroFFT(data, DATA_SIZE);
#if AUTOSCALE
//get the maximum value
float maxVal = 0;
//data is only meaningful up to sample rate/2, discard the other half
for(int i=0; i<DATA_SIZE/2; i++) if(data[i] > maxVal) maxVal = data[i];
//normalize to the maximum returned value
for(int i=0; i<DATA_SIZE/2; i++)
data[i] =(float)data[i] / maxVal * GRAPH_HEIGHT;
#endif
tft.fillScreen(ILI9341_BLACK);
drawReference();
//draw all points
int16_t lasty, thisy;
thisy = GRAPH_MIN;
for(int i=1; i<GRAPH_WIDTH; i++){
uint16_t ix = map(i, 0, GRAPH_WIDTH, 0, DATA_SIZE/2);
#if AUTOSCALE
thisy = constrain(map(data[ix], 0, GRAPH_HEIGHT, GRAPH_MIN, GRAPH_MAX), GRAPH_OFFSET, GRAPH_MIN);
#else
thisy = constrain(map(data[ix], 0, FFT_MAX, GRAPH_MIN, GRAPH_MAX), GRAPH_OFFSET, GRAPH_MIN);
#endif
tft.drawLine(i - 1, lasty, i, thisy, ILI9341_GREEN);
lasty = thisy;
}
//add a bit of delay because we redraw the screen each time and it gets a bit blinky
delay(250);
}
@@ -0,0 +1,171 @@
#include "Adafruit_ZeroFFT.h"
#include "Adafruit_ZeroPDM.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define SAMPLERATE_HZ 22000
#define DECIMATION 64
#define STMPE_CS 6
#define TFT_CS 9
#define TFT_DC 10
#define SD_CS 5
/*set this to a power of 2.
* Lower = faster, lower resolution
* Higher = slower, higher resolution
*/
#define DATA_SIZE 2048
#define NUM_REFERENCE_LINES 6
#define GRAPH_OFFSET 30
#define GRAPH_WIDTH (tft.width() - 3)
#define GRAPH_HEIGHT (tft.height() - GRAPH_OFFSET)
#define GRAPH_MIN (tft.height() - 2)
#define GRAPH_MAX (tft.height() - GRAPH_HEIGHT)
static float xScale;
Adafruit_ZeroPDM pdm = Adafruit_ZeroPDM(1, 4);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int16_t data[DATA_SIZE];
int16_t dataOld[320];
// a windowed sinc filter for 44 khz, 64 samples
uint16_t sincfilter[DECIMATION] = {0, 2, 9, 21, 39, 63, 94, 132, 179, 236, 302, 379, 467, 565, 674, 792, 920, 1055, 1196, 1341, 1487, 1633, 1776, 1913, 2042, 2159, 2263, 2352, 2422, 2474, 2506, 2516, 2506, 2474, 2422, 2352, 2263, 2159, 2042, 1913, 1776, 1633, 1487, 1341, 1196, 1055, 920, 792, 674, 565, 467, 379, 302, 236, 179, 132, 94, 63, 39, 21, 9, 2, 0, 0};
// a manual loop-unroller!
#define ADAPDM_REPEAT_LOOP_16(X) X X X X X X X X X X X X X X X X
void drawReference(bool withText = false){
//draw some reference lines
uint16_t refStep = DATA_SIZE / 2 / NUM_REFERENCE_LINES;
tft.setTextSize(2);
tft.setTextColor(ILI9341_RED);
for(int i=0; i<DATA_SIZE/2 - refStep; i+=refStep){
uint16_t fc = FFT_BIN(i, SAMPLERATE_HZ, DATA_SIZE);
uint16_t xpos = map(i, 0, DATA_SIZE/2, 0, GRAPH_WIDTH);
tft.drawLine(xpos, GRAPH_MIN, xpos, GRAPH_MAX, ILI9341_RED);
if(withText) {
tft.setCursor(xpos, 0);
tft.print(fc);
}
}
}
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(115200);
delay(10);
//while(!Serial);
Serial.println("FFT with TFT featherwing!");
// Initialize the PDM/I2S receiver
if (!pdm.begin()) {
Serial.println("Failed to initialize I2S/PDM!");
while (1);
}
// Configure PDM receiver, sample rate
if (!pdm.configure(SAMPLERATE_HZ * DECIMATION / 16, true)) {
Serial.println("Failed to configure PDM");
while (1);
}
Serial.println("PDM configured");
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
drawReference(true);
dataOld[0] = 0;
}
void loop() {
int32_t avg = 0;
for(int i=0; i<DATA_SIZE; i++){
uint16_t runningsum = 0;
uint16_t *sinc_ptr = sincfilter;
for (uint8_t samplenum=0; samplenum < (DECIMATION/16) ; samplenum++) {
uint16_t sample = pdm.read() & 0xFFFF; // we read 16 bits at a time, by default the low half
ADAPDM_REPEAT_LOOP_16( // manually unroll loop: for (int8_t b=0; b<16; b++)
{
// start at the LSB which is the 'first' bit to come down the line, chronologically
// (Note we had to set I2S_SERCTRL_BITREV to get this to work, but saves us time!)
if (sample & 0x1) {
runningsum += *sinc_ptr; // do the convolution
}
sinc_ptr++;
sample >>= 1;
}
)
}
avg += runningsum;
data[i] = runningsum;
}
//remove DC offset
avg = avg/DATA_SIZE;
for(int i=0; i<DATA_SIZE; i++) data[i] = (data[i] - avg);
//run the FFT
ZeroFFT(data, DATA_SIZE);
//get the maximum value
float maxVal = 0;
//data is only meaningful up to sample rate/2, discard the other half
for(int i=0; i<DATA_SIZE/2; i++) if(data[i] > maxVal) maxVal = data[i];
//normalize to the maximum returned value
int16_t ma = 0;
for(int i=0; i<DATA_SIZE/2; i++){
data[i] =(float)data[i] / maxVal * GRAPH_HEIGHT;
if(i > 3)
data[i] = (data[i] + data[i-1] + data[i-2] + data[i - 3]) / 4;
else data[i] = 0;
}
//normalize again
maxVal = 0;
for(int i=0; i<DATA_SIZE/2; i++) if(data[i] > maxVal) maxVal = data[i];
for(int i=0; i<DATA_SIZE/2; i++)
data[i] =(float)data[i] / maxVal * GRAPH_HEIGHT;
//go over old line
for(int i=1; i<GRAPH_WIDTH; i++){
tft.drawLine(i - 1, dataOld[i-1], i, dataOld[i], ILI9341_BLACK);
}
//redraw red reference lines
drawReference();
//draw all points
int16_t lasty, thisy;
thisy = GRAPH_MIN;
for(int i=1; i<GRAPH_WIDTH; i++){
uint16_t ix = map(i, 0, GRAPH_WIDTH, 0, DATA_SIZE/2);
thisy = constrain(map(data[ix], 0, GRAPH_HEIGHT, GRAPH_MIN, GRAPH_MAX), GRAPH_OFFSET, GRAPH_MIN);
tft.drawLine(i - 1, lasty, i, thisy, ILI9341_GREEN);
lasty = thisy;
//save line
dataOld[i] = thisy;
}
dataOld[0] = data[0];
}