Pages: [1]   Go Down
Author Topic: Uncooperative code: FHT vs Display  (Read 503 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi ther. I have 2 pieces of roughly working code. One is an FHT with an added piece of code to find the fundamental frequency and output it as a value 'k'. The other is a piece of code I wrote to take the value of k, compare it to stored values and eventually identify the note and whether or not it is sharp or flat etc.. At the moment they are just not working together at all. I've used existing code for the FHT and therefor have had to make some alterations to make it work together but this stops everything working. Please help.

FHT code:
Code:
#include <fix_fft.h>

/*
fht_adc.pde
guest openmusiclabs.com 9.5.12
ekample 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
float k;
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 = 0x41; // use adc1
  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 < 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
    }
    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.write(255); // send a start byte
    int j = 0;
int k;
for (int i = 0; i < FHT_N/2; i++) {
  if (fht_log_out[i] > j) {
    j = fht_log_out[i];
    k = i*150;
  }
}
Serial.println(k);
delay(20000);
  }
}

This is my note selection code:

Code:
#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);
float StrI[] = {
  285.43, 329.63, 369.99};
float StrII[] = {
  220.00, 246.94, 285.42};
float StrIII[] = {
  169.81, 196.00, 219.99};
float StrIV[] = {
  127.47, 146.83, 169.61};
float StrV[] = {
  95.25, 110.00, 127.57};
float StrVI[] = {
  65.406, 82.407, 95.15};
float Strings[] = {
  73.416, 95.00, 128.00, 170.00, 220.00, 285.00, 392.00};
float a = Strings[0];
float b = Strings[1];
float c = Strings[2];
float d = Strings[3];
float e = Strings[4];
float f = Strings[5];
float g = Strings[6];
const float x = random(30,400);
float h = 0;
float l = 0;
float jp = 0;

void setup(){
  Serial.begin(9600);
  pinMode(13, OUTPUT); 
  lcd.init();
  lcd.clear();
  lcd.printIn("Guitar Tuner");
  delay(1000);
  lcd.clear();
  lcd.printIn("By Liam Simpson");
  delay(1000);
  lcd.clear();
  lcd.printIn("E,A,D,G,b,e?");
  delay(3000);
  lcd.clear();
}
void loop(){
  float x = random(73,400);
  Serial.println(x);
  if(x > Strings[0] && x < Strings[1]) {
    float h = StrVI[1];
    float l = StrVI[2];
    float jp = StrVI[0];
    lcd.cursorTo(1,0);
    lcd.printIn("E");
    if(x > h+1 && x < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp"); 
    }
    else if(x < h-1 && x > jp) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat"); 
    }
    else if(x >= jp-1 && x <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!"); 
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();
  }
  if(x > Strings[1] && x < Strings[2]) {
    float h = StrV[1];
    float l = StrV[2];
    float jp = StrV[0];
    lcd.cursorTo(1,2);
    lcd.printIn("A");
    if(x > h+1 && x < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp"); 
    }
    else if(x < h-1 && x > jp) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat"); 
    }
    else if(x >= jp-1 && x <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!"); 
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();
  }
  if(x > Strings[2] && x < Strings[3]) {
    float h = StrIV[1];
    float l = StrIV[2];
    float jp = StrIV[0];
    lcd.cursorTo(1,4);
    lcd.printIn("D");
    if(x > h+1 && x < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp"); 
    }
    else if(x < h-1 && x > jp) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat"); 
    }
    else if(x >= jp-1 && x <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!"); 
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();
  }
  if(x > Strings[3] && x < Strings[4]) {
    float h = StrIII[1];
    float l = StrIII[2];
    float jp = StrIII[0];
    lcd.cursorTo(1,6);
    lcd.printIn("G");
    if(x > h+1 && x < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp"); 
    }
    else if(x < h-1 && x > jp) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat"); 
    }
    else if(x >= jp-1 && x <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!"); 
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();
  }
  if(x > Strings[4] && x < Strings[5]) {
    float h = StrII[1];
    float l = StrII[2];
    float jp = StrII[0];
    lcd.cursorTo(1,8);
    lcd.printIn("b");
    if(x > h+1 && x < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp"); 
    }
    else if(x < h-1 && x > jp) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat"); 
    }
    else if(x >= jp-1 && x <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!"); 
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear(); 
  }
  if(x > Strings[5] && x < Strings[6]) {
    float h = StrI[1];
    float l = StrI[2];
    float jp = StrI[0];
    lcd.cursorTo(1,10);
    lcd.printIn("e");
    if(x > h+1 && x < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp"); 
    }
    else if(x < h-1 && x > jp) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat"); 
    }
    else if(x >= jp-1 && x <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!"); 
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();
  }
}

The code all together is in the next post.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

By the way, In my note detection code I have simulated the FHT output by replacing the k variable with a randomly generated value of x, this is changed in the all together code.

Code:
void setup(){
  TIMSK0 = 0; // turn off timer0 for lower jitter
  ADCSRA = 0xe5; // set the adc to free running mode
  ADMUX = 0x41; // use adc1
  DIDR1 = 0x01; // turn off the digital input for adc0
}
void loop(){
  fht();
  detect();
  test();
}

void detect(){
  if(k > Strings[0] && k < Strings[1]) {
    float h = StrVI[1];
    float l = StrVI[2];
    float p = StrVI[0];
    lcd.cursorTo(1,0);
    lcd.printIn("E");
    Serial.println("  E");
    if(k > h+1 && k < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp");  
    }
    else if(k < h-1 && k > p) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat");  
    }
    else if(k >= p-1 && k <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!");  
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();
  }
  if(k > Strings[1] && k < Strings[2]) {
    float h = StrV[1];
    float l = StrV[2];
    float p = StrV[0];
    lcd.cursorTo(1,2);
    lcd.printIn("A");
    Serial.println("  A");
    if(k > h+1 && k < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp");  
    }
    else if(k < h-1 && k > p) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat");  
    }
    else if(k >= p-1 && k <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!");  
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();
  }
  if(k > Strings[2] && k < Strings[3]) {
    float h = StrIV[1];
    float l = StrIV[2];
    float p = StrIV[0];
    lcd.cursorTo(1,4);
    lcd.printIn("D");
    Serial.println("  D");
    if(k > h+1 && k < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp");  
    }
    else if(k < h-1 && k > p) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat");  
    }
    else if(k >= p-1 && k <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!");  
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();
  }
  if(k > Strings[3] && k < Strings[4]) {
    float h = StrIII[1];
    float l = StrIII[2];
    float p = StrIII[0];
    lcd.cursorTo(1,6);
    lcd.printIn("G");
    Serial.println("  G");
    if(k > h+1 && k < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp");  
    }
    else if(k < h-1 && k > p) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat");  
    }
    else if(k >= p-1 && k <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!");  
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();
  }
  if(k > Strings[4] && k < Strings[5]) {
    float h = StrII[1];
    float l = StrII[2];
    float p = StrII[0];
    lcd.cursorTo(1,8);
    lcd.printIn("b");
    Serial.println("  b");
    if(k > h+1 && k < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp");  
    }
    else if(k < h-1 && k > p) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat");  
    }
    else if(k >= p-1 && k <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!");  
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();  
  }
  if(k > Strings[5] && k < Strings[6]) {
    float h = StrI[1];
    float l = StrI[2];
    float p = StrI[0];
    lcd.cursorTo(1,10);
    lcd.printIn("e");
    Serial.println("  e");
    if(k > h+1 && k < l) {
      lcd.cursorTo(2,0);
      lcd.printIn("Sharp");  
    }
    else if(k < h-1 && k > p) {
      lcd.cursorTo(2,0);
      lcd.printIn("Flat");  
    }
    else if(k >= p-1 && k <= l+1){
      lcd.cursorTo(2,0);
      lcd.printIn("Bingo!");  
    }
    else{
      lcd.cursorTo(2,0);
      lcd.printIn("Undetectable");
    }
    delay(1000);
    lcd.clear();
  }
}
void test(){
  delay(8000);
  Serial.println(k);
  Serial.println(p);
  Serial.println(h);
  Serial.println(l);
}

void fht() {
  while(1) { // reduces jitter
    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
    }
    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
    int j = 0;
    int k;
    for (int i = 0; i < FHT_N/2; i++) {
      if (fht_log_out[i] > j) {
        j = fht_log_out[i];
        k = i*150;
      }
    }
  }
}

I usually have all the code in the void loop funtion but I put it like that to tidy it up a little.
« Last Edit: August 11, 2013, 05:33:48 pm by FantasticMrSimpson » Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 97
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

get rid of the lines:

TIMSK0 = 0;
cli();
sei();

and see if that helps.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for the suggestion but that's not working unfortunately. Still stuck :/

EDIT: The code works together now (somehow). I honestly have no idea how. But now i need to change the resolution. at the moment I have

ADSCRA = 0xe7;

and i understand that Is the smallest bin size It can go to. Any guidance appreciated.
« Last Edit: August 12, 2013, 09:50:35 am by FantasticMrSimpson » Logged

Pages: [1]   Go Up
Jump to: