#define LOG_OUT 1 // use the log output function
#define FFT_N 256 // FFT采样数:16.32.64.128.256
//#define DEBUG
#include <FFT.h> //快速傅里叶转换头文件声明
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
#define Cycle 3 //因为单次采样会有极大的噪音干扰,故用多次采样取平均值的方法
#define SIZE_WIDTH 32 //rgb显示宽度
//#define MAX_SPECTRUM 32
#define GAIN 2.3
//#define FREQUENCY_INDEX(I) ((I)*3 + 10)
int Spectrum[SIZE_WIDTH];//数组记录多次采样值并在最后取平均数
// Similar to F(), but for PROGMEM string pointers rather than literals
//#define F2(progmem_ptr) (const __FlashStringHelper *)progmem_ptr
#define CLK 11 // MUST be on PORTB! (Use pin 11 on Mega)
#define LAT A3
#define OE 9
#define A A4
#define B A1
#define C A2
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);
const unsigned char myBitmap [] PROGMEM = {
// 'Designbolts-Free-Valentine-Heart-Heart, 16x16px
0x00, 0x00, 0x1c, 0x38, 0x7e, 0x7e, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe,
0x3f, 0xfc, 0x1f, 0xf8, 0x1f, 0xf8, 0x0f, 0xf0, 0x03, 0xc0, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00,
};
void setup() {
Serial.begin(115200); // use the serial port
TIMSK0 = 0; // turn off timer0 for lower jitter - delay() and millis() killed
ADCSRA = 0xe5; // set the adc to free running mode
ADMUX = 0x40; // use adc0
DIDR0 = 0x01; // turn off the digital input for adc0
matrix.begin();
// matrix.fillScreen(matrix.Color888(0, 150, 255));
//while(true);
}
void loop() {
int ave;
// matrix.fillScreen(0);
for (int m=0;m<SIZE_WIDTH;m++){
Spectrum[m]=0;
}
for (int n=0;n<Cycle;n++){ //n记录采样次数
//while(1) { // reduces jitter
for (int i = 0 ; i < 512 ; i += 2) { // save 256 samples
// cli(); // UDRE interrupt slows this way down on arduino1.0
while(!(ADCSRA & 0x10)); // wait for adc to be ready
ADCSRA = 0xf5; // restart adc
byte m = ADCL; // fetch adc data
byte j = ADCH;
int k = (j << 8) | m; // form into an int
k -= 0x0200; // form into a signed int
k <<= 6; // form into a 16b signed int
fft_input[i] = k; // put real data into even bins
fft_input[i+1] = 0; // set odd bins to 0
}
// sei(); // turn interrupts back on
// window data, then reorder, then run, then take output
fft_window(); // window the data for better frequency response
fft_reorder(); // reorder the data before doing the fft
fft_run(); // process the data in the fft
fft_mag_log(); // take the output of the fft
/* for (byte i = 0 ; i < FFT_N/2 ; i++)
{
Serial.println(fft_log_out[i]); // <--输出到串口
}*/
//Serial.write(255); // send a start byte
//Serial.write(fft_log_out, 128); // send out the data
}
static int times = 0;
if(times++ == 0){
times=0;
for(int m=0;m<SIZE_WIDTH;m++){
ave=0;
for (byte i=m*4;i<(m+1)*4;i++){
ave+=fft_log_out[i];
}
ave/=4;
ave/=2;
Spectrum[m]=ave;
Spectrum[m]/=Cycle;
Serial.print(Spectrum[m]);
Serial.print("-");
if (m ==15)
{
Serial.println("||");
}
Spectrum[0] = Spectrum[1]-22;
//Spectrum[1] = Spectrum[1]-7;
//Spectrum[2] = Spectrum[2]-6;
//Spectrum[3] = Spectrum[3]-6;
int y = Spectrum[m]-8;
if(y>26)
y = 15;
/*if(y<=7){
//y = 0;
matrix.fillScreen(0);
}*/
matrix.drawLine(m, 0, m, y, matrix.Color888(0, 150, 255));
matrix.drawLine(m, y+1, m, 15, matrix.Color888(0, 0, 0));
}
}
}