如何知道Arduino采样率
How to know Arduino Sampling Rate
/*
fft_adc_serial.pde
guest openmusiclabs.com 7.7.14
example sketch for testing the fft library.
it takes in data on ADC0 (Analog0) and processes them
with the fft. the data is sent out over the serial
port at 115.2kb.
*/
#define LOG_OUT 1 // use the log output function
#define FFT_N 256 // set to 256 point fft
#include <FFT.h> // include the library
unsigned long time;
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
for (int i = 0 ; i < 512 ; i += 2) { // 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
fft_input[i] = k; // put real data into even bins
Serial.print("input ");
Serial.print(i);
Seirla.print(" = ");
Serial.println(k);
fft_input[i+1] = 0; // set odd bins to 0
}
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
sei();
Serial.println("start");
for (byte i = 0 ; i < FFT_N/2 ; i++) {
Serial.print("\t output");
Serial.print(i);
Serial.println(fft_log_out[i]); // send out the data
}
}
}
我将此 FFT 示例代码用于 FFT
cli(); // UDRE interrupt slows this way down on arduino1.0
for (int i = 0 ; i < 512 ; i += 2) { // 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
fft_input[i] = k; // put real data into even bins
Serial.print("input ");
Serial.print(i);
Seirla.print(" = ");
Serial.println(k);
fft_input[i+1] = 0; // set odd bins to 0
}
在这个输入部分输入多少时间段?
没有延迟();本例中的函数
while(!(ADCSRA & 0x10)); // wait for adc to be ready
这条线像 delay() 函数一样工作?这个函数等待 Analog0 多长时间?
采样率在wiring.c中设置:
https://code.google.com/p/arduino/source/browse/trunk/hardware/cores/arduino/wiring.c?r=565#210
因此,在 16mHz 上,arduino 的最大采样率为 9600hz,但实际采样率在很大程度上取决于转换之间的延迟。
由于您的波特率非常高并且您不需要进行大量计算,因此它应该接近 9600hz。
更新:
这里有一个更准确的答案:https://arduino.stackexchange.com/a/701
/*
fft_adc_serial.pde
guest openmusiclabs.com 7.7.14
example sketch for testing the fft library.
it takes in data on ADC0 (Analog0) and processes them
with the fft. the data is sent out over the serial
port at 115.2kb.
*/
#define LOG_OUT 1 // use the log output function
#define FFT_N 256 // set to 256 point fft
#include <FFT.h> // include the library
unsigned long time;
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
for (int i = 0 ; i < 512 ; i += 2) { // 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
fft_input[i] = k; // put real data into even bins
Serial.print("input ");
Serial.print(i);
Seirla.print(" = ");
Serial.println(k);
fft_input[i+1] = 0; // set odd bins to 0
}
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
sei();
Serial.println("start");
for (byte i = 0 ; i < FFT_N/2 ; i++) {
Serial.print("\t output");
Serial.print(i);
Serial.println(fft_log_out[i]); // send out the data
}
}
}
我将此 FFT 示例代码用于 FFT
cli(); // UDRE interrupt slows this way down on arduino1.0
for (int i = 0 ; i < 512 ; i += 2) { // 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
fft_input[i] = k; // put real data into even bins
Serial.print("input ");
Serial.print(i);
Seirla.print(" = ");
Serial.println(k);
fft_input[i+1] = 0; // set odd bins to 0
}
在这个输入部分输入多少时间段?
没有延迟();本例中的函数
while(!(ADCSRA & 0x10)); // wait for adc to be ready
这条线像 delay() 函数一样工作?这个函数等待 Analog0 多长时间?
采样率在wiring.c中设置: https://code.google.com/p/arduino/source/browse/trunk/hardware/cores/arduino/wiring.c?r=565#210
因此,在 16mHz 上,arduino 的最大采样率为 9600hz,但实际采样率在很大程度上取决于转换之间的延迟。 由于您的波特率非常高并且您不需要进行大量计算,因此它应该接近 9600hz。
更新: 这里有一个更准确的答案:https://arduino.stackexchange.com/a/701