# Path: pyfasst/tools/utils.py
# def nextpow2(i):
#     """
#     Find :math:`2^n` that is equal to or greater than.
#     
#     code taken from the website:
#     
#      http://www.phys.uu.nl/~haque/computing/WPark_recipes_in_python.html
#     """
#     n = 2
#     while n < i:
#         n = n * 2
#     return n
# 
# def sqrt_blackmanharris(M):
#     """A root-squared Blackman-Harris window function.
#     
#     For use in scholkhuber and klapuri's framework.
#     """
#     return np.sqrt(spsig.blackmanharris(M))


import numpy as np
import scipy.signal as spsig  # for the windows and filters
import scipy.interpolate as spinterp
        import matplotlib.pyplot as plt
from stft import stft, istft
from ..tools.utils import nextpow2, sqrt_blackmanharris
    def __init__(self, fmax, bins, fs,
                 q = 1,
                 atomHopFactor = 0.25,
                 thresh = 0.0005, 
                 winFunc = sqrt_blackmanharris,
                 perfRast = 0
                 ):
        """
        Generate the CQT kernel for one octave
        """
        # %% define
        # checking that fmax<fs/2
        if fmax >= fs/2.:
            raise ValueError("fmax (%s) is too big for fs (%s)"
                             %(str(fmax), str(fs)))
        # fmax = np.minimum(fmax, fs/2.)
        fmin = (fmax / 2.) * (2 ** (1./bins))
        Q = 1. / (2 ** (1./bins) - 1)
        Q = Q * q
        Nk_max = Q * fs / fmin
        Nk_max = np.round(Nk_max) # length of the largest atom [samples]
        
        # %% Compute FFT size, FFT hop, atom hop, ...
        # %  length of the shortest atom [samples]
        Nk_min = np.round( Q * fs /
                           (fmin * (2**((bins-1.)/bins))) )
        # %atom hop size
        # DJL: nextpow2, so that at all octaves, the frames are centered on
        #     integer valued samples of the (decimated) signal.
        # atomHOP = np.round(Nk_min * atomHopFactor)

# Path: pyfasst/tools/utils.py
# def nextpow2(i):
#     """
#     Find :math:`2^n` that is equal to or greater than.
#     
#     code taken from the website:
#     
#      http://www.phys.uu.nl/~haque/computing/WPark_recipes_in_python.html
#     """
#     n = 2
#     while n < i:
#         n = n * 2
#     return n
# 
# def sqrt_blackmanharris(M):
#     """A root-squared Blackman-Harris window function.
#     
#     For use in scholkhuber and klapuri's framework.
#     """
#     return np.sqrt(spsig.blackmanharris(M))


# Path: pyfasst/tftransforms/minqt.py
import numpy as np
import scipy.signal as spsig  # for the windows and filters
import scipy.interpolate as spinterp
        import matplotlib.pyplot as plt
from stft import stft, istft
from ..tools.utils import nextpow2, sqrt_blackmanharris

    def __init__(self, fmax, bins, fs,
                 q = 1,
                 atomHopFactor = 0.25,
                 thresh = 0.0005, 
                 winFunc = sqrt_blackmanharris,
                 perfRast = 0
                 ):
        """
        Generate the CQT kernel for one octave
        """
        # %% define
        # checking that fmax<fs/2
        if fmax >= fs/2.:
            raise ValueError("fmax (%s) is too big for fs (%s)"
                             %(str(fmax), str(fs)))
        # fmax = np.minimum(fmax, fs/2.)
        fmin = (fmax / 2.) * (2 ** (1./bins))
        Q = 1. / (2 ** (1./bins) - 1)
        Q = Q * q
        Nk_max = Q * fs / fmin
        Nk_max = np.round(Nk_max) # length of the largest atom [samples]
        
        # %% Compute FFT size, FFT hop, atom hop, ...
        # %  length of the shortest atom [samples]
        Nk_min = np.round( Q * fs /
                           (fmin * (2**((bins-1.)/bins))) )
        # %atom hop size
        # DJL: nextpow2, so that at all octaves, the frames are centered on
        #     integer valued samples of the (decimated) signal.
        # atomHOP = np.round(Nk_min * atomHopFactor)

        atomHOP = nextpow2(Nk_min * atomHopFactor)/2