Wavelet Codec¶
WaveletCodec compresses data by transforming it into a wavelet basis and keeping only the largest coefficients.
For background on the method, see the PyWavelets documentation.
Configuration¶
Set exactly one of keep_fraction or threshold.
Option |
Type |
Meaning |
|---|---|---|
|
|
PyWavelets wavelet name (default |
|
|
PyWavelets signal extension mode (default |
|
|
Fraction of all coefficients to keep. Gives a predictable compression ratio. |
|
|
Keep coefficients with magnitude greater than this value. Gives a predictable noise floor. |
As with all codecs, constructor options act as defaults and can be overridden per call:
codec = WaveletCodec(wavelet="sym8", keep_fraction=0.05)
compress_patch(patch, codec) # 5% of coefficients
compress_patch(patch, codec, keep_fraction=0.01) # 1% for this call only
compress_patch(patch, codec, wavelet="haar") # different wavelet
Example¶
import numpy as np
import dascore as dc
from comdas import WaveletCodec, compress_patch
patch = dc.get_example_patch("example_event_2")
for wavelet in ("haar", "db4", "sym8"):
codec = WaveletCodec(wavelet=wavelet, keep_fraction=0.05)
compressed = compress_patch(patch, codec)
error = np.linalg.norm(patch.data - np.asarray(compressed.data))
error /= np.linalg.norm(patch.data)
ratio = codec.compression_ratio(compressed.data.payload)
print(f"{wavelet:5s} ratio={ratio:5.1f}x relative error={error:.3f}")
API¶
See the module reference.