-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfft.cpp
486 lines (413 loc) · 13.8 KB
/
fft.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include "Python.h"
#include <cmath>
#include <complex>
#include <fftw3.h>
#ifdef _OPENMP
#include <omp.h>
// OpenMP scheduling method
#ifndef OMP_SCHEDULER
#define OMP_SCHEDULER dynamic
#endif
#endif
#define NO_IMPORT_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL psr_ARRAY_API
#include "numpy/arrayobject.h"
#include "numpy/npy_math.h"
#include "psr.hpp"
/*
Load in FFTW wisdom. Based on the read_wisdom function in PRESTO.
*/
void read_wisdom(char *filename, PyObject *m) {
int status = 0;
FILE *wisdomfile;
wisdomfile = fopen(filename, "r");
if( wisdomfile != NULL ) {
status = fftwf_import_wisdom_from_file(wisdomfile);
PyModule_AddObject(m, "useWisdom", PyBool_FromLong(status));
fclose(wisdomfile);
} else {
PyModule_AddObject(m, "useWisdom", PyBool_FromLong(status));
}
}
template<typename InType, typename OutType>
void pulsar_engine(long nStand,
long nSamps,
long nFFT,
int nChan,
InType const* data,
double const* window,
OutType* fdomain) {
// Setup
long ij, i, j, k;
Py_BEGIN_ALLOW_THREADS
// Create the FFTW plan
Complex32 *inP, *in;
inP = (Complex32*) fftwf_malloc(sizeof(Complex32) * nChan);
fftwf_plan p;
p = fftwf_plan_dft_1d(nChan,
reinterpret_cast<fftwf_complex*>(inP),
reinterpret_cast<fftwf_complex*>(inP),
FFTW_FORWARD, FFTW_ESTIMATE);
// FFT
long secStart;
#ifdef _OPENMP
#pragma omp parallel default(shared) private(in, secStart, i, j, k)
#endif
{
in = (Complex32*) fftwf_malloc(sizeof(Complex32) * nChan);
#ifdef _OPENMP
#pragma omp for schedule(OMP_SCHEDULER)
#endif
for(ij=0; ij<nStand*nFFT; ij++) {
i = ij / nFFT;
j = ij % nFFT;
secStart = nSamps * i + nChan*j;
for(k=0; k<nChan; k++) {
in[k] = Complex32(*(data + 2*secStart + 2*k + 0), *(data + 2*secStart + 2*k + 1));
if( window != NULL ) {
in[k] *= *(window + k);
}
}
fftwf_execute_dft(p,
reinterpret_cast<fftwf_complex*>(in),
reinterpret_cast<fftwf_complex*>(in));
for(k=0; k<nChan/2+nChan%2; k++) {
*(fdomain + nFFT*nChan*i + nFFT*(k + nChan/2) + j) = in[k] / (float) sqrt(nChan);
}
for(k=nChan/2+nChan%2; k<nChan; k++) {
*(fdomain + nFFT*nChan*i + nFFT*(k - nChan/2 - nChan%2) + j) = in[k] / (float) sqrt(nChan);
}
}
fftwf_free(in);
}
fftwf_destroy_plan(p);
fftwf_free(inP);
Py_END_ALLOW_THREADS
}
PyObject *PulsarEngineRaw(PyObject *self, PyObject *args, PyObject *kwds) {
PyObject *signals, *signalsF=NULL;
PyArrayObject *data=NULL, *dataF=NULL;
int nChan = 64;
long nStand, nSamps, nFFT;
char const* kwlist[] = {"signals", "LFFT", "signalsF", NULL};
if(!PyArg_ParseTupleAndKeywords(args, kwds, "O|iO", const_cast<char **>(kwlist), &signals, &nChan, &signalsF)) {
PyErr_Format(PyExc_RuntimeError, "Invalid parameters");
goto fail;
}
// Bring the data into C and make it usable
data = (PyArrayObject *) PyArray_ContiguousFromObject(signals, NPY_COMPLEX64, 2, 2);
if( data == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input signals array to 2-D complex64");
goto fail;
}
// Get the properties of the data
nStand = (long) PyArray_DIM(data, 0);
nSamps = (long) PyArray_DIM(data, 1);
// Find out how large the output array needs to be and initialize it
nFFT = nSamps / nChan;
npy_intp dims[3];
dims[0] = (npy_intp) nStand;
dims[1] = (npy_intp) nChan;
dims[2] = (npy_intp) (nSamps/nChan);
if( signalsF != NULL && signalsF != Py_None ) {
dataF = (PyArrayObject *) PyArray_ContiguousFromObject(signalsF, NPY_COMPLEX64, 3, 3);
if(dataF == NULL) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast output signalsF array to 3-D complex64");
goto fail;
}
if(PyArray_DIM(dataF, 0) != dims[0]) {
PyErr_Format(PyExc_RuntimeError, "signalsF has an unexpected number of stands");
goto fail;
}
if(PyArray_DIM(dataF, 1) != dims[1]) {
PyErr_Format(PyExc_RuntimeError, "signalsF has an unexpected number of channels");
goto fail;
}
if(PyArray_DIM(dataF, 2) != dims[2]) {
PyErr_Format(PyExc_RuntimeError, "signalsF has an unexpected number of FFT windows");
goto fail;
}
} else {
dataF = (PyArrayObject*) PyArray_EMPTY(3, dims, NPY_COMPLEX64, 0);
if(dataF == NULL) {
PyErr_Format(PyExc_MemoryError, "Cannot create output array");
goto fail;
}
}
#define LAUNCH_PULSAR_ENGINE(IterType) \
pulsar_engine<IterType>(nStand, nSamps, nFFT, nChan, \
(IterType const*) PyArray_DATA(data), \
NULL, \
(Complex32*) PyArray_DATA(dataF))
switch( PyArray_TYPE(data) ){
case( NPY_INT8 ): LAUNCH_PULSAR_ENGINE(int8_t); break;
case( NPY_COMPLEX64 ): LAUNCH_PULSAR_ENGINE(float); break;
default: PyErr_Format(PyExc_RuntimeError, "Unsupport input data type"); goto fail;
}
#undef LAUNCH_PULSAR_ENGINE
signalsF = Py_BuildValue("O", PyArray_Return(dataF));
Py_XDECREF(data);
Py_XDECREF(dataF);
return signalsF;
fail:
Py_XDECREF(data);
Py_XDECREF(dataF);
return NULL;
}
char PulsarEngineRaw_doc[] = PyDoc_STR(\
"Perform a series of Fourier transforms on complex-valued data to get sub-\n\
integration data with linear polarization\n\
\n\
Input arguments are:\n\
* signals: 2-D numpy.complex64 (stands by samples) array of data to FFT\n\
\n\
Input keywords are:\n\
* LFFT: number of FFT channels to make (default=64)\n\
\n\
Outputs:\n\
* sub-integration: 2-D numpy.complex64 (stands by channels) of FFT'd data\n\
");
PyObject *PulsarEngineRawWindow(PyObject *self, PyObject *args, PyObject *kwds) {
PyObject *signals, *window, *signalsF=NULL;
PyArrayObject *data=NULL, *win=NULL, *dataF=NULL;
int nChan = 64;
long nStand, nSamps, nFFT;
char const* kwlist[] = {"signals", "window", "LFFT", "signalsF", NULL};
if(!PyArg_ParseTupleAndKeywords(args, kwds, "OO|iO", const_cast<char **>(kwlist), &signals, &window, &nChan, &signalsF)) {
PyErr_Format(PyExc_RuntimeError, "Invalid parameters");
goto fail;
}
// Bring the data into C and make it usable
data = (PyArrayObject *) PyArray_ContiguousFromObject(signals, NPY_COMPLEX64, 2, 2);
win = (PyArrayObject *) PyArray_ContiguousFromObject(window, NPY_DOUBLE, 1, 1);
if( data == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input signals array to 2-D complex64");
goto fail;
}
if( win == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input window array to 1-D double");
goto fail;
}
// Get the properties of the data
nStand = (long) PyArray_DIM(data, 0);
nSamps = (long) PyArray_DIM(data, 1);
if( PyArray_DIM(win, 0) != nChan ) {
PyErr_Format(PyExc_RuntimeError, "Window length does not match requested FFT length");
goto fail;
}
// Find out how large the output array needs to be and initialize it
nFFT = nSamps / nChan;
npy_intp dims[3];
dims[0] = (npy_intp) nStand;
dims[1] = (npy_intp) nChan;
dims[2] = (npy_intp) (nSamps/nChan);
if( signalsF != NULL && signalsF != Py_None ) {
dataF = (PyArrayObject *) PyArray_ContiguousFromObject(signalsF, NPY_COMPLEX64, 3, 3);
if(dataF == NULL) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast output signalsF array to 3-D complex64");
Py_XDECREF(data);
return NULL;
}
if(PyArray_DIM(dataF, 0) != dims[0]) {
PyErr_Format(PyExc_RuntimeError, "signalsF has an unexpected number of stands");
Py_XDECREF(data);
Py_XDECREF(dataF);
return NULL;
}
if(PyArray_DIM(dataF, 1) != dims[1]) {
PyErr_Format(PyExc_RuntimeError, "signalsF has an unexpected number of channels");
Py_XDECREF(data);
Py_XDECREF(dataF);
return NULL;
}
if(PyArray_DIM(dataF, 2) != dims[2]) {
PyErr_Format(PyExc_RuntimeError, "signalsF has an unexpected number of FFT windows");
Py_XDECREF(data);
Py_XDECREF(dataF);
return NULL;
}
} else {
dataF = (PyArrayObject*) PyArray_EMPTY(3, dims, NPY_COMPLEX64, 0);
if(dataF == NULL) {
PyErr_Format(PyExc_MemoryError, "Cannot create output array");
Py_XDECREF(data);
return NULL;
}
}
#define LAUNCH_PULSAR_ENGINE(IterType) \
pulsar_engine<IterType>(nStand, nSamps, nFFT, nChan, \
(IterType const*) PyArray_DATA(data), \
(double const*) PyArray_DATA(win), \
(Complex32*) PyArray_DATA(dataF))
switch( PyArray_TYPE(data) ){
case( NPY_INT8 ): LAUNCH_PULSAR_ENGINE(int8_t); break;
case( NPY_COMPLEX64 ): LAUNCH_PULSAR_ENGINE(float); break;
default: PyErr_Format(PyExc_RuntimeError, "Unsupport input data type"); goto fail;
}
#undef LAUNCH_PULSAR_ENGINE
signalsF = Py_BuildValue("O", PyArray_Return(dataF));
Py_XDECREF(data);
Py_XDECREF(win);
Py_XDECREF(dataF);
return signalsF;
fail:
Py_XDECREF(data);
Py_XDECREF(win);
Py_XDECREF(dataF);
return NULL;
}
char PulsarEngineRawWindow_doc[] = PyDoc_STR(\
"Similar to PulsarEngineRaw but has an additional arugment of the window\n\
function to apply to the data prior to the FFT.\n\
\n\
Input arguments are:\n\
* signals: 2-D numpy.complex64 (stands by samples) array of data to FFT\n\
* window: 1-D numpy.float64 array of the window to use (must be the same\n\
length at the FFT)\n\
\n\
Input keywords are:\n\
* LFFT: number of FFT channels to make (default=64)\n\
\n\
Outputs:\n\
* sub-integration: 2-D numpy.complex64 (stands by channels) of FFT'd data\n\
");
PyObject *PhaseRotator(PyObject *self, PyObject *args, PyObject *kwds) {
PyObject *signals, *f1, *f2, *signalsF=NULL;
PyArrayObject *data=NULL, *freq1=NULL, *freq2=NULL, *dataF=NULL;
double delay;
long ij, i, j, k, nStand, nSamps, nChan, nFFT;
char const* kwlist[] = {"signals", "freq1", "freq2", "delays", "signalsF", NULL};
if(!PyArg_ParseTupleAndKeywords(args, kwds, "OOOd|O", const_cast<char **>(kwlist), &signals, &f1, &f2, &delay, &signalsF)) {
PyErr_Format(PyExc_RuntimeError, "Invalid parameters");
goto fail;
}
// Bring the data into C and make it usable
data = (PyArrayObject *) PyArray_ContiguousFromObject(signals, NPY_COMPLEX64, 3, 3);
freq1 = (PyArrayObject *) PyArray_ContiguousFromObject(f1, NPY_DOUBLE, 1, 1);
freq2 = (PyArrayObject *) PyArray_ContiguousFromObject(f2, NPY_DOUBLE, 1, 1);
if( data == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input signals array to 3-D complex64");
goto fail;
}
if( freq1 == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input f1 array to 1-D double");
goto fail;
}
if( freq1 == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input f2 array to 1-D double");
goto fail;
}
// Get the properties of the data
nStand = (long) PyArray_DIM(data, 0);
nChan = (long) PyArray_DIM(data, 1);
nFFT = (long) PyArray_DIM(data, 2);
nSamps = nChan*nFFT;
// Validate
if( PyArray_DIM(freq1, 0) != nChan ) {
PyErr_Format(PyExc_ValueError, "Frequency array 1 has different dimensions than rawSpectra");
goto fail;
}
if( PyArray_DIM(freq2, 0) != nChan ) {
PyErr_Format(PyExc_ValueError, "Frequency array 2 has different dimensions than rawSpectra");
goto fail;
}
// Find out how large the output array needs to be and initialize it
npy_intp dims[3];
dims[0] = (npy_intp) nStand;
dims[1] = (npy_intp) nChan;
dims[2] = (npy_intp) nFFT;
if( signalsF != NULL && signalsF != Py_None ) {
dataF = (PyArrayObject *) PyArray_ContiguousFromObject(signalsF, NPY_COMPLEX64, 3, 3);
if(dataF == NULL) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast output signalsF array to 3-D complex64");
Py_XDECREF(data);
return NULL;
}
if(PyArray_DIM(dataF, 0) != dims[0]) {
PyErr_Format(PyExc_RuntimeError, "signalsF has an unexpected number of stands");
Py_XDECREF(data);
Py_XDECREF(dataF);
return NULL;
}
if(PyArray_DIM(dataF, 1) != dims[1]) {
PyErr_Format(PyExc_RuntimeError, "signalsF has an unexpected number of channels");
Py_XDECREF(data);
Py_XDECREF(dataF);
return NULL;
}
if(PyArray_DIM(dataF, 2) != dims[2]) {
PyErr_Format(PyExc_RuntimeError, "signalsF has an unexpected number of FFT windows");
Py_XDECREF(data);
Py_XDECREF(dataF);
return NULL;
}
} else {
dataF = (PyArrayObject*) PyArray_ZEROS(3, dims, NPY_COMPLEX64, 0);
if(dataF == NULL) {
PyErr_Format(PyExc_MemoryError, "Cannot create output array");
goto fail;
}
}
Py_BEGIN_ALLOW_THREADS
// Go!
long secStart;
double tempF;
Complex32 const *a;
Complex32 *d;
double const *b, *c;
a = (Complex32 const*) PyArray_DATA(data);
b = (double const*) PyArray_DATA(freq1);
c = (double const*) PyArray_DATA(freq2);
d = (Complex32 *) PyArray_DATA(dataF);
#ifdef _OPENMP
#pragma omp parallel default(shared) private(secStart, i, j, k, tempF)
#endif
{
#ifdef _OPENMP
#pragma omp for schedule(OMP_SCHEDULER)
#endif
for(ij=0; ij<nStand*nChan; ij++) {
i = ij / nChan;
j = ij % nChan;
secStart = nSamps*i + nFFT*j;
if( i/2 == 0 ) {
tempF = *(b + j);
} else {
tempF = *(c + j);
}
for(k=0; k<nFFT; k++) {
*(d + secStart + k) = Complex64(*(a + secStart + k)) * exp(TPI*tempF*delay);
}
}
}
Py_END_ALLOW_THREADS
signalsF = Py_BuildValue("O", PyArray_Return(dataF));
Py_XDECREF(data);
Py_XDECREF(freq1);
Py_XDECREF(freq2);
Py_XDECREF(dataF);
return signalsF;
fail:
Py_XDECREF(data);
Py_XDECREF(freq1);
Py_XDECREF(freq2);
Py_XDECREF(dataF);
return NULL;
}
char PhaseRotator_doc[] = PyDoc_STR(\
"Given the output of PulsarEngineRaw, apply a sub-sample delay as a phase\n\
rotation to each channel\n\
\n\
Input arguments are:\n\
* signals: 3-D numpy.complex64 (stands by channels by integrations) array\n\
of data to FFT\n\
* freq1: 1-D numpy.float64 array of frequencies for each channel for the\n\
first two stands in signals\n\
* freq2: 1-D numpy.float64 array of frequencies for each channel for the\n\
second two stands in signals\n\
* delay: delay in seconds to apply\n\
\n\
Outputs:\n\
* signals: 3-D numpy.complex64 (stands by channels by integrations) of the\n\
phase-rotated spectra data\n\
");