Page 1 of 1

FHT arduino and HT1632c

PostPosted: Tue May 21, 2013 12:26 pm
by tj_thornsberry
Alrighty so the things up and running, besides a few ms of delay and some noise on the last 2 bands of the meter, 30, and 31, and some noise from the bottom of the spectrum, anyways can ya'll help with these issues?

Oh and yes I know the X and Y are backwards at first on the text, I screwed up there and just didn't wanna fix it yet.


Code: Select all
#include <ht1632c.h>

#define LOG_OUT 1 // use the log output function
#define FHT_N 64 // set to 256 point fft

#include <FHT.h> // include the library

ht1632c dotmatrix = ht1632c(PORTD, 7, 6, 4, 5, GEOM_32x16, 2);
int pinArray[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int LedXs[] = {0,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};
int countx = 0;
int county = 0;

void setup()
{
  Serial.begin(115200);
  dotmatrix.clear();
  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 < FHT_N ; i++) { // save 256 samples
      if ( (i & 0x01) == 0 ){county++; if(county >= 33){county = 0; } }/*needed to use 33 instead of 31
     because something do with counting evens, only reason I know that is because I asked Mark
     in the support email thing, so thanks mark.*/
      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();
   
    countx = (fht_log_out[county]/15.9375)-2; //The -2 is for the noise issue..
    Spectrum(countx, (county));
    dotmatrix.sendframe();
    //
   
  }
 
   
   
 
}

void Spectrum(int htX, int htY){
  switch(htX){
   case 0:
   dotmatrix.plot(htY, 0, BLACK);
   dotmatrix.plot(htY,1,BLACK);
   dotmatrix.plot(htY,2,BLACK);
   dotmatrix.plot(htY,3,BLACK);
   dotmatrix.plot(htY,4,BLACK);
   dotmatrix.plot(htY,5,BLACK);
   dotmatrix.plot(htY,6,BLACK);
   dotmatrix.plot(htY,7,BLACK);
   dotmatrix.plot(htY,8,BLACK);
   dotmatrix.plot(htY,9,BLACK);
   dotmatrix.plot(htY,10,BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 1:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, BLACK);
   dotmatrix.plot(htY,2,BLACK);
   dotmatrix.plot(htY,3,BLACK);
   dotmatrix.plot(htY,4,BLACK);
   dotmatrix.plot(htY,5,BLACK);
   dotmatrix.plot(htY,6,BLACK);
   dotmatrix.plot(htY,7,BLACK);
   dotmatrix.plot(htY,8,BLACK);
   dotmatrix.plot(htY,9,BLACK);
   dotmatrix.plot(htY,10,BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 2:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, BLACK);
   dotmatrix.plot(htY,3,BLACK);
   dotmatrix.plot(htY,4,BLACK);
   dotmatrix.plot(htY,5,BLACK);
   dotmatrix.plot(htY,6,BLACK);
   dotmatrix.plot(htY,7,BLACK);
   dotmatrix.plot(htY,8,BLACK);
   dotmatrix.plot(htY,9,BLACK);
   dotmatrix.plot(htY,10,BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 3:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, BLACK);
   dotmatrix.plot(htY,4,BLACK);
   dotmatrix.plot(htY,5,BLACK);
   dotmatrix.plot(htY,6,BLACK);
   dotmatrix.plot(htY,7,BLACK);
   dotmatrix.plot(htY,8,BLACK);
   dotmatrix.plot(htY,9,BLACK);
   dotmatrix.plot(htY,10,BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 4:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, BLACK);
   dotmatrix.plot(htY,5,BLACK);
   dotmatrix.plot(htY,6,BLACK);
   dotmatrix.plot(htY,7,BLACK);
   dotmatrix.plot(htY,8,BLACK);
   dotmatrix.plot(htY,9,BLACK);
   dotmatrix.plot(htY,10,BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 5:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, BLACK);
   dotmatrix.plot(htY,6,BLACK);
   dotmatrix.plot(htY,7,BLACK);
   dotmatrix.plot(htY,8,BLACK);
   dotmatrix.plot(htY,9,BLACK);
   dotmatrix.plot(htY,10,BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 6:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, BLACK);
   dotmatrix.plot(htY,7,BLACK);
   dotmatrix.plot(htY,8,BLACK);
   dotmatrix.plot(htY,9,BLACK);
   dotmatrix.plot(htY,10,BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 7:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, GREEN);
   dotmatrix.plot(htY, 7, BLACK);
   dotmatrix.plot(htY,8,BLACK);
   dotmatrix.plot(htY,9,BLACK);
   dotmatrix.plot(htY,10,BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 8:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, GREEN);
   dotmatrix.plot(htY, 7, GREEN);
   dotmatrix.plot(htY, 8, BLACK);
   dotmatrix.plot(htY,9,BLACK);
   dotmatrix.plot(htY,10,BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 9:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, GREEN);
   dotmatrix.plot(htY, 7, GREEN);
   dotmatrix.plot(htY, 8, ORANGE);
   dotmatrix.plot(htY, 9, BLACK);
   dotmatrix.plot(htY,10,BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 10:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, GREEN);
   dotmatrix.plot(htY, 7, GREEN);
   dotmatrix.plot(htY, 8, ORANGE);
   dotmatrix.plot(htY, 9, ORANGE);
   dotmatrix.plot(htY, 10, BLACK);
   dotmatrix.plot(htY,11,BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 11:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, GREEN);
   dotmatrix.plot(htY, 7, GREEN);
   dotmatrix.plot(htY, 8, ORANGE);
   dotmatrix.plot(htY, 9, ORANGE);
   dotmatrix.plot(htY, 10, ORANGE);
   dotmatrix.plot(htY, 11, BLACK);
   dotmatrix.plot(htY,12,BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 12:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, GREEN);
   dotmatrix.plot(htY, 7, GREEN);
   dotmatrix.plot(htY, 8, ORANGE);
   dotmatrix.plot(htY, 9, ORANGE);
   dotmatrix.plot(htY, 10, ORANGE);
   dotmatrix.plot(htY, 11, ORANGE);
   dotmatrix.plot(htY, 12, BLACK);
   dotmatrix.plot(htY,13,BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 13:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, GREEN);
   dotmatrix.plot(htY, 7, GREEN);
   dotmatrix.plot(htY, 8, ORANGE);
   dotmatrix.plot(htY, 9, ORANGE);
   dotmatrix.plot(htY, 10, ORANGE);
   dotmatrix.plot(htY, 11, ORANGE);
   dotmatrix.plot(htY, 12, ORANGE);
   dotmatrix.plot(htY, 13, BLACK);
   dotmatrix.plot(htY,14,BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 14:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, GREEN);
   dotmatrix.plot(htY, 7, GREEN);
   dotmatrix.plot(htY, 8, ORANGE);
   dotmatrix.plot(htY, 9, ORANGE);
   dotmatrix.plot(htY, 10, ORANGE);
   dotmatrix.plot(htY, 11, ORANGE);
   dotmatrix.plot(htY, 12, ORANGE);
   dotmatrix.plot(htY, 13, ORANGE);
   dotmatrix.plot(htY, 14, BLACK);
   dotmatrix.plot(htY,15,BLACK);
    break;
   case 15:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, GREEN);
   dotmatrix.plot(htY, 7, GREEN);
   dotmatrix.plot(htY, 8, ORANGE);
   dotmatrix.plot(htY, 9, ORANGE);
   dotmatrix.plot(htY, 10, ORANGE);
   dotmatrix.plot(htY, 11, ORANGE);
   dotmatrix.plot(htY, 12, ORANGE);
   dotmatrix.plot(htY, 13, ORANGE);
   dotmatrix.plot(htY, 14, RED);
   dotmatrix.plot(htY, 15, BLACK);
    break;
   case 16:
   dotmatrix.plot(htY, 0, GREEN);
   dotmatrix.plot(htY, 1, GREEN);
   dotmatrix.plot(htY, 2, GREEN);
   dotmatrix.plot(htY, 3, GREEN);
   dotmatrix.plot(htY, 4, GREEN);
   dotmatrix.plot(htY, 5, GREEN);
   dotmatrix.plot(htY, 6, GREEN);
   dotmatrix.plot(htY, 7, GREEN);
   dotmatrix.plot(htY, 8, ORANGE);
   dotmatrix.plot(htY, 9, ORANGE);
   dotmatrix.plot(htY, 10, ORANGE);
   dotmatrix.plot(htY, 11, ORANGE);
   dotmatrix.plot(htY, 12, ORANGE);
   dotmatrix.plot(htY, 13, ORANGE);
   dotmatrix.plot(htY, 14, RED);
   dotmatrix.plot(htY, 15, RED);
    break;
  }
}

Re: FHT arduino and HT1632c

PostPosted: Tue May 21, 2013 11:51 pm
by guest
looks good. im assuming you are putting audio into it. what does your audio input circuit look like? are you doing any filtering and setting the dc level? here is an example of a simple way to do that:

http://wiki.openmusiclabs.com/wiki/MiniArDSP

also, im not sure what this line does in the code. it looks like it increments county for every other sample that is taken, and then resets to 0 after all the samples are taken. but then it isnt used in the sample taking code.
Code: Select all
if ( (i & 0x01) == 0 ){county++; if(county >= 33){county = 0; } }


the code could speed up by interleaving the ht1632 data writes with the sample taking, but the speed up wouldnt be all that noticable. the eye cant detect faster than 15ms or so, and the samples are taken in 2ms.

its possible that the noise at the high end is due to aliasing of some sort. i will take a closer look at the code to see if i notice anything.

Re: FHT arduino and HT1632c

PostPosted: Wed May 22, 2013 12:08 am
by guest
ok, looking at it again, it looks like you are printing the various frequency bins out to the vertical bars on the led display. its probably best to do all of that in a for loop. right now, county is rolling over by 1 every time you run through the fht program, but this only gives you 1 bin to update per fht run through. it would be better to have all the bins get written, so the data on the dispaly represents one snapshot. a for loop would make this easier. for example
:

Code: Select all
for (county = 0 ; county < FHT_N/2 ; county++) {
  countx = (fht_log_out[county]/16)-2; //The -2 is for the noise issue..
  Spectrum(countx, (county));
  dotmatrix.sendframe();
}


note that i changed 15.9 to 16. dividing by a multiple of 2 takes orders of magnitude less time, as it can be done as a bit shift. it takes over 200 clock cycles to multiply by 15.9, but only 8 to do a divide by 16.

Re: FHT arduino and HT1632c

PostPosted: Wed May 22, 2013 1:49 am
by tj_thornsberry
Yes, I am putting audio into Analog 0, I don't have the correct parts for a standalone audio circuit yet so for now its just running off a amplified headphone port on my external sound card. I am also checking the input on the Pure Data thing and the noise issues aren't to bad to notice on the bottom bands.
What that code is doing is checking every time it's even, because I believe you store the real data in the evens so I was just trying to get a way to make sure I am getting the values every time its even.
I am not a expert though in c++ so I may be mistaking in some of this data.

About the speed, I know under normal circumstances it wouldn't be a noticible speed increase if I did that, but I am thinking the Ht1632 is taking a bit longer to write so for me I can tell there is a tad bit of a delay, it can also be that code to check the evens, its not the most ideal but I couldn't think of any other way.
The noise, after checking in the serial monitor, is a constant 14 for fht_log_out[0] and a 13 for fht_log_out[1] and the next ones are getting noise to but it progressivly drops so around log_out[8-9] its perty even.

I was thinking about doing a for loop, but wasn't sure if I should but the for loop outside or inside of the loop, and I wasn't even sure If I could put a loop inside a loop.
Also I was already playing with subtracting a few numbers for the noise issue :p.
And I had no clue that dividing by 15.9 and 16, make so much of a difference, that may be were some of my speed is going because I kinda rely of that part to get the info I need.
I am also in the process of changing the plotting on the LEDs so I may gain a few ms on that.

I was gonna say something else but all this typing and the fact that I am dead tired, I done forgot :/
Oh and thanks for the suggestion I will try it out as soon as I wake up tommorow, I'll redo the coding and post the results.

Re: FHT arduino and HT1632c

PostPosted: Wed May 22, 2013 2:13 am
by tj_thornsberry
Alrighty added a for loop like you suggested, ya sleep will come later, and it sped it up perty good, now I excluded the REORDER and the WINDOW and changed them to 1, off, and that fixed the noise completly, so it's some issue in there. But after all this I still got the very first, maybe it's the very last, ill check later, band at 14, so its always on.

Oh and it seems to have a flickering issue every now and then, it will go nicley for a good little bit, and flicker a couple of times, and repeat.

Re: FHT arduino and HT1632c

PostPosted: Wed May 22, 2013 2:25 am
by guest
you have to do REORDER, the FHT shouldnt be able to work without it. WINDOW isnt as critical, but the results should look nicer with it. the FHT uses both odd and even bins, as it takes in only real data. the FFT uses only the even bins, which is why the FHT is faster and uses less space, it uses all the bins.

im betting the noise issues are due to not having the audio set to the correct DC level. you should put in the circuit i linked to above. since its not at the right DC level, it has a large DC component, which are the lower bins. and, the audio is probably clipping, putting a bunch of higher harmonics in, making all the high bins go nuts. clipping will also put in harmonics way above the sampling rate, and give aliasing problems.

the /16 is critical to speeding it up.

best of luck

Re: FHT arduino and HT1632c

PostPosted: Wed May 22, 2013 2:33 am
by guest
in regards to divide by 16, sometimes i dont trust the complier to do what i want it to, so i write it this way instead:

(value >> 4) // same as divide by 16

Re: FHT arduino and HT1632c

PostPosted: Wed May 22, 2013 2:50 am
by tj_thornsberry
Well the REORDER is causing the noise issues, but seems to be working without the REORDER just fine, the WINDOW is making the Band 1 = 14 Band 2 = 13 and so on until around Band 8-9.
The DC level seems to be fine, I did some testing on it, only when it is turned all the way up I am getting the tinniest change. Just to be sure I tested with another stereo, and same issues with the codes. I will see about finding parts of the circuit though just incase.

And yea I now recall I started using the FFT before I decided I didn't want to and just assumed without checking, a bad habit of mine :/. The flickering I mentioned I don't mean its going crazy just seems to turn it all off starting at 0-31 and a high speed, so I assume (again) it is counting with the code, but it also maybe I am not power the HT1632 seperately so when all the lights need power it maybe dimming or shutting off some, not a big deal with low volume testing, just having got around to digging out the cord for it (mainly because my bucket of wires is so freakin tangled).

And I will be sure to change the divide by 16 thing to that.

Re: FHT arduino and HT1632c

PostPosted: Wed May 22, 2013 4:43 am
by guest
can you post up your current code, id like to figure out why REORDER is having the effect youre seeing. how are you testing the frequency response? do you have a sinewave generator that you can sweep through frequencies?

definitely try it with the circuit. if you dont find the exact values, dont worry. any 2 resistors of the same value will do the trick for the DC bias (should be 10k to 100k range, though). the capacitor can be anything from 1uF to 100uF. the 1k resistor and 15nF capacitor are not as critical. they just help roll off frequencies which are out of the sampling range.