# Path: friture/signal/decimate.py
# def decimate(bdec, adec, x, zi):
#     if len(x) == 0:
#         raise Exception("Filter input is too small")
# 
#     # could use a polyphase decimator here
#     x_dec, zf = pyx_lfilter_float64_1D(bdec, adec, x, zi)
# 
#     x_dec = x_dec[::2]
#     return x_dec, zf


from numpy import arange, sqrt, zeros, array
from friture_extensions.lfilter import pyx_lfilter_float64_1D
from .signal.decimate import decimate
# filter for the octave


def octave_filter_bank_decimation(blow, alow, forward, feedback, x, zis):
    # This function filters the waveform x with the array of filters
    # specified by the forward and feedback parameters. Each row
    # of the forward and feedback parameters are the parameters
    # to the Matlab builtin function "filter".
    bands_per_octave = len(forward)
    filter_count = NOCTAVE * bands_per_octave

    y = [0.] * filter_count
    dec = [0.] * filter_count

    x_dec = x

    zfs = []

    m = 0
    k = filter_count - 1

    for j in range(0, NOCTAVE):
        for i in range(0, bands_per_octave)[::-1]:
            filt, zf = pyx_lfilter_float64_1D(forward[i], feedback[i], x_dec, zis[m])
            m += 1
            # zf can be reused to restart the filter
            zfs += [zf]
            y[k] = filt
            dec[k] = 2 ** j
            k -= 1

# Path: friture/signal/decimate.py
# def decimate(bdec, adec, x, zi):
#     if len(x) == 0:
#         raise Exception("Filter input is too small")
# 
#     # could use a polyphase decimator here
#     x_dec, zf = pyx_lfilter_float64_1D(bdec, adec, x, zi)
# 
#     x_dec = x_dec[::2]
#     return x_dec, zf


# Path: friture/filter.py
from numpy import arange, sqrt, zeros, array
from friture_extensions.lfilter import pyx_lfilter_float64_1D
from .signal.decimate import decimate

# filter for the octave


def octave_filter_bank_decimation(blow, alow, forward, feedback, x, zis):
    # This function filters the waveform x with the array of filters
    # specified by the forward and feedback parameters. Each row
    # of the forward and feedback parameters are the parameters
    # to the Matlab builtin function "filter".
    bands_per_octave = len(forward)
    filter_count = NOCTAVE * bands_per_octave

    y = [0.] * filter_count
    dec = [0.] * filter_count

    x_dec = x

    zfs = []

    m = 0
    k = filter_count - 1

    for j in range(0, NOCTAVE):
        for i in range(0, bands_per_octave)[::-1]:
            filt, zf = pyx_lfilter_float64_1D(forward[i], feedback[i], x_dec, zis[m])
            m += 1
            # zf can be reused to restart the filter
            zfs += [zf]
            y[k] = filt
            dec[k] = 2 ** j
            k -= 1

        x_dec, zf = decimate(blow, alow, x_dec, zis[m])