1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/*
* Copyright (C) 2002-2004 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "FFT.h"
#include "Exception.h"
#include "Inline.h"
#include <cmath>
using namespace std;
#include <wx/object.h> // Not sure why !
#include <wx/debug.h>
CFFT::CFFT(int len) :
m_fftLen(len),
m_plan(NULL),
m_in(NULL),
m_out(NULL)
{
wxASSERT(m_fftLen > 0);
m_in = (fftw_complex*)::fftw_malloc(m_fftLen * sizeof(fftw_complex));
m_out = (fftw_complex*)::fftw_malloc(m_fftLen * sizeof(fftw_complex));
if (m_in == NULL || m_out == NULL)
throw CException(wxT("Cannot allocate FFT arrays"));
#if defined(HAVE_FFTW3)
m_plan = ::fftw_plan_dft_1d(m_fftLen, m_in, m_out, FFTW_FORWARD, FFTW_ESTIMATE);
#else
m_plan = ::fftw_create_plan(m_fftLen, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_OUT_OF_PLACE | FFTW_USE_WISDOM);
#endif
if (m_plan == NULL)
throw CException(wxT("Cannot create FFT plan"));
}
CFFT::~CFFT()
{
wxASSERT(m_plan != NULL);
wxASSERT(m_in != NULL);
wxASSERT(m_out != NULL);
::fftw_destroy_plan(m_plan);
::fftw_free(m_in);
::fftw_free(m_out);
}
void CFFT::process(double* in, double* bins)
{
wxASSERT(m_plan != NULL);
wxASSERT(m_in != NULL);
wxASSERT(m_out != NULL);
wxASSERT(in != NULL);
wxASSERT(bins != NULL);
#if defined(HAVE_FFTW3)
for (int i = 0; i < m_fftLen; i++) {
m_in[i][0] = in[i];
m_in[i][1] = 0.0;
}
::fftw_execute(m_plan);
for (int i = 0; i < m_fftLen; i++)
bins[i] = ::CABS(m_out[i][0], m_out[i][1]);
#else
for (int i = 0; i < m_fftLen; i++) {
c_re(m_in[i]) = in[i];
c_im(m_in[i]) = 0.0;
}
::fftw_one(m_plan, m_in, m_out);
for (int i = 0; i < m_fftLen; i++)
bins[i] = ::CABS(c_re(m_out[i]), c_im(m_out[i]));
#endif
}
void CFFT::process(double* in, complex<double>* bins)
{
wxASSERT(m_plan != NULL);
wxASSERT(m_in != NULL);
wxASSERT(m_out != NULL);
wxASSERT(in != NULL);
wxASSERT(bins != NULL);
#if defined(HAVE_FFTW3)
for (int i = 0; i < m_fftLen; i++) {
m_in[i][0] = in[i];
m_in[i][1] = 0.0;
}
::fftw_execute(m_plan);
for (int i = 0; i < m_fftLen; i++)
bins[i] = complex<double>(m_out[i][0], m_out[i][1]);
#else
for (int i = 0; i < m_fftLen; i++) {
c_re(m_in[i]) = in[i];
c_im(m_in[i]) = 0.0;
}
::fftw_one(m_plan, m_in, m_out);
for (int i = 0; i < m_fftLen; i++)
bins[i] = complex<double>(c_re(m_out[i]), c_im(m_out[i]));
#endif
}
|