Tutorial 2. Creating and Testing FFT code for Visual Studio

If you are using FFT Designer for the first time, it may be helpful to read through this tutorial. The following topics are covered.

  • Creating a 512 point FFT with a Kaiser window function
  • Selecting an output language and Class Name
  • Selecting an Algorithm Type and Options
  • Investigating the dynamic range and selectivity
  • Saving the finished design specification as a file
  • Generating the FFT Source-Code
  • Creating a test project under Microsoft Visual Studio 2008
  • Compiling and Testing the FFT code

1. Creating a 512 point FFT with a Kaiser window function


Starting a new design

Open FFT Designer and select File->New. This resets all design parameters to the default which is a Fixed Precision FFT and Hamming window.


Selecting the FFT Length and Window Type

For this tutorial we want an input length of 512, so select this using the "FFT Input Length" dropdown. Next use the "Window Function" dropdown to select the Kaiser window type.

Note that the α parameter is now enabled as this controls the shape of the Kaiser window type. Have a play around with different values then complete step 1 by entering a value of 2.7 as shown.

2. Selecting an output language and Class Name

First select the output language from the two available options, "Standard ANSI C" and "MSP430 Extended C". If your application requires a floating point FFT or is not for an MSP430 target, you should select "Standard ANSI C". In this tutorial, we are going to create a floating-point FFT for a PC target, so select "Standard ANSI C".


Selecting the Output Language and Class Name

The Class Name or Prefix is prepended to all FFT methods, defines and global variables. This allows you to have any number of different FFT algorithms within a single project or to avoid a conflict with existing code. For this tutorial, we will use the default "MyFFT". Complete this step by entering "MyFFT" onto the "Class Name / Prefix" box as shown.

3. Selecting an Algorithm Type and Options

Depending upon the Output Language, different algorithms and options are available. In our case, we have selected "Standard ANSI C" so an option is available for the floating point precision. For this tutorial, select the "Double Precision" option as this will match the native precision of a modern PC.


4. Investigating the dynamic range and selectivity

Now we are going to check out how the selected FFT and window function perform by using the dual frequency Test Signal Generator (TSG). Using this tool, we can quickly determine whether the FFT will do the job we require.

In this tutorial we will assume that we have a signal source sampled at 256Hz and we need to determine the dominant frequency component. First, we configure the display to represent meaningful frequencies and times. Enter 256 into the "Sample Rate" box and set the Units dropdown to Hz. Also set an initial test frequency of 64Hz by entering 64 into the "F1" box.

The first thing to note is that the "Buffer Fill Time" is 2 seconds. Since the Test Signal Generator outputs a continuous signal, this is not a problem. However, if our signal was not steady-state over the "Buffer Fill Time", the input length, and consequently the frequency resolution, may need to be reduced.

Now adjust the Display Range dropdown to -110dB. You should see one main peak at the F1 test frequency with sidelobes falling away at about 35Hz and 90Hz. The display is effectively showing us that the noise floor of the floating-point algorithm is well below -110dB and that the dominant limitation in close-in amplitude resolution is due to the window function. Try entering an α value of 4.0. The sidelobes will now drop away to -110dB much more quickly, but at the expense of resolution.

At this stage you may like to compare how the fixed point algorithm performs with respect to the floating point one. Testing on an AMD Athlon(tm) 64 X2 Dual Core processor TK-53 1.7GHz running Windows Vista 32-bit gave the following result. A 16384 point Integer DFT calculation took 2ms @ 50% CPU usage whereas the same calculation with Double precision calculation took 4.2ms @ 50% CPU usage. In many applications, either algorithm would be fast enough, so floating point would normally be the best choice.

Note. Tests were carried out using an executable compiled using the default "Release" options rather than "Debug" options and by running the executable outside of Visual Studio. Execution times were calculated from the average of 10,000 calls to MyFFT_Transform() and do not include windowing or data re-ordering.

Now add a second carrier by checking the F2 option and entering 50Hz with a level of -80dB. The second peak should be well defined as shown here and distinct from the first. Using the slider, move the second peak around and observe that the level is perfectly constant except as it approaches DC and Nyquist. This is perfectly normal and inherent in the FFT algorithm.

Using the tool, you can decide which configuration best suits your application. When you are done, go back to Double Precision Floating Point.


Investigating the dynamic range and selectivity

5. Saving the finished design specification as a file

Next click the File menu and select "Save". A standard file dialogue will appear. Save your design, either to the default folder or by browsing to a folder of your choice. FFT Designer remembers the folder where you last saved or loaded a design. Once the FFT design specification has been saved, you can recall it later on, either by double clicking the file, or by opening it from the program menu. Note. When FFT Designer opens, the controls are automatically restored to where you left them so even if you haven't explicitly saved the design, you can close and re-open the program without losing your work.


Saving the finished design specification as a file

6. Generating the FFT Source-Code

Before you click the "Generate FFT Source Code" button, note that FFT Designer will write your source-code to the current "Output Folder". Each file will be prefixed by the "Class Name / Prefix" entered earlier, so you can easily avoid a conflict with existing code. If you wish to check or change the location of this folder, go to the Tools menu and select Preferences. Click the "Output Folder" tab to display the current location and if needed, click the "Change Output Folder" button to select a new location. When you click OK, FFT Designer will remember the new output folder location.

Now click the "Generate FFT Source Code" button. Your source code files will be written to the current "Output Folder". Click "View Output Folder" and confirm that there are 8 files starting with the prefix. In this case "MyFFT".


Generating the FFT Source-Code

7. Creating a test project under Visual Studio 2008

The files generated by FFT Designer include an example as well as a set of test vectors. The test vectors can be used to verify that the Discrete Fourier Transform and window function calculated by the compiled source-code is an exact match of the result calculated and displayed by FFT Designer.

The first step towards verification is to create a project for the compiler we have targeted. In this tutorial, we will be using Microsoft Visual Studio 2008 though other versions should work fine.

Open Microsoft Visual Studio 2008 and click "Create New Project" from the Project menu.


Creating a New Project in Visual Studio 2008

Under Project Types, click "General" and select "Empty Project" from the list. In the lower part of the dialog, enter the project name of MyFFT and select a folder location. In this tutorial, we will create a project folder "MyFFT" on the C: drive.


Creating an "Empty Project" in folder C:\MyFFT

Click OK. An empty project named MyFFT will be created as shown below.

project created

Creating a VS2008 project "MyFFT"

Next we need to move or copy the FFT source code generated by FFT Designer to folder "C:\MyFFT\MyFFT\MyFFT". The easiest way to do this is to go back to FFT Designer and change the Project Output Folder to this location then regenerate the files. To do this, click "Tools -> Preferences" and select the "Output Folder" tab. Click the "Change Output Folder" button and select the "C:\MyFFT\MyFFT\MyFFT" folder as shown below.

change output folder

Once the FFTDesigner output folder has been changed, click "Generate FFT Source Code" to regenerate the FFT source files in the new location.

regenerating FFT source files in new location

Back in Visual Studio, right-click the "Source Files" folder and select "Add Existing Item..." from the menu.


Open the "Add Files" dialog

Select the four files, MyFFT.c, MyFFT_Example, MyFFT_Tables.c and MyFFT_Test_Harness.c as shown and click "Add Files".


Select the four .c Files

In this tutorial we are producing a floating-point algorithm. However, you may wish to try out the Integer (16Bit) algorithm instead at a later date. In order to compile fixed-point algorithms generated by FFT Designer, the fixed-width integer types introduced in C99 are required. These facilitate precise control over arithmetic precision which is required to ensure algorithm performance ties up exactly with the simulation. VS 2008 does not come with the required stdint.h header by default. However, Microsoft have provided one which can be downloaded from http://msinttypes.googlecode.com/svn/trunk/stdint.h. Download this file and save it to the MyFFT project source file folder "C:\MyFFT\MyFFT\MyFFT". You will then be able to compile both floating-point and fixed-point algorithms.

8. Compiling and Testing the FFT code

Select "Build Solution" from the "Build" menu. The process should complete with no errors or warnings as shown.


Build the project

Next open up the MyFFT_Example.c and set up three break-points as shown. The first two will trigger if the calculated FFT result does not exactly match the simulation. The last break-point will trigger when the test is complete.


Set Break Points

Finally click the "Start Debugging" icon (green triangle) to run the program. The program should break on the final while(1) indicating that the result calculated by your Visual Studio project exactly matches the result simulated by FFT Designer. Note. If you are generating floating-point code from FFT Designer, you should select a floating-point precision that matches the native precision of the target platform. In the case of a modern PC, this will be Double Precision. However, on a 16 bit microcontroller that does not have floating-point hardware acceleration, you may wish to select 32 Bit precision in order to trade improved computation time for reduced precision. In either case, if you do find that the calculated result does not exactly match the simulation, you should try changing the floating-point precision.


Run the simulation

9. Tutorial Complete

That's it! You have completed the tutorial in which you ....
  • Selected your FFT algorithm and window function
  • Simulated and analysed the performance
  • Generated the source code and test harness
  • Compiled it using Visual Studio 2008
  • Verified that the FFT performance matches the simulation