FFT LED Spectrum analyzer on PIC32

Discussion in 'Embedded Systems and Microcontrollers' started by stefan646, Dec 16, 2011.

  1. stefan646

    Thread Starter New Member

    Dec 16, 2011
    28
    0
    Hello! My name is Steven and i am from Romania (so excuze my english).
    I am beginner on programing microcontrolelr but i want that this project to be my initiation on PIC programing.

    I want to build a spectrum analyzer with 26x46 led matrix, controlled by an chipkit from Digilentinc.
    So my question are:
    1) how many FFT have to make the chipKIT to give me 46 bandpass? I read on diffrent places that have to be:" bandpass x2 FFT " to have a good sample....so 46 or 92 FFT?
    2)i alsow read on diffrent places that some tipes of chips can't make FFT on a frequency highter that 10khz...i want to build a 20hz to 20khz frequency range, is that posible?
    3)is a chipKIT fast enought to make this many FFT and witch is the maximum frequency that it can make FFT to have a good reflesh rate on the leds?
    4) wich kit i should choose?(i am attending to participate to a contes where i get extra points for buyng the chipest kit) chipKIT Uno32 or chipKIT max32? I think that the extra ports are not considerably due to the fact that i can use multiplexing to control my led, but my question is if the extra RAM 128k vs 16k will help the chip calculate the FFT more faster (that the human eye cand observe on the led matrix)?
    5)can someone write down for me the pseudocode to make this spectrum analyzer, so i can understand the working principe of this, and hopefully that will help me to write my own code.

    If is not allowed to specific the company name on this forum, i will rephrase my post.

    Thank you very much!!
     
    Last edited: Dec 16, 2011
  2. MrChips

    Moderator

    Oct 2, 2009
    12,411
    3,353
    Firstly, you need to understand what is the FFT and the sampling theorem, also called the Nyquist Theorem. Let us forget about the PIC chip for now.

    You only need one FFT. The FFT computes the entire frequency spectrum of the time signal.

    This has nothing to do with the chip but everything to do with how fast you sample the waveform. The Nyquist Theorem says that you must sample at least twice the highest input frequency. Hence if you want to measure up to 20kHz, you must sample at least 40kHz or higher. (That is why standard digital audio sampling rate is 44.1kHz). The corollary of the sampling theorem states that there must be no signal whose frequency is greater that one-half the sampling rate. In practice this means that you must use a low-pass filter that cuts out anything above 20kHz. This is called an anti-aliasing filter.

    This brings us to the capabilities of the chip. The chip must be capable of sampling the waveform at 40kHz or higher. Secondly, the actual FFT calculation is very math intensive. Does the chip have enough speed and memory capacity to perform the FFT in a short enough time?

    I am not aware of any chipKIT that meets the required performance to calculate the FFT in real time.

    Writing your own FFT program is not an easy task. Code is already available to do this.
     
  3. stefan646

    Thread Starter New Member

    Dec 16, 2011
    28
    0
    1)I just learn about FFT at school but it think is difrent from the implementation on microcontroller
    2)I get it....i missunderstood this from other sites
    3)I don't understand that: if i want to measure max 20Khz from an audio input from my PC, i must inplement a FFT that does sampling a 40khz signal....in this case why i have to make a filtre that cuts anything above 20 and no 40 khz?
    4)chipKIT Uno32 :Microchip® PIC32MX320F128 processor
    80 Mhz 32-bit MIPS
    128K Flash, 16K SRAM
    42 available I/O


    chipKIT Max32: Microchip® PIC32MX795F512 processor
    80 Mhz 32-bit MIPS
    512K Flash, 128K RAM
    83 available I/O

    5)I am not attending to write my own FFT code (there are library for it), i just want to understand the whole procese: how i "show" on the led the amplitude of eatch bandpass
     
  4. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
  5. MrChips

    Moderator

    Oct 2, 2009
    12,411
    3,353
    The FFT does not digitize the signal. Digitization and FFT are two different things.
    Digitization is what your analog-to-digital converter does.

    To measure a 20kHz signal you must digitize at 40kHz or higher. No ifs, ands or buts! That is the rule.

    Secondly, you must remove all frequencies above 20kHz. No ifs, ands or buts! That is the rule. If you do not do this your data will be corrupted with false data.

    That PIC is a fast chip and may be able to do it. With 16K or 128K RAM you should have enough memory space to hold the data and do the FFT in place. For 20Hz low end you only need to sample for about 25ms at a time. That would speed up the FFT. For 100ms at 40kHz you only need 4K RAM space for the data buffer.

    So if you want speed, sample 1024 data points at 48kHz or something around that rate and do a 1024-point FFT.
     
  6. stefan646

    Thread Starter New Member

    Dec 16, 2011
    28
    0
    thatoneguy: yes that's the chip


    1)MrChips i belive you, but i just want to understand entirely. I think i misunderstand something. Let me restate: from my pc is going out a audio signal abouth 40khz witch is applied in my PIC where a FFT measure a 20khz sample. If i put a filter in the fron of the PIC the FFT will measure less. Where do i get wrong?

    2) To many information in such a few lines :)) (sorry abouth that but i understand harder beacause english is not my mother language)
    So: at 20khz the FFT samples the signal once at every 25ms...i am right? after that you lost me :(
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,411
    3,353
    No. The FFT does not sample the waveform. The ADC does the sampling.

    1) From somewhere (it does not have to be your PC, it can be from a MP3 player) you generate music.

    2) You feed the music into a low-pass filter that cuts off everything above 20kHz.

    3) On your PIC you digitize the wave taking one sample every 25us.

    4) You do this 1024 times. That is you collect 1024 data points.

    This has nothing to do with FFT!
    Got that so far?
     
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
  9. stefan646

    Thread Starter New Member

    Dec 16, 2011
    28
    0
     
  10. RRITESH KAKKAR

    Senior Member

    Jun 29, 2010
    2,831
    89
    Thats very good implementation of FFT in practical even i like this thread. carry on
     
  11. MrChips

    Moderator

    Oct 2, 2009
    12,411
    3,353
    After you have acquired 1024 data points, you call the FFT program.
    The results will be 1024 points in frequency space.
    How you interpret the results is another matter.
     
  12. RRITESH KAKKAR

    Senior Member

    Jun 29, 2010
    2,831
    89
    It surprise when X(n) is converted to X(k) in freq domain without any impulse response...
     
  13. BillO

    Well-Known Member

    Nov 24, 2008
    985
    136
    If this is a spectrum analyzer, does it actually have to be real time?

    If the display was updated twice a second and was maybe a half second out of phase, would this be a problem? I'm assuming, because of the display requirements, this is going to be viewed by people and not be a real-time input to some other process. Given that, a high end 32 bit MCU shoud be able to handle this.

    I do have to agree with Chips on the sample frequency. If your doing spectrum analysis via FFT, 40 KHz would be the absolute minimum sample rate for a 20KHz bandwidth. Even then, you will have significant error. So again, what is the specific application? Will this be just a toy, or is it going to be used to calibrate professional recording environments?

    BTW, why filter out the higher frequencies? Because FFT will present you with some awesome errors otherwise. If the sample frequency is too low there will be an enormous amount of harmonic distortion. Think about it. If you try to digitize a 20KHz signal at a 20KHz rate, you will basically record sine waves as square waves. That will introduce a lot of erroneous harmonics that the FFT will pick up on and feed into you results.
     
  14. BillO

    Well-Known Member

    Nov 24, 2008
    985
    136
    This is not entirely true. If you were to sample a 1Hz signal for 1 Second at 1024 SPS, and do an FFT on the result, you should have a rather nice approximation of a delta function at 1Hz. Interpretation should be trivial.:D
     
  15. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
  16. RRITESH KAKKAR

    Senior Member

    Jun 29, 2010
    2,831
    89
    That was a good video.....
     
  17. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  18. stefan646

    Thread Starter New Member

    Dec 16, 2011
    28
    0
    Now i gett it....first i understand that if i want to measure a 20khz semnal i must digitalize a semnal of at least 40 khz......now i understand that if i want to measure a 20khz semnal i must take sample from that signal on a freq >40khz.....now i understand whay i must use a filter

    But that's raising a new question: what does really means i sample a signal at a 40khz freq? ...that i take one sample at 25X10*-6 seconds?(divide 1 second to 40k) ??

    thatoneguy: in fact i want something more like this http://www.youtube.com/watch?v=qHU_TGbBNWY, but bigger 26X46 leds

    BillO: i want to be as closser i can get to real time, that is why i use a led matrix display, to have a good reflesh rate
    http://www.youtube.com/watch?v=yoquqEL8OCc&context=C3153f84ADOEgsToPDskIpIJX1LJ-b-jiwF8wxHz0V
    this is a movie make with a chiKit Max32 (the one with 128k RAM), and it looks pretty close to real time for me :D

    I want to build this for myself, but i was thinking that if i invest so much money(1250 LEDs) an time in to this project, why should i not participate to a contest with this project. So until next week(23 Dec) i must submit a document where i must present a short breef of my project and where i must say which hardware i will use (in my case chipKit uno32 or Max32-i get extra point for chousing the chepest).
    So let's talk about them for a second:
    1)uno has 128k flash memory, max has 512k -this is irelevant due the fact that i think the code(plus some extra code to show a short text on the matrix) will not exced 128k (i am right?)
    2)uno has 42 I/O ports an max has 83 I/O ports-i think this is irevenat too, due the fact on uno i cand use multiplexing to control the leds(i am right here too?)
    3)this is where the major diference of the kits apears on this project: uno has 16kSRAM and max has 128k RAM. Is 16k enought for me or i must chose the 128k chip?

    Another problem is that this chip is programed on a Arduino like IDE, and the arduino FFT library breaks the audio signal in 64 frequency bands....if i was planyng to do only a 32 bandpass i simply "show" on the matrix 2 freq/bandpass....but i want to use 46 bandpass, that meens i will have data loss...what should i do?
     
  19. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Remember real time in this case will be with the human eye as the main sensor. And the eye is quite "slow" 24 frames per second is a Worldwide standard for movie theater film projectors. And that is enough to make it appear live. So anything around 20 to 50 updates pr second will give a realtime feeling
     
  20. stefan646

    Thread Starter New Member

    Dec 16, 2011
    28
    0
    Ok...got that :D

    Another thing: "In order to correctly sample the signal we have to do two things. Firstly we need to amplify the signal to ensure we can use as much of the 0-5V range as possible. Secondly we have to move the signal's ground (of 0 volts) to a 'virtual ground' of 2.5Vs. This will allow the PIC to sample both the positive and the negative sides of the input signal. To do this the demonstration board uses a simple amplifier IC (the LM386-1). "

    In my case the range is 0-3.3V.
    I must do the same thing?
     
Loading...