Sound spectrum analyzer in a AVR ?

Last post
28 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey
I'm very soon going to play with a big LED matrix I'm about to build...
And was thinking, if it is possible to use a AVR like mega32 or 64 to analyze a sinus wave from a mic. and turn it in to something I could show on the matrix...like the spectrum analyze thing in Winamp.. ??

Have any of you guys done this before ?

//B4Me

uC's: Atmega16, 32, 64, 128 and Attiny13
Lang.: C
Interests: Small scale robots AND sensor monitoring system

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can do an FFT but its sort of slow. How many updates per sec you want to see? 3? 4? I've been trying to do something like this.... for 'hi fi' you might want 20-20k... 40khz sampling freq.... cant quite get there with the internal a/d, even in 8 bit mode. Might could at 20mhz, but then you need to sample for at least one period of the lowest freq you want... 20hz... 50ms.... So you need to be able to grab 2000 samples... ok on a mega128, but not so good on a mega32. I was trying to do a simulation of the 3 opamp BP filter, and change the filter freq to step up in octaves, and resample the inputs for ea freq and post the avged ampl in db. It gave about a band per sec... dink dink dink dink.... needs some speedup tricks.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Instead of a FFT you could use a bandpass filter for each band. A single biquad isn't too complicated but isn't steep enough, so you would need to cascade a few. For each band you want. Involves a lot of multiplications and additions.

See here what a digital biquad filter is.

Maybe an 8 or 16 point FFT is faster.

I think it's not possible to reach a reasonable refresh rate.

Let's do some maths:

A sample is needed every 1/40Khz=25uS. All the filters need to be updated in this time. At 16Mhz each instruction takes 62.5ns. So the time available is 400 instructions before the next sample is due. At 10 bands that leaves 40 instructions to do each band. Then you still need to sample, average and multiplex.

Maybe use 'real old-fashioned' analogue electronics?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Maybe use 'real old-fashioned' analogue electronics?

Nah, it will never catch on.

Quote:

A sample is needed every 1/40Khz=25uS.

Agreed; let's start with that.

Quote:

All the filters need to be updated in this time.

Now here I don't necessarily agree. Even if one got all this "stuff", where are you going to put it and what are you going to do with it?

I'd guess that for most purposes it is like displaying a value on an LCD. No matter how fast you sample and recalculate it makes no sense to do more than a few re-displays of the now value each second. As a speculation on this app, perhaps 10 updates per second or so would be fine?

Lee

You can put lipstick on a pig, but it is still a pig.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Now here I don't necessarily agree. Even if one got all this "stuff", where are you going to put it and what are you going to do with it?

Average the outputs of the filters over a display frame period :)

I don't think this device will be a certified and calibrated scientific instrument, so maybe a few shortcuts could be made. Likely only the visual effect counts.

I don't know how many input samples for a filter are needed for a reasonable output. You need some kind of windowing to prevent all kinds of artifacts.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is a slightly better link:

http://elm-chan.org/works/akilcd/report_e.html

From this page I would conclude that the FFT soluton gives some usefull results.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My mega128 will do 128 sample FFTs really fast, but 1024 or 2048 point FFTs are sloooow, so you could do a 'speech band' FFT real time no prob. (I got the MixFFT pc example stuff to compile on the mega128)

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http:/

JChristoff
Illinois

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay :D
Thanks for the many replyes.

uC's: Atmega16, 32, 64, 128 and Attiny13
Lang.: C
Interests: Small scale robots AND sensor monitoring system

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Maybe use 'real old-fashioned' analogue electronics?

How about go further back that analogue electronics.

I play drums, when a frequency for example on a bass guitar is at the same resonent frequency of the snares on my snare drum , it vibrates big time. Suppose you use wires tuned to vibrate at the specific frequencies you are looking for and measure the vibration of all the wires? Perhaps connect the wires to a piezo device to give you a the amplitude of the vibration as an sine wave.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think that's exactly what the original Fourier frequency analyzer was... a bunch of tuned strings

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hehe ;)

plz correct me if I'm wrong, but all I have to do is like .... sample at ex. 1kHz...
and if I will se how much 500 Hz there is.. I shall just take every second sample and divide the sum with the number of samples ?

uC's: Atmega16, 32, 64, 128 and Attiny13
Lang.: C
Interests: Small scale robots AND sensor monitoring system

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you might be over simplifying things a bit... Have you thought about what would happen if "every second sample" happened to coincide with the zero crossings? You'd be led to believe that there was "not much" 500Hz there.
If it was that easy, why would anyone bother with Fourier?
(You did say "plz correct me if I'm wrong)

Act now, think later.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can make a freq analyzer using hw or sw. Prev msg suggested a bank of analog filters, just scan the outputs and draw graph in dB. My approach was to fill buf with samples, run thru one filter at 20hz, plot db of avg filter out. Set filter to 40 hz, run same samples thru this filter. Repeat for 80,160,320,640,1280,2500,5K,10K,20K, you're done. Except the filter only works up to about 1/6 the samp freq, so to get the 10k filter to work, I need to sample at 60k... thats a prob.... there are some articles that speak of 'double sampling', but that slows it down, and its already pretty slow. Then there's the FFT approach... take samples at twice the highest freq of interest for one period of the lowest freq of interest then start crunching. Go out and get a cup of coffee... there's you spectra! But they are all linearly spaced, and you probably want to see 20-20k log spaced... well, do you take the avg of a bunch of the spectral magnitudes, or the magnitude of the average of a bunch of spectra? (2nd one I think).

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

B4me:

No that would not work. Imagine every sample is say +42, this is clearly DC, no 500Hz sine at all, and your calculation is nonsense.

If you are interested in single frequencies, you could speed things up by doing DCT for just that frequency, but for a full band, FFT is a high speed special case of DCT.

- Jani

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Uh can see I was wrong :P
bobgardner:
I would make this so I have ... ex. 10 rows of LEDs, and they should show all freq. from 20~100 Hz to 20 kHz...
I don't know the correct name for that ?

And it would be best if I don't have to have a lot of HW... (little space on the PCB)

uC's: Atmega16, 32, 64, 128 and Attiny13
Lang.: C
Interests: Small scale robots AND sensor monitoring system

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Spectrum analyzer with octave spaced bands, as usually the center frequency is the twice the previous one.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so like ?
20, 40, 80, 160 and so on ?

uC's: Atmega16, 32, 64, 128 and Attiny13
Lang.: C
Interests: Small scale robots AND sensor monitoring system

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay :D
But can any of you in a short way tell me HOW FFT works ?
cause I just don't get it :(

uC's: Atmega16, 32, 64, 128 and Attiny13
Lang.: C
Interests: Small scale robots AND sensor monitoring system

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://en.wikipedia.org/wiki/Fft is a starting point.

You can treat a FFT as a black box, something goes in, something comes out, how it works isn't important really.

There are enough ready-to-use FFT packages on the net I think, but FFTs are quite math intense, and an AVR is simply not powerful enough for (and not meant to) these number crunching applications.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay.. but it can be done ? or is it not "real" FFT this guy is using ?
http://elm-chan.org/works/akilcd/report_e.html

uC's: Atmega16, 32, 64, 128 and Attiny13
Lang.: C
Interests: Small scale robots AND sensor monitoring system

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you watch the movie files? I'd say that was pretty conclusive proof that it CAN be done!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah I saw em :D
And I'm very impressed.

uC's: Atmega16, 32, 64, 128 and Attiny13
Lang.: C
Interests: Small scale robots AND sensor monitoring system

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

B4Me wrote:
Okay.. but it can be done ? or is it not "real" FFT this guy is using ?
http://elm-chan.org/works/akilcd/report_e.html

Can you read German?
Check this link.
http://www.mikrocontroller.net/topic/27001

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

RES wrote:

Can you read German?

Hmm I've learned it once in the school (Denmark), but thats almost 4 years ago :P
thx for the link :D

uC's: Atmega16, 32, 64, 128 and Attiny13
Lang.: C
Interests: Small scale robots AND sensor monitoring system

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've read the link, or done my best to read some of it (http://www.mikrocontroller.net/topic/27001)
And a little on Wiki about FFT and DFT...
And I can see it can be done with almost any mega i know (8,16,32,64,128). And the result on mikrocontroller.net (the AVI's) looks extremely nice... but I can see that I need to learn some asm. before I can do this ?
And I still not getting the... How they do it...

Think I need to get the LED matrix finish before I can begin to TRY to make some FFT code.....
//B4Me

uC's: Atmega16, 32, 64, 128 and Attiny13
Lang.: C
Interests: Small scale robots AND sensor monitoring system