
    }Kg&                     D   d Z ddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	gZ ed
      ddddddej                  dedededededej                  fd       Z ed
      ddddej                  dedededej                  f
d       Z edd      d        Zy) zFeature manipulation utilities    N)jit   )cache)ParameterError)Anydeltastack_memory(   )level	      interp)widthorderaxismodedatar   r   r   r   kwargsreturnc                   t        j                  |       } |dk(  r0|| j                  |   kD  rt        d| d| j                  |          |dk  st        j                  |d      dk7  rt        d      |dk  s t        |t        t         j                  f      st        d	      |j                  d
d       |j                  d|       t        j                  j                  | |f|||d|}|S )a
  Compute delta features: local estimate of the derivative
    of the input data along the selected axis.

    Delta features are computed Savitsky-Golay filtering.

    Parameters
    ----------
    data : np.ndarray
        the input data matrix (eg, spectrogram)

    width : int, positive, odd [scalar]
        Number of frames over which to compute the delta features.
        Cannot exceed the length of ``data`` along the specified axis.

        If ``mode='interp'``, then ``width`` must be at least ``data.shape[axis]``.

    order : int > 0 [scalar]
        the order of the difference operator.
        1 for first derivative, 2 for second, etc.

    axis : int [scalar]
        the axis along which to compute deltas.
        Default is -1 (columns).

    mode : str, {'interp', 'nearest', 'mirror', 'constant', 'wrap'}
        Padding mode for estimating differences at the boundaries.

    **kwargs : additional keyword arguments
        See `scipy.signal.savgol_filter`

    Returns
    -------
    delta_data : np.ndarray [shape=(..., t)]
        delta matrix of ``data`` at specified order

    Notes
    -----
    This function caches at level 40.

    See Also
    --------
    scipy.signal.savgol_filter

    Examples
    --------
    Compute MFCC deltas, delta-deltas

    >>> y, sr = librosa.load(librosa.ex('libri1'), duration=5)
    >>> mfcc = librosa.feature.mfcc(y=y, sr=sr)
    >>> mfcc_delta = librosa.feature.delta(mfcc)
    >>> mfcc_delta
    array([[-5.713e+02, -5.697e+02, ..., -1.522e+02, -1.224e+02],
           [ 1.104e+01,  1.330e+01, ...,  2.089e+02,  1.698e+02],
           ...,
           [ 2.829e+00,  1.933e+00, ..., -3.149e+00,  2.294e-01],
           [ 2.890e+00,  2.187e+00, ...,  6.959e+00, -1.039e+00]],
          dtype=float32)

    >>> mfcc_delta2 = librosa.feature.delta(mfcc, order=2)
    >>> mfcc_delta2
    array([[-1.195, -1.195, ..., -4.328, -4.328],
           [-1.566, -1.566, ..., -9.949, -9.949],
           ...,
           [ 0.707,  0.707, ...,  2.287,  2.287],
           [ 0.655,  0.655, ..., -1.719, -1.719]], dtype=float32)

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(nrows=3, sharex=True, sharey=True)
    >>> img1 = librosa.display.specshow(mfcc, ax=ax[0], x_axis='time')
    >>> ax[0].set(title='MFCC')
    >>> ax[0].label_outer()
    >>> img2 = librosa.display.specshow(mfcc_delta, ax=ax[1], x_axis='time')
    >>> ax[1].set(title=r'MFCC-$\Delta$')
    >>> ax[1].label_outer()
    >>> img3 = librosa.display.specshow(mfcc_delta2, ax=ax[2], x_axis='time')
    >>> ax[2].set(title=r'MFCC-$\Delta^2$')
    >>> fig.colorbar(img1, ax=[ax[0]])
    >>> fig.colorbar(img2, ax=[ax[1]])
    >>> fig.colorbar(img3, ax=[ax[2]])
    r   zwhen mode='interp', width=z  cannot exceed data.shape[axis]=   r   r   z!width must be an odd integer >= 3r   z order must be a positive integerderivN	polyorder)r   r   r   )np
atleast_1dshaper   mod
isinstanceintintegerpop
setdefaultscipysignalsavgol_filter)r   r   r   r   r   r   results          Y/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/librosa/feature/utils.pyr   r      s    t ==DxEDJJt$44( 0..2jj.>-?A
 	

 qyBFF5!$)@AAzEC+<=?@@
JJw
k5)33e t$:@F M    )n_stepsdelayr*   r+   c                   |dk  rt        d      |dk(  rt        d      t        j                  |       } | j                  d   }|dk  rt        d| j                         |j	                  dd       |d   dk(  r|j	                  d	dg       t        | j                        D cg c]  }d
 }}|dkD  rt        |dz
  |z        df|d<   ndt        |dz
  | z        f|d<   t        j                  | |fi |} t        | j                        }|d   |z  |d<   ||d<   t        |      }t        j                  | |      }t        || ||       |S c c}w )a  Short-term history embedding: vertically concatenate a data
    vector or matrix with delayed copies of itself.

    Each column ``data[:, i]`` is mapped to::

        data[..., i] ->  [data[..., i],
                          data[..., i - delay],
                          ...
                          data[..., i - (n_steps-1)*delay]]

    For columns ``i < (n_steps - 1) * delay``, the data will be padded.
    By default, the data is padded with zeros, but this behavior can be
    overridden by supplying additional keyword arguments which are passed
    to `np.pad()`.

    Parameters
    ----------
    data : np.ndarray [shape=(..., d, t)]
        Input data matrix.  If ``data`` is a vector (``data.ndim == 1``),
        it will be interpreted as a row matrix and reshaped to ``(1, t)``.

    n_steps : int > 0 [scalar]
        embedding dimension, the number of steps back in time to stack

    delay : int != 0 [scalar]
        the number of columns to step.

        Positive values embed from the past (previous columns).

        Negative values embed from the future (subsequent columns).

    **kwargs : additional keyword arguments
        Additional arguments to pass to `numpy.pad`

    Returns
    -------
    data_history : np.ndarray [shape=(..., m * d, t)]
        data augmented with lagged copies of itself,
        where ``m == n_steps - 1``.

    Notes
    -----
    This function caches at level 40.

    Examples
    --------
    Keep two steps (current and previous)

    >>> data = np.arange(-3, 3)
    >>> librosa.feature.stack_memory(data)
    array([[-3, -2, -1,  0,  1,  2],
           [ 0, -3, -2, -1,  0,  1]])

    Or three steps

    >>> librosa.feature.stack_memory(data, n_steps=3)
    array([[-3, -2, -1,  0,  1,  2],
           [ 0, -3, -2, -1,  0,  1],
           [ 0,  0, -3, -2, -1,  0]])

    Use reflection padding instead of zero-padding

    >>> librosa.feature.stack_memory(data, n_steps=3, mode='reflect')
    array([[-3, -2, -1,  0,  1,  2],
           [-2, -3, -2, -1,  0,  1],
           [-1, -2, -3, -2, -1,  0]])

    Or pad with edge-values, and delay by 2

    >>> librosa.feature.stack_memory(data, n_steps=3, delay=2, mode='edge')
    array([[-3, -2, -1,  0,  1,  2],
           [-3, -3, -3, -2, -1,  0],
           [-3, -3, -3, -3, -3, -2]])

    Stack time-lagged beat-synchronous chroma edge padding

    >>> y, sr = librosa.load(librosa.ex('sweetwaltz'), duration=10)
    >>> chroma = librosa.feature.chroma_cqt(y=y, sr=sr)
    >>> tempo, beats = librosa.beat.beat_track(y=y, sr=sr, hop_length=512)
    >>> beats = librosa.util.fix_frames(beats, x_min=0)
    >>> chroma_sync = librosa.util.sync(chroma, beats)
    >>> chroma_lag = librosa.feature.stack_memory(chroma_sync, n_steps=3,
    ...                                           mode='edge')

    Plot the result

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots()
    >>> beat_times = librosa.frames_to_time(beats, sr=sr, hop_length=512)
    >>> librosa.display.specshow(chroma_lag, y_axis='chroma', x_axis='time',
    ...                          x_coords=beat_times, ax=ax)
    >>> ax.text(1.0, 1/6, "Lag=0", transform=ax.transAxes, rotation=-90, ha="left", va="center")
    >>> ax.text(1.0, 3/6, "Lag=1", transform=ax.transAxes, rotation=-90, ha="left", va="center")
    >>> ax.text(1.0, 5/6, "Lag=2", transform=ax.transAxes, rotation=-90, ha="left", va="center")
    >>> ax.set(title='Time-lagged chroma', ylabel="")
    r   z"n_steps must be a positive integerr   z delay must be a non-zero integerr   zECannot stack memory when input data has no columns. Given data.shape=r   constantconstant_values)r   r   )r   )r   r   
atleast_2dr   r#   rangendimr    padlisttuple
empty_like__stack)	r   r*   r+   r   t_paddingr   historys	            r(   r	   r	      su   H {ABBz?@@==D

2A1u,,0JJ<9
 	
 fj)f~#+aS1$TYY/0/!v/G0 qyGaK50115#w{uf45666$*6*D Eb	G#E"IE"I%LE mmD.G GT7E*N- 1s   &	ET)nopythonr   c                 r   |j                   d   }| j                   d   }|dkD  r>t        |      D ]/  }|dz
  |z
  }|d||z  ||z  |z   f   | d||z  |dz   |z  ddf<   1 y|d| df   | d| dddf<   t        |dz
        D ]0  }|dz
  |z
  }|d| ||z  z   ||z  f   | d||z  |dz   |z  ddf<   2 y)af  Memory-stacking helper function.

    Parameters
    ----------
    history : output array (2-dimensional)
    data : pre-padded input array (2-dimensional)
    n_steps : int > 0, the number of steps to stack
    delay : int != 0, the amount of delay between steps

    Returns
    -------
    None
        Output is stored directly in the history array
    r/   r   r   r   .N)r   r1   )r;   r   r*   r+   dr8   stepqs           r(   r7   r7     s	   " 	

2A 	bAqy'ND!d"A9=QYUQ..:GCTAXN2A56 #  $C!H~aRS!'A+&D!d"A9=aR!e)^a%i//:GCTAXN2A56 'r)   )__doc__numpyr   scipy.signalr$   numbar   _cacher   util.exceptionsr   typingr   __all__ndarrayr    strr   r	   r7    r)   r(   <module>rL      s   %     , N
# R l
**l l 	l
 l l l ZZl l^ R()L
**L"%L25LEHLZZL L^ d$%  %r)   