Problems with FHT code
Hello,
Thanks for the great FHT/FFT libraries that you developed for the Arduino.
I am having a problem with the FHT library (haven't tested the FFT). Any mathematical operations such as multiplication or division, that take place after these sequences:
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; also with log
result in corrupt/bogus values. In addition micros() and millis() return bogus values.
Is there any way around this? Is the library doing something to the "math engine" of the chip?
Below is a code example that illustrates the error. Followed by the result.
As you can see 4194301183 - 3877611200 should be 316689983. Also those values came from micros() and appear to be bogus, since they don't change between iterations of the loop.
Any ideas on how to circumvent this?
Thanks,
Nils
Thanks for the great FHT/FFT libraries that you developed for the Arduino.
I am having a problem with the FHT library (haven't tested the FFT). Any mathematical operations such as multiplication or division, that take place after these sequences:
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; also with log
result in corrupt/bogus values. In addition micros() and millis() return bogus values.
Is there any way around this? Is the library doing something to the "math engine" of the chip?
Below is a code example that illustrates the error. Followed by the result.
/*
fht_adc.pde
guest openmusiclabs.com 9.5.12
example sketch for testing the fht library.
it takes in data on ADC0 (Analog0) and processes them
with the fht. the data is sent out over the serial
port at 115.2kb. there is a pure data patch for
visualizing the data.
*/
#define LOG_OUT 1 // use the log output function
#define FHT_N 256 // set to 256 point fht
#include <FHT.h> // include the library
void setup() {
Serial.begin(115200); // use the serial port
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
}
void loop() {
while(1) { // reduces jitter
cli(); // UDRE interrupt slows this way down on arduino1.0
unsigned long us1 = micros();
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 bins
}
unsigned long us2 = micros();
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_log(); // take the output of the fht
sei();
Serial.print("Testing of math: ");
Serial.print(us1);
Serial.print(" - ");
Serial.print(us2);
Serial.print(" = ");
Serial.println(us2 - us1);
//Serial.write(255); // send a start byte
//Serial.write(fht_log_out, FHT_N/2); // send out the data
}
}Testing of math: 4194301183 - 3877611200 = 3978277313As you can see 4194301183 - 3877611200 should be 316689983. Also those values came from micros() and appear to be bogus, since they don't change between iterations of the loop.
Any ideas on how to circumvent this?
Thanks,
Nils