473,760 Members | 8,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fast Fourier Transform (FFT) in VB .Net - Please Help

Hi, I have a FFT routine that I converted from C++ to VB in a module
as follows:

Const M_PI = 3.1415926535897 931

' Fast Fourier Transform
Public Sub FFT(ByRef rex() As Single, ByRef imx() As Single, ByVal
N As UShort)

Dim nm1 As UShort = CUShort(N - 1)
Dim nd2 As UShort = CUShort(N \ 2)
Dim m As UShort = Math.Log(N) \ Math.Log(2)
Dim j As UShort = nd2
Dim k As UShort
Dim i As UShort

' bit reversal sorting
For i = 1 To N - 2
If i < j Then
swap(rex(i), rex(j))
swap(imx(i), imx(j))
End If
k = nd2
Do While k <= j
j = CUShort(j - k)
k = CUShort(k \ 2)
Loop
j += k
Next i

' loop for each stage
Dim le As UShort
Dim le2 As UShort
Dim jm1 As UShort
Dim ip As UShort
Dim ur As Single
Dim ui As Single
Dim sr As Single
Dim si As Single
Dim tr As Single
Dim ti As Single
Dim el As UShort

For el = 1 To m
'le = Round(pow(2,el) ,0);
le = CUShort(1 << el)
le2 = CUShort(le >1)
ur = 1
ui = 0
' sine & cosine
sr = Math.Cos(M_PI / le2)
si = -Math.Sin(M_PI / le2)
' loop for each sub DFT
For j = 1 To le2
jm1 = CUShort(j - 1)
' loop for each butterfly
For i = jm1 To nm1 Step le
ip = CUShort(i + le2)
tr = (rex(ip) * ur) - (imx(ip) * ui)
ti = (rex(ip) * ui) + (imx(ip) * ur)
rex(ip) = rex(i) - tr
imx(ip) = imx(i) - ti
' TODO: there is cuck going on here, don't know
what.
rex(i) += tr
imx(i) += ti
Next i
tr = ur
ur = tr * sr - ui * si
ui = tr * si + ui * sr
Next j
Next el

End Sub

Public Sub swap(Of T)(ByRef a As T, ByRef b As T)

Dim temp As T
temp = a
a = b
b = temp

End Sub

I am using this routine to process an array of 256 singles in array
rex() and imx() from an audio buffer to produce a visual
representation of the audio, however, it is very slow, so the
application 'lags' behind the sound.

The problem appears to be these two lines, if they are commented out
the program is very responsive, but obviously the algorithm fails to
produce the desired results:
rex(i) += tr
imx(i) += ti

I have tried using a temporary variable and modifying the code as
such: rex(i) = rex(i) + tr, but it is still terribly slow and the lag
on a few seconds of audio buffer could be nearly 30 seconds.

Alternatively I have created a C++ DLL from the original code, but
because it uses pointers, I cannot use it from managed code from
VB.Net unless I use old-style function declarations and I would prefer
to use managed code, and my C++ skills are somewhat lacking. If
someone could point me in the direction of converting the following
from pointered code to managed code that may also work and solve the
problem using c++. Any help with issue would be very much
appreciated....

Phaedrus

Header file:

using namespace System;

namespace dm {

public ref class clsMaths
{
// TODO: Add your methods for this class here.
public:
void FFT(float* rex, float* imx, unsigned short){}
};
}

cpp file:

// This is the main DLL file.

#include "stdafx.h"
#include "math.h"
#include "doppler_maths. h"
#include "stdlib.h"
#include "malloc.h"

#define M_PI 3.1415926535897 931

template<class T>
void swap( T% t1, T% t2 )
{ T tmp( t1 ); t1 = t2; t2 = tmp; }

// Fast Fourier Transform
void FFT(float* rex, float* imx, unsigned short N)
{
unsigned short nm1 = (unsigned short)(N-1);
unsigned short nd2 = (unsigned short)(N/2);
unsigned short m = Math::Round(log ((float)N)/log((float)2),0 );
unsigned short j = nd2;
unsigned short k;

// Windowing
//Hanning(rex);

// bit reversal sorting
for (unsigned short i=1; i<=N-2; i++)
{
if (i < j)
{
swap(rex+i, rex+j);
swap(imx+i, imx+j);
}
k = nd2;
while (k <= j)
{
j = (unsigned short)(j-k);
k = (unsigned short)(k/2);
}
j += k;
}
// loop for each stage
unsigned short le, le2, jm1, ip;
float ur, ui, sr, si, tr, ti;
for (unsigned short el=1; el<=m; el++)
{
//le = Round(pow(2,el) ,0);
le = (unsigned short)(1<<el);
le2 = (unsigned short)(le>>1);
ur = 1;
ui = 0;
// sine & cosine
sr = cos(M_PI/le2);
si = -sin(M_PI/le2);
// loop for each sub DFT
for (j=1; j<=le2; j++)
{
jm1 = (unsigned short)(j-1);
// loop for each butterfly
for (int i=jm1; i<=nm1; i+=le)
{
ip = (unsigned short)(i + le2);
tr = rex[ip]*ur - imx[ip]*ui;
ti = rex[ip]*ui + imx[ip]*ur;
rex[ip] = rex[i] - tr;
imx[ip] = imx[i] - ti;
rex[i] += tr;
imx[i] += ti;
}
tr = ur;
ur = tr*sr - ui*si;
ui = tr*si + ui*sr;
}
}
}

Oct 11 '07 #1
2 10293
"IdlePhaedr us" <cr*******@gmai l.comschrieb
Thanks Mindgeek, but that does not seem to be where the bottleneck is
when the code is running. If I comment out these two lines:

rex(i) += tr
imx(i) += ti

Then the code is almost instantaneous.
I slight improvement might be if you declare the arrays ByVal. ByRef is not
necessary because you don't create new array, you only change the content of
the array.
Armin

Oct 12 '07 #2
Hi

Also try to use Integer and Double arrays...
From MSDN: "For variables that never contain fractions, the integral
data types are more efficient than the nonintegral types. In Visual Basic,
Integer and UInteger are the most efficient numeric types. For fractional
numbers, Double is the most efficient data type, because the processors on
current platforms perform floating-point operations in double precision.
However, operations with Double are not as fast as with the integral types
such as Integer."
Anderson
"Armin Zingler" <az*******@free net.deescreveu na mensagem
news:%2******** **********@TK2M SFTNGP05.phx.gb l...
"IdlePhaedr us" <cr*******@gmai l.comschrieb
>Thanks Mindgeek, but that does not seem to be where the bottleneck is
when the code is running. If I comment out these two lines:

rex(i) += tr
imx(i) += ti

Then the code is almost instantaneous.

I slight improvement might be if you declare the arrays ByVal. ByRef is
not necessary because you don't create new array, you only change the
content of the array.
Armin

Oct 16 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2844
by: Matthew | last post by:
Hello, For a few days now, I have tried a number of methods that are supposed to provide the a(k) and b(k) coefficients for a Fourier series. These methods are listed at the end of this post (in Java). However, not one of these methods seems to provide the correct coefficients for the following function; f(x) = 2 - 2 * cos(x)
3
7894
by: Johannes Ahl-mann | last post by:
hi, i've been looking all around the net (google is my friend ;-) for a module to apply fourier transformations on images. the different ones in numerical python and scientific python seem all to be operating on sequences and therefore seem to be 1D fourier transform. anyone know a library/module to do 2D image FFT in a simple manner. or am i just too dumb to see how this is supposed to work with the 1D
1
5494
by: Mathias | last post by:
Dear NG, I'm looking for a fast way to produce 2d-noise images with 1/f or 1/f^2 spectrum. I currently generate the noise via inverse FFT, but since I need lots of data (~10^7 for a monte carlo simulation) it needs to be really fast. Does someone know a faster way than my approach? - Dimensionality is between 20x20 and 100x100 - The spectrum doesn't need to be exactly pink/brown, an approximation is fine.
4
3662
by: SC | last post by:
I couldn't write a program that make fourier transform in one and two dimens in C++. I couldn't solve correctly determinant of 3*3 matrix ic C++ too. Can you help me about that?? Thanks!
0
1982
by: christopher diggins | last post by:
// The Diggins PDP (Public Domain Post) #4 // public domain code for computing the FFT // contributed by Christopher Diggins, 2005 template<int N> unsigned int bit_reverse(unsigned int x) { int n = 0; int mask = 0x1; for (int i=0; i < N; i++) { n <<= 1;
4
6735
by: Charles | last post by:
I am doing a Digital signal processing project using VB.net and need advance maths function and also signal processing function such as fast fourier transform. Can anyone advice me on how do I get a FFT function? Thanks for any reply.
17
15654
by: Soumyadip Rakshit | last post by:
Hi, Could anyone direct me to a very good 1D DCT Implementation in C/C++ My data is in sets of 8 numbers. Thanks a ton, Soumyadip.
2
22364
by: mcdurr | last post by:
I recently installed Python 2.5 on Windows and also installed numpy 1.0. I'd like to compute an FFT on an array of numbers but I can't seem to access the FFT function. I'm fairly new to Python (obviously) and I can't seem to find documentation to match my distribution of numpy and I can't figure out how to access the FFT function. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32 Type "copyright", "credits" or "license()" for...
0
1529
by: pmclinn | last post by:
I'm looking for the complex discrete fourier Transform of this EQ impulse response array. I know the output for the first 3 values: EQ.DFT MAG(floating)) should be 1.066769, 1.040460, 1.063209.... EQ. DFT MAG(Db) .561411,.344507,.532374.... CH Resp mag(dB) -.608192, .845447,2.019220.... I can for the life me not get this to balance against the returns below. Any help would be greatly appreciated? Dim real(23) As Double
0
9521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10107
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9945
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8768
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.