def wavedec(data, wavelet, mode='sym', level=None): """ Multilevel 1D Discrete Wavelet Transform of data. Returns coefficients list - [cAn, cDn, cDn-1, ..., cD2, cD1] data - input data wavelet - wavelet to use (Wavelet object or name string) mode - signal extension mode, see MODES level - decomposition level. If level is None then it will be calculated using `dwt_max_level` function. """ if not isinstance(wavelet, Wavelet): wavelet = Wavelet(wavelet) if level is None: level = dwt_max_level(len(data), wavelet.dec_len) elif level < 0: raise ValueError("Level value of %d is too low . Minimum level is 0." % level) coeffs_list = [] a = data for i in xrange(level): a, d = dwt(a, wavelet, mode) coeffs_list.append(d) coeffs_list.append(a) coeffs_list.reverse() return coeffs_list
def wavelet_for_name(name): if not isinstance(name, basestring): raise TypeError("Wavelet name must be of string type, not %s" % type(name)) try: wavelet = Wavelet(name) except ValueError: raise #raise ValueError("Invalid wavelet name - %s." % name) return wavelet
def __init__(self, data, wavelet, mode='sp1', maxlevel=None): super(WaveletPacket2D, self).__init__(None, data, "") if not isinstance(wavelet, Wavelet): wavelet = Wavelet(wavelet) self.wavelet = wavelet self.mode = mode if data is not None: data = numerix.as_float_array(data) assert len(data.shape) == 2 self.data_size = data.shape if maxlevel is None: maxlevel = dwt_max_level(min(self.data_size), self.wavelet) else: self.data_size = None self._maxlevel = maxlevel
def wavedec2(data, wavelet, mode='sym', level=None): """ Multilevel 2D Discrete Wavelet Transform. data - 2D input data wavelet - wavelet to use (Wavelet object or name string) mode - signal extension mode, see MODES level - decomposition level. If level is None then it will be calculated using `dwt_max_level` function . Returns coefficients list - [cAn, (cHn, cVn, cDn), ... (cH1, cV1, cD1)] """ data = as_float_array(data) if len(data.shape) != 2: raise ValueError("Expected 2D input data.") if not isinstance(wavelet, Wavelet): wavelet = Wavelet(wavelet) if level is None: size = min(data.shape) level = dwt_max_level(size, wavelet.dec_len) elif level < 0: raise ValueError("Level value of %d is too low . Minimum level is 0." % level) coeffs_list = [] a = data for i in xrange(level): a, ds = dwt2(a, wavelet, mode) coeffs_list.append(ds) coeffs_list.append(a) coeffs_list.reverse() return coeffs_list
def idwt2(coeffs, wavelet, mode='sym'): """ 2D Inverse Discrete Wavelet Transform. Reconstruct data from coefficients arrays. coeffs - four 2D coefficients arrays arranged as follows (in the same way as dwt2 output -- see dwt2 description for details): (approximation, (horizontal details, vertical details, diagonal details) ) wavelet - wavelet to use (Wavelet object or name string) mode - signal extension mode, see MODES """ if len(coeffs) != 2 or len(coeffs[1]) != 3: raise ValueError("Invalid coeffs param") # L -low-pass data, H - high-pass data LL, (LH, HL, HH) = coeffs if not LL is None: LL = transpose(LL) if not LH is None: LH = transpose(LH) if not HL is None: HL = transpose(HL) if not HH is None: HH = transpose(HH) all_none = True for arr in (LL, LH, HL, HH): if arr is not None: all_none = False if len(arr.shape) != 2: raise TypeError("All input coefficients arrays must be 2D.") del arr if all_none: raise ValueError( "At least one input coefficients array must not be None.") if not isinstance(wavelet, Wavelet): wavelet = Wavelet(wavelet) mode = MODES.from_object(mode) # idwt columns L = [] append_L = L.append if LL is None and LH is None: L = None else: if LL is None: LL = cycle([ None ]) # IDWT can handle None input values - equals to zero-array if LH is None: LH = cycle([ None ]) # IDWT can handle None input values - equals to zero-array for rowL, rowH in izip(LL, LH): append_L(idwt(rowL, rowH, wavelet, mode, 1)) del LL, LH H = [] append_H = H.append if HL is None and HH is None: H = None else: if HL is None: HL = cycle([ None ]) # IDWT can handle None input values - equals to zero-array if HH is None: HH = cycle([ None ]) # IDWT can handle None input values - equals to zero-array for rowL, rowH in izip(HL, HH): append_H(idwt(rowL, rowH, wavelet, mode, 1)) del HL, HH if L is not None: L = transpose(L) if H is not None: H = transpose(H) # idwt rows data = [] append_data = data.append if L is None: L = cycle( [None]) # IDWT can handle None input values - equals to zero-array if H is None: H = cycle( [None]) # IDWT can handle None input values - equals to zero-array for rowL, rowH in izip(L, H): append_data(idwt(rowL, rowH, wavelet, mode, 1)) return array(data, default_dtype)
def swt2(data, wavelet, level, start_level=0): """ 2D Stationary Wavelet Transform. data - 2D array with input data wavelet - wavelet to use (Wavelet object or name string) level - how many decomposition steps to perform start_level - the level at which the decomposition will start Returns list of approximation and details coefficients: [ (cA_n, (cH_n, cV_n, cD_n) ), (cA_n+1, (cH_n+1, cV_n+1, cD_n+1) ), ..., (cA_n+level, (cH_n+level, cV_n+level, cD_n+level) ) ] where cA is approximation, cH is horizontal details, cV is vertical details, cD is diagonal details and n is start_level. """ data = as_float_array(data) if len(data.shape) != 2: raise ValueError("Expected 2D data array") if not isinstance(wavelet, Wavelet): wavelet = Wavelet(wavelet) ret = [] for i in range(start_level, start_level + level): # filter rows H, L = [], [] append_L = L.append append_H = H.append for row in data: cA, cD = swt(row, wavelet, level=1, start_level=i)[0] append_L(cA) append_H(cD) del data # filter columns H = transpose(H) L = transpose(L) LL, LH = [], [] append_LL = LL.append append_LH = LH.append for row in L: cA, cD = swt(array(row, default_dtype), wavelet, level=1, start_level=i)[0] append_LL(cA) append_LH(cD) del L HL, HH = [], [] append_HL = HL.append append_HH = HH.append for row in H: cA, cD = swt(array(row, default_dtype), wavelet, level=1, start_level=i)[0] append_HL(cA) append_HH(cD) del H # build result structure # (approx., (horizontal, vertical, diagonal)) approx = transpose(LL) ret.append((approx, (transpose(LH), transpose(HL), transpose(HH)))) data = approx # for next iteration return ret
def dwt2(data, wavelet, mode='sym'): """ 2D Discrete Wavelet Transform. data - 2D array with input data wavelet - wavelet to use (Wavelet object or name string) mode - signal extension mode, see MODES Returns approximaion and three details 2D coefficients arrays. The result form four 2D coefficients arrays organized in tuples: (approximation, (horizontal details, vertical details, diagonal details) ) which sometimes is also interpreted as layed out in one 2D array of coefficients, where: ----------------- | | | | A(LL) | H(LH) | | | | (A, (H, V, D)) <---> ----------------- | | | | V(HL) | D(HH) | | | | ----------------- """ data = as_float_array(data) if len(data.shape) != 2: raise ValueError("Expected 2D data array") if not isinstance(wavelet, Wavelet): wavelet = Wavelet(wavelet) mode = MODES.from_object(mode) # filter rows H, L = [], [] append_L = L.append append_H = H.append for row in data: cA, cD = dwt(row, wavelet, mode) append_L(cA) append_H(cD) del data # filter columns H = transpose(H) L = transpose(L) LL, LH = [], [] append_LL = LL.append append_LH = LH.append for row in L: cA, cD = dwt(array(row, default_dtype), wavelet, mode) append_LL(cA) append_LH(cD) del L HL, HH = [], [] append_HL = HL.append append_HH = HH.append for row in H: cA, cD = dwt(array(row, default_dtype), wavelet, mode) append_HL(cA) append_HH(cD) del H # build result structure # (approx., (horizontal, vertical, diagonal)) ret = (transpose(LL), (transpose(LH), transpose(HL), transpose(HH))) return ret