annotate dsp/transforms/FFT.cpp @ 339:9c8ee77db9de

Tidy real-to-complex FFT -- forward and inverse have different arguments, so make them separate functions; document
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 02 Oct 2013 15:04:38 +0100
parents d5014ab8b0e5
children 6ec45e85ed81
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@339 18 FFT::FFT(int n) :
c@339 19 m_n(n)
c@225 20 {
c@289 21 if( !MathUtilities::isPowerOfTwo(m_n) )
c@289 22 {
c@289 23 std::cerr << "ERROR: FFT: Non-power-of-two FFT size "
c@289 24 << m_n << " not supported in this implementation"
c@289 25 << std::endl;
c@289 26 return;
c@289 27 }
c@225 28 }
c@225 29
c@225 30 FFT::~FFT()
c@225 31 {
c@225 32
c@225 33 }
c@225 34
c@339 35 FFTReal::FFTReal(int n) :
c@289 36 m_n(n),
c@339 37 m_fft(new FFT(n)),
c@339 38 m_r(new double[n]),
c@339 39 m_i(new double[n]),
c@339 40 m_discard(new double[n])
c@225 41 {
c@339 42 for (int i = 0; i < n; ++i) {
c@339 43 m_r[i] = 0;
c@339 44 m_i[i] = 0;
c@339 45 m_discard[i] = 0;
c@339 46 }
c@289 47 }
c@225 48
c@289 49 FFTReal::~FFTReal()
c@289 50 {
c@339 51 delete m_fft;
c@339 52 delete[] m_discard;
c@339 53 delete[] m_r;
c@339 54 delete[] m_i;
c@289 55 }
c@289 56
c@289 57 void
c@339 58 FFTReal::forward(const double *realIn,
c@289 59 double *realOut, double *imagOut)
c@289 60 {
c@339 61 m_fft->process(false, realIn, 0, realOut, imagOut);
c@289 62 }
c@289 63
c@339 64 void
c@339 65 FFTReal::inverse(const double *realIn, const double *imagIn,
c@339 66 double *realOut)
c@339 67 {
c@339 68 for (int i = 0; i < m_n/2 + 1; ++i) {
c@339 69 m_r[i] = realIn[i];
c@339 70 m_i[i] = imagIn[i];
c@339 71 if (i > 0 && i < m_n/2) {
c@339 72 m_r[m_n - i] = realIn[i];
c@339 73 m_i[m_n - i] = -imagIn[i];
c@339 74 }
c@339 75 }
c@339 76 m_fft->process(true, m_r, m_i, realOut, m_discard);
c@339 77 }
c@339 78
c@339 79 static int numberOfBitsNeeded(int p_nSamples)
c@289 80 {
c@289 81 int i;
c@289 82
c@289 83 if( p_nSamples < 2 )
c@289 84 {
c@289 85 return 0;
c@289 86 }
c@289 87
c@289 88 for ( i=0; ; i++ )
c@289 89 {
c@289 90 if( p_nSamples & (1 << i) ) return i;
c@289 91 }
c@289 92 }
c@289 93
c@339 94 static int reverseBits(int p_nIndex, int p_nBits)
c@289 95 {
c@339 96 int i, rev;
c@289 97
c@289 98 for(i=rev=0; i < p_nBits; i++)
c@289 99 {
c@289 100 rev = (rev << 1) | (p_nIndex & 1);
c@289 101 p_nIndex >>= 1;
c@289 102 }
c@289 103
c@289 104 return rev;
c@289 105 }
c@289 106
c@289 107 void
c@289 108 FFT::process(bool p_bInverseTransform,
c@289 109 const double *p_lpRealIn, const double *p_lpImagIn,
c@289 110 double *p_lpRealOut, double *p_lpImagOut)
c@289 111 {
c@291 112 if (!p_lpRealIn || !p_lpRealOut || !p_lpImagOut) return;
c@291 113
c@291 114 // std::cerr << "FFT::process(" << m_n << "," << p_bInverseTransform << ")" << std::endl;
c@225 115
c@339 116 int NumBits;
c@339 117 int i, j, k, n;
c@339 118 int BlockSize, BlockEnd;
c@225 119
c@225 120 double angle_numerator = 2.0 * M_PI;
c@225 121 double tr, ti;
c@225 122
c@289 123 if( !MathUtilities::isPowerOfTwo(m_n) )
c@225 124 {
c@280 125 std::cerr << "ERROR: FFT::process: Non-power-of-two FFT size "
c@289 126 << m_n << " not supported in this implementation"
c@280 127 << std::endl;
c@225 128 return;
c@225 129 }
c@225 130
c@225 131 if( p_bInverseTransform ) angle_numerator = -angle_numerator;
c@225 132
c@289 133 NumBits = numberOfBitsNeeded ( m_n );
c@225 134
c@225 135
c@339 136
c@289 137 for( i=0; i < m_n; i++ )
c@225 138 {
c@225 139 j = reverseBits ( i, NumBits );
c@225 140 p_lpRealOut[j] = p_lpRealIn[i];
c@225 141 p_lpImagOut[j] = (p_lpImagIn == 0) ? 0.0 : p_lpImagIn[i];
c@225 142 }
c@225 143
c@225 144
c@225 145 BlockEnd = 1;
c@289 146 for( BlockSize = 2; BlockSize <= m_n; BlockSize <<= 1 )
c@225 147 {
c@225 148 double delta_angle = angle_numerator / (double)BlockSize;
c@225 149 double sm2 = -sin ( -2 * delta_angle );
c@225 150 double sm1 = -sin ( -delta_angle );
c@225 151 double cm2 = cos ( -2 * delta_angle );
c@225 152 double cm1 = cos ( -delta_angle );
c@225 153 double w = 2 * cm1;
c@225 154 double ar[3], ai[3];
c@225 155
c@289 156 for( i=0; i < m_n; i += BlockSize )
c@225 157 {
c@225 158
c@225 159 ar[2] = cm2;
c@225 160 ar[1] = cm1;
c@225 161
c@225 162 ai[2] = sm2;
c@225 163 ai[1] = sm1;
c@225 164
c@225 165 for ( j=i, n=0; n < BlockEnd; j++, n++ )
c@225 166 {
c@225 167
c@225 168 ar[0] = w*ar[1] - ar[2];
c@225 169 ar[2] = ar[1];
c@225 170 ar[1] = ar[0];
c@225 171
c@225 172 ai[0] = w*ai[1] - ai[2];
c@225 173 ai[2] = ai[1];
c@225 174 ai[1] = ai[0];
c@225 175
c@225 176 k = j + BlockEnd;
c@225 177 tr = ar[0]*p_lpRealOut[k] - ai[0]*p_lpImagOut[k];
c@225 178 ti = ar[0]*p_lpImagOut[k] + ai[0]*p_lpRealOut[k];
c@225 179
c@225 180 p_lpRealOut[k] = p_lpRealOut[j] - tr;
c@225 181 p_lpImagOut[k] = p_lpImagOut[j] - ti;
c@225 182
c@225 183 p_lpRealOut[j] += tr;
c@225 184 p_lpImagOut[j] += ti;
c@225 185
c@225 186 }
c@225 187 }
c@225 188
c@225 189 BlockEnd = BlockSize;
c@225 190
c@225 191 }
c@225 192
c@225 193
c@225 194 if( p_bInverseTransform )
c@225 195 {
c@289 196 double denom = (double)m_n;
c@225 197
c@289 198 for ( i=0; i < m_n; i++ )
c@225 199 {
c@225 200 p_lpRealOut[i] /= denom;
c@225 201 p_lpImagOut[i] /= denom;
c@225 202 }
c@225 203 }
c@225 204 }
c@225 205