def test_merge(): """ This should return a sorted (from greatest to least) merged list from list1 and list2. """ list1 = [13, 11, 9, 7, 5, 3, 1] list2 = [14, 12, 10, 8, 6, 4, 2] result = list(util.merge([list1, list2])) assert result[::-1] == sorted(result) assert list(util.merge([[], [1], []])) == [1]
def combine_frequencies(cls, specs): """ Return new spectrogram that contains frequencies from all the spectrograms in spec. Only returns time intersection of all of them. Parameters ---------- spec : list List of spectrograms of which to combine the frequencies into one. """ if not specs: raise ValueError("Need at least one spectrogram.") specs = cls.intersect_time(specs) one = specs[0] dtype_ = max(sp.dtype for sp in specs) fsize = sum(sp.shape[0] for sp in specs) new = np.zeros((fsize, one.shape[1]), dtype=dtype_) freq_axis = np.zeros((fsize,)) for n, (data, row) in enumerate(merge( [ [(sp, n) for n in xrange(sp.shape[0])] for sp in specs ], key=lambda x: x[0].freq_axis[x[1]] )): new[n, :] = data[row, :] freq_axis[n] = data.freq_axis[row] params = { 'time_axis': one.time_axis, # Should be equal 'freq_axis': freq_axis, 'start': one.start, 'end': one.end, 't_delt': one.t_delt, 't_init': one.t_init, 't_label': one.t_label, 'f_label': one.f_label, 'content': one.content, 'instruments': _union(spec.instruments for spec in specs) } return common_base(specs)(new, **params)
def combine_frequencies(cls, specs): """ Return new spectrogram that contains frequencies from all the spectrograms in spec. Only returns time intersection of all of them. Parameters ---------- spec : list List of spectrograms of which to combine the frequencies into one. """ if not specs: raise ValueError("Need at least one spectrogram.") specs = cls.intersect_time(specs) one = specs[0] dtype_ = max(sp.dtype for sp in specs) fsize = sum(sp.shape[0] for sp in specs) new = np.zeros((fsize, one.shape[1]), dtype=dtype_) freq_axis = np.zeros((fsize, )) for n, (data, row) in enumerate( merge([[(sp, n) for n in xrange(sp.shape[0])] for sp in specs], key=lambda x: x[0].freq_axis[x[1]])): new[n, :] = data[row, :] freq_axis[n] = data.freq_axis[row] params = { 'time_axis': one.time_axis, # Should be equal 'freq_axis': freq_axis, 'start': one.start, 'end': one.end, 't_delt': one.t_delt, 't_init': one.t_init, 't_label': one.t_label, 'f_label': one.f_label, 'content': one.content, 'instruments': _union(spec.instruments for spec in specs) } return common_base(specs)(new, **params)