def test_coldfilt(): h0o, g0o, h1o, g1o = biort('near_sym_b') h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d') A = colifilt(mandrill, g0b, g0a) assert_almost_equal_to_summary(A, verif['mandrill_colifilt'], tolerance=TOLERANCE)
def test_different_size_h(): colifilt(lena, (-1,2,1), (-0.5,-1,2,-1,0.5))
def test_odd_filter(): colifilt(lena, (-1,2,-1), (-1,2,1))
def inverse_hu(self, pyramid, gain_mask=None): """Perform an *n*-level dual-tree complex wavelet (DTCWT) 1D reconstruction. :param pyramid: A :py:class:`DTCWT.Pyramid`-like object containing the transformed signal. :param gain_mask: Gain to be applied to each subband. :returns: Reconstructed real array. The *l*-th element of *gain_mask* is gain for wavelet subband at level l. If gain_mask[l] == 0, no computation is performed for band *l*. Default *gain_mask* is all ones. Note that *l* is 0-indexed. .. codeauthor:: Rich Wareham <*****@*****.**>, Aug 2013 .. codeauthor:: Nick Kingsbury, Cambridge University, May 2002 .. codeauthor:: Cian Shaffrey, Cambridge University, May 2002 """ # Which wavelets are to be used? biort = self.biort qshift = self.qshift Yl = pyramid.lowpass Yh = pyramid.highpasses a = len(Yh) # No of levels. if gain_mask is None: gain_mask = np.ones(a) # Default gain_mask. # Try to load coefficients if biort is a string parameter try: h0o, g0o, h1o, g1o = _biort(biort) except TypeError: h0o, g0o, h1o, g1o = biort # Try to load coefficients if qshift is a string parameter try: h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = _qshift(qshift) except TypeError: h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift level = a - 1 # No of levels = no of rows in L. if level < 0: # if there are no levels in the input, just return the Yl value return Yl """ 此处可以将 hi Lo 拆开 逐层恢复,考虑到卷积是可拆分的 (x1 + x2) * k = x1 * k + x2 * k """ low_band_signal = np.copy(Yl) high_band_signals = [c2q1d(np.copy(item)) for item in Yh[::-1]] for i in range(level): low_band_signal = colifilt(low_band_signal, g0b, g0a) high_band_signals[i] = colifilt(high_band_signals[i], g1b, g1a) if low_band_signal.shape[0] != high_band_signals[i + 1].shape[0]: low_band_signal = low_band_signal[1:-1, ...] if high_band_signals[i].shape[0] != high_band_signals[i + 1].shape[0]: high_band_signals[i] = high_band_signals[i][1:-1, ...] for j in range(i): high_band_signals[j] = colifilt(high_band_signals[j], g0b, g0a) if high_band_signals[j].shape[0] != high_band_signals[ i + 1].shape[0]: high_band_signals[j] = high_band_signals[j][1:-1, ...] low_band_signal = colfilter(low_band_signal, g0o) high_band_signals[-1] = colfilter(high_band_signals[-1], g1o) for i in range(level): high_band_signals[i] = colfilter(high_band_signals[i], g0o) return low_band_signal, high_band_signals
def test_different_size_h(): with raises(ValueError): colifilt(mandrill, (-1,2,1), (-0.5,-1,2,-1,0.5))
def test_output_size_non_mult_4(): Y = colifilt(lena, (-1,0,0,1), (1,0,0,-1)) assert Y.shape == (lena.shape[0]*2, lena.shape[1])
def test_good_input_size(): colifilt(lena[:,:511], (-1,1), (1,-1))
def test_non_orthogonal_input_non_mult_4(): Y = colifilt(mandrill, (1,0,0,1), (1,0,0,1)) assert Y.shape == (mandrill.shape[0]*2, mandrill.shape[1])
def test_output_size_non_mult_4(): Y = colifilt(mandrill, (-1,0,0,1), (1,0,0,-1)) assert Y.shape == (mandrill.shape[0]*2, mandrill.shape[1])
def test_output_size(): Y = colifilt(mandrill, (-1,1), (1,-1)) assert Y.shape == (mandrill.shape[0]*2, mandrill.shape[1])
def test_good_input_size(): colifilt(mandrill[:,:511], (-1,1), (1,-1))
def test_bad_input_size(): with raises(ValueError): colifilt(mandrill[:511,:], (-1,1), (1,-1))
def test_zero_input(): Y = colifilt(np.zeros_like(mandrill), (-1,1), (1,-1)) assert np.all(Y[:0] == 0)
def test_zero_input(): Y = colifilt(np.zeros_like(lena), (-1,1), (1,-1)) assert np.all(Y[:0] == 0)
def test_bad_input_size(): colifilt(lena[:511,:], (-1,1), (1,-1))
def inverse(self, pyramid, gain_mask=None): """Perform an *n*-level dual-tree complex wavelet (DTCWT) 1D reconstruction. :param pyramid: A :py:class:`dtcwt.Pyramid`-like object containing the transformed signal. :param gain_mask: Gain to be applied to each subband. :returns: Reconstructed real array. The *l*-th element of *gain_mask* is gain for wavelet subband at level l. If gain_mask[l] == 0, no computation is performed for band *l*. Default *gain_mask* is all ones. Note that *l* is 0-indexed. .. codeauthor:: Rich Wareham <*****@*****.**>, Aug 2013 .. codeauthor:: Nick Kingsbury, Cambridge University, May 2002 .. codeauthor:: Cian Shaffrey, Cambridge University, May 2002 """ # Which wavelets are to be used? biort = self.biort qshift = self.qshift Yl = pyramid.lowpass Yh = pyramid.highpasses a = len(Yh) # No of levels. if gain_mask is None: gain_mask = np.ones(a) # Default gain_mask. # Try to load coefficients if biort is a string parameter try: h0o, g0o, h1o, g1o = _biort(biort) except TypeError: h0o, g0o, h1o, g1o = biort # Try to load coefficients if qshift is a string parameter try: h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = _qshift(qshift) except TypeError: h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift level = a - 1 # No of levels = no of rows in L. if level < 0: # if there are no levels in the input, just return the Yl value return Yl Lo = Yl while level >= 1: # Reconstruct levels 2 and above in reverse order. Hi = c2q1d(Yh[level] * gain_mask[level]) Lo = colifilt(Lo, g0b, g0a) + colifilt(Hi, g1b, g1a) if Lo.shape[0] != 2 * Yh[level - 1].shape[ 0]: # If Lo is not the same length as the next Yh => t1 was extended. Lo = Lo[ 1:-1, ...] # Therefore we have to clip Lo so it is the same height as the next Yh. if np.any( np.asanyarray(Lo.shape) != np.asanyarray( Yh[level - 1].shape * np.array((2, 1)))): raise ValueError('Yh sizes are not valid for DTWAVEIFM') level -= 1 if level == 0: # Reconstruct level 1. Hi = c2q1d(Yh[level] * gain_mask[level]) Z = colfilter(Lo, g0o) + colfilter(Hi, g1o) # Return a 1d vector or a column vector if Z.shape[1] == 1: return Z.flatten() else: return Z
def test_output_size(): Y = colifilt(lena, (-1,1), (1,-1)) assert Y.shape == (lena.shape[0]*2, lena.shape[1])
def inverse(self, pyramid, gain_mask=None): """Perform an *n*-level dual-tree complex wavelet (DTCWT) 1D reconstruction. :param pyramid: A :py:class:`dtcwt.Pyramid`-like object containing the transformed signal. :param gain_mask: Gain to be applied to each subband. :returns: Reconstructed real array. The *l*-th element of *gain_mask* is gain for wavelet subband at level l. If gain_mask[l] == 0, no computation is performed for band *l*. Default *gain_mask* is all ones. Note that *l* is 0-indexed. .. codeauthor:: Rich Wareham <*****@*****.**>, Aug 2013 .. codeauthor:: Nick Kingsbury, Cambridge University, May 2002 .. codeauthor:: Cian Shaffrey, Cambridge University, May 2002 """ # Which wavelets are to be used? biort = self.biort qshift = self.qshift Yl = pyramid.lowpass Yh = pyramid.highpasses a = len(Yh) # No of levels. if gain_mask is None: gain_mask = np.ones(a) # Default gain_mask. # Try to load coefficients if biort is a string parameter try: h0o, g0o, h1o, g1o = _biort(biort) except TypeError: h0o, g0o, h1o, g1o = biort # Try to load coefficients if qshift is a string parameter try: h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = _qshift(qshift) except TypeError: h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift level = a - 1 # No of levels = no of rows in L. if level < 0: # if there are no levels in the input, just return the Yl value return Yl Lo = Yl while level >= 1: # Reconstruct levels 2 and above in reverse order. Hi = c2q1d(Yh[level] * gain_mask[level]) Lo = colifilt(Lo, g0b, g0a) + colifilt(Hi, g1b, g1a) if ( Lo.shape[0] != 2 * Yh[level - 1].shape[0] ): # If Lo is not the same length as the next Yh => t1 was extended. Lo = Lo[1:-1, ...] # Therefore we have to clip Lo so it is the same height as the next Yh. if np.any(np.asanyarray(Lo.shape) != np.asanyarray(Yh[level - 1].shape * np.array((2, 1)))): raise ValueError("Yh sizes are not valid for DTWAVEIFM") level -= 1 if level == 0: # Reconstruct level 1. Hi = c2q1d(Yh[level] * gain_mask[level]) Z = colfilter(Lo, g0o) + colfilter(Hi, g1o) # Return a 1d vector or a column vector if Z.shape[1] == 1: return Z.flatten() else: return Z
def test_non_orthogonal_input_non_mult_4(): Y = colifilt(lena, (1,0,0,1), (1,0,0,1)) assert Y.shape == (lena.shape[0]*2, lena.shape[1])
def test_odd_filter(): with raises(ValueError): colifilt(mandrill, (-1,2,-1), (-1,2,1))