Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyone know of any simple, not so accurate FFT code for a Freq Display

Status
Not open for further replies.

up2late

MIS
Mar 10, 2005
7
US
I looked around but must of the stuff i've found is for analyzing important data or quantum physics, all I need is to convert my voice print style display to an spectrum analyzer style display, ie. 23 bands of freqs showing a level for each, the input I have is in the form of pcm samples (-10000 to +10000) sampled at 23 points in the buffer.

It doesnt have to be accurate, just look accurate, after all, there is a sync issue anyway,

So far I have an array of 23 vars, and each range from the above -10000 to +10000, the output when displayed in the form of 1 to 16 bars high,23 across, looks like a voice print.

I was hoping someone may have found something like this before for something similar, and could post the code, form what I can tell, it should be possible with only 2 loops.
 
Here you find an Algorithm for FFT. The source I wrote about 10 Years ago.

On input you have to put the Time values in xr. All xi should be set to 0. On Output xr,xi contain the Real/Imaginary Part of the Frequency Values.

n is the number of Values (must be a power of 2)

The include File required is math.h. You should find out if there is another system include required, but that should be no problem.



void fft(float *xr,float *xi,const int n) /* FFT Algorithmus */
{
unsigned int i,j,k,l;
unsigned int h1,h2;
unsigned int m;
double ahr,ahi;
double ar,ai;
double hr,hi;

inversion(xr,xi,n);
for(m=1;(n>>m)>1;++m);
for (i=0;i<m;++i)
{
l=1<<(i+1);
ahr=1;
ahi=0;
ar=cos((double)(2*M_PI/l));
ai=-sin((double)(2*M_PI/l));
for (j=0;j<l/2;++j)
{
for(k=0;k<n/l;++k)
{
h1=k*l+j;
h2=h1+l/2;
hr=ahr*xr[h2]-ahi*xi[h2];
hi=ahr*xi[h2]+ahi*xr[h2];
xr[h2]=xr[h1]-hr; xi[h2]=xi[h1]-hi;
xr[h1]+=hr;xi[h1]+=hi;
}
hr=ahr*ar-ahi*ai;
hi=ahr*ai+ahi*ar;
ahr=hr;
ahi=hi;
}
}
}


void inversion(float *xr,float *xi,const unsigned int n)
/* binaere Inversion */
{
int i,j,k;
float hr,hi;
for(i=j=1;i<n;++i)
{
k=n/2;
if(j>i)
{
hr=xr[i-1]; hi=xi[i-1];
xr[i-1]=xr[j-1]; xi[i-1]=xi[j-1];
xr[j-1]=hr; xi[j-1]=hi;
}
for(;k<j;j-=k,k/=2);
j+=k;
}
}


hnd
hasso55@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top