annotate dsp/transforms/FFT.cpp @ 280:9c403afdd9e9

* Various fixes related to the bar estimator code
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 10 Feb 2009 16:37:11 +0000
parents 9edaa3ce62e8
children 6cb2b3cd5356
rev   line source
c@225 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@225 2
c@225 3 /*
c@225 4 QM DSP Library
c@225 5
c@225 6 Centre for Digital Music, Queen Mary, University of London.
c@225 7 This file is based on Don Cross's public domain FFT implementation.
c@225 8 */
c@225 9
c@225 10 #include "FFT.h"
c@280 11
c@280 12 #include "maths/MathUtilities.h"
c@280 13
c@225 14 #include <cmath>
c@225 15
c@280 16 #include <iostream>
c@280 17
c@225 18 //////////////////////////////////////////////////////////////////////
c@225 19 // Construction/Destruction
c@225 20 //////////////////////////////////////////////////////////////////////
c@225 21
c@225 22 FFT::FFT()
c@225 23 {
c@225 24
c@225 25 }
c@225 26
c@225 27 FFT::~FFT()
c@225 28 {
c@225 29
c@225 30 }
c@225 31
c@255 32 void FFT::process(unsigned int p_nSamples, bool p_bInverseTransform,
c@255 33 const double *p_lpRealIn, const double *p_lpImagIn,
c@255 34 double *p_lpRealOut, double *p_lpImagOut)
c@225 35 {
c@225 36
c@225 37 if(!p_lpRealIn || !p_lpRealOut || !p_lpImagOut) return;
c@225 38
c@225 39
c@225 40 unsigned int NumBits;
c@225 41 unsigned int i, j, k, n;
c@225 42 unsigned int BlockSize, BlockEnd;
c@225 43
c@225 44 double angle_numerator = 2.0 * M_PI;
c@225 45 double tr, ti;
c@225 46
c@280 47 if( !MathUtilities::isPowerOfTwo(p_nSamples) )
c@225 48 {
c@280 49 std::cerr << "ERROR: FFT::process: Non-power-of-two FFT size "
c@280 50 << p_nSamples << " not supported in this implementation"
c@280 51 << std::endl;
c@225 52 return;
c@225 53 }
c@225 54
c@225 55 if( p_bInverseTransform ) angle_numerator = -angle_numerator;
c@225 56
c@225 57 NumBits = numberOfBitsNeeded ( p_nSamples );
c@225 58
c@225 59
c@225 60 for( i=0; i < p_nSamples; i++ )
c@225 61 {
c@225 62 j = reverseBits ( i, NumBits );
c@225 63 p_lpRealOut[j] = p_lpRealIn[i];
c@225 64 p_lpImagOut[j] = (p_lpImagIn == 0) ? 0.0 : p_lpImagIn[i];
c@225 65 }
c@225 66
c@225 67
c@225 68 BlockEnd = 1;
c@225 69 for( BlockSize = 2; BlockSize <= p_nSamples; BlockSize <<= 1 )
c@225 70 {
c@225 71 double delta_angle = angle_numerator / (double)BlockSize;
c@225 72 double sm2 = -sin ( -2 * delta_angle );
c@225 73 double sm1 = -sin ( -delta_angle );
c@225 74 double cm2 = cos ( -2 * delta_angle );
c@225 75 double cm1 = cos ( -delta_angle );
c@225 76 double w = 2 * cm1;
c@225 77 double ar[3], ai[3];
c@225 78
c@225 79 for( i=0; i < p_nSamples; i += BlockSize )
c@225 80 {
c@225 81
c@225 82 ar[2] = cm2;
c@225 83 ar[1] = cm1;
c@225 84
c@225 85 ai[2] = sm2;
c@225 86 ai[1] = sm1;
c@225 87
c@225 88 for ( j=i, n=0; n < BlockEnd; j++, n++ )
c@225 89 {
c@225 90
c@225 91 ar[0] = w*ar[1] - ar[2];
c@225 92 ar[2] = ar[1];
c@225 93 ar[1] = ar[0];
c@225 94
c@225 95 ai[0] = w*ai[1] - ai[2];
c@225 96 ai[2] = ai[1];
c@225 97 ai[1] = ai[0];
c@225 98
c@225 99 k = j + BlockEnd;
c@225 100 tr = ar[0]*p_lpRealOut[k] - ai[0]*p_lpImagOut[k];
c@225 101 ti = ar[0]*p_lpImagOut[k] + ai[0]*p_lpRealOut[k];
c@225 102
c@225 103 p_lpRealOut[k] = p_lpRealOut[j] - tr;
c@225 104 p_lpImagOut[k] = p_lpImagOut[j] - ti;
c@225 105
c@225 106 p_lpRealOut[j] += tr;
c@225 107 p_lpImagOut[j] += ti;
c@225 108
c@225 109 }
c@225 110 }
c@225 111
c@225 112 BlockEnd = BlockSize;
c@225 113
c@225 114 }
c@225 115
c@225 116
c@225 117 if( p_bInverseTransform )
c@225 118 {
c@225 119 double denom = (double)p_nSamples;
c@225 120
c@225 121 for ( i=0; i < p_nSamples; i++ )
c@225 122 {
c@225 123 p_lpRealOut[i] /= denom;
c@225 124 p_lpImagOut[i] /= denom;
c@225 125 }
c@225 126 }
c@225 127 }
c@225 128
c@225 129 unsigned int FFT::numberOfBitsNeeded(unsigned int p_nSamples)
c@225 130 {
c@225 131 int i;
c@225 132
c@225 133 if( p_nSamples < 2 )
c@225 134 {
c@225 135 return 0;
c@225 136 }
c@225 137
c@225 138 for ( i=0; ; i++ )
c@225 139 {
c@225 140 if( p_nSamples & (1 << i) ) return i;
c@225 141 }
c@225 142 }
c@225 143
c@225 144 unsigned int FFT::reverseBits(unsigned int p_nIndex, unsigned int p_nBits)
c@225 145 {
c@225 146 unsigned int i, rev;
c@225 147
c@225 148 for(i=rev=0; i < p_nBits; i++)
c@225 149 {
c@225 150 rev = (rev << 1) | (p_nIndex & 1);
c@225 151 p_nIndex >>= 1;
c@225 152 }
c@225 153
c@225 154 return rev;
c@225 155 }