Difference between revisions of "PIC32MX: FFT of Analog Input"

From Mech
Jump to navigationJump to search
Line 22: Line 22:
The remaining connection is not essential for FFT caclulation but allows the user to monitor the calculation time. Pin A14 goes high during calculation and goes back low when the calculation is completed. This feature lets the calculation time be monitored on an oscilloscope.
The remaining connection is not essential for FFT caclulation but allows the user to monitor the calculation time. Pin A14 goes high during calculation and goes back low when the calculation is completed. This feature lets the calculation time be monitored on an oscilloscope.


The circuit diagram to the right illustrates the signal generator to PIC connection, the RS-232 connections, and the optional oscilloscope connection.
The following circuit diagram illustrates the signal generator to PIC connection, the RS-232 connections, and the optional oscilloscope connection.


== Code ==
== Code ==

Revision as of 22:00, 10 February 2010

Original Assignment

Do not erase this section!

Your assignment is to read in several cycles of a periodic signal from a function generator (e.g., a sine wave or a square wave), perform an FFT on your samples using your PIC32, and send back the results to a PC to display. Try this for 256 samples and 1024 samples, and determine how long it takes to compute the FFT for each, once you have collected the data. Verify that the results make sense according to the input signals you've used; for example, a sine wave should have all power at the chosen frequency, while a square wave will have much of the power at the frequency of the wave, but power at other frequencies, too.) Check out the MPLAB C32 Libraries manual to learn more about the dsplib_dsp.h, which you must include. Try both mips_fft16 and mips_fft32.

Overview

A fast Fourier transform (FFT) is a method to calculate a discrete Fourier transform (DFT). More information about FFTs and DFTs can be found on wikipedia (linked). The following circuit and code allow a user to put a signal into a PIC 32, perform an FFT on that signal, output the data to Matlab via RS-232, and view a plot showing the raw signal, the FFT as calculated by the PIC, and the FFT as calculated by Matlab. Viewing the Matlab calculation is for verification only and can be commented out of the Matlab code.


Commented C code and Matlab code are provided at the bottom of this page.

Circuit

Beyond simple inputs and outputs, there is no special circuitry required for computing FFTs. The signal input is inserted into pin B4 on the PIC and MUST BE BELOW 3.3 VOLTS. For data output, the PIC uses RS-232 communication. Three connections need to be made with the RS-232:

  • Orange RS-232 wire to pin F4 on the PIC
  • Yellow RS-232 wire to pin F5 on the PIC
  • Black RS-232 wire to ground

The remaining connection is not essential for FFT caclulation but allows the user to monitor the calculation time. Pin A14 goes high during calculation and goes back low when the calculation is completed. This feature lets the calculation time be monitored on an oscilloscope.

The following circuit diagram illustrates the signal generator to PIC connection, the RS-232 connections, and the optional oscilloscope connection.

Code

Where possible, make it a single piece of well-commented cut-and-pastable code, or at least make each function that way, so others can easily copy it. Most comments should be in the code itself; outside the code (on the wiki) should only be explanatory comments that are too cumbersome to include in the code.