summaryrefslogtreecommitdiff
path: root/arduino/fht_test.ino
blob: 0e2f83cdede2a13b3a0833ec623ab59fb7059a27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

// FHT Options
#define FHT_N 256
#define SCALE 256
#define WINDOW 1
#define REORDER 1
#define LOG_OUT 1
#define LIN_OUT 0
#define LIN_OUT8 0
#define OCTAVE 0
#define OCT_NORM 0

#define BINS 128

#include <FHT.h>

void aquire_data() {
  noInterrupts();

  for (int i = 0; i < FHT_N; i++) {
    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
  }

  interrupts();
}

void crunch_data() {
  fht_window();
  fht_reorder();
  fht_run();
  //fht_mag_octave();
  fht_mag_log();
}

void setup() {

  Serial.begin(115200);

  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

}

char buf[2550] = "";

void loop() {
  aquire_data();
  crunch_data();
  
  for (int i = 0; i < BINS; i++) {
    Serial.print(fht_log_out[i]);
    Serial.print(' ');
  }
  
  Serial.print('\n');
  
  delay(10000);
}