forked from mikgroup/sigpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavelet.py
64 lines (48 loc) · 1.96 KB
/
wavelet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
"""Wavelet transform functions.
"""
import numpy as np
import pywt
from sigpy import backend, util
__all__ = ['fwt', 'iwt']
def get_wavelet_shape(shape, wave_name='db4', axes=None, level=None):
zshape = [((i + 1) // 2) * 2 for i in shape]
tmp = pywt.wavedecn(
np.zeros(zshape), wave_name, mode='zero', axes=axes, level=level)
tmp, coeff_slices = pywt.coeffs_to_array(tmp, axes=axes)
oshape = tmp.shape
return oshape, coeff_slices
def fwt(input, wave_name='db4', axes=None, level=None):
"""Forward wavelet transform.
Args:
input (array): Input array.
axes (None or tuple of int): Axes to perform wavelet transform.
wave_name (str): Wavelet name.
level (None or int): Number of wavelet levels.
"""
device = backend.get_device(input)
input = backend.to_device(input, backend.cpu_device)
zshape = [((i + 1) // 2) * 2 for i in input.shape]
zinput = util.resize(input, zshape)
coeffs = pywt.wavedecn(
zinput, wave_name, mode='zero', axes=axes, level=level)
output, _ = pywt.coeffs_to_array(coeffs, axes=axes)
output = backend.to_device(output, device)
return output
def iwt(input, oshape, coeff_slices, wave_name='db4', axes=None, level=None):
"""Inverse wavelet transform.
Args:
input (array): Input array.
oshape (tuple of ints): Output shape.
coeff_slices (list of slice): Slices to split coefficients.
axes (None or tuple of int): Axes to perform wavelet transform.
wave_name (str): Wavelet name.
level (None or int): Number of wavelet levels.
"""
device = backend.get_device(input)
input = backend.to_device(input, backend.cpu_device)
input = pywt.array_to_coeffs(input, coeff_slices, output_format='wavedecn')
output = pywt.waverecn(input, wave_name, mode='zero', axes=axes)
output = util.resize(output, oshape)
output = backend.to_device(output, device)
return output