summaryrefslogtreecommitdiffstats
path: root/sdrbase/dsp/fftwengine.cpp
blob: 88556417432be52c7c47c70a9277ab9680c5051f (plain)
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
#include <QTime>
#include "dsp/fftwengine.h"

FFTWEngine::FFTWEngine() :
	m_plans(),
	m_currentPlan(NULL)
{
	configure(128, false);
	configure(256, false);
	configure(512, false);
	configure(1024, false);
	configure(2048, false);
	configure(4096, false);
	configure(8192, false);
}

FFTWEngine::~FFTWEngine()
{
	freeAll();
}

void FFTWEngine::configure(int n, bool inverse)
{
	for(Plans::const_iterator it = m_plans.begin(); it != m_plans.end(); ++it) {
		if(((*it)->n == n) && ((*it)->inverse == inverse)) {
			m_currentPlan = *it;
			return;
		}
	}

	m_globalPlanMutex.lock();
	m_currentPlan = new Plan;
	m_currentPlan->n = n;
	m_currentPlan->inverse = inverse;
	m_currentPlan->in = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * n);
	m_currentPlan->out = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * n);
	QTime t;
	t.start();
	m_currentPlan->plan = fftwf_plan_dft_1d(n, m_currentPlan->in, m_currentPlan->out, inverse ? FFTW_BACKWARD : FFTW_FORWARD, FFTW_PATIENT);
	m_globalPlanMutex.unlock();
	qDebug("FFT: creating FFTW plan (n=%d,%s) took %dms", n, inverse ? "inverse" : "forward", t.elapsed());
	m_plans.push_back(m_currentPlan);
}

void FFTWEngine::transform()
{
	if(m_currentPlan != NULL)
		fftwf_execute(m_currentPlan->plan);
}

Complex* FFTWEngine::in()
{
	if(m_currentPlan != NULL)
		return reinterpret_cast<Complex*>(m_currentPlan->in);
	else return NULL;
}

Complex* FFTWEngine::out()
{
	if(m_currentPlan != NULL)
		return reinterpret_cast<Complex*>(m_currentPlan->out);
	else return NULL;
}

QMutex FFTWEngine::m_globalPlanMutex;

void FFTWEngine::freeAll()
{
	for(Plans::iterator it = m_plans.begin(); it != m_plans.end(); ++it) {
		fftwf_destroy_plan((*it)->plan);
		fftwf_free((*it)->in);
		fftwf_free((*it)->out);
		delete *it;
	}
	m_plans.clear();
}