Fun with FFT on Arduino

Some posts in the Arduino forum got me interested in playing around with the fix_fft library which allows you to do a fixed-point FFT. I started out with a simple sketch which generated a waveform consisting of a single tone, passing that to the FFT function and then drawing the resulting spectrum. I then started adding other waveforms and plotting their spectrum. Then I started looking at the FFT code itself and found that the Sine lookup table can be reduced in size with only a few extra if statements. The table is small enough that it can probably be left in SRAM instead of using PROGMEM.

Here is the result of that messing around. Perhaps you'll find something useful or, at least, interesting here.

The sketch (for IDE V1.0) is here and my version of the fix_fft library is here and here. Make a directory named fft in your Arduino library directory and put fix_fft.cpp and fix_fft.h in there. The sketch runs on my Arduino Duemilanove and should run on most other Arduinos.

NOTE that in each spectrum (below) the "signals" with amplitudes at levels zero and one at the bottom of each diagram are just noise due to round off errors in the computation of the FFT and in the generation of the signal itself.

1. In the first example I have generated just a single 1280Hz tone. At a sampling frequency of 5120Hz each of the 128 "bins" in the output of the FFT represents 40Hz (5120/128) and so the 1280Hz tone will fall in bin 32 (1280=32*40). What the FFT has done is convert the original signal from the time domain into the frequency domain and it shows that the signal consists of a single frequency (as it should).

1. Single 1280Hz tone sampled at 5120Hz

 32                                 *                               
 31                                 *                               
 30                                 *                               
 29                                 *                               
 28                                 *                               
 27                                 *                               
 26                                 *                               
 25                                 *                               
 24                                 *                               
 23                                 *                               
 22                                 *                               
 21                                 *                               
 20                                 *                               
 19                                 *                               
 18                                 *                               
 17                                 *                               
 16                                 *                               
 15                                 *                               
 14                                 *                               
 13                                 *                               
 12                                 *                               
 11                                 *                               
 10                                 *                               
  9                                 *                               
  8                                 *                               
  7                                 *                               
  6                                 *                               
  5                                 *                               
  4                                 *                               
  3                                 *                               
  2                                 *                               
  1                                 *                              *
  0 ****************************************************************
    0123456789012345678901234567890123456789012345678901234567890123
              111111111122222222223333333333444444444455555555556666

2. In this example, two tones of 1280Hz and 1040Hz were added together. This produces two spikes, one in bin 32 (1280Hz) and the other in bin 26 (1040Hz). The 1040Hz tone was generated with an amplitude half that of the 1280Hz tone and this is reflected in the spectrum which shows the 1040Hz amplitude is roughly half that of the 1280Hz signal (the spectrum doesn't show it being exactly half because of round off errors and other computation problems with the FFT).

2. Two tones of 1040Hz and 1280Hz sampled at 5120Hz

 32                                 *                               
 31                                 *                               
 30                                 *                               
 29                                 *                               
 28                                 *                               
 27                                 *                               
 26                                 *                               
 25                                 *                               
 24                                 *                               
 23                                 *                               
 22                                 *                               
 21                                 *                               
 20                                 *                               
 19                                 *                               
 18                                 *                               
 17                           *     *                               
 16                           *     *                               
 15                           *     *                               
 14                           *     *                               
 13                           *     *                               
 12                           *     *                               
 11                           *     *                               
 10                           *     *                               
  9                           *     *                               
  8                           *     *                               
  7                           *     *                               
  6                           *     *                               
  5                           *     *                               
  4                           *     *                               
  3                           *     *                               
  2       *                   *     *     *                         
  1   *   *   *       *     * *     * *   *   *       *     * *   **
  0 ****************************************************************
    0123456789012345678901234567890123456789012345678901234567890123
              111111111122222222223333333333444444444455555555556666

3. In the previous example we simply added the two signals together. But in this example we have a "carrier" signal at 1280Hz which is multiplied (modulated) by an "audio" signal of 240Hz. The original carrier wave has an amplitude of +/-68 and this is modulated by the audio signal whose amplitude is +/-32 so that the final signal has a maximum amplitude of +/-100.

This time instead of getting one or two large frequency spikes we find that there is one large spike at the carrier frequency in bin 32 with two smaller spikes each 6 bins to either side. The two smaller spikes are called the lower sideband (at bin 26) and the upper sideband (bin 38). They are 6 bins on either side of the carrier because 6 bins corresponds to a frequency of 240Hz (6*40) which is exactly the modulating frequency we used.

So, amplitude modulating the 1280Hz carrier with a 240Hz signal results in the 1280Hz carrier plus a lower sideband signal at 1040Hz (1280-240) and an upper sideband signal at 1520Hz (1280+240). The bandwidth of this signal is 480Hz, twice the modulating frequency, because of the sidebands. An important implication of this is that you cannot alter the amplitude of a signal without also altering its frequency.

3. AM modulation - 1280Hz carrier modulated by 240Hz audio tone

 32                                 *                               
 31                                 *                               
 30                                 *                               
 29                                 *                               
 28                                 *                               
 27                                 *                               
 26                                 *                               
 25                                 *                               
 24                                 *                               
 23                                 *                               
 22                                 *                               
 21                                 *                               
 20                                 *                               
 19                                 *                               
 18                                 *                               
 17                                 *                               
 16                                 *                               
 15                                 *                               
 14                                 *                               
 13                                 *                               
 12                                 *                               
 11                                 *                               
 10                                 *                               
  9                           *     *     *                         
  8                           *     *     *                         
  7                           *     *     *                         
  6                           *     *     *                         
  5                           *     *     *                         
  4                           *     *     *                         
  3                           *     *     *                         
  2                           *     *     *                         
  1                           *   * * *   *                   *   **
  0 ****************************************************************
    0123456789012345678901234567890123456789012345678901234567890123
              111111111122222222223333333333444444444455555555556666

4. In general, the spectrum of an FM modulated signal is much more complex than one that is Amplitude Modulated. When we modulate the FM carrier we can choose how much the input signal changes the frequency of the carrier and so we would expect that the number of sidebands produced by FM would depend heavily upon how large a "deviation" we choose.

In this first example of FM, the deviation has been chosen to equal that of the modulation. We still have a carrier frequency in bin 32 (1280Hz) but its amplitude is somewhat lower than we had in the AM example even though the amplitude of the carrier signal in both cases was set to 68. The spectrum also shows that we still have the sideband frequencies at 1040 (bin 26) and 1520Hz (bin 38) but their amplitude is considerably greater than in the case of AM.

However, we also see that there are two more sideband signals at 800Hz (bin 20) and 1760Hz (bin 44). There is probably some energy in the next two sidebands in bins 14 and 50 as well but it is too small to distinguish from the noise. Notice too that each sideband is separated by 240Hz from either of its neighbours. It is obviously no coincidence that this is the frequency of the modulating signal.

This FM signal has a wider bandwidth than the AM signal in the previous example because it stretches from at least 800Hz up to 1760Hz giving a bandwidth of at least 960Hz or four times the modulating frequency.

4. FM modulation - 1280Hz carrier modulated by 240Hz audio tone with deviation of 240Hz

 25                                 *                               
 24                                 *                               
 23                                 *                               
 22                                 *                               
 21                                 *                               
 20                                 *                               
 19                                 *                               
 18                                 *                               
 17                                 *                               
 16                                 *                               
 15                                 *     *                         
 14                           *     *     *                         
 13                           *     *     *                         
 12                           *     *     *                         
 11                           *     *     *                         
 10                           *     *     *                         
  9                           *     *     *                         
  8                           *     *     *                         
  7                           *     *     *                         
  6                           *     *     *                         
  5                           *     *     *                         
  4                           *     *     *                         
  3                     *     *     *     *     *                   
  2                     *     *     *     *     *                   
  1               *   * * *   *   * *     *     * *****   *   *   **
  0 ****************************************************************
    0123456789012345678901234567890123456789012345678901234567890123
              111111111122222222223333333333444444444455555555556666

5. If we now increase the deviation to 480Hz the first thing to notice is that although the carrier is still there, it is much lower than some of the sidebands. This is actually a good thing because the carrier itself contains no information. All the information (i.e. the modulating signal) is carried in the sidebands (this is also true of AM signals) so it makes sense to have as much of the transmitter power as possible devoted to propagating the information rather than the carrier.

In this example the pairs of sidebands in bins 38 and 26, and bins 44 and 20 are still there but now the pair of sidebands in bins 50 and 14 are also clearly visible in the spectrum. The bandwidth of this signal is 1080Hz.

As an exercise, change the deviation in this example to four times the baseband signal i.e. change it to 960Hz and run the sketch again. You should get the spectrum labelled 5a which is shown at the bottom of this document. It will show sidebands extending out to the edges of the diagram in bins 2 and 62. The higher the deviation, the greater the bandwidth of an FM signal.

5. FM modulation - 1280Hz carrier modulated by 240Hz audio tone with deviation of 480Hz

 20                                       *                         
 19                           *           *                         
 18                           *           *                         
 17                           *           *                         
 16                           *           *                         
 15                           *           *                         
 14                           *           *                         
 13                           *           *                         
 12                           *           *                         
 11                           *           *     *                   
 10                     *     *           *     *                   
  9                     *     *           *     *                   
  8                     *     *           *     *                   
  7                     *     *     *     *     *                   
  6                     *     *     *     *     *                   
  5                     *     *     *     *     *     *             
  4               *     *     *     *     *     *     *             
  3               *     *     *     *     *     *     *             
  2               *     *     *     *     *     *     *             
  1         *     *   * * *   *     *     *     * *   *   * * *   **
  0 ****************************************************************
    0123456789012345678901234567890123456789012345678901234567890123
              111111111122222222223333333333444444444455555555556666

6. In this last case the deviation is set to 120Hz so that it is only half that of the modulating signal. If you compare this spectrum to that of the AM signal you will see that their bandwidths are essentially the same. When the deviation is much less than that of the modulating frequency, the modulation is called narrow band FM. This is what is used in GMRS and FRS radios because it means that the spectrum allocated to those radios can hold more channels than they could if a higher deviation were used. On the other hand, a signal which has a very high deviation compared to the modulating signal is called wide band FM and is what is used in commercial broadcast FM stations.

6. FM modulation - 1280Hz carrier modulated by 240Hz audio tone with deviation of 120Hz

 30                                 *                               
 29                                 *                               
 28                                 *                               
 27                                 *                               
 26                                 *                               
 25                                 *                               
 24                                 *                               
 23                                 *                               
 22                                 *                               
 21                                 *                               
 20                                 *                               
 19                                 *                               
 18                                 *                               
 17                                 *                               
 16                                 *                               
 15                                 *                               
 14                                 *                               
 13                                 *                               
 12                                 *                               
 11                                 *                               
 10                                 *                               
  9                                 *                               
  8                                 *     *                         
  7                           *     *     *                         
  6                           *     *     *                         
  5                           *     *     *                         
  4                           *     *     *                         
  3                           *     *     *                         
  2                           *     *     *                         
  1       *               *   *     *     *               *   * *   
  0 ****************************************************************
    0123456789012345678901234567890123456789012345678901234567890123
              111111111122222222223333333333444444444455555555556666
5a. FM modulation - 1280Hz carrier modulated by 240Hz audio tone with deviation of 960Hz

 15                                                   *             
 14               *                                   *             
 13               *                 *                 *             
 12               *     *           *           *     *             
 11               *     *           *           *     *             
 10               *     *           *           *     *             
  9         *     *     *           *           *     *     *       
  8         *     *     *           *           *     *     *       
  7         *     *     *           *           *     *     *       
  6         *     *     *           *           *     *     *       
  5         *     *     *           *           *     *     *     * 
  4   *     *     *     *           *           *     *     *     * 
  3   *     *     *     *           *           *     *     *     * 
  2   *     *     *     *     *     *     *     *     *     *     * 
  1 * * * * * * * *   * *   * *     * *   *   * *     *     *   * * 
  0 ****************************************************************
    0123456789012345678901234567890123456789012345678901234567890123
              111111111122222222223333333333444444444455555555556666

 

 

 

If you have any comments, send them to:

 

 This Web Page Created with PageBreeze Free Website Builder