PyWavelets / pywt

PyWavelets - Wavelet Transforms in Python
http://pywavelets.readthedocs.org
MIT License
2.03k stars 469 forks source link

Include `wavedec_coeff_len` from nigma/pywt #85

Closed aaren closed 7 years ago

aaren commented 9 years ago

This is an open PR on nigma's repo: https://github.com/nigma/pywt/pull/4

Leaving this here to remove it from #55.

rgommers commented 9 years ago

The code from the PR, just in case that PRs on that repo won't be accessible in the future (we may close them completely if people keep sending PRs there instead of here):

def wavedec_coeff_len(data_len, filter_len, mode, level=None):
    """
    Total number of coefficients in the output of `wavedec`, given data
    length, filter length, mode and level.
    Returns integer: len(cAn) + len(cDn) + len(cDn-1) + ... + len(cD1)
    data_len   - data length
    filter_len - filter length or Wavelet instance
    level      - decomposition level. If level is None then it will be
                 calculated using `dwt_max_level` function.
    """

    if level is None:
        level = dwt_max_level(data_len, filter_len)
    elif level < 0:
        raise ValueError(
            "Level value of %d is too low . Minimum level is 0." % level)
    total_len = 0

    for i in xrange(level):
        data_len = dwt_coeff_len(data_len, filter_len, mode)
        total_len += data_len 

    return total_len + data_len
rgommers commented 9 years ago

PR author is @belevtsoff

belevtsoff commented 9 years ago

Wow, thanks for resurrecting this, it's been 3 years :)

rgommers commented 9 years ago

time flies:)

rgommers commented 7 years ago

The motivation given on the original PR:

We used PyWavelets recently in our SpikeSort project. In particular, hstacked ouput from pywt.wavedec was used as a single feature vector. This approach is often used for spike sorting. The function I'm proposing, will be very handy when one wants to allocate a fixed-size numpy array to store wavelet coefficients (obtained from wavedec), before the input data is processed (for example, if one has a huge stack of equal-length independent signals to process and store).

@PyWavelets/core opinions on adding this function?

grlee77 commented 7 years ago

I think the implementation is correct and it would not be a burden to maintain. However, I'm not sure if there is still much of a use case for it (see below).

For batched 1D transforms, it should now be faster to run them all at once using the axis feature of wavedec (which was not present at the time the original PR was opened).

If wavedecn was used to do the 1D transforms, one could also then use coeffs_to_array to convert to a single array, which I think is the use case stated above?

e.g. to transform 1000 signals, each of length 256 and get the results in a single array:

coeffs = pywt.wavedecn(np.random.randn(256, 1000), 'db2', 'periodization', axes=(0, ))
coeff_array, slices = pywt.coeffs_to_array(coeffs, axes=(0, ))
rgommers commented 7 years ago

Thanks @grlee77, good points. And if one really wanted to do preallocation for some reason, it wouldn't be much of an issue to run a single transform once and obtain the length from the output of that.

So closing.