Example #1
0
    def harmonicFold(self, nfolds=1):
        """Perform Lyne-Ashworth harmonic folding of the power spectrum.

        :param nfolds: number of harmonic folds to perform (def=1)
        :type nfolds: int

        :return: A list of folded spectra where the i :sup:`th` element is the spectrum folded i times.
        :rtype: :func:`list` of :class:`~sigpyproc.FourierSeries.PowerSpectrum`
        """

        sum_ar = self.copy()
        sum_ar_c = as_c(sum_ar)

        nfold1 = 0  #int(self.header.tsamp*2*self.size/maxperiod)
        folds = []
        for ii in range(nfolds):
            nharm = 2**(ii + 1)
            nfoldi = int(max(1, min(nharm * nfold1 - nharm // 2, self.size)))
            harm_ar = np.array([
                int(kk * ll / float(nharm)) for ll in range(nharm)
                for kk in range(1, nharm, 2)
            ]).astype("int32")

            facts_ar = np.array([(kk * nfoldi + nharm / 2) / nharm
                                 for kk in range(1, nharm, 2)]).astype("int32")

            lib.sumHarms(as_c(self), sum_ar_c, as_c(harm_ar), as_c(facts_ar),
                         C.c_int(nharm), C.c_int(self.size), C.c_int(nfoldi))

            new_header = self.header.newHeader(
                {"tsamp": self.header.tsamp * nharm})
            folds.append(PowerSpectrum(sum_ar, new_header))
        return folds
Example #2
0
    def rednoise(self,startwidth=6,endwidth=100,endfreq=1.0):
        """Perform rednoise removal via Presto style method.

        :param startwidth: size of initial array for median calculation
        :type startwidth: int

        :param endwidth: size of largest array for median calculation
        :type endwidth: int

        :param endfreq: remove rednoise up to this frequency
        :type endfreq: float

        :return: whitened fourier series
        :rtype: :class:`~sigpyproc.FourierSeries.FourierSeries`

        """
        out_ar   = np.empty_like(self)
        buf_c1   = np.empty(2*endwidth,dtype="float32")
        buf_c2   = np.empty(2*endwidth,dtype="float32")
        buf_f1   = np.empty(endwidth,dtype="float32")
        lib.rednoise(as_c(self),
                     as_c(out_ar),
                     as_c(buf_c1),
                     as_c(buf_c2),
                     as_c(buf_f1),
                     C.c_int(self.size/2),
                     C.c_float(self.header.tsamp),
                     C.c_int(startwidth),
                     C.c_int(endwidth),
                     C.c_float(endfreq))
        return FourierSeries(out_ar,self.header.newHeader())
Example #3
0
    def rednoise(self, startwidth=6, endwidth=100, endfreq=1.0):
        """Perform rednoise removal via Presto style method.

        :param startwidth: size of initial array for median calculation
        :type startwidth: int

        :param endwidth: size of largest array for median calculation
        :type endwidth: int

        :param endfreq: remove rednoise up to this frequency
        :type endfreq: float

        :return: whitened fourier series
        :rtype: :class:`~sigpyproc.FourierSeries.FourierSeries`

        """
        out_ar = np.empty_like(self)
        buf_c1 = np.empty(2 * endwidth, dtype="float32")
        buf_c2 = np.empty(2 * endwidth, dtype="float32")
        buf_f1 = np.empty(endwidth, dtype="float32")
        lib.rednoise(as_c(self), as_c(out_ar), as_c(buf_c1), as_c(buf_c2),
                     as_c(buf_f1), C.c_int(self.size / 2),
                     C.c_float(self.header.tsamp), C.c_int(startwidth),
                     C.c_int(endwidth), C.c_float(endfreq))
        return FourierSeries(out_ar, self.header.newHeader())
Example #4
0
    def cwrite(self, ar):
        """Write an array to file.

        :param ar: a numpy array
        :type ar: :class:`numpy.ndarray`
        
        .. note::

           Regardless of the dtype of the array argument, the data will be packed
           with a bitsize determined by the nbits attribute of the File instance.
           To change this attribute, use the _setNbits methods.
           It is the responsibility of the user to ensure that values in the array
           do not go beyond the maximum and minimum values allowed by the nbits
           attribute.
        """
        if self.dtype != ar.dtype:
            warnings.warn("Given data (dtype={0}) will be unsafely cast to the \
                          requested dtype={1} before being written out to file"\
                          .format(ar.dtype, self.dtype), stacklevel=2)
            ar = ar.astype(self.dtype, casting='unsafe')

        #The lib.pack function has an assumption that the given array has 8-bit
        #data. If the given array was, say 32-bit floats and the requested nbits
        #is, say 2-bit, then the output will be garbage, hence the casting above is
        #necessary.
        if self.unpack:
            packed = np.empty(int(ar.size * self.bitfact), dtype=self.dtype)
            lib.pack(as_c(ar), as_c(packed), C.c_int(self.nbits),
                     C.c_int(ar.size))
            packed.tofile(self)
        else:
            ar.tofile(self)
Example #5
0
    def harmonicFold(self,nfolds=1):
        """Perform Lyne-Ashworth harmonic folding of the power spectrum.

        :param nfolds: number of harmonic folds to perform (def=1)
        :type nfolds: int

        :return: A list of folded spectra where the i :sup:`th` element is the spectrum folded i times.
        :rtype: :func:`list` of :class:`~sigpyproc.FourierSeries.PowerSpectrum`
        """

        sum_ar    = self.copy()
        sum_ar_c  = as_c(sum_ar)
        
        nfold1 = 0 #int(self.header.tsamp*2*self.size/maxperiod)
        folds = [] 
        for ii in range(nfolds):
            nharm = 2**(ii+1)
            nfoldi =int(max(1,min(nharm*nfold1-nharm/2,self.size)))
            harm_ar = np.array([int(kk*ll/float(nharm)) 
                                for ll in range(nharm) 
                                for kk in range(1,nharm,2)]).astype("int32")

            facts_ar = np.array([(kk*nfoldi+nharm/2)/nharm for kk in range(1,nharm,2)]).astype("int32")

            lib.sumHarms(as_c(self),
                         sum_ar_c,
                         as_c(harm_ar),
                         as_c(facts_ar),
                         C.c_int(nharm),
                         C.c_int(self.size),
                         C.c_int(nfoldi))
            
            new_header = self.header.newHeader({"tsamp":self.header.tsamp*nharm})
            folds.append(PowerSpectrum(sum_ar,new_header))
        return folds
Example #6
0
    def getStats(self, gulp=512):
        """Retrieve channelwise statistics of data.

        :param gulp: number of samples in each read
        :type gulp: int
        
        Function creates four instance attributes:
        
           * :attr:`chan_means`: the mean value of each channel
           * :attr:`chan_stdevs`: the standard deviation of each channel
           * :attr:`chan_max`: the maximum value of each channel
           * :attr:`chan_min`: the minimum value of each channel
        """
        maxima_ar = np.zeros(self.header.nchans, dtype="float32")
        minima_ar = np.zeros(self.header.nchans, dtype="float32")
        means_ar = np.zeros(self.header.nchans, dtype="float32")
        stdev_ar = np.zeros(self.header.nchans, dtype="float32")
        maxima_ar_c = as_c(maxima_ar)
        minima_ar_c = as_c(minima_ar)
        means_ar_c = as_c(means_ar)
        stdev_ar_c = as_c(stdev_ar)
        for nsamps, ii, data in self.readPlan(gulp):
            self.lib.getStats(as_c(data), means_ar_c, stdev_ar_c, maxima_ar_c,
                              minima_ar_c, C.c_int(self.header.nchans),
                              C.c_int(nsamps), C.c_int(ii))

        means_ar /= self.header.nsamples
        stdev_ar = np.sqrt((stdev_ar / self.header.nsamples) - means_ar**2)
        stdev_ar[np.where(np.isnan(stdev_ar))] = 0
        self.chan_means = means_ar
        self.chan_stdevs = stdev_ar
        self.chan_maxima = maxima_ar
        self.chan_minima = minima_ar
Example #7
0
    def downsample(self, tfactor=1, ffactor=1):
        """Downsample data block in frequency and/or time.

        :param tfactor: factor by which to downsample in time       
        :type tfactor: int                                              

        :param ffactor: factor by which to downsample in frequency                   
        :type ffactor: int

        :return: 2 dimensional array of downsampled data
        :rtype: :class:`~sigpyproc.Filterbank.FilterbankBlock`

        .. note::

                ffactor must be a factor of nchans.

        """
        if not self.shape[0] % ffactor == 0:
            raise ValueError("Bad frequency factor given")
        newnsamps = self.shape[1] - self.shape[1] % tfactor
        new_ar = np.empty(newnsamps * self.shape[0] // ffactor // tfactor,
                          dtype="float32")
        ar = self.transpose().ravel().copy()
        self.lib.downsample(as_c(ar), as_c(new_ar), C.c_int(tfactor),
                            C.c_int(ffactor), C.c_int(self.shape[0]),
                            C.c_int(newnsamps))
        new_ar = new_ar.reshape(newnsamps // tfactor,
                                self.shape[0] // ffactor).transpose()
        new_tsamp = self.header.tsamp * tfactor
        new_nchans = self.header.nchans // ffactor
        new_header = self.header.newHeader({
            "tsamp": new_tsamp,
            "nchans": new_nchans
        })
        return FilterbankBlock(new_ar, new_header)
Example #8
0
    def resample(self, accel, jerk=0):
        """Perform time domain resampling to remove acceleration and jerk.

        :param accel: The acceleration to remove from the time series
        :type accel: float
        
        :param jerk: The jerk/jolt to remove from the time series
        :type jerk: float

        :param period: The mimimum period that the resampling 
                       will be sensitive to.
        :type period: float

        :return: resampled time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
        """
        if accel > 0:
            new_size = self.size - 1
        else:
            new_size = self.size
        out_ar = np.zeros(new_size, dtype="float32")
        print new_size
        lib.resample(as_c(self), as_c(out_ar), C.c_int(new_size),
                     C.c_float(accel), C.c_float(self.header.tsamp))

        new_header = self.header.newHeader({
            "nsamples": out_ar.size,
            "accel": accel
        })
        return TimeSeries(out_ar, new_header)
Example #9
0
    def dedisperse(self,dm,gulp=10000):
        """Dedisperse the data to a time series.

        :param dm: dispersion measure to dedisperse to 
        :type dm: float

        :param gulp: number of samples in each read
        :type gulp: int

        :return: a dedispersed time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`

        .. note::
        
               If gulp < maximum dispersion delay, gulp is taken to be twice the maximum dispersion delay.
              
        """
        chan_delays   = self.header.getDMdelays(dm)
        chan_delays_c = as_c(chan_delays)
        max_delay     = int(chan_delays.max())
        gulp          = max(2*max_delay,gulp)
        tim_len       = self.header.nsamples-max_delay
        tim_ar        = np.zeros(tim_len,dtype="float32")
        tim_ar_c      = as_c(tim_ar) 
        for nsamps,ii,data in self.readPlan(gulp,skipback=max_delay):
            self.lib.dedisperse(as_c(data),
                                tim_ar_c,
                                chan_delays_c,
                                C.c_int(max_delay),
                                C.c_int(self.header.nchans), 
                                C.c_int(nsamps), 
                                C.c_int(ii*(gulp-max_delay)))
        return TimeSeries(tim_ar,self.header.newHeader({"nchans":1,"refdm":dm}))
Example #10
0
    def getChan(self, chan, gulp=512):
        """Retrieve a single frequency channel from the data.

        :param chan: channel to retrieve (0 is the highest frequency channel)
        :type chan: int

        :param gulp: number of samples in each read
        :type gulp: int

        :return: selected channel as a time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
        """
        if chan >= self.header.nchans or chan < 0:
            raise ValueError("Selected channel out of range.")
        tim_ar = np.empty(self.header.nsamples, dtype="float32")
        tim_ar_c = as_c(tim_ar)
        for nsamps, ii, data in self.readPlan(gulp):
            self.lib.getChan(as_c(data), tim_ar_c, C.c_int(chan),
                             C.c_int(self.header.nchans), C.c_int(nsamps),
                             C.c_int(ii * gulp))
        return TimeSeries(
            tim_ar,
            self.header.newHeader({
                "channel": chan,
                "refdm": 0.0,
                "nchans": 1
            }))
Example #11
0
    def fold(self, period, accel=0, nbins=50, nints=32):
        """Fold time series into discrete phase and subintegration bins.

        :param period: period in seconds to fold with
        :type period: float

        :param nbins: number of phase bins in output
        :type nbins: int

        :param nints: number of subintegrations in output
        :type nints: int

        :returns: data cube containing the folded data 
        :rtype: :class:`~sigpyproc.FoldedData.FoldedData`  
        """
        if self.size / (nbins * nints) < 10:
            raise ValueError, "nbins x nints is too large for length of data"
        fold_ar = np.zeros(nbins * nints, dtype="float64")
        count_ar = np.zeros(nbins * nints, dtype="int32")
        lib.foldTim(as_c(self), as_c(fold_ar), as_c(count_ar),
                    C.c_double(self.header.tsamp), C.c_double(period),
                    C.c_double(accel), C.c_int(self.size), C.c_int(nbins),
                    C.c_int(nints))
        fold_ar /= count_ar
        fold_ar = fold_ar.reshape(nints, 1, nbins)
        return FoldedData(fold_ar, self.header.newHeader(), period,
                          self.header.refdm, accel)
Example #12
0
    def resample(self,accel,jerk=0):
        """Perform time domain resampling to remove acceleration and jerk.

        :param accel: The acceleration to remove from the time series
        :type accel: float
        
        :param jerk: The jerk/jolt to remove from the time series
        :type jerk: float

        :param period: The mimimum period that the resampling 
                       will be sensitive to.
        :type period: float

        :return: resampled time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
        """
        if accel > 0:
            new_size = self.size-1
        else:
            new_size = self.size
        out_ar = np.zeros(new_size,dtype="float32")
        print new_size
        lib.resample(as_c(self),
                     as_c(out_ar),
                     C.c_int(new_size),
                     C.c_float(accel),
                     C.c_float(self.header.tsamp))

        new_header = self.header.newHeader({"nsamples":out_ar.size,
                                            "accel":accel})
        return TimeSeries(out_ar,new_header)
Example #13
0
    def fold(self,period,accel=0,nbins=50,nints=32):
        """Fold time series into discrete phase and subintegration bins.

        :param period: period in seconds to fold with
        :type period: float

        :param nbins: number of phase bins in output
        :type nbins: int

        :param nints: number of subintegrations in output
        :type nints: int

        :returns: data cube containing the folded data 
        :rtype: :class:`~sigpyproc.FoldedData.FoldedData`  
        """
        if self.size/(nbins*nints) < 10: 
            raise ValueError,"nbins x nints is too large for length of data"
        fold_ar  = np.zeros(nbins*nints,dtype="float64")
        count_ar = np.zeros(nbins*nints,dtype="int32")
        lib.foldTim(as_c(self),
                    as_c(fold_ar),
                    as_c(count_ar),
                    C.c_double(self.header.tsamp),
                    C.c_double(period),
                    C.c_double(accel),
                    C.c_int(self.size),
                    C.c_int(nbins),
                    C.c_int(nints))
        fold_ar/=count_ar
        fold_ar = fold_ar.reshape(nints,1,nbins)
        return FoldedData(fold_ar,
                          self.header.newHeader(),
                          period,
                          self.header.refdm,
                          accel)
Example #14
0
    def applyChannelMask(self,
                         chanmask,
                         outfilename=None,
                         gulp=512,
                         back_compatible=True):
        """Set the data in the given channels to zero.

        :param outfilename: name of the output filterbank file
        :type outfilename: str

        :param chanmask: binary channel mask (0 for bad channel, 1 for good)
        :type chanmask: list

        :param gulp: number of samples in each read
        :type gulp: int

        :param back_compatible: sigproc compatibility flag
        :type back_compatible: bool

        :return: outfile name
        :rtype: str
        """
        if outfilename is None:
            outfilename = f"{self.header.basename}_masked.fil"
        mask = np.array(chanmask).astype("ubyte")
        out_file = self.header.prepOutfile(outfilename,
                                           back_compatible=back_compatible)
        for nsamps, ii, data in self.readPlan(gulp):
            self.lib.maskChannels(as_c(data), as_c(mask),
                                  C.c_int(self.header.nchans), C.c_int(nsamps))
            out_file.cwrite(data)
        return out_file.name
Example #15
0
    def applyChannelMask(self,chanmask,outfilename=None,gulp=512,back_compatible=True):
        """Set the data in the given channels to zero.

        :param outfilename: name of the output filterbank file
        :type outfilename: str

        :param chanmask: binary channel mask (0 for bad channel, 1 for good)
        :type chanmask: list

        :param gulp: number of samples in each read
        :type gulp: int

        :param back_compatible: sigproc compatibility flag
        :type back_compatible: bool

        :return: outfile name
        :rtype: str
        """
        if outfilename is None:
            outfilename = "%s_masked.fil"%(self.header.basename)
        mask = np.array(chanmask).astype("ubyte")
        out_file = self.header.prepOutfile(outfilename,back_compatible=back_compatible)
        for nsamps,ii,data in self.readPlan(gulp):
            self.lib.maskChannels(as_c(data),
                                  as_c(mask),
                                  C.c_int(self.header.nchans),
                                  C.c_int(nsamps))
            out_file.cwrite(data)
        return out_file.name
Example #16
0
    def downsample(self,tfactor=1,ffactor=1):
        """Downsample data block in frequency and/or time.

        :param tfactor: factor by which to downsample in time       
        :type tfactor: int                                              

        :param ffactor: factor by which to downsample in frequency                   
        :type ffactor: int

        :return: 2 dimensional array of downsampled data
        :rtype: :class:`~sigpyproc.Filterbank.FilterbankBlock`

        .. note::

                ffactor must be a factor of nchans.

        """ 
        if not self.shape[0]%ffactor == 0:
            raise ValueError,"Bad frequency factor given"
        newnsamps = self.shape[1] - self.shape[0]%tfactor
        new_ar = np.empty(newnsamps*self.shape[0]/ffactor/tfactor,dtype="float32")
        ar = self.transpose().ravel().copy()
        self.lib.downsample(as_c(ar),
                            as_c(new_ar),
                            C.c_int(tfactor),
                            C.c_int(ffactor),
                            C.c_int(self.shape[0]),
                            C.c_int(newnsamps))
        new_ar = new_ar.reshape(newnsamps//tfactor,self.shape[0]//ffactor).transpose()
        new_tsamp = self.header.tsamp/tfactor
        new_nchans = self.header.nchans//ffactor
        new_header = self.header.newHeader({"tsamp":new_tsamp,"nchans":new_nchans})
        return FilterbankBlock(new_ar,new_header)
Example #17
0
    def dedisperse(self, dm, gulp=10000):
        """Dedisperse the data to a time series.

        :param dm: dispersion measure to dedisperse to 
        :type dm: float

        :param gulp: number of samples in each read
        :type gulp: int

        :return: a dedispersed time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`

        .. note::
        
               If gulp < maximum dispersion delay, gulp is taken to be twice the maximum dispersion delay.
              
        """
        chan_delays = self.header.getDMdelays(dm)
        chan_delays_c = as_c(chan_delays)
        max_delay = int(chan_delays.max())
        gulp = max(2 * max_delay, gulp)
        tim_len = self.header.nsamples - max_delay
        tim_ar = np.zeros(tim_len, dtype="float32")
        tim_ar_c = as_c(tim_ar)
        for nsamps, ii, data in self.readPlan(gulp, skipback=max_delay):
            self.lib.dedisperse(as_c(data), tim_ar_c, chan_delays_c,
                                C.c_int(max_delay),
                                C.c_int(self.header.nchans), C.c_int(nsamps),
                                C.c_int(ii * (gulp - max_delay)))
        return TimeSeries(tim_ar,
                          self.header.newHeader({
                              "nchans": 1,
                              "refdm": dm
                          }))
Example #18
0
    def invertFreq(self,
                   gulp=512,
                   start=0,
                   nsamps=None,
                   filename=None,
                   back_compatible=True):
        """Invert the frequency ordering of the data and write new data to a new file.
        
        :param gulp: number of samples in each read                                                                                      
        :type gulp: int
        
        :param start: start sample                                                                       
        :type start: int                                                                                     
        
        :param nsamps: number of samples in split                                                
        :type nsamps: int

        :param filename: name of output file (defaults to ``basename_inverted.fil``)
        :type filename: string

        :param back_compatible: sigproc compatibility flag (legacy code)
        :type back_compatible: bool
        
        :return: name of output file
        :return type: :func:`str`
        """
        if filename is None:
            filename = f"{self.header.basename}_inverted.fil"

        if nsamps is None:
            size = self.header.nsamples - start
        else:
            size = nsamps
        out_ar = np.empty(size * self.header.nchans, dtype=self.header.dtype)

        if self.header.foff >= 0.0:
            sign = 1.0
        else:
            sign = -1.0
        changes = {
            "fch1":
            self.header.fch1 + sign *
            (self.header.nchans - 1) * self.header.foff,
            "foff":
            self.header.foff * sign * (-1.0)
        }
        #NB bandwidth is +ive by default
        out_file = self.header.prepOutfile(filename,
                                           changes,
                                           nbits=self.header.nbits,
                                           back_compatible=back_compatible)
        for nsamps, ii, data in self.readPlan(gulp, start=start,
                                              nsamps=nsamps):
            self.lib.invertFreq(as_c(data), as_c(out_ar),
                                C.c_int(self.header.nchans), C.c_int(nsamps))
            out_file.cwrite(out_ar[:nsamps * self.header.nchans])
        out_file.close()
        return out_file.name
Example #19
0
 def seek_gpu(self):
     steps = self._get_accel_steps()
     #Pad the time series to force it to have length 2^N
     self._pad()
     #Remove red noise from time series
     self._clean()
     lib.seekGPU(as_c(self.tim), C.c_size_t(self.tim.size), as_c(steps),
                 C.c_size_t(steps.size), C.c_float(self.header.tsamp))
     error_check()
Example #20
0
    def iFFT(self):
        """Perform 1-D complex to real inverse FFT using FFTW3.

        :return: a time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
        """
        tim_ar = np.empty(self.size - 2, dtype="float32")
        lib.ifft(as_c(self), as_c(tim_ar), C.c_int(self.size - 2))
        return TimeSeries.TimeSeries(tim_ar, self.header.newHeader())
Example #21
0
 def __mul__(self, other):
     if type(other) == type(self):
         if other.size != self.size:
             raise Exception("Instances must be the same size")
         else:
             out_ar = np.empty_like(self)
             lib.multiply_fs(as_c(self), as_c(other), as_c(out_ar),
                             C.c_int(self.size))
             return FourierSeries(out_ar, self.header.newHeader())
     else:
         return super(FourierSeries, self).__mul__(other)
Example #22
0
    def iFFT(self):
        """Perform 1-D complex to real inverse FFT using FFTW3.

        :return: a time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
        """
        tim_ar = np.empty(self.size-2,dtype="float32")
        lib.ifft(as_c(self),
                 as_c(tim_ar),
                 C.c_int(self.size-2))
        return TimeSeries(tim_ar,self.header.newHeader())
Example #23
0
 def seek_gpu(self):
     steps = self._get_accel_steps()
     #Pad the time series to force it to have length 2^N
     self._pad()
     #Remove red noise from time series
     self._clean()
     lib.seekGPU(as_c(self.tim),
                 C.c_size_t(self.tim.size),
                 as_c(steps),
                 C.c_size_t(steps.size),
                 C.c_float(self.header.tsamp))
     error_check()
Example #24
0
    def subband(self, dm, nsub, filename=None, gulp=10000):
        """Produce a set of dedispersed subbands from the data.

        :param dm: the DM of the subbands
        :type dm: float
        
        :param nsub: the number of subbands to produce
        :type nsub: int

        :param filename: output file name of subbands (def=basename_DM.subbands)
        :type filename: :func:`str`
        
        :param gulp: number of samples in each read
        :type gulp: int

        :return: name of output subbands file
        :rtype: :func:`str`
        """

        subfactor = self.header.nchans // nsub
        chan_delays = self.header.getDMdelays(dm)
        chan_delays_c = as_c(chan_delays)
        max_delay = int(chan_delays.max())
        gulp = max(2 * max_delay, gulp)
        out_ar = np.empty((gulp - max_delay) * nsub,
                          dtype="float32")  #must be memset to zero in c code
        out_ar_c = as_c(out_ar)
        new_foff = self.header.foff * self.header.nchans // nsub
        new_fch1 = self.header.ftop - new_foff / 2.
        chan_to_sub = np.arange(self.header.nchans, dtype="int32") // subfactor
        chan_to_sub_c = as_c(chan_to_sub)
        changes = {
            "fch1": new_fch1,
            "foff": new_foff,
            "refdm": dm,
            "nchans": nsub,
            "nbits": 32
        }
        if filename is None:
            filename = f"{self.header.basename}_DM{dm:06.2f}.subbands"
        out_file = self.header.prepOutfile(filename,
                                           changes,
                                           nbits=32,
                                           back_compatible=True)

        for nsamps, ii, data in self.readPlan(gulp, skipback=max_delay):
            self.lib.subband(as_c(data), out_ar_c, chan_delays_c,
                             chan_to_sub_c, C.c_int(max_delay),
                             C.c_int(self.header.nchans), C.c_int(nsub),
                             C.c_int(nsamps))
            out_file.cwrite(out_ar[:(nsamps - max_delay) * nsub])
        return filename
Example #25
0
 def rFFT(self):
     """Perform 1-D real to complex forward FFT.
     
     :return: output of FFTW3
     :rtype: :class:`~sigpyproc.FourierSeries.FourierSeries`
     """
     if self.size % 2 == 0:
         fftsize = self.size
     else:
         fftsize = self.size - 1
     fft_ar = np.empty(fftsize + 2, dtype="float32")
     lib.rfft(as_c(self), as_c(fft_ar), fftsize)
     return FourierSeries(fft_ar, self.header.newHeader())
Example #26
0
 def __mul__(self,other):
     if type(other) == type(self):
         if other.size != self.size:
             raise Exception("Instances must be the same size")
         else:
             out_ar = np.empty_like(self)
             lib.multiply_fs(as_c(self),
                             as_c(other),
                             as_c(out_ar),
                             C.c_int(self.size))
             return FourierSeries(out_ar,self.header.newHeader())
     else:
         return super(FourierSeries,self).__mul__(other)
Example #27
0
    def subband(self,dm,nsub,filename=None,gulp=10000):
        """Produce a set of dedispersed subbands from the data.

        :param dm: the DM of the subbands
        :type dm: float
        
        :param nsub: the number of subbands to produce
        :type nsub: int

        :param filename: output file name of subbands (def=basename_DM.subbands)
        :type filename: :func:`str`
        
        :param gulp: number of samples in each read
        :type gulp: int

        :return: name of output subbands file
        :rtype: :func:`str`
        """
        
        subfactor     = self.header.nchans/nsub
        chan_delays   = self.header.getDMdelays(dm)
        chan_delays_c = as_c(chan_delays)
        max_delay     = int(chan_delays.max())
        gulp          = max(2*max_delay,gulp)
        out_ar        = np.empty((gulp-max_delay)*nsub,dtype="float32") #must be memset to zero in c code
        out_ar_c      = as_c(out_ar)
        new_foff      = self.header.foff*self.header.nchans/nsub
        new_fch1      = self.header.ftop-new_foff/2.
        chan_to_sub   = np.arange(self.header.nchans,dtype="int32")/subfactor
        chan_to_sub_c = as_c(chan_to_sub)
        changes       = {"fch1"  :new_fch1,
                         "foff"  :new_foff,
                         "refdm" :dm,
                         "nchans":nsub,
                         "nbits" :32}
        if filename is None:
            filename = "%s_DM%06.2f.subbands"%(self.header.basename,dm)
        out_file = self.header.prepOutfile(filename, changes, nbits=32,
                                           back_compatible=True)
        
        for nsamps,ii,data in self.readPlan(gulp,skipback=max_delay):
            self.lib.subband(as_c(data),
                             out_ar_c,
                             chan_delays_c,
                             chan_to_sub_c,
                             C.c_int(max_delay),
                             C.c_int(self.header.nchans),
                             C.c_int(nsub),
                             C.c_int(nsamps))
            out_file.cwrite(out_ar[:(nsamps-max_delay)*nsub])
        return filename
Example #28
0
    def conjugate(self):
        """Conjugate the Fourier series.

        :return: conjugated Fourier series.
        :rtype: :class:`sigpyproc.FourierSeries.FourierSeries`

        .. note::
        
           Function assumes that the Fourier series is the non-conjugated
           product of a real to complex FFT.
        """
        out_ar = np.empty(2 * self.size - 2, dtype="float32")
        lib.conjugate(as_c(self), as_c(out_ar), C.c_int(self.size))
        return FourierSeries(out_ar, self.header.newHeader())
Example #29
0
    def downsample(self,
                   tfactor=1,
                   ffactor=1,
                   gulp=512,
                   filename=None,
                   back_compatible=True):
        """Downsample data in time and/or frequency and write to file.

        :param tfactor: factor by which to downsample in time
        :type tfactor: int

        :param ffactor: factor by which to downsample in frequency
        :type ffactor: int

        :param gulp: number of samples in each read
        :type gulp: int

        :param filename: name of file to write to (defaults to ``basename_tfactor_ffactor.fil``)
        :type filename: str

        :param back_compatible: sigproc compatibility flag (legacy code)
        :type back_compatible: bool

        :return: output file name
        :rtype: :func:`str`
        """
        if filename is None:
            filename = "%s_f%d_t%d.fil" % (self.header.basename, ffactor,
                                           tfactor)
        if not self.header.nchans % ffactor == 0:
            raise ValueError("Bad frequency factor given")
        if not gulp % tfactor == 0:
            raise ValueError("Gulp must be a multiple of tfactor")
        out_file = self.header.prepOutfile(filename, {
            "tsamp": self.header.tsamp * tfactor,
            "nchans": self.header.nchans / ffactor,
            "foff": self.header.foff * ffactor
        },
                                           back_compatible=back_compatible)

        write_ar = np.zeros(gulp * self.header.nchans / ffactor / tfactor,
                            dtype=self.header.dtype)
        write_ar_c = as_c(write_ar)
        for nsamps, ii, data in self.readPlan(gulp):
            self.lib.downsample(as_c(data), write_ar_c, C.c_int(tfactor),
                                C.c_int(ffactor), C.c_int(self.header.nchans),
                                C.c_int(nsamps))
            out_file.cwrite(write_ar)
        return out_file.name
Example #30
0
 def rFFT(self):
     """Perform 1-D real to complex forward FFT.
     
     :return: output of FFTW3
     :rtype: :class:`~sigpyproc.FourierSeries.FourierSeries`
     """
     if self.size%2 ==0:
         fftsize = self.size
     else:
         fftsize = self.size-1
     fft_ar = np.empty(fftsize+2,dtype="float32")
     lib.rfft(as_c(self),
              as_c(fft_ar),
              fftsize)
     return FourierSeries(fft_ar,self.header.newHeader())
Example #31
0
    def bandpass(self, gulp=512):
        """Sum across each time sample for all frequencies.

        :param gulp: number of samples in each read
        :type gulp: int

        :return: the bandpass of the data
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
        """
        bpass_ar = np.zeros(self.header.nchans, dtype="float32")
        bpass_ar_c = as_c(bpass_ar)
        for nsamps, ii, data in self.readPlan(gulp):
            self.lib.getBpass(as_c(data), bpass_ar_c,
                              C.c_int(self.header.nchans), C.c_int(nsamps))
        return TimeSeries(bpass_ar, self.header.newHeader({"nchans": 1}))
Example #32
0
    def invertFreq(self,gulp=512,start=0,nsamps=None,filename=None,back_compatible=True):
        """Invert the frequency ordering of the data and write new data to a new file.
        
        :param gulp: number of samples in each read                                                                                      
        :type gulp: int
        
        :param start: start sample                                                                       
        :type start: int                                                                                     
        
        :param nsamps: number of samples in split                                                
        :type nsamps: int

        :param filename: name of output file (defaults to ``basename_inverted.fil``)
        :type filename: string

        :param back_compatible: sigproc compatibility flag (legacy code)
        :type back_compatible: bool
        
        :return: name of output file
        :return type: :func:`str`
        """
        if filename is None:
            filename = "%s_inverted.fil"%(self.header.basename)

        if nsamps is None:
            size = self.header.nsamples-start
        else:
            size = nsamps
        out_ar = np.empty(size*self.header.nchans,dtype=self.header.dtype)

        if self.header.foff >= 0.0: 
            sign = 1.0
        else:
            sign = -1.0
        changes    = {"fch1":self.header.fch1+sign*(self.header.nchans-1)*self.header.foff,
                      "foff":self.header.foff*sign*(-1.0)}
        #NB bandwidth is +ive by default
        out_file = self.header.prepOutfile(filename, changes,
                                          nbits=self.header.nbits,
                                          back_compatible=back_compatible)
        for nsamps,ii,data in self.readPlan(gulp,start=start,nsamps=nsamps):
            self.lib.invertFreq(as_c(data),
                                as_c(out_ar), 
                                C.c_int(self.header.nchans),
                                C.c_int(nsamps))
            out_file.cwrite(out_ar[:nsamps*self.header.nchans])
        out_file.close()
        return out_file.name
Example #33
0
    def conjugate(self):
        """Conjugate the Fourier series.

        :return: conjugated Fourier series.
        :rtype: :class:`sigpyproc.FourierSeries.FourierSeries`

        .. note::
        
           Function assumes that the Fourier series is the non-conjugated
           product of a real to complex FFT.
        """
        out_ar = np.empty(2*self.size-2,dtype="float32")
        lib.conjugate(as_c(self),
                      as_c(out_ar),
                      C.c_int(self.size))
        return FourierSeries(out_ar,self.header.newHeader())
def hampel_filter_2d(array2d,
                     threshold,
                     window_size_x=16,
                     window_size_y=16):

    outarray = np.empty(array2d.shape, dtype="float32")

    lib.hampel_filter_2d(as_c(array2d),
                         C.c_int(array2d.shape[0]),
                         C.c_int(array2d.shape[1]),
                         as_c(outarray),
                         C.c_float(threshold),
                         C.c_int(window_size_x),
                         C.c_int(window_size_y))

    return outarray
Example #35
0
    def runningMedian(self, window=10001):
        """Filter time series with a running median.
        
        :param window: width in bins of running median filter
        :type window: int 
        
        :returns: filtered time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`

        .. note::
        
                Window edges will be dealt with only at the start of the time series.
        """
        tim_ar = np.empty_like(self)
        lib.runningMedian(as_c(self), as_c(tim_ar), C.c_int(window),
                          C.c_int(self.size))
        return tim_ar.view(TimeSeries)
Example #36
0
    def formSpec(self, interpolated=True):
        """Form power spectrum.
        
        :param interpolated: flag to set nearest bin interpolation (def=True)
        :type interpolated: bool
        
        :return: a power spectrum
        :rtype: :class:`~sigpyproc.FourierSeries.PowerSpectrum`
        """
        spec_ar = np.empty(self.size / 2, dtype="float32")
        if interpolated:
            lib.formSpecInterpolated(as_c(self), as_c(spec_ar),
                                     C.c_int(self.size / 2))
        else:
            lib.formSpec(as_c(self), as_c(spec_ar), C.c_int(self.size))

        return PowerSpectrum(spec_ar, self.header.newHeader())
Example #37
0
    def bandpass(self,gulp=512):
        """Sum across each time sample for all frequencies.

        :param gulp: number of samples in each read
        :type gulp: int

        :return: the bandpass of the data
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
        """
        bpass_ar = np.zeros(self.header.nchans,dtype="float32")
        bpass_ar_c = as_c(bpass_ar)
        for nsamps,ii,data in self.readPlan(gulp):
            self.lib.getBpass(as_c(data),
                              bpass_ar_c,
                              C.c_int(self.header.nchans),
                              C.c_int(nsamps))
        return TimeSeries(bpass_ar,self.header.newHeader({"nchans":1}))
Example #38
0
    def applyBoxcar(self, width):
        """Apply a boxcar filter to the time series.

        :param width: width in bins of filter
        :type width: int
        
        :return: filtered time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`

        .. note::
        
                Time series returned is of size nsamples-width with width/2 removed removed from either end.
        """
        tim_ar = np.empty_like(self)
        lib.runBoxcar(as_c(self), as_c(tim_ar), C.c_int(width),
                      C.c_int(self.size))
        return tim_ar.view(TimeSeries)
Example #39
0
    def downsample(self,tfactor=1,ffactor=1,gulp=512,filename=None,back_compatible=True):
        """Downsample data in time and/or frequency and write to file.

        :param tfactor: factor by which to downsample in time
        :type tfactor: int

        :param ffactor: factor by which to downsample in frequency
        :type ffactor: int

        :param gulp: number of samples in each read
        :type gulp: int

        :param filename: name of file to write to (defaults to ``basename_tfactor_ffactor.fil``)
        :type filename: str

        :param back_compatible: sigproc compatibility flag (legacy code)
        :type back_compatible: bool

        :return: output file name
        :rtype: :func:`str`
        """
        if filename is None:
            filename = "%s_f%d_t%d.fil"%(self.header.basename,ffactor,tfactor)
        if not self.header.nchans%ffactor == 0:
            raise ValueError,"Bad frequency factor given"
        if not gulp%tfactor == 0:
            raise ValueError,"Gulp must be a multiple of tfactor"
        out_file = self.header.prepOutfile(filename,
                                   {"tsamp":self.header.tsamp*tfactor,
                                    "nchans":self.header.nchans/ffactor,
                                    "foff":self.header.foff*ffactor},
                                   back_compatible=back_compatible)

        write_ar = np.zeros(gulp*self.header.nchans/ffactor/tfactor,dtype=self.header.dtype)
        write_ar_c = as_c(write_ar)
        for nsamps,ii,data in self.readPlan(gulp):
            self.lib.downsample(as_c(data),
                                write_ar_c,
                                C.c_int(tfactor),
                                C.c_int(ffactor),
                                C.c_int(self.header.nchans),
                                C.c_int(nsamps))
            out_file.cwrite(write_ar)
        return out_file.name
Example #40
0
    def runningMedian(self,window=10001):
        """Filter time series with a running median.
        
        :param window: width in bins of running median filter
        :type window: int 
        
        :returns: filtered time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`

        .. note::
        
                Window edges will be dealt with only at the start of the time series.
        """
        tim_ar  = np.empty_like(self)
        lib.runningMedian(as_c(self),
                          as_c(tim_ar),
                          C.c_int(window),
                          C.c_int(self.size))
        return tim_ar.view(TimeSeries)
Example #41
0
    def cread(self, nunits):
        """Read nunits of data from the file.

        :param nunits: number of units to be read from file 
        :type nunits: int
        
        :return: an array containing the read data
        :rtype: :class:`numpy.ndarray`
        """

        count = int(nunits * self.bitfact)
        data = np.fromfile(self, count=count, dtype=self.dtype)
        if self.unpack:
            unpacked = np.empty(nunits, dtype=self.dtype)
            lib.unpack(as_c(data), as_c(unpacked), C.c_int(self.nbits),
                       C.c_int(data.size))
            return unpacked
        else:
            return data
Example #42
0
    def applyBoxcar(self,width):
        """Apply a boxcar filter to the time series.

        :param width: width in bins of filter
        :type width: int
        
        :return: filtered time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`

        .. note::
        
                Time series returned is of size nsamples-width with width/2 removed removed from either end.
        """
        tim_ar  = np.empty_like(self)
        lib.runBoxcar(as_c(self),
                      as_c(tim_ar),
                      C.c_int(width),
                      C.c_int(self.size))
        return tim_ar.view(TimeSeries)
Example #43
0
 def formSpec(self,interpolated=True):
     """Form power spectrum.
     
     :param interpolated: flag to set nearest bin interpolation (def=True)
     :type interpolated: bool
     
     :return: a power spectrum
     :rtype: :class:`~sigpyproc.FourierSeries.PowerSpectrum`
     """
     spec_ar = np.empty(self.size/2,dtype="float32")
     if interpolated:
         lib.formSpecInterpolated(as_c(self),
                                  as_c(spec_ar),
                                  C.c_int(self.size/2))
     else:
         lib.formSpec(as_c(self),
                      as_c(spec_ar),
                      C.c_int(self.size))
     
     return PowerSpectrum(spec_ar,self.header.newHeader())
Example #44
0
    def cwrite(self, ar):
        """Write an array to file.

        :param ar: a numpy array
        :type ar: :class:`numpy.ndarray`
        
        .. note::

           Regardless of the dtype of the array argument, the data will be packed
           with a bitsize determined by the nbits attribute of the File instance.
           To change this attribute, use the _setNbits methods.
        """

        if self.unpack:
            packed = np.empty(int(ar.size * self.bitfact), dtype=self.dtype)
            lib.pack(as_c(ar), as_c(packed), C.c_int(self.nbits),
                     C.c_int(ar.size))
            packed.tofile(self)
        else:
            ar.tofile(self)
Example #45
0
    def splitToChans(self, gulp=1024, back_compatible=True):
        """Split the data into component channels and write each to file.

        :param gulp: number of samples in each read
        :type gulp: int 

        :param back_compatible: sigproc compatibility flag (legacy code)
        :type back_compatible: bool

        :return: names of all files written to disk
        :rtype: :func:`list` of :func:`str`
        
        .. note::
        
                Time series are written to disk with names based on channel number.

        """
        tim_ar = np.empty([self.header.nchans, gulp], dtype="float32")
        tim_ar_c = as_c(tim_ar)
        out_files = [
            self.header.prepOutfile(f"{self.header.basename}_chan{ii:04d}.tim",
                                    {
                                        "nchans": 1,
                                        "nbits": 32,
                                        "data_type": 2
                                    },
                                    back_compatible=back_compatible,
                                    nbits=32)
            for ii in range(self.header.nchans)
        ]
        for nsamps, ii, data in self.readPlan(gulp):
            self.lib.splitToChans(as_c(data), tim_ar_c,
                                  C.c_int(self.header.nchans), C.c_int(nsamps),
                                  C.c_int(gulp))
            for ii, f in enumerate(out_files):
                f.cwrite(tim_ar[ii][:nsamps])

        for f in out_files:
            f.close()

        return [f.name for f in out_files]
Example #46
0
    def cread(self,nunits):
        """Read nunits of data from the file.

        :param nunits: number of units to be read from file 
        :type nunits: int
        
        :return: an array containing the read data
        :rtype: :class:`numpy.ndarray`
        """

        count = int(nunits*self.bitfact)
        data = np.fromfile(self,count=count,dtype=self.dtype)
        if self.unpack:
            unpacked = np.empty(nunits,dtype=self.dtype)
            lib.unpack(as_c(data),
                       as_c(unpacked),
                       C.c_int(self.nbits),
                       C.c_int(data.size))
            return unpacked
        else:
            return data
Example #47
0
    def downsample(self, factor):
        """Downsample the time series.

        :param factor: factor by which time series will be downsampled
        :type factor: int
        
        :return: downsampled time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
 
        .. note::
        
                Returned time series is of size nsamples//factor
        """
        if factor == 1: return self
        newLen = self.size // factor
        tim_ar = np.zeros(newLen, dtype="float32")
        lib.downsampleTim(as_c(self), as_c(tim_ar), C.c_int(factor),
                          C.c_int(newLen))
        return TimeSeries(
            tim_ar,
            self.header.newHeader({'tsamp': self.header.tsamp * factor}))
Example #48
0
    def downsample(self,factor):
        """Downsample the time series.

        :param factor: factor by which time series will be downsampled
        :type factor: int
        
        :return: downsampled time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
 
        .. note::
        
                Returned time series is of size nsamples//factor
        """
        if factor == 1: return self
        newLen = self.size//factor
        tim_ar  = np.zeros(newLen,dtype="float32")
        lib.downsampleTim(as_c(self),
                          as_c(tim_ar),
                          C.c_int(factor),
                          C.c_int(newLen))
        return TimeSeries(tim_ar,self.header.newHeader({'tsamp':self.header.tsamp*factor}))
Example #49
0
    def collapse(self,gulp=512,start=0,nsamps=None):
        """Sum across all frequencies for each time sample.

        :param gulp: number of samples in each read
        :type gulp: int

        :return: A zero-DM time series 
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
        """
        if nsamps is None:
            size = self.header.nsamples-start
        else:
            size = nsamps
        timar = np.zeros(size,dtype="float32")
        timar_c = as_c(timar)
        for nsamps,ii,data in self.readPlan(gulp,start=start,nsamps=nsamps):
            self.lib.getTim(as_c(data),
                            timar_c,
                            C.c_int(self.header.nchans),
                            C.c_int(nsamps),
                            C.c_int(ii*gulp))
        return TimeSeries(timar,self.header.newHeader({"nchans":1,"refdm":0.0}))
Example #50
0
    def collapse(self,gulp=512,start=0,nsamps=None):
        """Sum across all frequencies for each time sample.

        :param gulp: number of samples in each read
        :type gulp: int

        :return: A zero-DM time series 
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
        """
        if nsamps is None:
            size = self.header.nsamples-start
        else:
            size = nsamps
        timar = np.zeros(size,dtype="float32")
        timar_c = as_c(timar)
        for nsamps,ii,data in self.readPlan(gulp,start=start,nsamps=nsamps):
            self.lib.getTim(as_c(data),
                            timar_c,
                            C.c_int(self.header.nchans),
                            C.c_int(nsamps),
                            C.c_int(ii*gulp))
        return TimeSeries(timar,self.header.newHeader({"nchans":1,"refdm":0.0}))
Example #51
0
    def cwrite(self,ar):
        """Write an array to file.

        :param ar: a numpy array
        :type ar: :class:`numpy.ndarray`
        
        .. note::

           Regardless of the dtype of the array argument, the data will be packed
           with a bitsize determined by the nbits attribute of the File instance.
           To change this attribute, use the _setNbits methods.
        """
        
        if self.unpack:
            packed = np.empty(int(ar.size*self.bitfact),dtype=self.dtype)
            lib.pack(as_c(ar),
                     as_c(packed),
                     C.c_int(self.nbits),
                     C.c_int(ar.size))
            packed.tofile(self)
        else:
            ar.tofile(self)
Example #52
0
    def getStats(self,gulp=512):
        """Retrieve channelwise statistics of data.

        :param gulp: number of samples in each read
        :type gulp: int
        
        Function creates four instance attributes:
        
           * :attr:`chan_means`: the mean value of each channel
           * :attr:`chan_stdevs`: the standard deviation of each channel
           * :attr:`chan_max`: the maximum value of each channel
           * :attr:`chan_min`: the minimum value of each channel
        """
        maxima_ar = np.zeros(self.header.nchans,dtype="float32")
        minima_ar = np.zeros(self.header.nchans,dtype="float32")
        means_ar  = np.zeros(self.header.nchans,dtype="float32")
        stdev_ar  = np.zeros(self.header.nchans,dtype="float32")
        maxima_ar_c = as_c(maxima_ar)
        minima_ar_c = as_c(minima_ar)
        means_ar_c  = as_c(means_ar)
        stdev_ar_c  = as_c(stdev_ar)
        for nsamps,ii,data in self.readPlan(gulp):
            self.lib.getStats(as_c(data),
                              means_ar_c,
                              stdev_ar_c,
                              maxima_ar_c,
                              minima_ar_c,
                              C.c_int(self.header.nchans),
                              C.c_int(nsamps),
                              C.c_int(ii))

        means_ar /= self.header.nsamples
        stdev_ar = np.sqrt((stdev_ar/self.header.nsamples)-means_ar**2)
        stdev_ar[np.where(np.isnan(stdev_ar))] = 0        
        self.chan_means = means_ar
        self.chan_stdevs = stdev_ar
        self.chan_maxima = maxima_ar
        self.chan_minima = minima_ar
Example #53
0
    def splitToChans(self,gulp=1024,back_compatible=True):
        """Split the data into component channels and write each to file.

        :param gulp: number of samples in each read
        :type gulp: int 

        :param back_compatible: sigproc compatibility flag (legacy code)
        :type back_compatible: bool

        :return: names of all files written to disk
        :rtype: :func:`list` of :func:`str`
        
        .. note::
        
                Time series are written to disk with names based on channel number.

        """
        tim_ar   = np.empty([self.header.nchans,gulp],dtype="float32")
        tim_ar_c = as_c(tim_ar)
        out_files = [self.header.prepOutfile("%s_chan%04d.tim"%(self.header.basename,ii),
                                             {"nchans":1,"nbits":32,"data_type":2},
                                             back_compatible=back_compatible,nbits=32)
            
                     for ii in xrange(self.header.nchans)]
        for nsamps,ii,data in self.readPlan(gulp):
            self.lib.splitToChans(as_c(data),
                                  tim_ar_c,
                                  C.c_int(self.header.nchans),
                                  C.c_int(nsamps),
                                  C.c_int(gulp))
            for ii,f in enumerate(out_files):
                f.cwrite(tim_ar[ii][:nsamps])
                
        for f in out_files:
            f.close()
            
        return [f.name for f in out_files]
Example #54
0
    def getChan(self,chan,gulp=512):
        """Retrieve a single frequency channel from the data.

        :param chan: channel to retrieve (0 is the highest frequency channel)
        :type chan: int

        :param gulp: number of samples in each read
        :type gulp: int

        :return: selected channel as a time series
        :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries`
        """
        if chan >= self.header.nchans or chan < 0:
            raise ValueError,"Selected channel out of range."
        tim_ar   = np.empty(self.header.nsamples,dtype="float32")
        tim_ar_c = as_c(tim_ar)
        for nsamps,ii,data in self.readPlan(gulp):
            self.lib.getChan(as_c(data),
                             tim_ar_c,
                             C.c_int(chan),
                             C.c_int(self.header.nchans),
                             C.c_int(nsamps),
                             C.c_int(ii*gulp))
        return TimeSeries(tim_ar,self.header.newHeader({"channel":chan,"refdm":0.0,"nchans":1}))
Example #55
0
#!/bin/env python

import numpy
from numpy.random import randn
import ctypes as C
from numpy.ctypeslib import as_ctypes as as_c

lib = C.CDLL("./libspsearch.so")

fftlen = 2**16
fwidth = 4
dfloc = 128

#Make some test data
fakedata = randn(fftlen)
#fakedata=numpy.zeros(fftlen, dtype='float32')
convdata = numpy.zeros(fftlen, dtype='float32')
#fakedata[dfloc]=1

lib.ts_convolve(as_c(fakedata), C.c_int(fwidth), C.c_int(fftlen),
                as_c(convdata))

print convdata[dfloc - fwidth:dfloc + fwidth]
print "Max: ", max(convdata)
print "Max loc:", convdata.argmax()
Example #56
0
    def fold(self,
             period,
             dm,
             accel=0,
             nbins=50,
             nints=32,
             nbands=32,
             gulp=10000):
        """Fold data into discrete phase, subintegration and subband bins. 
        
        :param period: period in seconds to fold with
        :type period: float

        :param dm: dispersion measure to dedisperse to
        :type dm: float

        :param accel: acceleration in m/s/s to fold with
        :type accel: float

        :param nbins: number of phase bins in output
        :type nbins: int

        :param nints: number of subintegrations in output
        :type nints: int

        :param nbands: number of subbands in output
        :type nbands: int

        :param gulp: number of samples in each read
        :type gulp: int

        :return: 3 dimensional data cube
        :rtype: :class:`~sigpyproc.FoldedData.FoldedData`
        
        .. note::

                If gulp < maximum dispersion delay, gulp is taken to be twice the maximum dispersion delay.      

        """
        if np.modf(period / self.header.tsamp)[0] < 0.001:
            print(
                "WARNING: Foldng interval is an integer multiple of the sampling time"
            )
        if nbins > period / self.header.tsamp:
            print(
                "WARNING: Number of phase bins is greater than period/sampling time"
            )
        if (self.header.nsamples * self.header.nchans) // (nbands * nints *
                                                           nbins) < 10:
            raise ValueError("nbands x nints x nbins is too large.")
        nbands = min(nbands, self.header.nchans)
        chan_delays = self.header.getDMdelays(dm)
        chan_delays_c = as_c(chan_delays)
        max_delay = int(chan_delays.max())
        gulp = max(2 * max_delay, gulp)
        fold_ar = np.zeros(nbins * nints * nbands, dtype="float32")
        fold_ar_c = as_c(fold_ar)
        count_ar = np.zeros(nbins * nints * nbands, dtype="int32")
        count_ar_c = as_c(count_ar)
        for nsamps, ii, data in self.readPlan(gulp, skipback=max_delay):
            self.lib.foldFil(as_c(data), fold_ar_c, count_ar_c, chan_delays_c,
                             C.c_int(max_delay), C.c_double(self.header.tsamp),
                             C.c_double(period), C.c_double(accel),
                             C.c_int(self.header.nsamples), C.c_int(nsamps),
                             C.c_int(self.header.nchans), C.c_int(nbins),
                             C.c_int(nints), C.c_int(nbands),
                             C.c_int(ii * (gulp - max_delay)))
        fold_ar /= count_ar
        fold_ar = fold_ar.reshape(nints, nbands, nbins)
        return FoldedData(fold_ar, self.header.newHeader(), period, dm, accel)
Example #57
0
#!/bin/env python

import numpy
from numpy.random import randn 
import ctypes as C
from numpy.ctypeslib import as_ctypes as as_c

lib=C.CDLL("./libspsearch.so")

fftlen=2**16
fwidth=4
dfloc=128

#Make some test data
fakedata=randn(fftlen)
#fakedata=numpy.zeros(fftlen, dtype='float32')
convdata=numpy.zeros(fftlen, dtype='float32')
#fakedata[dfloc]=1

lib.ts_convolve(as_c(fakedata), C.c_int(fwidth), C.c_int(fftlen), as_c(convdata))

print convdata[dfloc-fwidth:dfloc+fwidth]
print "Max: ", max(convdata)
print "Max loc:", convdata.argmax()
Example #58
0
    def fold(self,period,dm,accel=0,nbins=50,nints=32,nbands=32,gulp=10000):
        """Fold data into discrete phase, subintegration and subband bins. 
        
        :param period: period in seconds to fold with
        :type period: float

        :param dm: dispersion measure to dedisperse to
        :type dm: float

        :param accel: acceleration in m/s/s to fold with
        :type accel: float

        :param nbins: number of phase bins in output
        :type nbins: int

        :param nints: number of subintegrations in output
        :type nints: int

        :param nbands: number of subbands in output
        :type nbands: int

        :param gulp: number of samples in each read
        :type gulp: int

        :return: 3 dimensional data cube
        :rtype: :class:`~sigpyproc.FoldedData.FoldedData`
        
        .. note::

                If gulp < maximum dispersion delay, gulp is taken to be twice the maximum dispersion delay.      

        """
        if np.modf(period/self.header.tsamp)[0]<0.001:
            print "WARNING: Foldng interval is an integer multiple of the sampling time"
        if nbins > period/self.header.tsamp:
            print "WARNING: Number of phase bins is greater than period/sampling time"
        if (self.header.nsamples*self.header.nchans)/(nbands*nints*nbins) < 10:
            raise ValueError,"nbands x nints x nbins is too large."
        nbands        = min(nbands,self.header.nchans)
        chan_delays   = self.header.getDMdelays(dm)
        chan_delays_c = as_c(chan_delays)
        max_delay     = int(chan_delays.max())
        gulp          = max(2*max_delay,gulp)
        fold_ar       = np.zeros(nbins*nints*nbands,dtype="float32")
        fold_ar_c     = as_c(fold_ar)
        count_ar      = np.zeros(nbins*nints*nbands,dtype="int32")
        count_ar_c    = as_c(count_ar)
        for nsamps,ii,data in self.readPlan(gulp,skipback=max_delay):
            self.lib.foldFil(as_c(data),
                             fold_ar_c,
                             count_ar_c,
                             chan_delays_c,
                             C.c_int(max_delay),
                             C.c_double(self.header.tsamp),
                             C.c_double(period),
                             C.c_double(accel),
                             C.c_int(self.header.nsamples), 
                             C.c_int(nsamps), 
                             C.c_int(self.header.nchans), 
                             C.c_int(nbins),
                             C.c_int(nints), 
                             C.c_int(nbands), 
                             C.c_int(ii*(gulp-max_delay)))
        fold_ar/=count_ar
        fold_ar = fold_ar.reshape(nints,nbands,nbins)
        return FoldedData(fold_ar,self.header.newHeader(),period,dm,accel)