def test_odd_filter(): colifilt(lena, (-1,2,-1), (-1,2,1))
def test_different_size_h(): colifilt(lena, (-1,2,1), (-0.5,-1,2,-1,0.5))
def _level2_ifm(Yl, Yh, g0a, g0b, g1a, g1b, ext_mode, prev_level_size): """Perform level 2 or greater of the 3d inverse transform. """ # Create work area work = np.zeros(np.asanyarray(Yl.shape)*2, dtype=Yl.dtype) # Form some useful slices s0a = slice(None, work.shape[0] >> 1) s1a = slice(None, work.shape[1] >> 1) s2a = slice(None, work.shape[2] >> 1) s0b = slice(work.shape[0] >> 1, None) s1b = slice(work.shape[1] >> 1, None) s2b = slice(work.shape[2] >> 1, None) # Assign regions of work area work[s0a, s1a, s2a] = Yl work[s0a, s1b, s2a] = c2cube(Yh[:,:,:, 0:4 ]) work[s0b, s1a, s2a] = c2cube(Yh[:,:,:, 4:8 ]) work[s0b, s1b, s2a] = c2cube(Yh[:,:,:, 8:12]) work[s0a, s1a, s2b] = c2cube(Yh[:,:,:,12:16]) work[s0a, s1b, s2b] = c2cube(Yh[:,:,:,16:20]) work[s0b, s1a, s2b] = c2cube(Yh[:,:,:,20:24]) work[s0b, s1b, s2b] = c2cube(Yh[:,:,:,24:28]) for f in xrange(work.shape[2]): # Do even Qshift filters on rows. y = colifilt(work[:, s1a, f].T, g0b, g0a) + colifilt(work[:, s1b, f].T, g1b, g1a) # Do even Qshift filters on columns. work[:, :, f] = colifilt(y[:, s0a].T, g0b, g0a) + colifilt(y[:,s0b].T, g1b, g1a) for f in xrange(work.shape[1]): # Do even Qshift filters on 3rd dim. y = work[:, f, :].T work[:, f, :] = (colifilt(y[s2a, :], g0b, g0a) + colifilt(y[s2b, :], g1b, g1a)).T # Now check if the size of the previous level is exactly twice the size of # the current level. If YES, this means we have not done the extension in # the previous level. If NO, then we have to remove the appended row / # column / frame from the previous level DTCWT coefs. prev_level_size = np.asarray(prev_level_size) curr_level_size = np.asarray(Yh.shape) if ext_mode == 4: if curr_level_size[0] * 2 != prev_level_size[0]: # Discard the top and bottom rows work = work[1:-1,:,:] if curr_level_size[1] * 2 != prev_level_size[1]: # Discard the top and bottom rows work = work[:,1:-1,:] if curr_level_size[2] * 2 != prev_level_size[2]: # Discard the top and bottom rows work = work[:,:,1:-1] elif ext_mode == 8: if curr_level_size[0] * 2 != prev_level_size[0]: # Discard the top and bottom rows work = work[2:-2,:,:] if curr_level_size[1] * 2 != prev_level_size[1]: # Discard the top and bottom rows work = work[:,2:-2,:] if curr_level_size[2] * 2 != prev_level_size[2]: # Discard the top and bottom rows work = work[:,:,2:-2] return work
def dtwaveifm(Yl, Yh, biort=DEFAULT_BIORT, qshift=DEFAULT_QSHIFT, gain_mask=None): """Perform an *n*-level dual-tree complex wavelet (DTCWT) 1D reconstruction. :param Yl: The real lowpass subband from the final level :param Yh: A sequence containing the complex highpass subband for each level. :param biort: Level 1 wavelets to use. See :py:func:`biort`. :param qshift: Level >= 2 wavelets to use. See :py:func:`qshift`. :param gain_mask: Gain to be applied to each subband. :returns Z: 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. If *biort* or *qshift* are strings, they are used as an argument to the :py:func:`biort` or :py:func:`qshift` functions. Otherwise, they are interpreted as tuples of vectors giving filter coefficients. In the *biort* case, this should be (h0o, g0o, h1o, g1o). In the *qshift* case, this should be (h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b). Example:: # Performs a reconstruction from Yl,Yh using the 13,19-tap filters # for level 1 and the Q-shift 14-tap filters for levels >= 2. Z = dtwaveifm(Yl, Yh, 'near_sym_b', 'qshift_b') .. codeauthor:: Rich Wareham <*****@*****.**>, Aug 2013 .. codeauthor:: Nick Kingsbury, Cambridge University, May 2002 .. codeauthor:: Cian Shaffrey, Cambridge University, May 2002 """ 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_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 dtwaveifm2(Yl,Yh,biort=DEFAULT_BIORT,qshift=DEFAULT_QSHIFT,gain_mask=None): """Perform an *n*-level dual-tree complex wavelet (DTCWT) 2D reconstruction. :param Yl: The real lowpass subband from the final level :param Yh: A sequence containing the complex highpass subband for each level. :param biort: Level 1 wavelets to use. See :py:func:`biort`. :param qshift: Level >= 2 wavelets to use. See :py:func:`qshift`. :param gain_mask: Gain to be applied to each subband. :returns Z: Reconstructed real array The (*d*, *l*)-th element of *gain_mask* is gain for subband with direction *d* at level *l*. If gain_mask[d,l] == 0, no computation is performed for band (d,l). Default *gain_mask* is all ones. Note that both *d* and *l* are zero-indexed. If *biort* or *qshift* are strings, they are used as an argument to the :py:func:`biort` or :py:func:`qshift` functions. Otherwise, they are interpreted as tuples of vectors giving filter coefficients. In the *biort* case, this should be (h0o, g0o, h1o, g1o). In the *qshift* case, this should be (h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b). Example:: # Performs a 3-level reconstruction from Yl,Yh using the 13,19-tap # filters for level 1 and the Q-shift 14-tap filters for levels >= 2. Z = dtwaveifm2(Yl, Yh, 'near_sym_b', 'qshift_b') .. codeauthor:: Rich Wareham <*****@*****.**>, Aug 2013 .. codeauthor:: Nick Kingsbury, Cambridge University, May 2002 .. codeauthor:: Cian Shaffrey, Cambridge University, May 2002 """ a = len(Yh) # No of levels. if gain_mask is None: gain_mask = np.ones((6,a)) # Default gain_mask. gain_mask = np.array(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 current_level = a Z = Yl while current_level >= 2: # this ensures that for level 1 we never do the following lh = c2q(Yh[current_level-1][:,:,[0, 5]], gain_mask[[0, 5], current_level-1]) hl = c2q(Yh[current_level-1][:,:,[2, 3]], gain_mask[[2, 3], current_level-1]) hh = c2q(Yh[current_level-1][:,:,[1, 4]], gain_mask[[1, 4], current_level-1]) # Do even Qshift filters on columns. y1 = colifilt(Z,g0b,g0a) + colifilt(lh,g1b,g1a) y2 = colifilt(hl,g0b,g0a) + colifilt(hh,g1b,g1a) # Do even Qshift filters on rows. Z = (colifilt(y1.T,g0b,g0a) + colifilt(y2.T,g1b,g1a)).T # Check size of Z and crop as required [row_size, col_size] = Z.shape S = 2*np.array(Yh[current_level-2].shape) if row_size != S[0]: # check to see if this result needs to be cropped for the rows Z = Z[1:-1,:] if col_size != S[1]: # check to see if this result needs to be cropped for the cols Z = Z[:,1:-1] if np.any(np.array(Z.shape) != S[:2]): raise ValueError('Sizes of subbands are not valid for DTWAVEIFM2') current_level = current_level - 1 if current_level == 1: lh = c2q(Yh[current_level-1][:,:,[0, 5]],gain_mask[[0, 5],current_level-1]) hl = c2q(Yh[current_level-1][:,:,[2, 3]],gain_mask[[2, 3],current_level-1]) hh = c2q(Yh[current_level-1][:,:,[1, 4]],gain_mask[[1, 4],current_level-1]) # Do odd top-level filters on columns. y1 = colfilter(Z,g0o) + colfilter(lh,g1o) y2 = colfilter(hl,g0o) + colfilter(hh,g1o) # Do odd top-level filters on rows. Z = (colfilter(y1.T,g0o) + colfilter(y2.T,g1o)).T return Z
def test_output_size(): Y = colifilt(lena, (-1, 1), (1, -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_bad_input_size(): colifilt(lena[:511, :], (-1, 1), (1, -1))
def test_good_input_size(): colifilt(lena[:, :511], (-1, 1), (1, -1))
def test_zero_input(): Y = colifilt(np.zeros_like(lena), (-1, 1), (1, -1)) assert np.all(Y[:0] == 0)
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 test_zero_input(): Y = colifilt(np.zeros_like(lena), (-1,1), (1,-1)) assert np.all(Y[:0] == 0)
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_bad_input_size(): colifilt(lena[:511,:], (-1,1), (1,-1))
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_output_size(): Y = colifilt(lena, (-1,1), (1,-1)) assert Y.shape == (lena.shape[0]*2, lena.shape[1])
def dtwaveifm(Yl, Yh, biort=DEFAULT_BIORT, qshift=DEFAULT_QSHIFT, gain_mask=None): """Perform an *n*-level dual-tree complex wavelet (DTCWT) 1D reconstruction. :param Yl: The real lowpass subband from the final level :param Yh: A sequence containing the complex highpass subband for each level. :param biort: Level 1 wavelets to use. See :py:func:`biort`. :param qshift: Level >= 2 wavelets to use. See :py:func:`qshift`. :param gain_mask: Gain to be applied to each subband. :returns Z: 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. If *biort* or *qshift* are strings, they are used as an argument to the :py:func:`biort` or :py:func:`qshift` functions. Otherwise, they are interpreted as tuples of vectors giving filter coefficients. In the *biort* case, this should be (h0o, g0o, h1o, g1o). In the *qshift* case, this should be (h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b). Example:: # Performs a reconstruction from Yl,Yh using the 13,19-tap filters # for level 1 and the Q-shift 14-tap filters for levels >= 2. Z = dtwaveifm(Yl, Yh, 'near_sym_b', 'qshift_b') .. codeauthor:: Rich Wareham <*****@*****.**>, Aug 2013 .. codeauthor:: Nick Kingsbury, Cambridge University, May 2002 .. codeauthor:: Cian Shaffrey, Cambridge University, May 2002 """ 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 dtwaveifm2(Yl, Yh, biort=DEFAULT_BIORT, qshift=DEFAULT_QSHIFT, gain_mask=None): """Perform an *n*-level dual-tree complex wavelet (DTCWT) 2D reconstruction. :param Yl: The real lowpass subband from the final level :param Yh: A sequence containing the complex highpass subband for each level. :param biort: Level 1 wavelets to use. See :py:func:`biort`. :param qshift: Level >= 2 wavelets to use. See :py:func:`qshift`. :param gain_mask: Gain to be applied to each subband. :returns Z: Reconstructed real array The (*d*, *l*)-th element of *gain_mask* is gain for subband with direction *d* at level *l*. If gain_mask[d,l] == 0, no computation is performed for band (d,l). Default *gain_mask* is all ones. Note that both *d* and *l* are zero-indexed. If *biort* or *qshift* are strings, they are used as an argument to the :py:func:`biort` or :py:func:`qshift` functions. Otherwise, they are interpreted as tuples of vectors giving filter coefficients. In the *biort* case, this should be (h0o, g0o, h1o, g1o). In the *qshift* case, this should be (h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b). Example:: # Performs a 3-level reconstruction from Yl,Yh using the 13,19-tap # filters for level 1 and the Q-shift 14-tap filters for levels >= 2. Z = dtwaveifm2(Yl, Yh, 'near_sym_b', 'qshift_b') .. codeauthor:: Rich Wareham <*****@*****.**>, Aug 2013 .. codeauthor:: Nick Kingsbury, Cambridge University, May 2002 .. codeauthor:: Cian Shaffrey, Cambridge University, May 2002 """ a = len(Yh) # No of levels. if gain_mask is None: gain_mask = np.ones((6, a)) # Default gain_mask. gain_mask = np.array(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 current_level = a Z = Yl while current_level >= 2: # this ensures that for level 1 we never do the following lh = c2q(Yh[current_level - 1][:, :, [0, 5]], gain_mask[[0, 5], current_level - 1]) hl = c2q(Yh[current_level - 1][:, :, [2, 3]], gain_mask[[2, 3], current_level - 1]) hh = c2q(Yh[current_level - 1][:, :, [1, 4]], gain_mask[[1, 4], current_level - 1]) # Do even Qshift filters on columns. y1 = colifilt(Z, g0b, g0a) + colifilt(lh, g1b, g1a) y2 = colifilt(hl, g0b, g0a) + colifilt(hh, g1b, g1a) # Do even Qshift filters on rows. Z = (colifilt(y1.T, g0b, g0a) + colifilt(y2.T, g1b, g1a)).T # Check size of Z and crop as required [row_size, col_size] = Z.shape S = 2 * np.array(Yh[current_level - 2].shape) if row_size != S[0]: # check to see if this result needs to be cropped for the rows Z = Z[1:-1, :] if col_size != S[1]: # check to see if this result needs to be cropped for the cols Z = Z[:, 1:-1] if np.any(np.array(Z.shape) != S[:2]): raise ValueError("Sizes of subbands are not valid for DTWAVEIFM2") current_level = current_level - 1 if current_level == 1: lh = c2q(Yh[current_level - 1][:, :, [0, 5]], gain_mask[[0, 5], current_level - 1]) hl = c2q(Yh[current_level - 1][:, :, [2, 3]], gain_mask[[2, 3], current_level - 1]) hh = c2q(Yh[current_level - 1][:, :, [1, 4]], gain_mask[[1, 4], current_level - 1]) # Do odd top-level filters on columns. y1 = colfilter(Z, g0o) + colfilter(lh, g1o) y2 = colfilter(hl, g0o) + colfilter(hh, g1o) # Do odd top-level filters on rows. Z = (colfilter(y1.T, g0o) + colfilter(y2.T, g1o)).T return Z