Click here to Skip to main content
11,707,167 members (58,456 online)
Click here to Skip to main content

FFT of waveIn audio signals

, 3 Apr 2007 CPOL 450.7K 16K 251
Rate this:
Please Sign up or sign in to vote.
An article on using the Fast Fourier Transform on audio signals.

Sample Image - waveInFFT.jpg

Sample Image - waveInFFT_microeq.jpg

Sample Image - waveInFFT_Oscilliscope.jpg

Sample Image - waveInFFT_peak.jpg

Sample Image - waveInFFT_peakalt.jpg

Sample Image - waveInFFT_PixelGram.jpg

Sample Image - waveInFFT_spectrum.jpg

Introduction

The Fast Fourier Transform (FFT) allows users to view the spectrum content of an audio signal. The FFT code presented here was written by Don Cross, his homepage appears to have subsequently been taken down. Rather than explain the mathematical theory of the FFT, I will attempt to explain its usefulness as it relates to audio signals.

The FFT allows users to obtain the spectral makeup of an audio signal, obtain the decibels of its various frequencies, or obtain the intensity of its various frequencies. Spectral viewers (shown in the image above), Equalizers, or VU-Meters may all use the FFT in order to display their results. The difference between them then depends upon one of a couple of equations that take the real and imaginary components of the FFT, and return either the intensity or decibel levels to be used in the graphed result. The following code takes both the real and imaginary components of the FFT result, and returns the intensity and decibels.

inline double GetFrequencyIntensity(double re, double im)
{
    return sqrt((re*re)+(im*im));
}
#define mag_sqrd(re,im) (re*re+im*im)
#define Decibels(re,im) ((re == 0 && im == 0) ? (0) : 
        10.0 * log10(double(mag_sqrd(re,im))))
#define Amplitude(re,im,len) (GetFrequencyIntensity(re,im)/(len))
#define AmplitudeScaled(re,im,len,scale) ((int)Amplitude(re,im,len)%scale)

The FFT uses the audio signal as its real component, and uses a NULL pointer for its imaginary component indicating that the imaginary data does not exist. Upon its return, the FFT will return both the real and imaginary data components based upon the data given as the real component. The is mirrored with the return samples so that 0-FFT_LEN/2 contains the data, and FFT_LEN/2 to FFT_LEN contains a reverse of the data. This mistake was corrected in my code. The code that performs the FFT follows:

DWORD nCount = 0;
for (DWORD dw = 0; dw < FFT_LEN; dw++)
{
    {
        //copy audio signal to fft real component for left channel
        finleft[nCount] = (double)((short*)pwh->lpData)[dw++];
        //copy audio signal to fft real component for right channel
        finright[nCount++] = (double)((short*)pwh->lpData)[dw];
    }
}
//Perform FFT on left channel
fft_double(FFT_LEN/2,0,finleft,NULL,fout,foutimg);
float re,im,fmax=-99999.9f,fmin=99999.9f;
for(int i=1;i < FFT_LEN/4-1;i++)
//Use FFT_LEN/4 since the data is mirrored within the array.
{
    re = fout[i];
    im = foutimg[i];
    //get amplitude and scale to 0..256 range
    fdraw[i]=AmplitudeScaled(re,im,FFT_LEN/2,256);
    if (fdraw[i] > fmax)
    {
        fmax = fdraw[i];
    }
    if (fdraw[i] < fmin)
    {
        fmin = fdraw[i];
    }
}
//Use this to send the average band amplitude to something
int nAvg, nBars=16, nCur = 0;
for(int i=1;i < FFT_LEN/4;i++)
{
    nAvg = 0;
    for (int n=0; n < nBars; n++)
    {
        nAvg += (int)fdraw[i];
    }
    nAvg /= nBars;
    //Send data here to something,
    //nothing to send it to so we print it.
    TRACE("Average for Bar#%d is %d\n",nCur++,nAvg);
    i+=nBars-1;
}
DataHolder* pDataHolder = (DataHolder*)lpData;
// Draw left channel
CFrequencyGraph* pPeak = (CFrequencyGraph*)pDataHolder->pData;
if (::IsWindow(pPeak->GetSafeHwnd()))
{
    pPeak->SetYRange(0,256);
    pPeak->Update(FFT_LEN/4,fdraw);
}

// Perform FFT on right channel
fmax=-99999.9f,fmin=99999.9f;
fft_double(FFT_LEN/2,0,finright,NULL,fout,foutimg);
fdraw[0] = fdraw[FFT_LEN/4] = 0;
for(i=1;i < FFT_LEN/4-1;i++)
//Use FFT_LEN/4 since the data is mirrored within the array.
{
    re = fout[i];
    im = foutimg[i];
    //get Decibels in 0-110 range
    fdraw[i] = Decibels(re,im);
    if (fdraw[i] > fmax)
    {
        fmax = fdraw[i];
    }
    if (fdraw[i] < fmin)
    {
        fmin = fdraw[i];
    }
}
//Draw right channel
CFrequencyGraph* pPeak2 = (CFrequencyGraph*)pDataHolder->pData2;
if (::IsWindow(pPeak2->GetSafeHwnd()))
{
    pPeak2->SetNumberOfSteps(50);
    //Use updated dynamic range for scaling
    pPeak2->SetYRange((int)fmin,(int)fmax);
    pPeak2->Update(FFT_LEN/4,fdraw);
}

This code is contained in a callback function that is called every time the waveIn functions return with updated audio signal data. The code that actually performs the FFT looks like:

void fft_double (unsigned int p_nSamples, bool p_bInverseTransform, 
    double *p_lpRealIn, double *p_lpImagIn, 
    double *p_lpRealOut, double *p_lpImagOut)
{

    if(!p_lpRealIn || !p_lpRealOut || !p_lpImagOut) return;


    unsigned int NumBits;
    unsigned int i, j, k, n;
    unsigned int BlockSize, BlockEnd;

    double angle_numerator = 2.0 * PI;
    double tr, ti;

    if( !IsPowerOfTwo(p_nSamples) )
    {
        return;
    }

    if( p_bInverseTransform ) angle_numerator = -angle_numerator;

    NumBits = NumberOfBitsNeeded ( p_nSamples );


    for( i=0; i < p_nSamples; i++ )
    {
        j = ReverseBits ( i, NumBits );
        p_lpRealOut[j] = p_lpRealIn[i];
        p_lpImagOut[j] = (p_lpImagIn == NULL) ? 0.0 : p_lpImagIn[i];
    }


    BlockEnd = 1;
    for( BlockSize = 2; BlockSize <= p_nSamples; BlockSize <<= 1 )
    {
        double delta_angle = angle_numerator / (double)BlockSize;
        double sm2 = sin ( -2 * delta_angle );
        double sm1 = sin ( -delta_angle );
        double cm2 = cos ( -2 * delta_angle );
        double cm1 = cos ( -delta_angle );
        double w = 2 * cm1;
        double ar[3], ai[3];

        for( i=0; i < p_nSamples; i += BlockSize )
        {

            ar[2] = cm2;
            ar[1] = cm1;

            ai[2] = sm2;
            ai[1] = sm1;

            for ( j=i, n=0; n < BlockEnd; j++, n++ )
            {

                ar[0] = w*ar[1] - ar[2];
                ar[2] = ar[1];
                ar[1] = ar[0];

                ai[0] = w*ai[1] - ai[2];
                ai[2] = ai[1];
                ai[1] = ai[0];

                k = j + BlockEnd;
                tr = ar[0]*p_lpRealOut[k] - ai[0]*p_lpImagOut[k];
                ti = ar[0]*p_lpImagOut[k] + ai[0]*p_lpRealOut[k];

                p_lpRealOut[k] = p_lpRealOut[j] - tr;
                p_lpImagOut[k] = p_lpImagOut[j] - ti;

                p_lpRealOut[j] += tr;
                p_lpImagOut[j] += ti;

            }
        }

        BlockEnd = BlockSize;

    }


    if( p_bInverseTransform )
    {
        double denom = (double)p_nSamples;

        for ( i=0; i < p_nSamples; i++ )
        {
            p_lpRealOut[i] /= denom;
            p_lpImagOut[i] /= denom;
        }
    }

}

And it requires the following supporting functions:

///////////////////////////////////////////////////////////
// check is a number is a power of 2
///////////////////////////////////////////////////////////

bool IsPowerOfTwo( unsigned int p_nX )
{

    if( p_nX < 2 ) return false;

    if( p_nX & (p_nX-1) ) return false;

    return true;

}


///////////////////////////////////////////////////////////
// return needed bits for fft
///////////////////////////////////////////////////////////

unsigned int NumberOfBitsNeeded( unsigned int p_nSamples )
{

    int i;

    if( p_nSamples < 2 )
    {
        return 0;
    }

    for ( i=0; ; i++ )
    {
        if( p_nSamples & (1 << i) ) return i;
    }

}



///////////////////////////////////////////////////////////
// ?
///////////////////////////////////////////////////////////

unsigned int ReverseBits(unsigned int p_nIndex, unsigned int p_nBits)
{

    unsigned int i, rev;

    for(i=rev=0; i < p_nBits; i++)
    {
        rev = (rev << 1) | (p_nIndex & 1);
        p_nIndex >>= 1;
    }

    return rev;
}



///////////////////////////////////////////////////////////
// return a frequency from the basefreq and num of samples
///////////////////////////////////////////////////////////

double Index_to_frequency(unsigned int p_nBaseFreq, 
    unsigned int p_nSamples, unsigned int p_nIndex)
{

    if(p_nIndex >= p_nSamples)
    {
        return 0.0;
    }
    else if(p_nIndex <= p_nSamples/2)
    {
        return ( (double)p_nIndex / 
                 (double)p_nSamples * p_nBaseFreq );
    }
    else
    {
        return ( -(double)(p_nSamples-p_nIndex) / 
                  (double)p_nSamples * p_nBaseFreq );
    }

}

The included sample class, CFrequencyGraph, will draw a sample EQ, peak meter, and spectral graph using the frequency intensity. Hopefully, this serves as a decent introduction to the uses and basics of the Fast Fourier Transform for audio signals. Other functions of the FFT include using it in combination with a Beat Detection Algorithm to detect beats in an audio signal. Another page with useful FFT information is located at FFT Spectrum Analyser.

This article uses waveIn* functions to retrieve the soundcard's data from the playing source. Therefore, you must manually open your soundcard properties and change the recording source to use mono/stereo mix or waveIn as the selected recording line. Also, if you set the playback wave option to false, this will override the recording options and no sound will appear. For information about doing an inverse FFT for transforming frequencies into an audio signal, please do a Google search on "Inverse FFT" or "IFFT".

History

  • Version 1.3: Fixed Spectrum drawing, Changed Pixelgram color, changed Process routine to match new Recording parameters.
  • Version 1.2: Fixed many drawing bugs, changed to use amplitude scaled and decibels.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Fred Ackers
Web Developer
United States United States
Programming using MFC and ATL for almost 12 years now. Currently studying Operating System implementation as well as Image processing. Previously worked on DSP and the use of FFT for audio application. Programmed using ADO, ODBC, ATL, COM, MFC for shell interfacing, databasing tasks, Internet items, and customization programs.

You may also be interested in...

Comments and Discussions

 
GeneralCode contribution Pin
pigeonb25-Mar-13 15:39
memberpigeonb25-Mar-13 15:39 
QuestionI want to know FFT with Flow to detect Pitch.... Pin
Mrugrajsinh24-Oct-12 4:48
memberMrugrajsinh24-Oct-12 4:48 
AnswerRe: I want to know FFT with Flow to detect Pitch.... Pin
Fred Ackers9-Nov-12 10:30
memberFred Ackers9-Nov-12 10:30 
GeneralRe: I want to know FFT with Flow to detect Pitch.... Pin
Mrugrajsinh14-Nov-12 4:36
memberMrugrajsinh14-Nov-12 4:36 
NewsDIRECT SOUND ''Live'' Pin
LuizSp Antonio13-Aug-12 18:29
memberLuizSp Antonio13-Aug-12 18:29 
GeneralRe: DIRECT SOUND ''Live'' Pin
Fred Ackers14-Aug-12 8:57
memberFred Ackers14-Aug-12 8:57 
GeneralRe: DIRECT SOUND ''Live'' Pin
LuizSp Antonio15-Aug-12 10:29
memberLuizSp Antonio15-Aug-12 10:29 
GeneralRe: DIRECT SOUND ''Live'' Pin
Fred Ackers18-Oct-12 12:12
memberFred Ackers18-Oct-12 12:12 
QuestionAssertion failure at line 183 Pin
hahakimkim27-May-12 15:09
memberhahakimkim27-May-12 15:09 
AnswerRe: Assertion failure at line 183 Pin
Fred Ackers28-May-12 19:43
memberFred Ackers28-May-12 19:43 
GeneralRe: Assertion failure at line 183 Pin
hahakimkim3-Jun-12 4:06
memberhahakimkim3-Jun-12 4:06 
GeneralRe: Assertion failure at line 183 Pin
Fred Ackers18-Oct-12 12:15
memberFred Ackers18-Oct-12 12:15 
QuestionDoesn't satisfy Parseval Pin
Member 773975324-Apr-12 7:49
memberMember 773975324-Apr-12 7:49 
GeneralRe: Doesn't satisfy Parseval Pin
Fred Ackers28-May-12 19:53
memberFred Ackers28-May-12 19:53 
QuestionTrying to get this done in C# stuck here..did I translate this right? Pin
mangotj1-Mar-11 7:37
membermangotj1-Mar-11 7:37 
GeneralMy vote of 5 Pin
dspmx26-Oct-10 3:37
memberdspmx26-Oct-10 3:37 
GeneralDecibels Value! Pin
mobailwang18-Oct-10 23:51
membermobailwang18-Oct-10 23:51 
GeneralMy vote of 5 Pin
mrk1000017-Oct-10 17:51
membermrk1000017-Oct-10 17:51 
Questionsampling frequency Pin
smart_dummies30-Mar-10 22:51
membersmart_dummies30-Mar-10 22:51 
GeneralMeasure units for Real and Ima. Data Pin
juanjo.montero21-Oct-09 9:11
memberjuanjo.montero21-Oct-09 9:11 
Generalconver to C# Pin
enenegn13-May-09 3:42
memberenenegn13-May-09 3:42 
QuestionDecibels explained? Pin
jweston15-Apr-09 11:53
memberjweston15-Apr-09 11:53 
AnswerRe: Decibels explained? Pin
RichardMant1-Jun-10 4:41
memberRichardMant1-Jun-10 4:41 
GeneralFrequency Pin
sarlacc17-Mar-09 3:15
membersarlacc17-Mar-09 3:15 
GeneralInternet archive for Don Cross' original page Pin
jo0ls4-Jan-09 7:51
memberjo0ls4-Jan-09 7:51 
Questiona few question about play wave file with DirectSound and display spectrum. Pin
jacky_zz4-Sep-08 20:23
memberjacky_zz4-Sep-08 20:23 
GeneralQuestion about scaling the amplitude Pin
tabor2514-Jul-08 2:36
membertabor2514-Jul-08 2:36 
GeneralRe: Question about scaling the amplitude Pin
LLSS_China10-Mar-09 1:13
memberLLSS_China10-Mar-09 1:13 
GeneralRe: Question about scaling the amplitude Pin
RichardMant1-Jun-10 4:46
memberRichardMant1-Jun-10 4:46 
GeneralRe: Question about scaling the amplitude Pin
skydre1-Jul-10 3:50
memberskydre1-Jul-10 3:50 
GeneralRe: Question about scaling the amplitude Pin
RichardMant1-Jul-10 4:42
memberRichardMant1-Jul-10 4:42 
General[Message Deleted] Pin
Danny Rodriguez27-Jan-08 9:09
memberDanny Rodriguez27-Jan-08 9:09 
Generalxcode version Pin
eoghancunneen9-Jan-08 3:25
membereoghancunneen9-Jan-08 3:25 
Generalfor another condition Pin
aa_esa23-Nov-07 18:07
memberaa_esa23-Nov-07 18:07 
Generaluse many cpu percenge Pin
shallopquan13-Aug-07 0:15
membershallopquan13-Aug-07 0:15 
Generalre Pin
Leon.Minos2-Aug-07 18:39
memberLeon.Minos2-Aug-07 18:39 
QuestionVisualC++ Error Pin
edguardian1-Aug-07 11:45
memberedguardian1-Aug-07 11:45 
AnswerRe: VisualC++ Error Pin
Fred Ackers4-Aug-07 8:19
memberFred Ackers4-Aug-07 8:19 
AnswerRe: VisualC++ Error Pin
AlexanderGao1-Apr-10 2:55
memberAlexanderGao1-Apr-10 2:55 
QuestionPoor audio quality Pin
ChrisMitchell_26-May-07 19:23
memberChrisMitchell_26-May-07 19:23 
AnswerRe: Poor audio quality Pin
Fred Ackers29-May-07 12:32
memberFred Ackers29-May-07 12:32 
GeneralRe: Poor audio quality [modified] Pin
ChrisMitchell_30-May-07 8:59
memberChrisMitchell_30-May-07 8:59 
GeneralRe: Poor audio quality Pin
Fred Ackers31-May-07 12:51
memberFred Ackers31-May-07 12:51 
QuestionRe: Poor audio quality Pin
bquintero27-Jun-07 18:27
memberbquintero27-Jun-07 18:27 
GeneralThank you Pin
DanMax5-Apr-07 23:05
memberDanMax5-Apr-07 23:05 
QuestionNumber of channels? Pin
jakeparks30-Mar-07 8:08
memberjakeparks30-Mar-07 8:08 
AnswerRe: Number of channels? Pin
Fred Ackers31-Mar-07 13:03
memberFred Ackers31-Mar-07 13:03 
GeneralRe: Number of channels? Pin
jakeparks2-Apr-07 13:50
memberjakeparks2-Apr-07 13:50 
GeneralRe: Number of channels? Pin
Fred Ackers3-Apr-07 10:02
memberFred Ackers3-Apr-07 10:02 
GeneralDon Cross reference Pin
stug29-Mar-07 17:18
memberstug29-Mar-07 17:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 3 Apr 2007
Article Copyright 2004 by Fred Ackers
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid