123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- #include <math.h>
- #include <memory.h>
- #include "fft.h"
- #define PI 3.141592653589793238462643383279502884197169399f
- #define SafeDeleteArray(x) { if (x) { delete [] x; x = 0; } }
- FFT::FFT()
- {
- NFREQ = 0;
- envelope = 0;
- equalize = 0;
- bitrevtable = 0;
- cossintable = 0;
- temp1 = 0;
- temp2 = 0;
- }
- FFT::~FFT()
- {
- CleanUp();
- }
- void FFT::Init(int samples_in, int samples_out, int bEqualize, float envelope_power)
- {
-
-
-
-
-
-
- CleanUp();
- m_samples_in = samples_in;
- NFREQ = samples_out*2;
- InitBitRevTable();
- InitCosSinTable();
- if (envelope_power > 0)
- InitEnvelopeTable(envelope_power);
- if (bEqualize)
- InitEqualizeTable();
- temp1 = new float[NFREQ];
- temp2 = new float[NFREQ];
- }
- void FFT::CleanUp()
- {
- SafeDeleteArray(envelope);
- SafeDeleteArray(equalize);
- SafeDeleteArray(bitrevtable);
- SafeDeleteArray(cossintable);
- SafeDeleteArray(temp1);
- SafeDeleteArray(temp2);
- }
- void FFT::InitEqualizeTable()
- {
- int i;
- float scaling = -0.02f;
- float inv_half_nfreq = 1.0f/(float)(NFREQ/2);
- equalize = new float[NFREQ/2];
- for (i=0; i<NFREQ/2; i++)
- equalize[i] = scaling * logf( (float)(NFREQ/2-i)*inv_half_nfreq );
- }
- void FFT::InitEnvelopeTable(float power)
- {
-
-
-
-
-
-
-
-
-
-
- int i;
- float mult = 1.0f/(float)m_samples_in * 6.2831853f;
- envelope = new float[m_samples_in];
- if (power==1.0f)
- for (i=0; i<m_samples_in; i++)
- envelope[i] = 0.5f + 0.5f*sinf(i*mult - 1.5707963268f);
- else
- for (i=0; i<m_samples_in; i++)
- envelope[i] = powf(0.5f + 0.5f*sinf(i*mult - 1.5707963268f), power);
- }
- void FFT::InitBitRevTable()
- {
- int i,j,m,temp;
- bitrevtable = new int[NFREQ];
- for (i=0; i<NFREQ; i++)
- bitrevtable[i] = i;
- for (i=0,j=0; i < NFREQ; i++)
- {
- if (j > i)
- {
- temp = bitrevtable[i];
- bitrevtable[i] = bitrevtable[j];
- bitrevtable[j] = temp;
- }
-
- m = NFREQ >> 1;
-
- while (m >= 1 && j >= m)
- {
- j -= m;
- m >>= 1;
- }
- j += m;
- }
- }
- void FFT::InitCosSinTable()
- {
- int i,dftsize,tabsize;
- float theta;
- dftsize = 2;
- tabsize = 0;
- while (dftsize <= NFREQ)
- {
- tabsize++;
- dftsize <<= 1;
- }
- cossintable = new float[tabsize][2];
- dftsize = 2;
- i = 0;
- while (dftsize <= NFREQ)
- {
- theta = (float)(-2.0f*PI/(float)dftsize);
- cossintable[i][0] = (float)cosf(theta);
- cossintable[i][1] = (float)sinf(theta);
- i++;
- dftsize <<= 1;
- }
- }
- void FFT::time_to_frequency_domain(float *in_wavedata, float *out_spectraldata)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- int j, m, i, dftsize, hdftsize, t;
- float wr, wi, wpi, wpr, wtemp, tempr, tempi;
- if (!bitrevtable) return;
-
-
- if (!temp1) return;
- if (!temp2) return;
- if (!cossintable) return;
-
- if (envelope)
- {
- for (i=0; i<NFREQ; i++)
- {
- int idx = bitrevtable[i];
- if (idx < m_samples_in)
- temp1[i] = in_wavedata[idx] * envelope[idx];
- else
- temp1[i] = 0;
- }
- }
- else
- {
- for (i=0; i<NFREQ; i++)
- {
- int idx = bitrevtable[i];
- if (idx < m_samples_in)
- temp1[i] = in_wavedata[idx];
- else
- temp1[i] = 0;
- }
- }
- memset(temp2, 0, sizeof(float)*NFREQ);
-
-
- float *real = temp1;
- float *imag = temp2;
- dftsize = 2;
- t = 0;
- while (dftsize <= NFREQ)
- {
- wpr = cossintable[t][0];
- wpi = cossintable[t][1];
- wr = 1.0f;
- wi = 0.0f;
- hdftsize = dftsize >> 1;
- for (m = 0; m < hdftsize; m+=1)
- {
- for (i = m; i < NFREQ; i+=dftsize)
- {
- j = i + hdftsize;
- tempr = wr*real[j] - wi*imag[j];
- tempi = wr*imag[j] + wi*real[j];
- real[j] = real[i] - tempr;
- imag[j] = imag[i] - tempi;
- real[i] += tempr;
- imag[i] += tempi;
- }
- wr = (wtemp=wr)*wpr - wi*wpi;
- wi = wi*wpr + wtemp*wpi;
- }
- dftsize <<= 1;
- t++;
- }
-
- if (equalize)
- for (i=0; i<NFREQ/2; i++)
- out_spectraldata[i] = equalize[i] * sqrtf(temp1[i]*temp1[i] + temp2[i]*temp2[i]);
- else
- for (i=0; i<NFREQ/2; i++)
- out_spectraldata[i] = sqrtf(temp1[i]*temp1[i] + temp2[i]*temp2[i]);
- }
|