def main():
    sys.stdout.write("Working...")
    sys.stdout.flush()

    # Open infile
    infbfile = filterbank.filterbank(options.infile)

    # Open outfile
    outfbfile = open(options.outname, 'wb')

    # Write header
    header = infbfile.header
    header_params = infbfile.header_params
    write_header(outfbfile, header_params, header)
    sys.stdout.write("\rHeader written... Writing data...")
    sys.stdout.flush()

    # Write file
    write_data(infbfile, outfbfile)
    sys.stdout.write("\rDone!" + " "*50 + "\n")
    sys.stdout.flush()
    
    # Close files
    outfbfile.close()
    infbfile.close()
Exemple #2
0
def main():
    warnings.warn(colour.cstring("Not checking if .fil files are contiguous " \
                                "frequency bands, or the same length, etc.", \
                                'warning'))
    
    sys.stdout.write("Working...")
    sys.stdout.flush()
    
    # Open infiles
    infbfiles = [filterbank.filterbank(infile) for infile in options.infiles]

    outfile = open(options.outname, 'wb')
    
    # Write header
    write_header(infbfiles, outfile)
    sys.stdout.write("\rHeader written... Writing data...")
    sys.stdout.flush()
    
    # Write data
    write_data(infbfiles, outfile)
    sys.stdout.write("\rDone!" + " "*50 + "\n")
    sys.stdout.flush()
    
    outfile.close()
    
    # Close infiles
    for fb in infbfiles:
        fb.close()
def main():
    # Open infile
    infbfile = filterbank.filterbank(options.infile)

    # Write .inf file (open/write/close)
    writeInfoFile(infbfile)

    # Open outfiles
    if infbfile.header['foff'] > 0:
        filenames = ["%s.sub%04d" % (options.outname, subnum) for subnum in range(0,infbfile.header['nchans'])]
    elif infbfile.header['foff'] < 0:
        # Invert subband files with respect to filterbank file.
        filenames = ["%s.sub%04d" % (options.outname, subnum) for subnum in range(infbfile.header['nchans']-1,0-1,-1)]
    else:
        sys.stderr.write("Channel bandwidth is 0! Exiting...\n")
        sys.exit(1)
        
    outfiles = [open(fn, 'wb') for fn in filenames]

    sys.stdout.write("Working...")
    # Loop
    for i in range(1, infbfile.number_of_samples/SAMPLES_PER_READ):
        # Read samples
        data = infbfile.read_Nsamples(SAMPLES_PER_READ)
        # Re-organize data
        data.shape = (SAMPLES_PER_READ, infbfile.header['nchans'])
        data = data.transpose()
        # Loop
        for j in range(0, infbfile.header['nchans']):
            # Write to outfiles
            data[j].tofile(outfiles[j])
    
    # Write leftover samples to outfiles
    num_samples_leftover = infbfile.number_of_samples % SAMPLES_PER_READ
    data = infbfile.read_Nsamples(num_samples_leftover)
    # Re-organize data
    data.shape = (num_samples_leftover, infbfile.header['nchans'])
    data = data.transpose()
    # Loop
    for j in range(0, infbfile.header['nchans']):
        # Write to outfiles
        data[j].tofile(outfiles[j])
    
    # Close infile
    infbfile.close()
    # Close outfiles
    for outfile in outfiles:
        outfile.close()
    
    sys.stdout.write('\rDone!       \n')
    sys.stdout.flush()
Exemple #4
0
    def __init__(self, filfns):
        fbs = [filterbank.filterbank(fn) for fn in filfns]
        startmjds = np.array([fb.header['tstart'] for fb in fbs])
        sortinds = startmjds.argsort()

        self.fbs = [fbs[ii] for ii in sortinds]
        self.filenames = [fb.filename for fb in fbs]
        self.numfiles = len(self.fbs)
        self.startmjds = startmjds[sortinds]
        self.nsamps = np.array([fb.number_of_samples for fb in self.fbs])
        self.tsamp = fbs[0].header['tsamp'] # Assume sample time is uniform over observation
        self.lengths = self.nsamps*self.tsamp # length of each file in seconds

        self.endsamps = np.cumsum(self.nsamps)
        self.endtimes = self.endsamps*self.tsamp
        self.startsamps = np.concatenate(([0],self.endsamps[:-1]))
        self.starttimes = self.startsamps*self.tsamp

        self.obslen = self.endtimes[-1]
        self.number_of_samples = self.endsamps[-1]

        self.fbs[0].compute_frequencies()
        self.frequencies = self.fbs[0].frequencies
        self.nchans = self.fbs[0].header['nchans']
                      help="If --smooth option is used,smoothing to the bandpass before bandpass correction is applied (otherwise no smoothing).", default=0)
    (options, args) = parser.parse_args()
    print(args)
    if len(args) == 0:
        parser.print_help()
        sys.exit(1)
    elif len(args) != 1:
        sys.stderr.write("Only one input file must be provided!\n")
    else:
        options.infile = args[-1]

    dm = options.dm
    filename = options.infile

    if filename.endswith(".fil"):
        fil = filterbank.filterbank(filename)
        total_N = fil.number_of_samples
        tot_freq = fil.header['nchans']
        picklename = re.search('(.*).fil', filename).group(1)
        t_samp = fil.header['tsamp']
        tstart = fil.header['tstart']
        freqs = np.flip(fil.frequencies)

    if filename.endswith(".fits"):
        fits = psrfits.PsrfitsFile(filename)
        total_N = fits.specinfo.N
        t_samp = fits.specinfo.dt
        freqs = np.flip(fits.frequencies)
        if options.AO == True:
            total_N = int(0.2 / t_samp)
        picklename = re.search('(.*).fits', filename).group(1)
Exemple #6
0
def waterfall(filename,
              start,
              duration,
              dm=0,
              mask=False,
              maskfn=None,
              favg=1,
              tavg=1,
              scaleindep=False,
              extra_begin_chan=None,
              extra_end_chan=None):
    """

    """
    if filename.endswith('.fil'):
        rawdatafile = filterbank.filterbank(filename)
        tsamp = rawdatafile.header['tsamp']
        nchans = rawdatafile.header['nchans']
        freqs = rawdatafile.frequencies
        total_N = rawdatafile.number_of_samples
        df = np.abs(freqs[-1] - freqs[0])
        fres = df / int(nchans + 1)
        scan_start = rawdatafile.tstart

    if filename.endswith('.fits'):
        rawdatafile = psrfits.PsrfitsFile(filename)
        tsamp = rawdatafile.tsamp
        nchans = rawdatafile.nchan
        freqs = rawdatafile.frequencies
        total_N = rawdatafile.specinfo.N
        df = np.abs(freqs[-1] - freqs[0])
        fres = df / int(nchans + 1)
        scan_start = rawdatafile.header['STT_IMJD'] + (
            rawdatafile.header['STT_SMJD'] +
            rawdatafile.header['STT_OFFS']) / (24. * 3600.)

    start_bin = np.round(start / tsamp).astype(
        'int')  #convert begin time to bins
    dmdelay_coeff = 4.15e3 * np.abs(1. / freqs[0]**2 - 1. / freqs[-1]**2)
    nbins = np.round(duration / tsamp).astype('int')  #convert duration to bins

    if dm != 0:
        nbinsextra = np.round(
            (duration + dmdelay_coeff * dm) / tsamp).astype('int')
    else:
        nbinsextra = nbins

    # If at end of observation
    if (start_bin + nbinsextra) > total_N - 1:
        nbinsextra = total_N - 1 - start_bin

    data = rawdatafile.get_spectra(start_bin, nbinsextra)

    #masking
    if mask and maskfn:
        data, masked_chans = maskfile(maskfn,
                                      data,
                                      start_bin,
                                      nbinsextra,
                                      extra_begin_chan=extra_begin_chan,
                                      extra_end_chan=extra_end_chan)
    else:
        masked_chans = np.zeros(nchans, dtype=bool)

    data_masked = np.ma.masked_array(data.data)
    data_masked[masked_chans] = np.ma.masked
    data.data = data_masked

    if dm != 0:
        data.dedisperse(dm, padval='mean')

    data.downsample(tavg)

    # scale data
    data_noscale = data
    data = data.scaled(scaleindep)

    return data, data_noscale, nbinsextra, nbins, start, tsamp, fres, scan_start
Exemple #7
0
def analyse(s, Fs, D, L, rank, n=128, l=129, rmax=None):
    """
   Estimation of the complex poles and the associated complex amplitudes for the Exponential Sinusoidal Model (ESM)
   using an adaptive ESPRIT algorithm.

   See Roland Badeau PhD thesis for more information about the algorithms.

   Parameters
   ----------
   s : numpy array (n,)
        input data
   Fs : int
        sampling frequency
   D : int
        number of positive frequency subbands
   L : int, MUST BE ODD (!!!)
        length of the analysis filters
   rank : numpy array of integer (kk x 1, with kk <= D)
        rank of the model in each subband. If rank[k] == 0, then the rank of the model in the subband k
         is estimated automatically
   n : int
        dimension of the data vectors (used for the Hankel matrix constructed on N = n+l-1 samples of s)
   l : int
        number of data vectors for each analysis window ( which size is N = n+l-1)
   rmax : int
        maximum rank of the ESM. Only used if rank[k] == 0. Then the "ESTER criterion" is used to select the rank
        (i.e. number of poles)

   Returns
   -------
   The_z : numpy array of complex numbers (? x ?)
        estimated poles of the sin model in all the subbands
   The_alpha : numpy  array of complex numbers (same size as The_z)
        estimated amplitude of the model

   Copyright (C) 2004-2008 Roland Badeau
   Send bugs and requests to [email protected]
   Translation from Matlab to python : ELB - [email protected] - 08/2018
    """
    M = 2 * D  # total number of subbands (D postive frequency subbands and D negative frequency subbands)
    h = filterbank.filterbank(L, D)  # computation of the analysis filters

    The_alpha = np.zeros([], dtype=np.complex128)
    The_z = np.zeros([], dtype=np.complex128)
    The_Freq = np.zeros([], dtype=np.complex128)
    s2 = lfilter([1, -0.98], 1, s, axis=0)  # pre-emphasis, "!!! axis = 0 !!!
    for k in range(len(rank)):
        y0 = lfilter(h[:, k], 1, s2, axis=0)  # filter bank
        y1 = y0[::D]
        z, alpha = estim(y1, rank[k], n, l, rmax)
        Freq = np.mod(np.angle(z) / (2 * np.pi),
                      1)  # computationn of the frequencies in the subbands
        # INITIALIZATIONS
        Freq2 = np.zeros_like(Freq)  # frequencies in the full bandwidth
        z2 = np.zeros_like(z)  # complex poles in the full bandwidth
        alpha2 = np.zeros_like(
            alpha)  # complex amplitudes in the full bandwidth
        # RE-MAP THE FREQUENCIES IN THE FULL BANDWIDTH
        for t in range(Freq.shape[1]):  # for each analysis time points
            if np.mod(k + 1, 2) == 1:  # for odd subbands
                I = Freq[:,
                         t] < .5  # keep only the frequencies between 0 and 0.5
                # (the others are aliased)
                Freq2[:sum(I), t] = (k + 2 * Freq[I, t]) / M  # re-map
            else:  # case of even subbands
                I = Freq[:,
                         t] >= .5  # keep only the frequencies between 0.5 et 1
                #  (the others are aliased)
                Freq2[:np.sum(I),
                      t] = (k + 2 * (Freq[I, t] - .5)) / M  # re-map

            # re-map the poles in the full bandwidth
            z2[:np.sum(I), t] = np.exp(
                np.log(np.abs(z[I, t])) / D +
                1j * 2 * np.pi * Freq2[:np.sum(I), t])

            # CORRECTION OF THE COMPLEX AMPLITUDES
            V = np.exp(-np.outer(np.log(z2[:np.sum(I), t]),
                                 np.arange(h.shape[0])))  # Vandermonde matrix
            # built on the poles
            alpha2[:np.sum(I),
                   t] = alpha[I, t] / (V.dot(h[:, k]) * V[:, :2].dot(
                       np.array([1, -0.98])))  # compensation
            # for the pre-emphasis step

        # CONCATENATION OF THE PARAMETERS FROM THE SUBBANDS
        if k == 0:
            The_alpha = alpha2
            The_z = z2
            The_Freq = Freq2
        else:
            The_alpha = np.concatenate((The_alpha, alpha2), axis=0)
            The_z = np.concatenate((The_z, z2), axis=0)
            The_Freq = np.concatenate((The_Freq, Freq2), axis=0)

    return The_z, The_alpha
for value in np.arange(int(np.ceil(blocks_without_overlap/2))):
    if np.sum(Y_predict[zz:zz+4]) >= 3: 
        lead = np.append(lead, ones_vec)
    else:                    
        lead = np.append(lead, zeros_vec)
    zz += 4
lead = np.array(lead)    

# get lead-vector that has same length as signal, because we are calculating
# a little bit more than that
lead = lead[0:signal_length]

# divide signal into three filter-bands
lower_border_freq = 250
upper_border_freq = 4000
filt = filterbank.filterbank(fs, lower_border_freq, upper_border_freq)
low_sig, mid_sig, high_sig = filt.filter(signal)

# compress signal in filter-bands depending on where speech was detected
comp = compressor.compressor(fs=fs, ratio=2, mu_auto=True)
gain = 1/4 # -12 dB
compressed_low = comp.compress_mono(low_sig, lead)
compressed_mid = comp.compress_mono(mid_sig, lead)
compressed_high = comp.compress_mono(high_sig, lead)
signal_out = gain * compressed_low + compressed_mid + gain * compressed_high

# write signal into file so that you can (hopefully) hear a difference
sf.write('test_signal_compressed.wav', data=signal_out, samplerate=fs)

# plotting classifier output (lead) and the original audio signal
time_vec = np.linspace(0, signal_length/fs, signal_length )