-
Notifications
You must be signed in to change notification settings - Fork 0
/
fftthread.cpp
67 lines (57 loc) · 1.88 KB
/
fftthread.cpp
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
#include "fftthread.h"
#include "signaldata.h"
fftthread::fftthread(QObject* parent) : QThread(parent), p_plan(0), p_plansize(65536)
{
p_input = fftw_alloc_real(p_plansize*sizeof(double));
p_output = fftw_alloc_real(p_plansize*sizeof(double));
}
fftthread::~fftthread()
{
if( isRunning() ) {
stop();
if( !wait(200000) )
terminate();
}
fftw_free(p_input);
fftw_free(p_output);
}
dataset* fftthread::getDataset()
{
return p_ds;
}
bool fftthread::setDataset(dataset* ds)
{
if( !isRunning() && ds != p_ds ) { //drop datasets if machine is still busy -> too slow
p_ds = ds;
p_ds->flags++;
return true;
}
return false;
}
void fftthread::run()
{
p_stop = false;
for(unsigned int i = 0; i < 4 && !p_stop; i++ ) {
signaldata* inputData = p_ds->channel[i];
if( inputData->size() == 0 )
continue;
memcpy(p_input,inputData->rawData(),inputData->size()*sizeof(double));
memset(p_input+inputData->size(),0,(p_plansize-inputData->size())*sizeof(double)); //zero-padding
if( p_plan == 0 ) { //plan is not yet created
p_plan = fftw_plan_r2r_1d(p_plansize,p_input,p_output,FFTW_R2HC,FFTW_MEASURE); //create a "good" plan to save time in each execute
}
fftw_execute(p_plan);
inputData->setFft(new signaldata());
double* target = inputData->getFft()->rawData(p_plansize); //rawData(size) resizes the data appropriately
for(unsigned int i = 0; i < p_plansize; i++) {
target[i] = sqrt(p_output[i]*p_output[i]+p_output[p_plansize-i]*p_output[p_plansize-i])*2/inputData->size();
}
inputData->getFft()->setInterval(1000.0/p_plansize/inputData->getInterval()); //set frequency to input's fft
}
p_ds->flags--;
}
void fftthread::stop()
{
p_stop = true;
qDebug() << "[fftthread] stop requested";
}