示例#1
0
    def tempo_search(db, Key, tempo):
        """
        ::

            Static tempo-invariant search
            Returns search results for query resampled over a range of tempos.
        """
        if not db.configCheck():
            print("Failed configCheck in query spec.")
            print(db.configQuery)
            return None
        prop = 1. / tempo  # the proportion of original samples required for new tempo
        qconf = db.configQuery.copy()
        X = db.retrieve_datum(Key)
        P = db.retrieve_datum(Key, powers=True)
        X_m = pylab.mat(X.mean(0))
        X_resamp = pylab.array(
            adb.resample_vector(X - pylab.mat(pylab.ones(X.shape[0])).T * X_m,
                                prop))
        X_resamp += pylab.mat(pylab.ones(X_resamp.shape[0])).T * X_m
        P_resamp = pylab.array(adb.resample_vector(P, prop))
        seqStart = int(pylab.around(qconf['seqStart'] * prop))
        qconf['seqStart'] = seqStart
        seqLength = int(pylab.around(qconf['seqLength'] * prop))
        qconf['seqLength'] = seqLength
        tmpconf = db.configQuery
        db.configQuery = qconf
        res = db.query_data(featData=X_resamp, powerData=P_resamp)
        res_resorted = adb.sort_search_result(res.rawData)
        db.configQuery = tmpconf
        return res_resorted
示例#2
0
    def resample_vector(data, prop):
        """
        ::

            resample the columns of data by a factor of prop e.g. 0.75, 1.25,...)
        """
        new_features = resample(data, pylab.around(data.shape[0] * prop))
        return new_features
def mu_law(a, mu=256, MAX=None):
    mu = mu - 1
    a = P.array(a)
    MAX = a.max() if MAX is None else MAX
    a = a / MAX
    y = (1 + P.sign(a) * P.log(1 + mu * abs(a)) / P.log(1 + mu)) / 2
    inds = P.around(y * mu).astype(P.uint8)
    return inds
示例#4
0
    def set_seq_length(self, seq_length, query_duration):
        """
        ::

             check if we are using duration (in seconds) and set seq_length
             according to adb.delta_time, otherwise just use seq_length
        """
        if query_duration:
            seq_length = int(pylab.around(query_duration / self.adb.delta_time))
        if not seq_length:
            print("ERROR: You must specify a sequence length or query_duration")
            raise
        return seq_length
示例#5
0
def variable_phase_vocoder(D, times_steps, hop_length=None):
    n_fft = 2 * (D.shape[0] - 1)

    if hop_length is None:
        hop_length = int(n_fft // 4)

    # time_steps = P.arange(0, D.shape[1], rate, dtype=P.double)
    # time_steps = P.concatenate([
    #   P.arange(0, D.shape[1]/2, .5, dtype=P.double),
    #   P.arange(D.shape[1]/2, D.shape[1], 2, dtype=P.double)
    #   ])

    # Create an empty output array
    d_stretch = P.zeros((D.shape[0], len(time_steps)), D.dtype, order='F')

    # Expected phase advance in each bin
    phi_advance = P.linspace(0, P.pi * hop_length, D.shape[0])

    # Phase accumulator; initialize to the first sample
    phase_acc = P.angle(D[:, 0])

    # Pad 0 columns to simplify boundary logic
    D = P.pad(D, [(0, 0), (0, 2)], mode='constant')

    for (t, step) in enumerate(time_steps):
        columns = D[:, int(step):int(step + 2)]

        # Weighting for linear magnitude interpolation
        alpha = P.mod(step, 1.0)
        mag = ((1.0 - alpha) * abs(columns[:, 0]) + alpha * abs(columns[:, 1]))

        # Store to output array
        d_stretch[:, t] = mag * P.exp(1.j * phase_acc)

        # Compute phase advance
        dphase = (P.angle(columns[:, 1]) - P.angle(columns[:, 0]) -
                  phi_advance)

        # Wrap to -pi:pi range
        dphase = dphase - 2.0 * P.pi * P.around(dphase / (2.0 * P.pi))

        # Accumulate phase
        phase_acc += phi_advance + dphase

    return d_stretch
示例#6
0
 def initialize_search(self, t_chan, seq_length, tempo=1.0):
     """
     Initialize search parameters: include list for t_chan, exclude list for !t_chan,
     Key format is nnnnnncc where nnnnnn is the track id, and cc is the tc id.
     """
     adb = self.adb
     if tempo != 1.0:
         seq_lower_bound = int(pylab.around(seq_length / tempo))
     else:
         seq_lower_bound = seq_length
     #print "sequence-lower-bound = ", seq_lower_bound
     gt_orig_keys, gt_orig_len = self.get_gt_lists(t_chan)
     gt_orig = zip(gt_orig_keys, gt_orig_len)
     gt_list_keys, gt_list_len = self.lower_bound_list_by_length(
         gt_orig, seq_lower_bound, tempo)
     gt_list = zip(gt_list_keys, gt_list_len)
     #print "GT query / retrieval list length = ", len(gt_list_keys)
     tc_keys, tc_lens = self.get_adb_lists(t_chan)
     excl_keys, excl_lengths = self.upper_bound_list_by_length(
         zip(tc_keys, tc_lens), seq_lower_bound)
     #print "Database exclude list length = ", len(excl_keys)
     includeKeys = list(pylab.setdiff1d(tc_keys, excl_keys))
     adb.configQuery[
         'absThres'] = 0.0  # We'll take care of probability threshold in distance
     adb.configQuery['accumulation'] = 'track'  # per-track accumulation
     adb.configQuery['npoints'] = 1  # closest matching shingle
     adb.configQuery['ntracks'] = len(
         includeKeys)  # How many tracks to report
     adb.configQuery['distance'] = 'euclidean'
     adb.configQuery['radius'] = 0.0
     adb.configQuery['seqLength'] = seq_length
     adb.configQuery['seqStart'] = 0
     adb.configQuery['exhaustive'] = True  # all sub-sequences search
     adb.configQuery['hopSize'] = 1  # all sub-sequences search with hop 1
     adb.configQuery[
         'includeKeys'] = includeKeys  # include the non GT_ITEMs in search
     adb.configQuery['excludeKeys'] = [
     ]  #excludeKeys # exclude the GT_ITEM from search
     if not self.adb.configCheck():
         print("Invalid query configuartion")
         raise
     return gt_list, gt_orig
示例#7
0
    def initialize_search(self, seq_length, tempo=1.0):
        """
        ::

            Initializes the evaluation loop search parameters
             build sequence length lower-bound list of included GT items >= seq_length
             build sequence length upper-bound list of excluded database items < seq_length
             set adb.configQuery parameters based on seq_length, tempo, and ground_truth
            returns gt_lower_bound_list, gt_orig_list
        """
        if tempo != 1.0:
            seq_lower_bound = int(pylab.around(seq_length / tempo))
        else:
            seq_lower_bound = seq_length
        print("sequence-lower-bound = ", seq_lower_bound)
        gt_orig_keys, gt_orig_len = self.get_gt_lists()
        gt_orig = zip(gt_orig_keys, gt_orig_len)
        gt_list_keys, gt_list_len = self.lower_bound_list_by_length(
            gt_orig, seq_lower_bound, tempo)
        gt_list = zip(gt_list_keys, gt_list_len)
        print("GT query / retrieval list length = ", len(gt_list_keys))
        excl_keys, excl_lengths = self.upper_bound_list_by_length(
            self.adb.liszt(), seq_lower_bound)
        print("Database exclude list length = ", len(excl_keys))
        if len(excl_keys):
            self.adb.configQuery['excludeKeys'] = list(excl_keys)
        else:
            self.adb.configQuery['excludeKeys'] = []
        self.adb.configQuery['seqStart'] = 0
        self.adb.configQuery['seqLength'] = seq_length
        self.adb.configQuery['accumulation'] = 'track'
        self.adb.configQuery['distance'] = 'euclidean'
        self.adb.configQuery['radius'] = 0.0
        self.adb.configQuery['ntracks'] = len(self.adb.liszt())
        self.adb.configQuery['npoints'] = 1
        if not self.adb.configCheck():
            print("Invalid query configuartion")
            raise
        return gt_list, gt_orig
C._h = curr_h

for _ in range(3):
    print()
print("Comparison of bandwidths for Palmer")
for h, ll in sorted(LL.items()):
    print("h: {0:.2f},\tll: {1:.8f}".format(h, ll))
for h, ll in sorted(LL_static.items()):
    print("h: {0:.2f},\tll: {1:.8f}".format(h, ll))

for h, item in enumerate(items):
    if not (h + 1) in [6, 13]:
        continue
    params = P.array(item.get_params())
    s1 = len(params) // 2
    a, b = map(list, P.around([params[:s1], params[s1:]], 2))
    i = P.searchsorted(strokes[h], pars[h])
    a.insert(i, "N/A")
    b.insert(i, "N/A")
    p = P.array(item(theta.reshape(-1, 1), [strokes[h]]))
    P.plot(theta, p, linewidth=2)
    P.ylabel("Probability of getting stroke count")
    P.xlabel(r"$\theta$ (ability)")
    P.legend([
        "{s} strokes, a={aa}, b={bb}.".format(s=strokes[h][j],
                                              aa=a[j],
                                              bb=b[j])
        for j in range(len(strokes[h]))
    ])
    P.title(
        "Hole \#{h}, Par {p}"\