Hi all. I've been trying to make a spectrum analyzer, but the LED matrix output is a bit delayed from the sound I'm hearing and I was wondering if I can get some advice on how to reduce that. First off some specs:
To get the sound into the arduino, I'm using what I think to be a pretty standard setup shown below.
The audio is coming from a breadboard aux jack, amplified by the non inverting amplifier and level shifted to oscillate around 2.5V. I've oscilliscoped the input vs the output of this stage into the arduino and there seems to be little delay, although I'm obviously open to the chance something might be happening in this analog portion.
Once inside the arduino, I'm processing with an FHT and outputting to an 8x8 MAX7221. The code is below and the fht implementation can be found here.
I've tried both the fft and fht from the same source and the fht is noticeably faster. I was wondering what the difference in the output is between the two (FFT I understand pretty well), but that may be a conversation for another topic. In any case, the FHT seems faster.
The MAX7221 is being controlled with another library which can be found here.
My full code is below.
//
#define LOG_OUT 0 // use the log output function
#define FHT_N 256 // set to 256 point fht
#define LIN_OUT 1
//#define DIN 12
//#define CS 11
//#define CLK 10
#include <LedControl.h>
#include <FHT.h>
int DIN = 12;
int CS = 11;
int CLK = 10;
//int delayTime = 5;
//const float factors[8] = {1, 1.1, 1.35, 1.45, 1.55, 1.7, 1.9, 2.1}; //factors to increase the height of each column
//const float factors[8] = {1, 1, 1, 1, 1, 1.1, 1.3, 1.3}; //factors to increase the height of each column
const float factors[8] = {1, 1, 1, 1, 1, 1, 1, 1}; //factors to increase the height of each column
boolean clipping = 0;
int clippingCounter = 5000;
unsigned char hs[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //height of each column
LedControl lc = LedControl(DIN, CLK, CS, 0);
void setup() {
lc.shutdown(0, false); //The MAX72XX is in power-saving mode on startup
lc.setIntensity(0, 2); // Set the brightness to maximum value
lc.clearDisplay(0); // and clear the display
TIMSK0 = 0; // turn off timer0 for lower jitter
ADCSRA = 0xe5; // set the adc to free running mode
ADMUX = 0x40; // use adc0
DIDR0 = 0x01; // turn off the digital input for adc0
Serial.begin(115200); // use the serial port
pinMode(13,OUTPUT);//led indicator pin
}
//const unsigned char borders[9] = {0, 3, 5, 7, 13, 32, 64, 100, 127}; //borders of the frequency areas
//const unsigned char borders[9] = {0, 3, 7, 10, 13, 16, 24, 32, 69}; //borders of the frequency areas
const unsigned char borders[9] = {0, 3, 6, 9, 10, 13, 20, 32, 69}; //borders of the frequency areas
void setColumn(int column, unsigned char height) {
// maps height from 0 to 255 to 0 to 8
unsigned char h = (unsigned char) map(height, 0, 255, 0, 8);
// apply multipliers to heights
h = (unsigned char)(h * factors[column]);
// if new h is less than previous value, decrement
if (h < hs[column]) {
hs[column]--;
} else if (h > hs[column]) {
hs[column] = h;
}
// hs[column] = h;
// light up LEDs
for (unsigned char y = 0; y < 8; y++) {
if (hs[column] > y) {
// addr then col then row
lc.setLed(0, column, y, true);
} else {
lc.setLed(0, column, y, false);
}
}
}
void loop() {
while (1) { // reduces jitter
cli(); // UDRE interrupt slows this way down on arduino1.0
for (int i = 0 ; i < FHT_N ; i++) { // save 256 samples
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
fht_input[i] = k; // put real data into even bins
// fht_input[i + 1] = 0; // set odd bins to 0
if (j == 0 || j == 255){//if clipping
digitalWrite(13,HIGH);//set pin 13 high
clipping = 1;//currently clipping
clippingCounter = 5000;
}
if (clipping){//if currently clipping
clipping = 0;
}else{
clippingCounter--;
if (clippingCounter == 0){
digitalWrite(13,LOW);//turn off clipping led indicator (pin 13)
}
}
}
fht_window(); // window the data for better frequency response
fht_reorder(); // reorder the data before doing the fht
fht_run(); // process the data in the fht
fht_mag_lin(); // take the output of the fht
sei();
// sets DC components to 0
fht_lin_out[0] = 0;
fht_lin_out[1] = 0;
// for each column
for (int i = 0; i < 8; i++) {
// find the maximum value within the specified frequency borders
unsigned char maxVal = 0;
for (unsigned char j = borders[i]; j < borders[i + 1]; j++) {
if ((unsigned char)fht_lin_out[j] > maxVal) {
maxVal = (unsigned char) fht_lin_out[j];
}
// if (maxVal < 100){
// maxVal = 1;
// }
// if (maxVal < 50){
// maxVal = 0;
// }
}
// determine height and light up LED
setColumn(i, maxVal);
Serial.print(maxVal);
Serial.print("\t");
}
Serial.println("");
}
}
Ultimately, I'm wondering if the Arduino UNO simply isn't fast enough to run this FHT and send out the data to the ports. My other suspicion is that either the LedControl library is too slow, or the LED drivers on the MAX7221 are the problem. The datasheet for the MAX7221 can be found here.
Any suggestions would be very helpful. I can also make a small video if necessary to show the delay.
Thank you!
