2018-01-29 15:14:50 +00:00
|
|
|
include "config.pxi"
|
|
|
|
|
2018-02-14 15:56:31 +00:00
|
|
|
setTracerLevel(15)
|
2018-02-09 10:56:49 +00:00
|
|
|
cdef extern from "cblas.h":
|
|
|
|
int openblas_get_num_threads()
|
|
|
|
void openblas_set_num_threads(int)
|
|
|
|
|
|
|
|
# If we touch this variable: we get segfaults when running from
|
|
|
|
# Spyder!
|
2018-02-10 20:14:17 +00:00
|
|
|
# openblas_set_num_threads(8)
|
2018-02-09 10:56:49 +00:00
|
|
|
# print("Number of threads: ",
|
|
|
|
# openblas_get_num_threads())
|
|
|
|
|
|
|
|
def cls():
|
|
|
|
clearScreen()
|
2018-02-10 20:14:17 +00:00
|
|
|
# cls()
|
2018-02-06 11:01:27 +00:00
|
|
|
|
2018-01-29 15:14:50 +00:00
|
|
|
cdef extern from "fft.h":
|
|
|
|
ctypedef struct c_Fft "Fft"
|
2018-02-10 20:14:17 +00:00
|
|
|
c_Fft* Fft_alloc(us nfft)
|
2018-01-29 15:14:50 +00:00
|
|
|
void Fft_free(c_Fft*)
|
|
|
|
void Fft_fft(c_Fft*,dmat * timedate,cmat * res) nogil
|
|
|
|
us Fft_nfft(c_Fft*)
|
|
|
|
|
2018-02-12 18:32:23 +00:00
|
|
|
|
|
|
|
|
2018-01-29 15:14:50 +00:00
|
|
|
cdef class Fft:
|
|
|
|
cdef:
|
|
|
|
c_Fft* _fft
|
|
|
|
|
2018-02-10 20:14:17 +00:00
|
|
|
def __cinit__(self, us nfft):
|
|
|
|
self._fft = Fft_alloc(nfft)
|
2018-01-29 15:14:50 +00:00
|
|
|
if self._fft == NULL:
|
|
|
|
raise RuntimeError('Fft allocation failed')
|
|
|
|
|
|
|
|
def __dealloc__(self):
|
|
|
|
if self._fft!=NULL:
|
|
|
|
Fft_free(self._fft)
|
|
|
|
|
|
|
|
def fft(self,d[::1,:] timedata):
|
|
|
|
|
|
|
|
cdef us nfft = Fft_nfft(self._fft)
|
2018-02-10 20:14:17 +00:00
|
|
|
cdef us nchannels = timedata.shape[1]
|
2018-01-29 15:14:50 +00:00
|
|
|
assert timedata.shape[0] ==nfft
|
|
|
|
|
2018-02-09 10:56:49 +00:00
|
|
|
result = np.empty((nfft//2+1,nchannels),
|
|
|
|
dtype=NUMPY_COMPLEX_TYPE,
|
|
|
|
order='F')
|
|
|
|
|
2018-01-29 15:14:50 +00:00
|
|
|
# result[:,:] = np.nan+1j*np.nan
|
2018-02-06 11:01:27 +00:00
|
|
|
cdef c[::1,:] result_view = result
|
|
|
|
cdef cmat r = cmat_foreign(result.shape[0],
|
|
|
|
result.shape[1],
|
|
|
|
&result_view[0,0])
|
2018-02-09 10:56:49 +00:00
|
|
|
|
2018-02-06 11:01:27 +00:00
|
|
|
cdef dmat t = dmat_foreign(timedata.shape[0],
|
|
|
|
timedata.shape[1],
|
|
|
|
&timedata[0,0])
|
2018-01-29 15:14:50 +00:00
|
|
|
|
|
|
|
Fft_fft(self._fft,&t,&r)
|
|
|
|
|
2018-02-06 11:01:27 +00:00
|
|
|
dmat_free(&t)
|
|
|
|
cmat_free(&r)
|
|
|
|
|
2018-01-29 15:14:50 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
cdef extern from "window.h":
|
|
|
|
ctypedef enum WindowType:
|
2018-02-10 20:14:17 +00:00
|
|
|
Hann
|
|
|
|
Hamming
|
|
|
|
Rectangular
|
|
|
|
Bartlett
|
|
|
|
Blackman
|
|
|
|
|
|
|
|
# Export these constants to Python
|
|
|
|
hann = Hann
|
|
|
|
hamming = Hamming
|
|
|
|
rectangular = Rectangular
|
|
|
|
bartlett = Bartlett
|
|
|
|
blackman = Blackman
|
2018-01-29 15:14:50 +00:00
|
|
|
|
|
|
|
cdef extern from "ps.h":
|
|
|
|
ctypedef struct c_PowerSpectra "PowerSpectra"
|
|
|
|
c_PowerSpectra* PowerSpectra_alloc(const us nfft,
|
2018-02-06 11:01:27 +00:00
|
|
|
const WindowType wt)
|
2018-02-10 20:14:17 +00:00
|
|
|
|
2018-02-06 11:01:27 +00:00
|
|
|
void PowerSpectra_compute(const c_PowerSpectra* ps,
|
2018-01-29 15:14:50 +00:00
|
|
|
const dmat * timedata,
|
|
|
|
cmat * result)
|
|
|
|
|
|
|
|
|
|
|
|
void PowerSpectra_free(c_PowerSpectra*)
|
|
|
|
|
2018-02-06 11:01:27 +00:00
|
|
|
cdef class PowerSpectra:
|
|
|
|
cdef:
|
|
|
|
c_PowerSpectra* _ps
|
|
|
|
|
2018-02-13 06:57:50 +00:00
|
|
|
def __cinit__(self, us nfft,us window=rectangular):
|
|
|
|
self._ps = PowerSpectra_alloc(nfft,<WindowType> window)
|
2018-02-06 11:01:27 +00:00
|
|
|
if self._ps == NULL:
|
|
|
|
raise RuntimeError('PowerSpectra allocation failed')
|
|
|
|
|
|
|
|
def compute(self,d[::1,:] timedata):
|
|
|
|
cdef:
|
|
|
|
us nchannels = timedata.shape[1]
|
|
|
|
us nfft = timedata.shape[0]
|
|
|
|
int rv
|
|
|
|
dmat td
|
|
|
|
cmat result_mat
|
2018-02-09 10:56:49 +00:00
|
|
|
|
|
|
|
|
2018-02-06 11:01:27 +00:00
|
|
|
td = dmat_foreign(nfft,
|
|
|
|
nchannels,
|
|
|
|
&timedata[0,0])
|
|
|
|
|
|
|
|
# The array here is created in such a way that the strides
|
|
|
|
# increase with increasing dimension. This is required for
|
|
|
|
# interoperability with the C-code, that stores all
|
|
|
|
# cross-spectra in a 2D matrix, where the first axis is the
|
|
|
|
# frequency axis, and the second axis corresponds to a certain
|
|
|
|
# cross-spectrum, as C_ij(f) = result[freq,i+j*nchannels]
|
|
|
|
|
|
|
|
result = np.empty((nfft//2+1,nchannels,nchannels),
|
|
|
|
dtype = NUMPY_COMPLEX_TYPE,
|
|
|
|
order='F')
|
2018-02-09 10:56:49 +00:00
|
|
|
|
2018-02-06 11:01:27 +00:00
|
|
|
cdef c[::1,:,:] result_view = result
|
|
|
|
|
|
|
|
result_mat = cmat_foreign(nfft//2+1,
|
|
|
|
nchannels*nchannels,
|
|
|
|
&result_view[0,0,0])
|
|
|
|
|
2018-02-09 10:56:49 +00:00
|
|
|
|
|
|
|
|
2018-02-06 11:01:27 +00:00
|
|
|
PowerSpectra_compute(self._ps,&td,&result_mat)
|
|
|
|
|
|
|
|
dmat_free(&td)
|
|
|
|
cmat_free(&result_mat)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def __dealloc__(self):
|
|
|
|
if self._ps != NULL:
|
|
|
|
PowerSpectra_free(self._ps)
|
|
|
|
|
|
|
|
|
|
|
|
cdef extern from "aps.h":
|
|
|
|
ctypedef struct c_AvPowerSpectra "AvPowerSpectra"
|
|
|
|
c_AvPowerSpectra* AvPowerSpectra_alloc(const us nfft,
|
|
|
|
const us nchannels,
|
|
|
|
d overlap_percentage,
|
|
|
|
const WindowType wt)
|
|
|
|
|
|
|
|
cmat* AvPowerSpectra_addTimeData(const c_AvPowerSpectra* ps,
|
|
|
|
const dmat * timedata)
|
|
|
|
|
|
|
|
|
|
|
|
void AvPowerSpectra_free(c_AvPowerSpectra*)
|
2018-02-10 20:14:17 +00:00
|
|
|
us AvPowerSpectra_getAverages(const c_AvPowerSpectra*);
|
2018-02-06 11:01:27 +00:00
|
|
|
|
|
|
|
cdef class AvPowerSpectra:
|
|
|
|
cdef:
|
|
|
|
c_AvPowerSpectra* aps
|
|
|
|
us nfft, nchannels
|
|
|
|
|
2018-02-09 10:56:49 +00:00
|
|
|
def __cinit__(self,us nfft,
|
|
|
|
us nchannels,
|
|
|
|
d overlap_percentage,
|
|
|
|
us window=rectangular):
|
2018-02-06 11:01:27 +00:00
|
|
|
self.aps = AvPowerSpectra_alloc(nfft,
|
|
|
|
nchannels,
|
|
|
|
overlap_percentage,
|
2018-02-09 10:56:49 +00:00
|
|
|
<WindowType> window)
|
2018-02-06 11:01:27 +00:00
|
|
|
self.nchannels = nchannels
|
|
|
|
self.nfft = nfft
|
|
|
|
|
|
|
|
if self.aps == NULL:
|
|
|
|
raise RuntimeError('AvPowerSpectra allocation failed')
|
|
|
|
|
|
|
|
def __dealloc__(self):
|
|
|
|
if self.aps:
|
|
|
|
AvPowerSpectra_free(self.aps)
|
2018-02-10 20:14:17 +00:00
|
|
|
def getAverages(self):
|
|
|
|
return AvPowerSpectra_getAverages(self.aps)
|
|
|
|
|
2018-02-06 11:01:27 +00:00
|
|
|
def addTimeData(self,d[::1,:] timedata):
|
|
|
|
"""!
|
|
|
|
Adds time data, returns current result
|
|
|
|
"""
|
|
|
|
cdef:
|
|
|
|
us nsamples = timedata.shape[0]
|
|
|
|
us nchannels = timedata.shape[1]
|
|
|
|
dmat td
|
|
|
|
cmat* result_ptr
|
|
|
|
|
|
|
|
if nsamples < self.nfft:
|
|
|
|
raise RuntimeError('Number of samples should be > nfft')
|
|
|
|
if nchannels != self.nchannels:
|
|
|
|
raise RuntimeError('Invalid number of channels')
|
|
|
|
|
|
|
|
td = dmat_foreign(nsamples,
|
|
|
|
nchannels,
|
|
|
|
&timedata[0,0])
|
|
|
|
|
|
|
|
result_ptr = AvPowerSpectra_addTimeData(self.aps,
|
|
|
|
&td)
|
|
|
|
|
|
|
|
# The array here is created in such a way that the strides
|
|
|
|
# increase with increasing dimension. This is required for
|
|
|
|
# interoperability with the C-code, that stores all
|
|
|
|
# cross-spectra in a 2D matrix, where the first axis is the
|
|
|
|
# frequency axis, and the second axis corresponds to a certain
|
|
|
|
# cross-spectrum, as C_ij(f) = result[freq,i+j*nchannels]
|
|
|
|
|
|
|
|
result = np.empty((self.nfft//2+1,nchannels,nchannels),
|
|
|
|
dtype = NUMPY_COMPLEX_TYPE,
|
|
|
|
order='F')
|
|
|
|
cdef c[::1,:,:] result_view = result
|
|
|
|
cdef cmat res = cmat_foreign(self.nfft//2+1,
|
|
|
|
nchannels*nchannels,
|
|
|
|
&result_view[0,0,0])
|
|
|
|
# Copy result
|
|
|
|
cmat_copy(&res,result_ptr)
|
|
|
|
|
|
|
|
cmat_free(&res)
|
|
|
|
dmat_free(&td)
|
|
|
|
|
|
|
|
return result
|