-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdedispersion.cpp
More file actions
443 lines (380 loc) · 13.5 KB
/
Copy pathdedispersion.cpp
File metadata and controls
443 lines (380 loc) · 13.5 KB
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
#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"
/*
getCoherentSampleSize - Estimate the number of samples needed to
successfully apply coherent dedispersion to a data stream.
*/
long getCoherentSampleSize(double centralFreq, double sampleRate, double DM) {
double delayBand;
long samples;
delayBand = DM*DCONST * (pow(1e6/(centralFreq-sampleRate/2.0), 2) - pow(1e6/(centralFreq+sampleRate/2.0), 2));
delayBand *= sampleRate;
samples = (long) ceil( log(delayBand)/log(2.0) );
if( samples < 0 ) {
samples = 0;
}
samples = (long) 1<<samples;
samples *= 2;
return samples;
}
/*
getFFTChannelFreq - Compute the frequency of a given FFT channel.
*/
double getFFTChannelFreq(long i, long LFFT, double centralFreq, double sampleRate) {
long N;
double val;
N = (LFFT-1) / 2 + 1;
val = sampleRate / (double) LFFT;
if( i < N ) {
return i * val + centralFreq;
} else {
return (i - N - LFFT/2) * val + centralFreq;
}
}
/*
chirpFunction - Chirp function for coherent dedispersion for a given set of
frequencies (in Hz). Based on Equation (6) of "Pulsar Observations II --
Coherent Dedispersion, Polarimetry, and Timing" By Stairs, I. H.
*/
void chirpFunction(long LFFT, double centralFreq, double sampleRate, double DM, Complex32 *chirp) {
int i;
double freqMHz;
double fMHz0, fMHz1;
// Find the center of the band in MHz
fMHz0 = centralFreq / 1e6;
// Compute the chirp
for(i=0; i<LFFT; i++) {
freqMHz = getFFTChannelFreq(i, LFFT, centralFreq, sampleRate) / 1e6;
fMHz1 = freqMHz - fMHz0;
*(chirp + i) = exp(-TPI*DCONST*1e6 * DM*fMHz1*fMHz1 / (fMHz0*fMHz0 * freqMHz));
}
}
PyObject *MultiChannelCD(PyObject *self, PyObject *args, PyObject *kwds) {
PyObject *drxData, *spectraFreq1, *spectraFreq2, *prevData, *nextData, *drxDataF=NULL;
PyArrayObject *data=NULL, *freq1=NULL, *freq2=NULL;
PyArrayObject *pData=NULL, *nData=NULL, *dataF=NULL;
double sRate, DM;
long i, j, k, l, nStand, nChan, nFFT;
char const* kwlist[] = {"rawSpectra", "freq1", "freq2", "sampleRate", "DM", "prevRawSpectra", "nextRawSpectra", "outRawSpectra", NULL};
if(!PyArg_ParseTupleAndKeywords(args, kwds, "OOOddOO|O", const_cast<char **>(kwlist), &drxData, &spectraFreq1, &spectraFreq2, &sRate, &DM, &prevData, &nextData, &drxDataF)) {
PyErr_Format(PyExc_RuntimeError, "Invalid parameters");
goto fail;
}
// Bring the data into C and make it usable
data = (PyArrayObject *) PyArray_ContiguousFromObject(drxData, NPY_COMPLEX64, 3, 3);
freq1 = (PyArrayObject *) PyArray_ContiguousFromObject(spectraFreq1, NPY_DOUBLE, 1, 1);
freq2 = (PyArrayObject *) PyArray_ContiguousFromObject(spectraFreq2, NPY_DOUBLE, 1, 1);
pData = (PyArrayObject *) PyArray_ContiguousFromObject(prevData, NPY_COMPLEX64, 3, 3);
nData = (PyArrayObject *) PyArray_ContiguousFromObject(nextData, NPY_COMPLEX64, 3, 3);
if( data == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input drxData array to 3-D complex64");
goto fail;
}
if( freq1 == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input spectraFreq1 to 1-D double");
goto fail;
}
if( freq2 == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input spectraFreq2 to 1-D double");
goto fail;
}
if( pData == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input prevData array to 3-D complex64");
goto fail;
}
if( nData == NULL ) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast input nextData array to 3-D complex64");
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);
// Validate
if( PyArray_DIM(freq1, 0) != nChan ) {
PyErr_Format(PyExc_ValueError, "freq1 array has different dimensions than rawSpectra");
goto fail;
}
if( PyArray_DIM(freq2, 0) != nChan ) {
PyErr_Format(PyExc_ValueError, "freq2 array has different dimensions than rawSpectra");
goto fail;
}
if( PyArray_DIM(data, 0) != PyArray_DIM(pData, 0) ) {
PyErr_Format(PyExc_ValueError, "prevRawSpectra array has different stand dimension than rawSpectra");
goto fail;
}
if( PyArray_DIM(data, 1) != PyArray_DIM(pData, 1) ) {
PyErr_Format(PyExc_ValueError, "prevRawSpectra array has different channel dimension than rawSpectra");
goto fail;
}
if( PyArray_DIM(data, 2) != PyArray_DIM(pData, 2) ) {
PyErr_Format(PyExc_ValueError, "prevRawSpectra array has different FFT count dimension than rawSpectra");
goto fail;
}
if( PyArray_DIM(data, 0) != PyArray_DIM(nData, 0) ) {
PyErr_Format(PyExc_ValueError, "nextRawSpectra array has different stand dimension than rawSpectra");
goto fail;
}
if( PyArray_DIM(data, 1) != PyArray_DIM(nData, 1) ) {
PyErr_Format(PyExc_ValueError, "nextRawSpectra array has different channel dimension than rawSpectra");
goto fail;
}
if( PyArray_DIM(data, 2) != PyArray_DIM(nData, 2) ) {
PyErr_Format(PyExc_ValueError, "nextRawSpectra array has different FFT count dimension than rawSpectra");
goto fail;
}
// Find out how large the output arrays needs to be and initialize then
npy_intp dims[3];
dims[0] = (npy_intp) nStand;
dims[1] = (npy_intp) nChan;
dims[2] = (npy_intp) nFFT;
if( drxDataF != NULL && drxDataF != Py_None ) {
dataF = (PyArrayObject *) PyArray_ContiguousFromObject(drxDataF, NPY_COMPLEX64, 3, 3);
if(dataF == NULL) {
PyErr_Format(PyExc_RuntimeError, "Cannot cast output outRawSpectra array to 3-D complex64");
goto fail;
}
if(PyArray_DIM(dataF, 0) != dims[0]) {
PyErr_Format(PyExc_RuntimeError, "outRawSpectra has an unexpected number of stands");
goto fail;
}
if(PyArray_DIM(dataF, 1) != dims[1]) {
PyErr_Format(PyExc_RuntimeError, "outRawSpectra has an unexpected number of channels");
goto fail;
}
if(PyArray_DIM(dataF, 2) != dims[2]) {
PyErr_Format(PyExc_RuntimeError, "outRawSpectra has an unexpected number of FFT windows");
goto fail;
}
} else {
dataF = (PyArrayObject*) PyArray_ZEROS(3, dims, NPY_COMPLEX64, 0);
if(dataF == NULL) {
PyErr_Format(PyExc_MemoryError, "Cannot create output data array");
goto fail;
}
}
Py_BEGIN_ALLOW_THREADS
// Get access to the frequency information
double *cf1, *cf2;
cf1 = (double *) PyArray_DATA(freq1);
cf2 = (double *) PyArray_DATA(freq2);
// Create the FFTW plans
long N;
Complex32 *inP;
fftwf_plan *plansF, *plansB;
plansF = (fftwf_plan *) malloc(nStand/2*nChan*sizeof(fftwf_plan));
plansB = (fftwf_plan *) malloc(nStand/2*nChan*sizeof(fftwf_plan));
for(j=0; j<nChan; j++) {
for(i=0; i<nStand; i+=2) {
//Compute the number of FFT channels to use
if( i/2 == 0 ) {
N = getCoherentSampleSize(*(cf1 + j), sRate, DM);
} else {
N = getCoherentSampleSize(*(cf2 + j), sRate, DM);
}
inP = (Complex32 *) fftwf_malloc(N*sizeof(Complex32));
*(plansF + i/2*nChan + j) = fftwf_plan_dft_1d(N,
reinterpret_cast<fftwf_complex*>(inP),
reinterpret_cast<fftwf_complex*>(inP),
FFTW_FORWARD, FFTW_ESTIMATE);
*(plansB + i/2*nChan + j) = fftwf_plan_dft_1d(N,
reinterpret_cast<fftwf_complex*>(inP),
reinterpret_cast<fftwf_complex*>(inP),
FFTW_BACKWARD, FFTW_ESTIMATE);
fftwf_free(inP);
}
}
// Go!
long nSets, start, stop, secStartX, secStartY;
double cFreq;
Complex32 *chirp;
Complex32 *d0, *d1, *d2, *dF;
Complex32 *inX, *inY;
d0 = (Complex32 *) PyArray_DATA(pData);
d1 = (Complex32 *) PyArray_DATA(data);
d2 = (Complex32 *) PyArray_DATA(nData);
dF = (Complex32 *) PyArray_DATA(dataF);
#ifdef _OPENMP
#pragma omp parallel default(shared) private(secStartX, secStartY, i, j, k, l, N, nSets, start, stop, cFreq, chirp, inX, inY)
#endif
{
#ifdef _OPENMP
#pragma omp for schedule(OMP_SCHEDULER)
#endif
for(j=0; j<nChan; j++) {
for(i=0; i<nStand; i+=2) {
// Section start offset
secStartX = i*nChan*nFFT + j*nFFT;
secStartY = (i+1)*nChan*nFFT + j*nFFT;
// Get the correct center frequency to use
if( i/2 == 0 ) {
cFreq = *(cf1 + j);
} else {
cFreq = *(cf2 + j);
}
//Compute the number of FFT channels to use
N = getCoherentSampleSize(cFreq, sRate, DM);
// Compute the number of windows we need to use for CD
nSets = nFFT / N;
// Compute the chirp function
chirp = (Complex32 *) malloc(N*sizeof(Complex32));
chirpFunction(N, cFreq, sRate, DM, chirp);
// Create the FFTW array
inX = (Complex32 *) fftwf_malloc(N*sizeof(Complex32));
inY = (Complex32 *) fftwf_malloc(N*sizeof(Complex32));
// Loop over the sets
for(l=0; l<2*nSets+1; l++) {
start = N/2*l - N/4;
stop = start + N;
// Load in the data
if( start < 0 ) {
// "Previous" buffering
for(k=0; k<-start; k++) {
inX[k] = *(d0 + secStartX + k + nFFT + start);
inY[k] = *(d0 + secStartY + k + nFFT + start);
}
// Current data
for(k=-start; k<N; k++) {
inX[k] = *(d1 + secStartX + k + start);
inY[k] = *(d1 + secStartY + k + start);
}
} else if( stop > nFFT ) {
// Current data
if( start < nFFT ) {
for(k=0; k<nFFT-start; k++) {
inX[k] = *(d1 + secStartX + k + start);
inY[k] = *(d1 + secStartY + k + start);
}
}
// "Next" buffering
for(k=nFFT-start; k<N; k++) {
inX[k] = *(d2 + secStartX + k - nFFT + start);
inY[k] = *(d2 + secStartY + k - nFFT + start);
}
} else {
// Current data
for(k=0; k<N; k++) {
inX[k] = *(d1 + secStartX + k + start);
inY[k] = *(d1 + secStartY + k + start);
}
}
// Forward FFT
fftwf_execute_dft(*(plansF + i/2*nChan + j),
reinterpret_cast<fftwf_complex*>(inX),
reinterpret_cast<fftwf_complex*>(inX));
fftwf_execute_dft(*(plansF + i/2*nChan + j),
reinterpret_cast<fftwf_complex*>(inY),
reinterpret_cast<fftwf_complex*>(inY));
// Chirp
for(k=0; k<N; k++) {
inX[k] *= *(chirp + k) / (float) N;
inY[k] *= *(chirp + k) / (float) N;
}
// Backward FFT
fftwf_execute_dft(*(plansB + i/2*nChan + j),
reinterpret_cast<fftwf_complex*>(inX),
reinterpret_cast<fftwf_complex*>(inX));
fftwf_execute_dft(*(plansB + i/2*nChan + j),
reinterpret_cast<fftwf_complex*>(inY),
reinterpret_cast<fftwf_complex*>(inY));
// Save
start = N/2*l;
stop = start + N/2;
if( stop > nFFT ) {
stop = nFFT;
}
for(k=start; k<stop; k++) {
*(dF + secStartX + k) = inX[k - start + N/4];
*(dF + secStartY + k) = inY[k - start + N/4];
}
if( stop == nFFT ) {
break;
}
}
// Cleanup
free(chirp);
fftwf_free(inX);
fftwf_free(inY);
}
}
}
// Cleanup
for(j=0; j<nChan; j++) {
for(i=0; i<nStand; i+=2) {
fftwf_destroy_plan(*(plansF + i/2*nChan + j));
fftwf_destroy_plan(*(plansB + i/2*nChan + j));
}
}
free(plansF);
free(plansB);
Py_END_ALLOW_THREADS
drxDataF = Py_BuildValue("O", PyArray_Return(dataF));
Py_XDECREF(data);
Py_XDECREF(freq1);
Py_XDECREF(freq2);
Py_XDECREF(pData);
Py_XDECREF(nData);
Py_XDECREF(dataF);
return drxDataF;
fail:
Py_XDECREF(data);
Py_XDECREF(freq1);
Py_XDECREF(freq2);
Py_XDECREF(pData);
Py_XDECREF(nData);
Py_XDECREF(dataF);
return NULL;
}
char MultiChannelCD_doc[] = PyDoc_STR(\
"Given the output of one of the 'PulsarEngine' functions and information about\n\
the time and frequencies, apply coherent dedispersion to the data and return a\n\
two-element tuple giving the time and dedispersed data.\n\
\n\
Input arguments are:\n\
* rawSpectra - 3-D numpy.complex64 (stands by channels by samples) of raw\n\
spectra data generated by 'PulsarEngineRaw' or 'PulsarEngineWindow'\n\
* freq1 - 1-D numpy.float64 (channels) array of frequencies for the first\n\
set of two stands in Hz\n\
* freq2 - 1-D numpy.float64 (channels) array of frequency for the second\n\
set of two stands in Hz\n\
* sampleRate - Channelized data sample rate in Hz\n\
* DM - dispersion measure in pc cm^-3 to dedisperse at\n\
* prevRawSpectra - 3-D numpy.complex64 (stands by channels by samples) of\n\
the previously input data\n\
* prevRawSpectra - 3-D numpy.complex64 (stands by channels by samples) of\n\
the following input data\n\
\n\
Outputs:\n\
* dedispRawSpectra - 3-D numpy.complex64 (stands by channels by samples) of\n\
the coherently dedispersed spectra\n\
\n\
.. note::\n\
\tThere are a few things that look a little strange here. First, the\n\
\tsample rate specified is that of the output spectra. This is related\n\
\tto the input data range and channel count via <input rate> / <channel\n\
\tcount>. Second, the time/prevTime/nextTime, etc. variables might be\n\
\ta little confusing. Consider the following time/data flow in a file:\n\
\t t0/d0, t1/d1, t2/d2, t3/d3, ...\n\
\tFor the first call to MultiDisp() the arguments are:\n\
\t * t1/d1 are time/rawSpectra\n\
\t * t0/d0 are prevTime/prevRawSpectra\n\
\t * t2/d2 are nextTime/nextRawSpectra\n\
\tand for the subsequent call to MultiDisp() they are:\n\
\t * t2/d2 are time/rawSpectra\n\
\t * t1/d1 are prevTime/prevRawSpectra\n\
\t * t3/d3 are nextTime/nextRawSpectra\n\
");