Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1448

Module for direclty using FFTW in VB6

$
0
0
As you probably remember, I created a DLL file, using ASM code (which you can find here http://www.vbforums.com/showthread.p...libfftw3-3-dll), that had STDCALL functions, that called the CDECL functions in the FFTW DLL file. However, that requires distributing one more dependency with programs that use FFTW. I found a way, using Windows API calls in a standard VB6 module, to directly call all the CDECL functions from within VB6. The file is called FFTW3.bas (as it is designed to work with version 3 of FFTW, which is the latest version). Below is a link to the code for that module on PasteBin (sorry, it's too many characters to paste on VBForums, as it goes over the 15000 character limit).

http://pastebin.com/cnJw8mB1

The first thing you want to do when your program starts is call InitFFTW.
The way that InitFFTW works is, it first uses LoadLibrary to load libfftw3-3.dll into memory, and get a handle to it. It then uses a series of calls to the GetProcAddress function, to get the addresses of all the functions that are most important in FFTW (there's a few that the DLL file has that are much more advanced, but 99% of the time you won't be needing them, so they aren't included in this module).

When your program closes, the last action your program takes should be to call CloseFFTW. This empties the collection that has stored the addresses for all the functions, and then frees the DLL for FFTW itself by calling FreeLibrary.


Now after you have called InitFFTW, you can use any of the other public functions in this module. The first one you are going to need to use is one of the Plan functions. This generates what the makers of FFTW call a "plan", which is an opaque structure that has all the stuff setup in memory needed to perform a particular type of timedomain2frequencydomain transform (or frequencydomain2timedomain transform). The value returned from such a Plan function is a handle to the plan. After the plan is set up, you can use it with either the initial data source and destination that was set up at the time of making the plan (via the FFTWExecute method), or use it on data sources or destinations other than those that were used at the time the plan was created (via one of the following methods, FFTWExecuteDFT, FFTWExecuteDFTR2C, FFTWExecuteDFTC2R, or FFTWExecuteR2R).

After you are done with executing the transform, if you don't need to use it again, you can call FFTWDestroyPlan. This will allow you to free the memory occupied by that plan. However, it doesn't get rid everything that was created when the plan was created. When the plan was created, something else called "wisdom" was also create. This is additional data that is created when the plan was created, which can optimize the internal workings of FFTW, so that future calls to plan creation functions can be speed up. The more plans you create, the more "wisdom" gets created, so use it, the faster it is able to operate when creating additional plans, because the more it is able to optimize its internal workings. It's a sort of "machine learning" algorithm that they seem to have implemented. However, if you want to clear out all the memory that got allocated by the use of FFTW (including all plans, and all created wisdom), without actually freeing the entire DLL file from memory, then you can use FFTWCleanup.

Note that, except for InitFFTW and CloseFFTW, all of the public methods in this module actually don't directly make a call to libfftw3-3.dll. Instead they make a call to a private function in my module called CallCDECL, which in turn uses the Windows API function called DispCallFunc to actually call the CDECL functions in the FFTW DLL file.

Viewing all articles
Browse latest Browse all 1448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>