
    }Kg                   	   d Z ddlmZ ddlZddlZddlZddl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
lmZ d	dlmZ d	dlmZ d	dlmZmZ d	dlmZ ddlmZ ddlmZmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z& d	dl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. g dZ/ ed      ddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d`d       Z0 ed      ddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dad       Z1 edd      d         Z2	 	 	 	 	 	 	 	 	 db	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dcd"Z3	 	 	 	 	 	 	 	 	 db	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dcd#Z4d!ddddddddd$d%dddd&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddd'Z5dd(ded)Z6ddd*	 	 	 	 	 	 	 	 	 dfd+Z7 ed      d!dddd,dd-d.d/	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dgd0       Z8e$d1d1d1d2	 	 	 	 	 	 	 	 	 dhd3       Z9e$d1d1d1d2	 	 	 	 	 	 	 	 	 did4       Z9e$d1d1d1d2	 	 	 	 	 	 	 	 	 djd5       Z9 ed      d6d7d8d2	 	 	 	 	 	 	 	 	 djd9       Z9e$d1d:	 	 	 	 	 dkd;       Z:e$d1d:	 	 	 	 	 dld<       Z:e$d1d:	 	 	 	 	 dmd=       Z: ed      d6d:dmd>       Z:e$d1d1d1d2	 	 	 	 	 	 	 	 	 dhd?       Z;e$d1d1d1d2	 	 	 	 	 	 	 	 	 did@       Z;e$d1d1d1d2	 	 	 	 	 	 	 	 	 djdA       Z; ed      d6dBd8d2	 	 	 	 	 	 	 	 	 djdC       Z;e$d1d:	 	 	 	 	 dkdD       Z<e$d1d:	 	 	 	 	 dldE       Z<e$d1d:	 	 	 	 	 dmdF       Z< ed      d6d:dmdG       Z< ed      dHdI	 	 	 	 	 	 	 	 	 dndJ       Z= ed      dKddLdKddMdN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dodO       Z>e$d1d1d1d1d1d1d1d1d1d1d1d1d1d1dP	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dpdQ       Z?e$d1d1d1d1d1d1d1d1d1d1d1d1d1dR	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dqdS       Z?e$d1d1d1d1d1d1d1d1d1d1d1d1d1d1dP	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 drdT       Z? ed      d!dUdVd	dKdWd$ddddMddd%dP	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 drdX       Z?dYdddddddddZd[dd\	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dsd]Z@ddddUdddddd^		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dtd_ZAy)uz!Utilities for spectral processing    )annotationsN)jit   )convert)
get_fftlib)resample   )cache)util)ParameterError)
get_windowsemitone_filterbank)window_sumsquare)	DTypeLike)AnyCallableOptionalTupleListUnionoverload)Literal)_WindowSpec_PadMode_PadModeSTFT_SequenceLike_ScalarOrSequence_ComplexLike_co_FloatLike_co)stftistftmagphaseiirtreassigned_spectrogramphase_vocoderperceptual_weightingpower_to_dbdb_to_poweramplitude_to_dbdb_to_amplitudefmtpcen
griffinlim   )level   hannTconstant)n_fft
hop_length
win_lengthwindowcenterdtypepad_modeoutc               
   ||}|t        |dz        }n$t        j                  |      st        d| d      t        j                  | d       t        ||d      }	t        j                  |	|	      }	t        j                  |	d
| j                  z   d      }	|r
|dv rt        d| d      || j                  d   kD  r(t        j                  d| d| j                  d           t        | j                        D 
cg c]  }
d }}
t        t        j                  |dz  |z              }| j                  d   |dz  z   |z
  |z  d
z   }||k  r+d}d}|dz  |dz  f|d<   t        j                  | ||      } n]||z  |dz  z
  }|dz  df|d<   t        j                  | dd|d
z
  |z  |dz  z
  |z   d
z   f   ||      }t        j                   |||      }|dd|f   }|j                  d   }||z  |dz  z
  |z   | j                  d   |dz  z   k  r]d|dz  f|d<   t        j                  | d||z  |dz  z
  df   ||      }t        j                   |||      }||j                  d   z  }nft#        |j                        }d|d<   t        j$                  ||      }n4|| j                  d   kD  rt        d| d| j                  d          d}d}t'               }|t        j(                  | j*                        }t        j                   | d|df   ||      }t#        |j                        }d
|dz  z   |d<   |dxx   |z  cc<   |t        j,                  ||d      }nt        j.                  |j                  dd |dd       r|j                  d   |d   k\  st        d|j                   d|       t        j0                  |      st        d|j*                   d       t        j.                  ||j                        r|}n|dd|d   f   }|rd|dkD  r_j                  d   }|j3                  |	|z  d!      |dd|f<   j                  d   }|dkD  r!|j3                  |	|z  d!      |d| df<   nd}t        t        j4                  t        j6                  |j                  dd       |j8                  z  z        }t;        |d
      }t        d|j                  d   |      D ]H  }t=        ||z   |j                  d         }|j3                  |	|d||f   z  d!      |d||z   ||z   f<   J |S c c}
w )"a  Short-time Fourier transform (STFT).

    The STFT represents a signal in the time-frequency domain by
    computing discrete Fourier transforms (DFT) over short overlapping
    windows.

    This function returns a complex-valued matrix D such that

    - ``np.abs(D[..., f, t])`` is the magnitude of frequency bin ``f``
      at frame ``t``, and

    - ``np.angle(D[..., f, t])`` is the phase of frequency bin ``f``
      at frame ``t``.

    The integers ``t`` and ``f`` can be converted to physical units by means
    of the utility functions `frames_to_samples` and `fft_frequencies`.

    Parameters
    ----------
    y : np.ndarray [shape=(..., n)], real-valued
        input signal. Multi-channel is supported.

    n_fft : int > 0 [scalar]
        length of the windowed signal after padding with zeros.
        The number of rows in the STFT matrix ``D`` is ``(1 + n_fft/2)``.
        The default value, ``n_fft=2048`` samples, corresponds to a physical
        duration of 93 milliseconds at a sample rate of 22050 Hz, i.e. the
        default sample rate in librosa. This value is well adapted for music
        signals. However, in speech processing, the recommended value is 512,
        corresponding to 23 milliseconds at a sample rate of 22050 Hz.
        In any case, we recommend setting ``n_fft`` to a power of two for
        optimizing the speed of the fast Fourier transform (FFT) algorithm.

    hop_length : int > 0 [scalar]
        number of audio samples between adjacent STFT columns.

        Smaller values increase the number of columns in ``D`` without
        affecting the frequency resolution of the STFT.

        If unspecified, defaults to ``win_length // 4`` (see below).

    win_length : int <= n_fft [scalar]
        Each frame of audio is windowed by ``window`` of length ``win_length``
        and then padded with zeros to match ``n_fft``.  Padding is added on
        both the left- and the right-side of the window so that the window
        is centered within the frame.

        Smaller values improve the temporal resolution of the STFT (i.e. the
        ability to discriminate impulses that are closely spaced in time)
        at the expense of frequency resolution (i.e. the ability to discriminate
        pure tones that are closely spaced in frequency). This effect is known
        as the time-frequency localization trade-off and needs to be adjusted
        according to the properties of the input signal ``y``.

        If unspecified, defaults to ``win_length = n_fft``.

    window : string, tuple, number, function, or np.ndarray [shape=(n_fft,)]
        Either:

        - a window specification (string, tuple, or number);
          see `scipy.signal.get_window`
        - a window function, such as `scipy.signal.windows.hann`
        - a vector or array of length ``n_fft``

        Defaults to a raised cosine window (`'hann'`), which is adequate for
        most applications in audio signal processing.

        .. see also:: `filters.get_window`

    center : boolean
        If ``True``, the signal ``y`` is padded so that frame
        ``D[:, t]`` is centered at ``y[t * hop_length]``.

        If ``False``, then ``D[:, t]`` begins at ``y[t * hop_length]``.

        Defaults to ``True``,  which simplifies the alignment of ``D`` onto a
        time grid by means of `librosa.frames_to_samples`.
        Note, however, that ``center`` must be set to `False` when analyzing
        signals with `librosa.stream`.

        .. see also:: `librosa.stream`

    dtype : np.dtype, optional
        Complex numeric type for ``D``.  Default is inferred to match the
        precision of the input signal.

    pad_mode : string or function
        If ``center=True``, this argument is passed to `np.pad` for padding
        the edges of the signal ``y``. By default (``pad_mode="constant"``),
        ``y`` is padded on both sides with zeros.

        .. note:: Not all padding modes supported by `numpy.pad` are supported here.
            `wrap`, `mean`, `maximum`, `median`, and `minimum` are not supported.

            Other modes that depend at most on input values at the edges of the
            signal (e.g., `constant`, `edge`, `linear_ramp`) are supported.

        If ``center=False``,  this argument is ignored.

        .. see also:: `numpy.pad`

    out : np.ndarray or None
        A pre-allocated, complex-valued array to store the STFT results.
        This must be of compatible shape and dtype for the given input parameters.

        If `out` is larger than necessary for the provided input signal, then only
        a prefix slice of `out` will be used.

        If not provided, a new array is allocated and returned.

    Returns
    -------
    D : np.ndarray [shape=(..., 1 + n_fft/2, n_frames), dtype=dtype]
        Complex-valued matrix of short-term Fourier transform
        coefficients.

        If a pre-allocated `out` array is provided, then `D` will be
        a reference to `out`.

        If `out` is larger than necessary, then `D` will be a sliced
        view: `D = out[..., :n_frames]`.

    See Also
    --------
    istft : Inverse STFT
    reassigned_spectrogram : Time-frequency reassigned spectrogram

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

    Examples
    --------
    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> S = np.abs(librosa.stft(y))
    >>> S
    array([[5.395e-03, 3.332e-03, ..., 9.862e-07, 1.201e-05],
           [3.244e-03, 2.690e-03, ..., 9.536e-07, 1.201e-05],
           ...,
           [7.523e-05, 3.722e-05, ..., 1.188e-04, 1.031e-03],
           [7.640e-05, 3.944e-05, ..., 5.180e-04, 1.346e-03]],
          dtype=float32)

    Use left-aligned frames, instead of centered frames

    >>> S_left = librosa.stft(y, center=False)

    Use a shorter hop length

    >>> D_short = librosa.stft(y, hop_length=64)

    Display a spectrogram

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots()
    >>> img = librosa.display.specshow(librosa.amplitude_to_db(S,
    ...                                                        ref=np.max),
    ...                                y_axis='log', x_axis='time', ax=ax)
    >>> ax.set_title('Power spectrogram')
    >>> fig.colorbar(img, ax=ax, format="%+2.0f dB")
    N   zhop_length= must be a positive integerFmonoTfftbinssizer   ndimaxes)wrapmaximummeanmedianminimumz
pad_mode='z"' is not supported by librosa.stftzn_fft=z) is too large for input signal of length=r   r   r	   r   mode.)frame_lengthr4   shapez@ is too large for uncentered analysis of input signal of length=F)r8   order3Shape mismatch for provided output array out.shape=z and target shape=zoutput with dtype=z is not of complex typeaxis)intr   is_positive_intr   valid_audior   
pad_center	expand_torF   rS   warningswarnrangenpceilpadframelist
empty_liker   	dtype_r2cr8   zerosallcloseiscomplexobjrfftMAX_MEM_BLOCKproditemsizemaxmin)yr3   r4   r5   r6   r7   r8   r9   r:   
fft_window_paddingstart_ktail_kstartextray_prey_frames_prey_posty_frames_post
post_shapeffty_framesrS   stft_matrix	off_startoff_end	n_columnsbl_sbl_ts                                 Y/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/librosa/core/spectrum.pyr    r    6   s   ^ 
 q)
!!*-{:,6QRSS 	QU#FJ=J %8J 
QVV"EJ GG !XJ&HI  1772;MMHQSV
 $)=1=a6=1 bggeqj:567 ''"+
*U2zAAEWEE A:uz2GBKq'1A j(5A:5E A:q/GBK FF#L'A+3eqj@5H1LLLME
  ::e%JWL'XgX6L !&&r*E 
"UaZ/%71772;RS;SS %1*ocFj05A:=??@'PX !%

:! ,,R00 ","4"45
!"
2 "l* M1772; _`a`g`ghj`k_lm 
 
,C}qww' zz!CK.uTH  E EQJE"I 
"II
{hhuE=kk#))CR.%*5#))B-5QS9:TA#))L^_d^ef
 	
 __S!1#))<STUU;;ucii(Kc;U2Y;./K %!) &&r*	'*xx
\0IPRx'SC)O$%%b)Q;*-((:3MTV(*WKgXY'	rwwx~~cr':;h>O>OOPI Iq!Ia+Y74)#X^^B%78@C#tDy.11 AI A
C	)D9,<<<= 8 o 2s   	U   r4   r5   r3   r6   r7   r8   lengthr:   c          	     h   |d| j                   d   dz
  z  }||}|t        |dz        }t        ||d      }	t        j                  |	|      }	t        j
                  |	| j                  d	      }	|rI|r|d|dz  z  z   }
n|}
t        | j                   d
   t        t        j                  |
|z                    }n| j                   d
   }|t        j                  | j                        }t        | j                   dd       }|||dz
  z  z   }|r|}n|r|d|dz  z  z  }|j                  |       |t        j                  ||      }nNt        j                  |j                   |      st!        d|j                    d|       |}|j#                  d       t%               }|rt        t        j                  |dz  |z              }|	|j'                  | dd|f   |d      z  }|||dz
  z  z   |d
<   t        j                  ||      }t)        |||       |j                   d
   |d
   |dz  z
  k  r'|d|dz  |j                   d
   |dz  z   f   |dddf<   n|d|dz  df   |dd|d
   |dz  z
  f<   ||z  |dz  z
  }nd}d}t        t        j*                  t        j,                  | j                   dd
       | j.                  z  z        }t1        |d      }d}t3        |||      D ]Q  }t        ||z   |      }|	|j'                  | d||f   |d      z  }t)        |d||z  |z   df   ||       |||z
  z  }S t5        ||||||      }|r|dz  }nd}t        j6                  |d|df   |j                   d
         }|t        j8                  |      kD  }|d|fxx   ||   z  cc<   |S )u  
    Inverse short-time Fourier transform (ISTFT).

    Converts a complex-valued spectrogram ``stft_matrix`` to time-series ``y``
    by minimizing the mean squared error between ``stft_matrix`` and STFT of
    ``y`` as described in [#]_ up to Section 2 (reconstruction from MSTFT).

    In general, window function, hop length and other parameters should be same
    as in stft, which mostly leads to perfect reconstruction of a signal from
    unmodified ``stft_matrix``.

    .. [#] D. W. Griffin and J. S. Lim,
        "Signal estimation from modified short-time Fourier transform,"
        IEEE Trans. ASSP, vol.32, no.2, pp.236–243, Apr. 1984.

    Parameters
    ----------
    stft_matrix : np.ndarray [shape=(..., 1 + n_fft//2, t)]
        STFT matrix from ``stft``

    hop_length : int > 0 [scalar]
        Number of frames between STFT columns.
        If unspecified, defaults to ``win_length // 4``.

    win_length : int <= n_fft = 2 * (stft_matrix.shape[0] - 1)
        When reconstructing the time series, each frame is windowed
        and each sample is normalized by the sum of squared window
        according to the ``window`` function (see below).

        If unspecified, defaults to ``n_fft``.

    n_fft : int > 0 or None
        The number of samples per frame in the input spectrogram.
        By default, this will be inferred from the shape of ``stft_matrix``.
        However, if an odd frame length was used, you can specify the correct
        length by setting ``n_fft``.

    window : string, tuple, number, function, np.ndarray [shape=(n_fft,)]
        - a window specification (string, tuple, or number);
          see `scipy.signal.get_window`
        - a window function, such as `scipy.signal.windows.hann`
        - a user-specified window vector of length ``n_fft``

        .. see also:: `filters.get_window`

    center : boolean
        - If ``True``, ``D`` is assumed to have centered frames.
        - If ``False``, ``D`` is assumed to have left-aligned frames.

    dtype : numeric type
        Real numeric type for ``y``.  Default is to match the numerical
        precision of the input spectrogram.

    length : int > 0, optional
        If provided, the output ``y`` is zero-padded or clipped to exactly
        ``length`` samples.

    out : np.ndarray or None
        A pre-allocated, complex-valued array to store the reconstructed signal
        ``y``.  This must be of the correct shape for the given input parameters.

        If not provided, a new array is allocated and returned.

    Returns
    -------
    y : np.ndarray [shape=(..., n)]
        time domain signal reconstructed from ``stft_matrix``.
        If ``stft_matrix`` contains more than two axes
        (e.g., from a stereo input signal), then ``y`` will match shape on the leading dimensions.

    See Also
    --------
    stft : Short-time Fourier Transform

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

    Examples
    --------
    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> D = librosa.stft(y)
    >>> y_hat = librosa.istft(D)
    >>> y_hat
    array([-1.407e-03, -4.461e-04, ...,  5.131e-06, -1.417e-05],
          dtype=float32)

    Exactly preserving length of the input signal requires explicit padding.
    Otherwise, a partial frame at the end of ``y`` will not be represented.

    >>> n = len(y)
    >>> n_fft = 2048
    >>> y_pad = librosa.util.fix_length(y, size=n + n_fft // 2)
    >>> D = librosa.stft(y_pad, n_fft=n_fft)
    >>> y_out = librosa.istft(D, length=n)
    >>> np.max(np.abs(y - y_out))
    8.940697e-08
    Nr	   rD   r   r<   Tr@   rB   rE   rM   r8   rV   z !=         .)nrX   r   )r6   n_framesr5   r3   r4   r8   )rS   rY   r   r   r\   r]   rF   rp   ra   rb   	dtype_c2rr8   re   appendrh   ri   r   fillr   irfft__overlap_addrl   rm   rn   ro   r`   r   
fix_lengthtiny)r   r4   r5   r3   r6   r7   r8   r   r:   ifft_windowpadded_lengthr   rS   expected_signal_lenrq   r~   start_frameytmphead_bufferoffsetr   rd   r   r   ifft_window_sumrw   approx_nonzero_indicess                              r   r!   r!     s&   ^ }[&&r*Q./ 
 q)
VZ>K //+E:K..;3C3C"MK "Q%1*%55M"M{((,c"''-*:T2U.VW$$R(}{001""3B'(E*1"==$	qEQJ//	LL$%
{HHU%([[E*A#))DQVPWX
 	
 	s
,C "''5A:";<= SYY{33D'EUWYXXJ+/::b	hhuE2k44 772;rUaZ//#C!aggbkEQJ6N)N$NOAc1fI 0;3
;L/MAc+U2Y!+++, z)EQJ6 rww{'8'8"'=>AUAUUVI Iq!IEk8Y74)#X. SYY{3T	>'BeRTYUU 	aUZ/&8::;T:N 8 'O 
oooc56k&BQSUO,tyy/IIc!!"o6L&MM"H    )nopythonr
   c                    |j                   d   }|}t        |j                   d         D ]J  }||z  }|| j                   d   |z
  kD  r| j                   d   |z
  }| d|||z   fxx   |dd ||f   z  cc<   L y )NrD   rM   .)rS   r`   )rq   r   r4   r3   Nrd   samples          r   r   r   u  s     JJrNEAtzz"~&#qwwr{V##f$A	#v!$
$%c2A2un)==% 'r   "V  c
           	     d   ||}t        ||d      }t        j                  ||      }|4|t        j                  | j                        }t        | ||||||	      }
n||j                  }|}
t        j                  |      }t        | ||||||	      }t        j                  d      5  t        j                  ||
z         }ddd       t        j                  ||      }t        j                  |j                  d	
      |d|z  t        j                  z  z  z   }||
fS # 1 sw Y   `xY w)ad  Instantaneous frequencies based on a spectrogram representation.

    The reassignment vector is calculated using equation 5.20 in Flandrin,
    Auger, & Chassande-Mottin 2002::

        omega_reassigned = omega - np.imag(S_dh/S_h)

    where ``S_h`` is the complex STFT calculated using the original window, and
    ``S_dh`` is the complex STFT calculated using the derivative of the original
    window.

    See `reassigned_spectrogram` for references.

    It is recommended to use ``pad_mode="wrap"`` or else ``center=False``, rather
    than the defaults. Frequency reassignment assumes that the energy in each
    FFT bin is associated with exactly one signal component. Reflection padding
    at the edges of the signal may invalidate the reassigned estimates in the
    boundary frames.

    Parameters
    ----------
    y : np.ndarray [shape=(..., n,)], real-valued
        audio time series. Multi-channel is supported.

    sr : number > 0 [scalar]
        sampling rate of ``y``

    S : np.ndarray [shape=(..., d, t)] or None
        (optional) complex STFT calculated using the other arguments provided
        to `__reassign_frequencies`

    n_fft : int > 0 [scalar]
        FFT window size. Defaults to 2048.

    hop_length : int > 0 [scalar]
        hop length, number samples between subsequent frames.
        If not supplied, defaults to ``win_length // 4``.

    win_length : int > 0, <= n_fft
        Window length. Defaults to ``n_fft``.
        See ``stft`` for details.

    window : string, tuple, number, function, or np.ndarray [shape=(n_fft,)]
        - a window specification (string, tuple, number);
          see `scipy.signal.get_window`
        - a window function, such as `scipy.signal.windows.hann`
        - a user-specified window vector of length ``n_fft``

        See `stft` for details.

        .. see also:: `filters.get_window`

    center : boolean
        - If ``True``, the signal ``y`` is padded so that frame
          ``S[:, t]`` is centered at ``y[t * hop_length]``.
        - If ``False``, then ``S[:, t]`` begins at ``y[t * hop_length]``.

    dtype : numeric type
        Complex numeric type for ``S``. Default is inferred to match
        the numerical precision of the input signal.

    pad_mode : string
        If ``center=True``, the padding mode to use at the edges of the signal.
        By default, STFT uses zero padding.

    Returns
    -------
    freqs : np.ndarray [shape=(..., 1 + n_fft/2, t), dtype=real]
        Instantaneous frequencies:
        ``freqs[f, t]`` is the frequency for bin ``f``, frame ``t``.
    S : np.ndarray [shape=(..., 1 + n_fft/2, t), dtype=complex]
        Short-time Fourier transform

    Warns
    -----
    RuntimeWarning
        Frequencies with zero support will produce a divide-by-zero warning and
        will be returned as `np.nan`.

    See Also
    --------
    stft : Short-time Fourier Transform
    reassigned_spectrogram : Time-frequency reassigned spectrogram

    Examples
    --------
    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> frequencies, S = librosa.core.spectrum.__reassign_frequencies(y, sr=sr)
    >>> frequencies
    array([[0.000e+00, 0.000e+00, ..., 0.000e+00, 0.000e+00],
           [3.628e+00, 4.698e+00, ..., 1.239e+01, 1.072e+01],
           ...,
           [1.101e+04, 1.102e+04, ..., 1.105e+04, 1.102e+04],
           [1.102e+04, 1.102e+04, ..., 1.102e+04, 1.102e+04]])
    NTr@   rB   rq   r3   r4   r6   r7   r8   r9   ignoreinvalidsrr3   rD   rE         ?)r   r   r\   rg   r8   r    cyclic_gradientra   errstateimagr   fft_frequenciesr]   rF   pi)rq   r   Sr3   r4   r5   r6   r7   r8   r9   S_hwindow_derivativeS_dh
correctionfreqss                  r   __reassign_frequenciesr     s4   Z 

D9F__V%0Fy=NN177+E!
 =GGE ,,V4
 D 
X	&ggdSj))
 
' ##r7ENN5zR@:b255D E #: 
'	&s   -D&&D/c
           	        ||}t        ||d      }t        j                  ||      }|t        |dz        }|4|t        j                  | j
                        }t        | ||||||	      }
n||j
                  }|}
|dz  }|dz  rt        j                  | |dz         }nt        j                  d	|z
  |      }||z  }t        | ||||||	      }t        j                  d
      5  t        j                  ||
z        }ddd       |rd}n|}t        j                  t        j                  |
j                  d         |||      }t        j                  |j                  d      ||z  z   }||
fS # 1 sw Y   uxY w)ad  Time reassignments based on a spectrogram representation.

    The reassignment vector is calculated using equation 5.23 in Flandrin,
    Auger, & Chassande-Mottin 2002::

        t_reassigned = t + np.real(S_th/S_h)

    where ``S_h`` is the complex STFT calculated using the original window, and
    ``S_th`` is the complex STFT calculated using the original window multiplied
    by the time offset from the window center.

    See `reassigned_spectrogram` for references.

    It is recommended to use ``pad_mode="constant"`` (zero padding) or else
    ``center=False``, rather than the defaults. Time reassignment assumes that
    the energy in each FFT bin is associated with exactly one impulse event.
    Reflection padding at the edges of the signal may invalidate the reassigned
    estimates in the boundary frames.

    Parameters
    ----------
    y : np.ndarray [shape=(..., n,)], real-valued
        audio time series. Multi-channel is supported.

    sr : number > 0 [scalar]
        sampling rate of ``y``

    S : np.ndarray [shape=(..., d, t)] or None
        (optional) complex STFT calculated using the other arguments provided
        to `__reassign_times`

    n_fft : int > 0 [scalar]
        FFT window size. Defaults to 2048.

    hop_length : int > 0 [scalar]
        hop length, number samples between subsequent frames.
        If not supplied, defaults to ``win_length // 4``.

    win_length : int > 0, <= n_fft
        Window length. Defaults to ``n_fft``.
        See `stft` for details.

    window : string, tuple, number, function, or np.ndarray [shape=(n_fft,)]
        - a window specification (string, tuple, number);
          see `scipy.signal.get_window`
        - a window function, such as `scipy.signal.windows.hann`
        - a user-specified window vector of length ``n_fft``

        See `stft` for details.

        .. see also:: `filters.get_window`

    center : boolean
        - If ``True``, the signal ``y`` is padded so that frame
          ``S[:, t]`` is centered at ``y[t * hop_length]``.
        - If ``False``, then ``S[:, t]`` begins at ``y[t * hop_length]``.

    dtype : numeric type
        Complex numeric type for ``S``. Default is inferred to match
        the precision of the input signal.

    pad_mode : string
        If ``center=True``, the padding mode to use at the edges of the signal.
        By default, STFT uses zero padding.

    Returns
    -------
    times : np.ndarray [shape=(..., 1 + n_fft/2, t), dtype=real]
        Reassigned times:
        ``times[f, t]`` is the time for bin ``f``, frame ``t``.
    S : np.ndarray [shape=(..., 1 + n_fft/2, t), dtype=complex]
        Short-time Fourier transform

    Warns
    -----
    RuntimeWarning
        Time estimates with zero support will produce a divide-by-zero warning
        and will be returned as `np.nan`.

    See Also
    --------
    stft : Short-time Fourier Transform
    reassigned_spectrogram : Time-frequency reassigned spectrogram

    Examples
    --------
    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> times, S = librosa.core.spectrum.__reassign_times(y, sr=sr)
    >>> times
    array([[ 2.268e-05,  1.144e-02, ...,  5.332e+00,  5.333e+00],
           [ 2.268e-05,  1.451e-02, ...,  5.334e+00,  5.333e+00],
           ...,
           [ 2.268e-05, -6.177e-04, ...,  5.368e+00,  5.327e+00],
           [ 2.268e-05,  1.420e-03, ...,  5.307e+00,  5.328e+00]])
    NTr@   rB   r<   r   r	   r   r   r   r   rM   )r   r4   r3   rE   )r   r   r\   rY   rg   r8   r    ra   aranger   realr   frames_to_timerS   r]   rF   )rq   r   r   r3   r4   r5   r6   r7   r8   r9   r   
half_widthwindow_timeswindow_time_weightedS_thr   
pad_lengthtimess                     r   __reassign_timesr   )  s   Z 

D9F__V%0F q)
y=NN177+E!
 =GGE !J qyyy*j1n= yyz!1:>!L0
#D 
X	&WWTCZ(
 
' 
 
""
		#))B- RJjE NN5zR@:PR?RE#:! 
'	&s   +E88Fgư>F)r   r   r3   r4   r5   r6   r7   reassign_frequenciesreassign_times	ref_powerfill_nanclipr8   r9   c               T   t        |
      s|
dk  rt        d      |s|	st        d      ||}|t        |dz        }|rt        | |||||||||
      \  }}|	rt	        | |||||||||
      \  }}|J t        j                  |      }|s|r|	sW|rd}n|}t        j                  ||      }t        j                  t        j                  |j                  d         |||	      }t        |
      r |
|d
z        }n|
}t        j                  ||dz  t        j                  |             }|rx|dkD  rt
        j                  |<   |r?t        j                  t        j                        ddt
        j                   f   |      }|rRt        j"                  d|dz  |       n5t        j$                  ddt
        j                   f   |j                        }|	r|dkD  rt
        j                  |<   |r?t        j                  t        j                        t
        j                   ddf   |      }|rht        j"                  d| j                  d   t'        |      z  |       n5t        j$                  t
        j                   ddf   |j                        }|fS )a  Time-frequency reassigned spectrogram.

    The reassignment vectors are calculated using equations 5.20 and 5.23 in
    [#]_::

        t_reassigned = t + np.real(S_th/S_h)
        omega_reassigned = omega - np.imag(S_dh/S_h)

    where ``S_h`` is the complex STFT calculated using the original window,
    ``S_dh`` is the complex STFT calculated using the derivative of the original
    window, and ``S_th`` is the complex STFT calculated using the original window
    multiplied by the time offset from the window center. See [#]_ for
    additional algorithms, and [#]_ and [#]_ for history and discussion of the
    method.

    .. [#] Flandrin, P., Auger, F., & Chassande-Mottin, E. (2002).
        Time-Frequency reassignment: From principles to algorithms. In
        Applications in Time-Frequency Signal Processing (Vol. 10, pp.
        179-204). CRC Press.

    .. [#] Fulop, S. A., & Fitz, K. (2006). Algorithms for computing the
        time-corrected instantaneous frequency (reassigned) spectrogram, with
        applications. The Journal of the Acoustical Society of America, 119(1),
        360. doi:10.1121/1.2133000

    .. [#] Auger, F., Flandrin, P., Lin, Y.-T., McLaughlin, S., Meignen, S.,
        Oberlin, T., & Wu, H.-T. (2013). Time-Frequency Reassignment and
        Synchrosqueezing: An Overview. IEEE Signal Processing Magazine, 30(6),
        32-41. doi:10.1109/MSP.2013.2265316

    .. [#] Hainsworth, S., Macleod, M. (2003). Time-frequency reassignment: a
        review and analysis. Tech. Rep. CUED/FINFENG/TR.459, Cambridge
        University Engineering Department

    Parameters
    ----------
    y : np.ndarray [shape=(..., n)], real-valued
        audio time series. Multi-channel is supported.

    sr : number > 0 [scalar]
        sampling rate of ``y``

    S : np.ndarray [shape=(..., d, t)] or None
        (optional) complex STFT calculated using the other arguments provided
        to ``reassigned_spectrogram``

    n_fft : int > 0 [scalar]
        FFT window size. Defaults to 2048.

    hop_length : int > 0 [scalar]
        hop length, number samples between subsequent frames.
        If not supplied, defaults to ``win_length // 4``.

    win_length : int > 0, <= n_fft
        Window length. Defaults to ``n_fft``.
        See `stft` for details.

    window : string, tuple, number, function, or np.ndarray [shape=(n_fft,)]
        - a window specification (string, tuple, number);
          see `scipy.signal.get_window`
        - a window function, such as `scipy.signal.windows.hann`
        - a user-specified window vector of length ``n_fft``

        See `stft` for details.

        .. see also:: `filters.get_window`

    center : boolean
        - If ``True`` (default), the signal ``y`` is padded so that frame
          ``S[:, t]`` is centered at ``y[t * hop_length]``. See `Notes` for
          recommended usage in this function.
        - If ``False``, then ``S[:, t]`` begins at ``y[t * hop_length]``.

    reassign_frequencies : boolean
        - If ``True`` (default), the returned frequencies will be instantaneous
          frequency estimates.
        - If ``False``, the returned frequencies will be a read-only view of the
          STFT bin frequencies for all frames.

    reassign_times : boolean
        - If ``True`` (default), the returned times will be corrected
          (reassigned) time estimates for each bin.
        - If ``False``, the returned times will be a read-only view of the STFT
          frame times for all bins.

    ref_power : float >= 0 or callable
        Minimum power threshold for estimating time-frequency reassignments.
        Any bin with ``np.abs(S[f, t])**2 < ref_power`` will be returned as
        `np.nan` in both frequency and time, unless ``fill_nan`` is ``True``. If 0
        is provided, then only bins with zero power will be returned as
        `np.nan` (unless ``fill_nan=True``).

    fill_nan : boolean
        - If ``False`` (default), the frequency and time reassignments for bins
          below the power threshold provided in ``ref_power`` will be returned as
          `np.nan`.
        - If ``True``, the frequency and time reassignments for these bins will
          be returned as the bin center frequencies and frame times.

    clip : boolean
        - If ``True`` (default), estimated frequencies outside the range
          `[0, 0.5 * sr]` or times outside the range `[0, len(y) / sr]` will be
          clipped to those ranges.
        - If ``False``, estimated frequencies and times beyond the bounds of the
          spectrogram may be returned.

    dtype : numeric type
        Complex numeric type for STFT calculation. Default is inferred to match
        the precision of the input signal.

    pad_mode : string
        If ``center=True``, the padding mode to use at the edges of the signal.
        By default, STFT uses zero padding.

    Returns
    -------
    freqs, times, mags : np.ndarray [shape=(..., 1 + n_fft/2, t), dtype=real]
        Instantaneous frequencies:
            ``freqs[..., f, t]`` is the frequency for bin ``f``, frame ``t``.
            If ``reassign_frequencies=False``, this will instead be a read-only array
            of the same shape containing the bin center frequencies for all frames.

        Reassigned times:
            ``times[..., f, t]`` is the time for bin ``f``, frame ``t``.
            If ``reassign_times=False``, this will instead be a read-only array of
            the same shape containing the frame times for all bins.

        Magnitudes from short-time Fourier transform:
            ``mags[..., f, t]`` is the magnitude for bin ``f``, frame ``t``.

    Warns
    -----
    RuntimeWarning
        Frequency or time estimates with zero support will produce a
        divide-by-zero warning, and will be returned as `np.nan` unless
        ``fill_nan=True``.

    See Also
    --------
    stft : Short-time Fourier Transform

    Notes
    -----
    It is recommended to use ``center=False`` with this function rather than the
    librosa default ``True``. Unlike ``stft``, reassigned times are not aligned to
    the left or center of each frame, so padding the signal does not affect the
    meaning of the reassigned times. However, reassignment assumes that the
    energy in each FFT bin is associated with exactly one signal component and
    impulse event.

    If ``reassign_times`` is ``False``, the frame times that are returned will be
    aligned to the left or center of the frame, depending on the value of
    ``center``. In this case, if ``center`` is ``True``, then ``pad_mode="wrap"`` is
    recommended for valid estimation of the instantaneous frequencies in the
    boundary frames.

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> amin = 1e-10
    >>> n_fft = 64
    >>> sr = 4000
    >>> y = 1e-3 * librosa.clicks(times=[0.3], sr=sr, click_duration=1.0,
    ...                           click_freq=1200.0, length=8000) +\
    ...     1e-3 * librosa.clicks(times=[1.5], sr=sr, click_duration=0.5,
    ...                           click_freq=400.0, length=8000) +\
    ...     1e-3 * librosa.chirp(fmin=200, fmax=1600, sr=sr, duration=2.0) +\
    ...     1e-6 * np.random.randn(2*sr)
    >>> freqs, times, mags = librosa.reassigned_spectrogram(y=y, sr=sr,
    ...                                                     n_fft=n_fft)
    >>> mags_db = librosa.amplitude_to_db(mags, ref=np.max)

    >>> fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
    >>> img = librosa.display.specshow(mags_db, x_axis="s", y_axis="linear", sr=sr,
    ...                          hop_length=n_fft//4, ax=ax[0])
    >>> ax[0].set(title="Spectrogram", xlabel=None)
    >>> ax[0].label_outer()
    >>> ax[1].scatter(times, freqs, c=mags_db, cmap="magma", alpha=0.1, s=5)
    >>> ax[1].set_title("Reassigned spectrogram")
    >>> fig.colorbar(img, ax=ax, format="%+2.f dB")
    r   z+ref_power must be non-negative or callable.z4reassign_frequencies or reassign_times must be True.Nr<   )
rq   r   r   r3   r4   r5   r6   r7   r8   r9   r   rM   )framesr   r4   r3   r	   r   )where       @r:   )callabler   rY   r   r   ra   absr   r   r   r   rS   lessisnannanr   newaxisr   broadcast_tofloat)rq   r   r   r3   r4   r5   r6   r7   r   r   r   r   r   r8   r9   r   r   magsr   	bin_freqsframe_timesref_pmags_lows                          r   r$   r$     su   N I9q=JKKSTT
q)
 )!!
q #!!
q ==vvayD +>J J++r?	,,99QWWR[)!	
 	$'"wwtUCZ?H
 19 ffE(OHHRXXe_i2::.FNEGGE1b3hE2
 	!RZZ- 8!''B19 ffE(OHHRXXe_k"**a-.H%PEGGE1aggbkE"I55A BJJM :AGGD%r   )powerc                  t        j                  |       }|dk(  }||z   }t        j                  | t        j                  | j
                              }| j                  |z  |z   |_        | j                  |z  |_        ||z  }||fS )ay  Separate a complex-valued spectrogram D into its magnitude (S)
    and phase (P) components, so that ``D = S * P``.

    Parameters
    ----------
    D : np.ndarray [shape=(..., d, t), dtype=complex]
        complex-valued spectrogram
    power : float > 0
        Exponent for the magnitude spectrogram,
        e.g., 1 for energy, 2 for power, etc.

    Returns
    -------
    D_mag : np.ndarray [shape=(..., d, t), dtype=real]
        magnitude of ``D``, raised to ``power``
    D_phase : np.ndarray [shape=(..., d, t), dtype=complex]
        ``exp(1.j * phi)`` where ``phi`` is the phase of ``D``

    Examples
    --------
    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> D = librosa.stft(y)
    >>> magnitude, phase = librosa.magphase(D)
    >>> magnitude
    array([[5.395e-03, 3.332e-03, ..., 9.862e-07, 1.201e-05],
           [3.244e-03, 2.690e-03, ..., 9.536e-07, 1.201e-05],
           ...,
           [7.523e-05, 3.722e-05, ..., 1.188e-04, 1.031e-03],
           [7.640e-05, 3.944e-05, ..., 5.180e-04, 1.346e-03]],
          dtype=float32)
    >>> phase
    array([[ 1.   +0.000e+00j,  1.   +0.000e+00j, ...,
            -1.   -8.742e-08j, -1.   -8.742e-08j],
           [-1.   -8.742e-08j, -0.775-6.317e-01j, ...,
            -0.885-4.648e-01j,  0.472-8.815e-01j],
           ...,
           [ 1.   -4.342e-12j,  0.028-9.996e-01j, ...,
            -0.222-9.751e-01j, -0.75 -6.610e-01j],
           [-1.   -8.742e-08j, -1.   -8.742e-08j, ...,
             1.   +0.000e+00j,  1.   +0.000e+00j]], dtype=complex64)

    Or get the phase angle (in radians)

    >>> np.angle(phase)
    array([[ 0.000e+00,  0.000e+00, ..., -3.142e+00, -3.142e+00],
           [-3.142e+00, -2.458e+00, ..., -2.658e+00, -1.079e+00],
           ...,
           [-4.342e-12, -1.543e+00, ..., -1.794e+00, -2.419e+00],
           [-3.142e+00, -3.142e+00, ...,  0.000e+00,  0.000e+00]],
          dtype=float32)
    r   r   )ra   r   rf   r   rg   r8   r   r   )Dr   magzeros_to_onesmag_nonzerophases         r   r"   r"     s~    h &&)C 1HM%K MM!4>>!''#:;E+%5EJ+%EJEMC:r   )r4   r3   c               b   |d| j                   d   dz
  z  }|t        |dz        }t        j                  d| j                   d   |t        j                        }t        | j                         }t        |      |d<   t        j                  | |      }|t        j                  dt        j                  z  |	      z  }t        j                  | d
         }| j                   D 	cg c]  }	d }
}	d|
d<   t        j                  | |
d      } t        |      D ]  \  }}| dt        |      t        |dz         f   }t        j                  |d      }d|z
  t        j                  |d
         z  |t        j                  |d         z  z   }t!        j"                  ||      |d|f<   t        j                  |d         t        j                  |d
         z
  |z
  }|dt        j                  z  t        j$                  |dt        j                  z  z        z  z
  }|||z   z  } |S c c}	w )a  Phase vocoder.  Given an STFT matrix D, speed up by a factor of ``rate``

    Based on the implementation provided by [#]_.

    This is a simplified implementation, intended primarily for
    reference and pedagogical purposes.  It makes no attempt to
    handle transients, and is likely to produce many audible
    artifacts.  For a higher quality implementation, we recommend
    the RubberBand library [#]_ and its Python wrapper `pyrubberband`.

    .. [#] Ellis, D. P. W. "A phase vocoder in Matlab."
        Columbia University, 2002.
        https://www.ee.columbia.edu/~dpwe/resources/matlab/pvoc/

    .. [#] https://breakfastquay.com/rubberband/

    Examples
    --------
    >>> # Play at double speed
    >>> y, sr   = librosa.load(librosa.ex('trumpet'))
    >>> D       = librosa.stft(y, n_fft=2048, hop_length=512)
    >>> D_fast  = librosa.phase_vocoder(D, rate=2.0, hop_length=512)
    >>> y_fast  = librosa.istft(D_fast, hop_length=512)

    >>> # Or play at 1/3 speed
    >>> y, sr   = librosa.load(librosa.ex('trumpet'))
    >>> D       = librosa.stft(y, n_fft=2048, hop_length=512)
    >>> D_slow  = librosa.phase_vocoder(D, rate=1./3, hop_length=512)
    >>> y_slow  = librosa.istft(D_slow, hop_length=512)

    Parameters
    ----------
    D : np.ndarray [shape=(..., d, t), dtype=complex]
        STFT matrix

    rate : float > 0 [scalar]
        Speed-up factor: ``rate > 1`` is faster, ``rate < 1`` is slower.

    hop_length : int > 0 [scalar] or None
        The number of samples between successive columns of ``D``.

        If None, defaults to ``n_fft//4 = (D.shape[0]-1)//2``

    n_fft : int > 0 or None
        The number of samples per frame in D.
        By default (None), this will be inferred from the shape of D.
        However, if D was constructed using an odd-length window, the correct
        frame length can be specified here.

    Returns
    -------
    D_stretched : np.ndarray [shape=(..., d, t / rate), dtype=complex]
        time-stretched STFT

    See Also
    --------
    pyrubberband
    r	   rD   r   r<   r   rM   r   rR   r   ).r   rN   )r   r	   r2   rO   .      ?).r   )r   r   )rS   rY   ra   r   float64re   len
zeros_liker   r   r   anglerc   	enumeratemodr   r   phasorround)r   rater4   r3   
time_stepsrS   	d_stretchphi_advance	phase_accrs   rt   tstepcolumnsalphar   dphases                    r   r%   r%   U  s   B }QWWR[1_%!_
1aggbk4rzzBJ MEJE"Iau-I w66!bee)5QQK 6#I  !ww'w!vwG'GBK
q'
+AZ(4CTS]223 tS!U{bffWV_55wv@W8WW !KK	s;	#q& '&/*RXXgfo-FFT #+3;1G(HHH 	[6))	# )& / (s   '	H,r   sossoxr_hq)r   r5   r4   r7   tuningr9   flayoutres_typec          	     v   |dvrt        d|       t        j                  | d       ||dz  }|r>| j                  D 
cg c]  }
d }}
|dz  |dz  f|d	<   t	        j
                  | ||
      } t        d||d|	\  }}g }t	        j                  |      }|D ]   }|j                  t        | |||             " t        d| j                  d	   |z
  |z  z         }t        | j                        }||d	<   |j                  d	t        |             t	        j                  | |      }|j                  D 
cg c]  }
t        d       }}
t!        t#        ||            D ]  \  }\  }}||d<   t	        j$                  ||k(        d   }|dk(  r-t&        j(                  j+                  |d   |d   ||   d	      }n*|dk(  r%t&        j(                  j-                  |||   d	      }||z  }||z  }t        t/        ||z              }t	        j0                  dj                  d	   |z
  |      }t        |      |k  rbt        t	        j2                  ||z              |z   }t        j4                  ||      }t	        j0                  d|j                  d	   |z
  |      }t	        j.                  |      j7                  t              d| }t        j8                  j;                  |t	        j0                  |            }|t	        j<                  |d|f   dz  d	      z  |t?        |      <    |S c c}
w c c}
w )u  Time-frequency representation using IIR filters

    This function will return a time-frequency representation
    using a multirate filter bank consisting of IIR filters. [#]_

    First, ``y`` is resampled as needed according to the provided ``sample_rates``.

    Then, a filterbank with with ``n`` band-pass filters is designed.

    The resampled input signals are processed by the filterbank as a whole.
    (`scipy.signal.filtfilt` resp. `sosfiltfilt` is used to make the phase linear.)
    The output of the filterbank is cut into frames.
    For each band, the short-time mean-square power (STMSP) is calculated by
    summing ``win_length`` subsequent filtered time samples.

    When called with the default set of parameters, it will generate the TF-representation
    (pitch filterbank):

        * 85 filters with MIDI pitches [24, 108] as ``center_freqs``.
        * each filter having a bandwidth of one semitone.

    .. [#] Müller, Meinard.
           "Information Retrieval for Music and Motion."
           Springer Verlag. 2007.

    Parameters
    ----------
    y : np.ndarray [shape=(..., n)]
        audio time series. Multi-channel is supported.
    sr : number > 0 [scalar]
        sampling rate of ``y``
    win_length : int > 0, <= n_fft
        Window length.
    hop_length : int > 0 [scalar]
        Hop length, number samples between subsequent frames.
        If not supplied, defaults to ``win_length // 4``.
    center : boolean
        - If ``True``, the signal ``y`` is padded so that frame
          ``D[..., :, t]`` is centered at ``y[t * hop_length]``.
        - If ``False``, then `D[..., :, t]`` begins at ``y[t * hop_length]``
    tuning : float [scalar]
        Tuning deviation from A440 in fractions of a bin.
    pad_mode : string
        If ``center=True``, the padding mode to use at the edges of the signal.
        By default, this function uses zero padding.
    flayout : string
        - If `sos` (default), a series of second-order filters is used for filtering with `scipy.signal.sosfiltfilt`.
          Minimizes numerical precision errors for high-order filters, but is slower.
        - If `ba`, the standard difference equation is used for filtering with `scipy.signal.filtfilt`.
          Can be unstable for high-order filters.
    res_type : string
        The resampling mode.  See `librosa.resample` for details.
    **kwargs : additional keyword arguments
        Additional arguments for `librosa.filters.semitone_filterbank`
        (e.g., could be used to provide another set of ``center_freqs`` and ``sample_rates``).

    Returns
    -------
    bands_power : np.ndarray [shape=(..., n, t), dtype=dtype]
        Short-time mean-square power for the input signal.

    Raises
    ------
    ParameterError
        If ``flayout`` is not None, `ba`, or `sos`.

    See Also
    --------
    librosa.filters.semitone_filterbank
    librosa.filters.mr_frequencies
    librosa.cqt
    scipy.signal.filtfilt
    scipy.signal.sosfiltfilt

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> y, sr = librosa.load(librosa.ex('trumpet'), duration=3)
    >>> D = np.abs(librosa.iirt(y, sr=sr))
    >>> C = np.abs(librosa.cqt(y=y, sr=sr))
    >>> fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
    >>> img = librosa.display.specshow(librosa.amplitude_to_db(C, ref=np.max),
    ...                                y_axis='cqt_hz', x_axis='time', ax=ax[0])
    >>> ax[0].set(title='Constant-Q transform')
    >>> ax[0].label_outer()
    >>> img = librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max),
    ...                                y_axis='cqt_hz', x_axis='time', ax=ax[1])
    >>> ax[1].set_title('Semitone spectrogram (iirt)')
    >>> fig.colorbar(img, ax=ax, format="%+2.0f dB")
    )bar   zUnsupported flayout=Fr>   Nr<   rN   r	   rM   rO   )r   r   )orig_sr	target_srr   r   rR   rD   r   r   rW   r   rB   . ) r   r   r[   rS   ra   rc   r   uniquer   r   rY   re   insertr   rf   slicer   zipflatnonzeroscipysignalfiltfiltsosfiltfiltr   r   rb   r   astypeaddoutersumtuple)rq   r   r5   r4   r7   r   r9   r   r   kwargsrs   rt   filterbank_ctsample_ratesy_resampledy_srscur_srr   rS   bands_powerslicesi
cur_filter
cur_sr_idxcur_filter_outputfactorhop_length_STMSPwin_length_STMSP_round	start_idx
min_lengthidxs                                  r   r#   r#     sS   P m#3G9=>> 	QU# 1_
 #$77+7a67+!Q
a8FF1gH- #6 #w#*0#M<
 KIIl#E8ArVhWX  1j0Z??@H MEE"I	LLS'(--/K<G<M<M&N<MquT{<MF&N#,S}-M#NFJr
 ^^EVO4Q7
d? % 5 51z!}k*.EB !6 !  % 8 8K
3" !9 ! f%.!$U:+>%?!@ II &&r*-CCEU
	 y>H$BGGH'7789<RR  !%0A
 S		!''+.DD I
 HHY'..s3IX>	ffll9bii0F&GH%+bffc3h'1,2/
 &
E&M"M $OT Q ,: 'Os   	L19L6.refamintop_dbc                    y Nr   r   r!  r"  r#  s       r   r'   r'          r   c                    y r%  r   r&  s       r   r'   r'     r'  r   c                    y r%  r   r&  s       r   r'   r'     r'  r   r   g|=g      T@c                  t        j                  |       } |dk  rt        d      t        j                  | j                  t         j
                        r-t        j                  dd       t        j                  |       }n| }t        |      r	 ||      }nt        j                  |      }dt        j                  t        j                  ||            z  }|dt        j                  t        j                  ||            z  z  }|7|dk  rt        d      t        j                  ||j                         |z
        }|S )a
  Convert a power spectrogram (amplitude squared) to decibel (dB) units

    This computes the scaling ``10 * log10(S / ref)`` in a numerically
    stable way.

    Parameters
    ----------
    S : np.ndarray
        input power

    ref : scalar or callable
        If scalar, the amplitude ``abs(S)`` is scaled relative to ``ref``::

            10 * log10(S / ref)

        Zeros in the output correspond to positions where ``S == ref``.

        If callable, the reference value is computed as ``ref(S)``.

    amin : float > 0 [scalar]
        minimum threshold for ``abs(S)`` and ``ref``

    top_db : float >= 0 [scalar]
        threshold the output at ``top_db`` below the peak:
        ``max(10 * log10(S/ref)) - top_db``

    Returns
    -------
    S_db : np.ndarray
        ``S_db ~= 10 * log10(S) - 10 * log10(ref)``

    See Also
    --------
    perceptual_weighting
    db_to_power
    amplitude_to_db
    db_to_amplitude

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

    Examples
    --------
    Get a power spectrogram from a waveform ``y``

    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> S = np.abs(librosa.stft(y))
    >>> librosa.power_to_db(S**2)
    array([[-41.809, -41.809, ..., -41.809, -41.809],
           [-41.809, -41.809, ..., -41.809, -41.809],
           ...,
           [-41.809, -41.809, ..., -41.809, -41.809],
           [-41.809, -41.809, ..., -41.809, -41.809]], dtype=float32)

    Compute dB relative to peak power

    >>> librosa.power_to_db(S**2, ref=np.max)
    array([[-80., -80., ..., -80., -80.],
           [-80., -80., ..., -80., -80.],
           ...,
           [-80., -80., ..., -80., -80.],
           [-80., -80., ..., -80., -80.]], dtype=float32)

    Or compare to median power

    >>> librosa.power_to_db(S**2, ref=np.median)
    array([[16.578, 16.578, ..., 16.578, 16.578],
           [16.578, 16.578, ..., 16.578, 16.578],
           ...,
           [16.578, 16.578, ..., 16.578, 16.578],
           [16.578, 16.578, ..., 16.578, 16.578]], dtype=float32)

    And plot the results

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
    >>> imgpow = librosa.display.specshow(S**2, sr=sr, y_axis='log', x_axis='time',
    ...                                   ax=ax[0])
    >>> ax[0].set(title='Power spectrogram')
    >>> ax[0].label_outer()
    >>> imgdb = librosa.display.specshow(librosa.power_to_db(S**2, ref=np.max),
    ...                                  sr=sr, y_axis='log', x_axis='time', ax=ax[1])
    >>> ax[1].set(title='Log-Power spectrogram')
    >>> fig.colorbar(imgpow, ax=ax[0])
    >>> fig.colorbar(imgdb, ax=ax[1], format="%+2.0f dB")
    r   zamin must be strictly positivezpower_to_db was called on complex input so phase information will be discarded. To suppress this warning, call power_to_db(np.abs(D)**2) instead.r	   
stacklevel      $@ztop_db must be non-negative)ra   asarrayr   
issubdtyper8   complexfloatingr^   r_   r   r   log10rI   ro   )r   r!  r"  r#  	magnitude	ref_valuelog_specs          r   r'   r'     s    ~ 	

1Aqy=>>	}}QWWb0016 		
 FF1I		}	N	FF3K	"((2::dI+F"GGHrxx

4 ;<<<HA: !>??::h(?@Or   r!  c                    y r%  r   S_dbr!  s     r   r(   r(   !       r   c                    y r%  r   r7  s     r   r(   r(   )  r9  r   c                    y r%  r   r7  s     r   r(   r(   1  r9  r   c               :    |t        j                  d| dz        z  S )a  Convert dB-scale values to a power values.

    This effectively inverts ``power_to_db``::

        db_to_power(S_db) ~= ref * 10.0**(S_db / 10)

    Parameters
    ----------
    S_db : np.ndarray
        dB-scaled values
    ref : number > 0
        Reference power: output will be scaled by this value

    Returns
    -------
    S : np.ndarray
        Power values

    Notes
    -----
    This function caches at level 30.
    r-  g?)ra   r   r7  s     r   r(   r(   9  s    0 $s
+++r   c                    y r%  r   r&  s       r   r)   r)   T  r'  r   c                    y r%  r   r&  s       r   r)   r)   ^  r'  r   c                    y r%  r   r&  s       r   r)   r)   h  r'  r   gh㈵>c                  t        j                  |       } t        j                  | j                  t         j                        rt        j                  dd       t        j                  |       }t        |      r	 ||      }nt        j                  |      }t        |t         j                        r|nd}t        j                  ||      }t        ||dz  |dz  |      }|S )a  Convert an amplitude spectrogram to dB-scaled spectrogram.

    This is equivalent to ``power_to_db(S**2, ref=ref**2, amin=amin**2, top_db=top_db)``,
    but is provided for convenience.

    Parameters
    ----------
    S : np.ndarray
        input amplitude

    ref : scalar or callable
        If scalar, the amplitude ``abs(S)`` is scaled relative to ``ref``:
        ``20 * log10(S / ref)``.
        Zeros in the output correspond to positions where ``S == ref``.

        If callable, the reference value is computed as ``ref(S)``.

    amin : float > 0 [scalar]
        minimum threshold for ``S`` and ``ref``

    top_db : float >= 0 [scalar]
        threshold the output at ``top_db`` below the peak:
        ``max(20 * log10(S/ref)) - top_db``

    Returns
    -------
    S_db : np.ndarray
        ``S`` measured in dB

    See Also
    --------
    power_to_db, db_to_amplitude

    Notes
    -----
    This function caches at level 30.
    zamplitude_to_db was called on complex input so phase information will be discarded. To suppress this warning, call amplitude_to_db(np.abs(S)) instead.r	   r+  Nr   r   )ra   r.  r/  r8   r0  r^   r_   r   r   
isinstancendarraysquarer'   )	r   r!  r"  r#  r2  r3  	out_arrayr   dbs	            r   r)   r)   r  s    Z 	

1A	}}QWWb0017 		
 q	I}	N	FF3K	'	2::>	DIIIiY/E IqLtQwvVBIr   c                    y r%  r   r7  s     r   r*   r*     r9  r   c                    y r%  r   r7  s     r   r*   r*     r9  r   c                    y r%  r   r7  s     r   r*   r*     r9  r   c               (    t        | |dz        dz  S )a  Convert a dB-scaled spectrogram to an amplitude spectrogram.

    This effectively inverts `amplitude_to_db`::

        db_to_amplitude(S_db) ~= 10.0**(0.5 * S_db/10 + log10(ref))

    Parameters
    ----------
    S_db : np.ndarray
        dB-scaled values
    ref : number > 0
        Optional reference amplitude.

    Returns
    -------
    S : np.ndarray
        Linear magnitude values

    Notes
    -----
    This function caches at level 30.
    r	   r5  r   )r(   r7  s     r   r*   r*     s    0 ta(C//r   Akindc               p    t        j                  ||      j                  d      }|t        | fi |z   }|S )a	  Perceptual weighting of a power spectrogram::

        S_p[..., f, :] = frequency_weighting(f, 'A') + 10*log(S[..., f, :] / ref)

    Parameters
    ----------
    S : np.ndarray [shape=(..., d, t)]
        Power spectrogram
    frequencies : np.ndarray [shape=(d,)]
        Center frequency for each row of` `S``
    kind : str
        The frequency weighting curve to use.
        e.g. `'A'`, `'B'`, `'C'`, `'D'`, `None or 'Z'`
    **kwargs : additional keyword arguments
        Additional keyword arguments to `power_to_db`.

    Returns
    -------
    S_p : np.ndarray [shape=(..., d, t)]
        perceptually weighted version of ``S``

    See Also
    --------
    power_to_db

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

    Examples
    --------
    Re-weight a CQT power spectrum, using peak power as reference

    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> C = np.abs(librosa.cqt(y, sr=sr, fmin=librosa.note_to_hz('A1')))
    >>> freqs = librosa.cqt_frequencies(C.shape[0],
    ...                                 fmin=librosa.note_to_hz('A1'))
    >>> perceptual_CQT = librosa.perceptual_weighting(C**2,
    ...                                               freqs,
    ...                                               ref=np.max)
    >>> perceptual_CQT
    array([[ -96.528,  -97.101, ..., -108.561, -108.561],
           [ -95.88 ,  -96.479, ..., -107.551, -107.551],
           ...,
           [ -65.142,  -53.256, ...,  -80.098,  -80.098],
           [ -71.542,  -53.197, ...,  -80.311,  -80.311]])

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
    >>> img = librosa.display.specshow(librosa.amplitude_to_db(C,
    ...                                                        ref=np.max),
    ...                                fmin=librosa.note_to_hz('A1'),
    ...                                y_axis='cqt_hz', x_axis='time',
    ...                                ax=ax[0])
    >>> ax[0].set(title='Log CQT power')
    >>> ax[0].label_outer()
    >>> imgp = librosa.display.specshow(perceptual_CQT, y_axis='cqt_hz',
    ...                                 fmin=librosa.note_to_hz('A1'),
    ...                                 x_axis='time', ax=ax[1])
    >>> ax[1].set(title='Perceptually weighted log CQT')
    >>> fig.colorbar(img, ax=ax[0], format="%+2.0f dB")
    >>> fig.colorbar(imgp, ax=ax[1], format="%+2.0f dB")
    rK  )rM   r   )r   frequency_weightingreshaper'   )r   frequenciesrL  r  r   results         r   r&   r&     s;    F ((4@HHQF+a":6"::FMr   r   cubicrM   )t_minn_fmtrL  betaover_samplerX   c          	        | j                   |   }|dk  rt        d| d| d      |dk  rt        d| d      ||d	k  rt        d
| d      t        j                  |d	z
        t        j                  |dz
        z
  }t	        t        j
                  |t        j                  |d	z
        t        j                  |      z
  z  |z              }nH|dk  rt        d| d      t        j                  |d	z
        t        j                  |dz
        z
  |z  }t        j                  t        j                  |             st        d      t        j                  |      }	t        j                  dd	|d      }
t        j                  j                  |
| ||      }t	        t        j
                  |            }t        j                  t        j                  |      t        j                  |      z
  |z  d||z   d|	      d|  }|d   |k  s|d   t        |dz
        |z  kD  r&t        j                  |t        |      |z  |
d         }t!        t        j"                  |            t!        |      k7  rt        d       ||      }d	g|j$                  z  }d||<   t'               }|j)                  |||z  j+                  |      t        j,                  |      z  |z  z  |      }|S )ar  Fast Mellin transform (FMT)

    The Mellin of a signal `y` is performed by interpolating `y` on an exponential time
    axis, applying a polynomial window, and then taking the discrete Fourier transform.

    When the Mellin parameter (beta) is 1/2, it is also known as the scale transform. [#]_
    The scale transform can be useful for audio analysis because its magnitude is invariant
    to scaling of the domain (e.g., time stretching or compression).  This is analogous
    to the magnitude of the Fourier transform being invariant to shifts in the input domain.

    .. [#] De Sena, Antonio, and Davide Rocchesso.
        "A fast Mellin and scale transform."
        EURASIP Journal on Applied Signal Processing 2007.1 (2007): 75-75.

    .. [#] Cohen, L.
        "The scale representation."
        IEEE Transactions on Signal Processing 41, no. 12 (1993): 3275-3292.

    Parameters
    ----------
    y : np.ndarray, real-valued
        The input signal(s).  Can be multidimensional.
        The target axis must contain at least 3 samples.

    t_min : float > 0
        The minimum time spacing (in samples).
        This value should generally be less than 1 to preserve as much information as
        possible.

    n_fmt : int > 2 or None
        The number of scale transform bins to use.
        If None, then ``n_bins = over_sample * ceil(n * log((n-1)/t_min))`` is taken,
        where ``n = y.shape[axis]``

    kind : str
        The type of interpolation to use when re-sampling the input.
        See `scipy.interpolate.interp1d` for possible values.

        Note that the default is to use high-precision (cubic) interpolation.
        This can be slow in practice; if speed is preferred over accuracy,
        then consider using ``kind='linear'``.

    beta : float
        The Mellin parameter.  ``beta=0.5`` provides the scale transform.

    over_sample : float >= 1
        Over-sampling factor for exponential resampling.

    axis : int
        The axis along which to transform ``y``

    Returns
    -------
    x_scale : np.ndarray [dtype=complex]
        The scale transform of ``y`` along the ``axis`` dimension.

    Raises
    ------
    ParameterError
        if ``n_fmt < 2`` or ``t_min <= 0``
        or if ``y`` is not finite
        or if ``y.shape[axis] < 3``.

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

    Examples
    --------
    >>> # Generate a signal and time-stretch it (with energy normalization)
    >>> scale = 1.25
    >>> freq = 3.0
    >>> x1 = np.linspace(0, 1, num=1024, endpoint=False)
    >>> x2 = np.linspace(0, 1, num=int(scale * len(x1)), endpoint=False)
    >>> y1 = np.sin(2 * np.pi * freq * x1)
    >>> y2 = np.sin(2 * np.pi * freq * x2) / np.sqrt(scale)
    >>> # Verify that the two signals have the same energy
    >>> np.sum(np.abs(y1)**2), np.sum(np.abs(y2)**2)
        (255.99999999999997, 255.99999999999969)
    >>> scale1 = librosa.fmt(y1, n_fmt=512)
    >>> scale2 = librosa.fmt(y2, n_fmt=512)

    >>> # And plot the results
    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(nrows=2)
    >>> ax[0].plot(y1, label='Original')
    >>> ax[0].plot(y2, linestyle='--', label='Stretched')
    >>> ax[0].set(xlabel='time (samples)', title='Input signals')
    >>> ax[0].legend()
    >>> ax[1].semilogy(np.abs(scale1), label='Original')
    >>> ax[1].semilogy(np.abs(scale2), linestyle='--', label='Stretched')
    >>> ax[1].set(xlabel='scale coefficients', title='Scale transform magnitude')
    >>> ax[1].legend()

    >>> # Plot the scale transform of an onset strength autocorrelation
    >>> y, sr = librosa.load(librosa.ex('choice'))
    >>> odf = librosa.onset.onset_strength(y=y, sr=sr)
    >>> # Auto-correlate with up to 10 seconds lag
    >>> odf_ac = librosa.autocorrelate(odf, max_size=10 * sr // 512)
    >>> # Normalize
    >>> odf_ac = librosa.util.normalize(odf_ac, norm=np.inf)
    >>> # Compute the scale transform
    >>> odf_ac_scale = librosa.fmt(librosa.util.normalize(odf_ac), n_fmt=512)
    >>> # Plot the results
    >>> fig, ax = plt.subplots(nrows=3)
    >>> ax[0].plot(odf, label='Onset strength')
    >>> ax[0].set(xlabel='Time (frames)', title='Onset strength')
    >>> ax[1].plot(odf_ac, label='Onset autocorrelation')
    >>> ax[1].set(xlabel='Lag (frames)', title='Onset autocorrelation')
    >>> ax[2].semilogy(np.abs(odf_ac_scale), label='Scale transform magnitude')
    >>> ax[2].set(xlabel='scale coefficients')
       zy.shape[z]==z < 3r   zt_min=z must be a positive numberNr   zover_sample=z must be >= 1r	   zn_fmt==zy must be finite everywhereF)numendpoint)rL  rX   )rY  rZ  baserM   r   z.Redundant sample positions in Mellin transformrW   )rS   r   ra   logrY   rb   allisfiniteexplinspacer  interpolateinterp1dlogspacer   r   r   r   rF   r   rk   rO  sqrt)rq   rS  rT  rL  rU  rV  rX   r   log_baser[  xf_interpn_overx_expy_resrS   r~   rQ  s                     r   r+   r+   4  s   v 	
A1uxvS4899zveW,FGHH}? <}M!JKK
 66!a%=266!a%=0BGGK266!a%=266%=+HIHTUV	wugT233FF519%uqy(99[H66"++a.!:;;66(D 	Aqa%0A   ))!QT)EH %&FKK		"h.	FN wE Qx%59uQW~'99uU|a/27 299UE
*MNN UOE C%**EE$K ,C%+&&u-
:UBC$ " F Mr   )r   r4   gainbiasr   time_constantepsbmax_sizer!  rX   max_axiszi	return_zfc                    y r%  r   r   r   r4   rk  rl  r   rm  rn  ro  rp  r!  rX   rq  rr  rs  s                  r   r,   r,         & r   )r   r4   rk  rl  r   rm  rn  ro  rp  r!  rX   rq  rr  c                    y r%  r   ru  s                  r   r,   r,   	  rv  r   c                    y r%  r   ru  s                  r   r,   r,   '	  rv  r   i   g\(\?g?c               p   |dk  rt        d| d      |dk  rt        d| d      |dk  rt        d| d      |dk  rt        d| d      |dk  rt        d	| d      t        j                  |	      st        d
|	 d      |;||z  t        |      z  }t	        j
                  dd|dz  z  z         dz
  d|dz  z  z  }d|cxk  rdk  sn t        d| d      t	        j                  | j                  t        j                        r,t        j                  dd       t	        j                  |       } |
|	dk(  r| }
n| j                  dk(  rt        d      |B| j                  dk7  rt        d| j                  dd      t	        j                  d|z
  d      }t        j                  j!                  | |	|      }
|Zt#        dg|
j                  z        }t	        j$                  |      }t        j&                  j)                  |gd|dz
  g      dd |dd t        j&                  j+                  |gd|dz
  g|
||      \  }}t	        j,                  | t	        j.                  |      t	        j0                  ||z        z   z        }|dk(  rt	        j0                  | |z        }n~|dk(  rBt	        j,                  |t	        j.                  |       t	        j.                  |      z   z        }n7||z  t	        j2                  |t	        j0                  | |z  |z        z        z  }|r||fS |S )a  Per-channel energy normalization (PCEN)

    This function normalizes a time-frequency representation ``S`` by
    performing automatic gain control, followed by nonlinear compression [#]_ ::

        P[f, t] = (S / (eps + M[f, t])**gain + bias)**power - bias**power

    IMPORTANT: the default values of eps, gain, bias, and power match the
    original publication, in which ``S`` is a 40-band mel-frequency
    spectrogram with 25 ms windowing, 10 ms frame shift, and raw audio values
    in the interval [-2**31; 2**31-1[. If you use these default values, we
    recommend to make sure that the raw audio is properly scaled to this
    interval, and not to [-1, 1[ as is most often the case.

    The matrix ``M`` is the result of applying a low-pass, temporal IIR filter
    to ``S``::

        M[f, t] = (1 - b) * M[f, t - 1] + b * S[f, t]

    If ``b`` is not provided, it is calculated as::

        b = (sqrt(1 + 4* T**2) - 1) / (2 * T**2)

    where ``T = time_constant * sr / hop_length``. [#]_

    This normalization is designed to suppress background noise and
    emphasize foreground signals, and can be used as an alternative to
    decibel scaling (`amplitude_to_db`).

    This implementation also supports smoothing across frequency bins
    by specifying ``max_size > 1``.  If this option is used, the filtered
    spectrogram ``M`` is computed as::

        M[f, t] = (1 - b) * M[f, t - 1] + b * R[f, t]

    where ``R`` has been max-filtered along the frequency axis, similar to
    the SuperFlux algorithm implemented in `onset.onset_strength`::

        R[f, t] = max(S[f - max_size//2: f + max_size//2, t])

    This can be used to perform automatic gain control on signals that cross
    or span multiple frequency bans, which may be desirable for spectrograms
    with high frequency resolution.

    .. [#] Wang, Y., Getreuer, P., Hughes, T., Lyon, R. F., & Saurous, R. A.
       (2017, March). Trainable frontend for robust and far-field keyword spotting.
       In Acoustics, Speech and Signal Processing (ICASSP), 2017
       IEEE International Conference on (pp. 5670-5674). IEEE.

    .. [#] Lostanlen, V., Salamon, J., McFee, B., Cartwright, M., Farnsworth, A.,
       Kelling, S., and Bello, J. P. Per-Channel Energy Normalization: Why and How.
       IEEE Signal Processing Letters, 26(1), 39-43.

    Parameters
    ----------
    S : np.ndarray (non-negative)
        The input (magnitude) spectrogram

    sr : number > 0 [scalar]
        The audio sampling rate

    hop_length : int > 0 [scalar]
        The hop length of ``S``, expressed in samples

    gain : number >= 0 [scalar]
        The gain factor.  Typical values should be slightly less than 1.

    bias : number >= 0 [scalar]
        The bias point of the nonlinear compression (default: 2)

    power : number >= 0 [scalar]
        The compression exponent.  Typical values should be between 0 and 0.5.
        Smaller values of ``power`` result in stronger compression.
        At the limit ``power=0``, polynomial compression becomes logarithmic.

    time_constant : number > 0 [scalar]
        The time constant for IIR filtering, measured in seconds.

    eps : number > 0 [scalar]
        A small constant used to ensure numerical stability of the filter.

    b : number in [0, 1]  [scalar]
        The filter coefficient for the low-pass filter.
        If not provided, it will be inferred from ``time_constant``.

    max_size : int > 0 [scalar]
        The width of the max filter applied to the frequency axis.
        If left as `1`, no filtering is performed.

    ref : None or np.ndarray (shape=S.shape)
        An optional pre-computed reference spectrum (``R`` in the above).
        If not provided it will be computed from ``S``.

    axis : int [scalar]
        The (time) axis of the input spectrogram.

    max_axis : None or int [scalar]
        The frequency axis of the input spectrogram.
        If `None`, and ``S`` is two-dimensional, it will be inferred
        as the opposite from ``axis``.
        If ``S`` is not two-dimensional, and ``max_size > 1``, an error
        will be raised.

    zi : np.ndarray
        The initial filter delay values.

        This may be the ``zf`` (final delay values) of a previous call to ``pcen``, or
        computed by `scipy.signal.lfilter_zi`.

    return_zf : bool
        If ``True``, return the final filter delay values along with the PCEN output ``P``.
        This is primarily useful in streaming contexts, where the final state of one
        block of processing should be used to initialize the next block.

        If ``False`` (default) only the PCEN values ``P`` are returned.

    Returns
    -------
    P : np.ndarray, non-negative [shape=(n, m)]
        The per-channel energy normalized version of ``S``.
    zf : np.ndarray (optional)
        The final filter delay values.  Only returned if ``return_zf=True``.

    See Also
    --------
    amplitude_to_db
    librosa.onset.onset_strength

    Examples
    --------
    Compare PCEN to log amplitude (dB) scaling on Mel spectra

    >>> import matplotlib.pyplot as plt
    >>> y, sr = librosa.load(librosa.ex('robin'))

    >>> # We recommend scaling y to the range [-2**31, 2**31[ before applying
    >>> # PCEN's default parameters. Furthermore, we use power=1 to get a
    >>> # magnitude spectrum instead of a power spectrum.
    >>> S = librosa.feature.melspectrogram(y=y, sr=sr, power=1)
    >>> log_S = librosa.amplitude_to_db(S, ref=np.max)
    >>> pcen_S = librosa.pcen(S * (2**31))
    >>> fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
    >>> img = librosa.display.specshow(log_S, x_axis='time', y_axis='mel', ax=ax[0])
    >>> ax[0].set(title='log amplitude (dB)', xlabel=None)
    >>> ax[0].label_outer()
    >>> imgpcen = librosa.display.specshow(pcen_S, x_axis='time', y_axis='mel', ax=ax[1])
    >>> ax[1].set(title='Per-channel energy normalization')
    >>> fig.colorbar(img, ax=ax[0], format="%+2.0f dB")
    >>> fig.colorbar(imgpcen, ax=ax[1])

    Compare PCEN with and without max-filtering

    >>> pcen_max = librosa.pcen(S * (2**31), max_size=3)
    >>> fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
    >>> librosa.display.specshow(pcen_S, x_axis='time', y_axis='mel', ax=ax[0])
    >>> ax[0].set(title='Per-channel energy normalization (no max-filter)')
    >>> ax[0].label_outer()
    >>> img = librosa.display.specshow(pcen_max, x_axis='time', y_axis='mel', ax=ax[1])
    >>> ax[1].set(title='Per-channel energy normalization (max_size=3)')
    >>> fig.colorbar(img, ax=ax)
    r   zpower=z must be nonnegativezgain=z must be non-negativezbias=zeps=z must be strictly positiveztime_constant=z	max_size=r=   Nr   r<   r	   zb=z must be between 0 and 1zpcen was called on complex input so phase information will be discarded. To suppress this warning, call pcen(np.abs(D)) instead.r+  z6Max-filtering cannot be applied to 1-dimensional inputzMax-filtering a dz9-dimensional spectrogram requires you to specify max_axisrW   )rr  rX   )r   r   rZ   r   ra   rd  r/  r8   r0  r^   r_   r   rF   r   r  ndimagemaximum_filter1dr  emptyr  
lfilter_zilfilterr_  r\  log1pexpm1)r   r   r4   rk  rl  r   rm  rn  ro  rp  r!  rX   rq  rr  rs  t_framesrS   S_smoothzfsmoothS_outs                        r   r,   r,   =	  s'   h qyveW,@ABBaxuTF*?@AAaxuTF*?@AA
axtC5(BCDD~m_<VWXX)y
2MNOOy 2%j(99 WWQXq[()A-!hk/B;Q;r!$<=>>	}}QWWb001, 		
 FF1I
{q=CVVq[ H  66Q;(*166!* 5; ;  66!d(A.--00H80LC	zqcCHHn%XXe_''aQZ8;1
 <<''aQZ$'OLHb VVTERVVC[288HsN+CCDEF zV$	uq	BFF6N :;<u!f*t:K1L)L MMbyr       gGz?random)n_iterr4   r5   r3   r6   r7   r8   r   r9   momentuminitrandom_statec               f   |t         j                  j                         }nt        |t              r!t         j                  j                  |      }nOt        |t         j                  j
                  t         j                  j                  f      r|}nt        d|      |
dkD  rt        j                  d|
 dd       n|
d	k  rt        d
|
 d      |d| j                  d   dz
  z  }t        j                  | j                  t        j                  | j                              }t        j                  |      }|dk(  rGt        j                   dt         j"                  z  |j                  | j                        z        |dd n|d|dd nt        d| d      d}d}d}|| z  }t%        |      D ]b  }t'        |||||||||	      }t)        |||||||	|      }||dd |||
d|
z   z  |z  z  }|t        j*                  |      |z   z  }|| z  }||}}d t'        |||||||||	      S )u  Approximate magnitude spectrogram inversion using the "fast" Griffin-Lim algorithm.

    Given a short-time Fourier transform magnitude matrix (``S``), the algorithm randomly
    initializes phase estimates, and then alternates forward- and inverse-STFT
    operations. [#]_

    Note that this assumes reconstruction of a real-valued time-domain signal, and
    that ``S`` contains only the non-negative frequencies (as computed by
    `stft`).

    The "fast" GL method [#]_ uses a momentum parameter to accelerate convergence.

    .. [#] D. W. Griffin and J. S. Lim,
        "Signal estimation from modified short-time Fourier transform,"
        IEEE Trans. ASSP, vol.32, no.2, pp.236–243, Apr. 1984.

    .. [#] Perraudin, N., Balazs, P., & Søndergaard, P. L.
        "A fast Griffin-Lim algorithm,"
        IEEE Workshop on Applications of Signal Processing to Audio and Acoustics (pp. 1-4),
        Oct. 2013.

    Parameters
    ----------
    S : np.ndarray [shape=(..., n_fft // 2 + 1, t), non-negative]
        An array of short-time Fourier transform magnitudes as produced by
        `stft`.

    n_iter : int > 0
        The number of iterations to run

    hop_length : None or int > 0
        The hop length of the STFT.  If not provided, it will default to ``n_fft // 4``

    win_length : None or int > 0
        The window length of the STFT.  By default, it will equal ``n_fft``

    n_fft : None or int > 0
        The number of samples per frame.
        By default, this will be inferred from the shape of ``S`` as an even number.
        However, if an odd frame length was used, you can explicitly set ``n_fft``.

    window : string, tuple, number, function, or np.ndarray [shape=(n_fft,)]
        A window specification as supported by `stft` or `istft`

    center : boolean
        If ``True``, the STFT is assumed to use centered frames.
        If ``False``, the STFT is assumed to use left-aligned frames.

    dtype : np.dtype
        Real numeric type for the time-domain signal.  Default is inferred
        to match the precision of the input spectrogram.

    length : None or int > 0
        If provided, the output ``y`` is zero-padded or clipped to exactly ``length``
        samples.

    pad_mode : string
        If ``center=True``, the padding mode to use at the edges of the signal.
        By default, STFT uses zero padding.

    momentum : number >= 0
        The momentum parameter for fast Griffin-Lim.
        Setting this to 0 recovers the original Griffin-Lim method [1]_.
        Values near 1 can lead to faster convergence, but above 1 may not converge.

    init : None or 'random' [default]
        If 'random' (the default), then phase values are initialized randomly
        according to ``random_state``.  This is recommended when the input ``S`` is
        a magnitude spectrogram with no initial phase estimates.

        If `None`, then the phase is initialized from ``S``.  This is useful when
        an initial guess for phase can be provided, or when you want to resume
        Griffin-Lim from a previous output.

    random_state : None, int, np.random.RandomState, or np.random.Generator
        If int, random_state is the seed used by the random number generator
        for phase initialization.

        If `np.random.RandomState` or `np.random.Generator` instance, the random number
        generator itself.

        If `None`, defaults to the `np.random.default_rng()` object.

    Returns
    -------
    y : np.ndarray [shape=(..., n)]
        time-domain signal reconstructed from ``S``

    See Also
    --------
    stft
    istft
    magphase
    filters.get_window

    Examples
    --------
    A basic STFT inverse example

    >>> y, sr = librosa.load(librosa.ex('trumpet'))
    >>> # Get the magnitude spectrogram
    >>> S = np.abs(librosa.stft(y))
    >>> # Invert using Griffin-Lim
    >>> y_inv = librosa.griffinlim(S)
    >>> # Invert without estimating phase
    >>> y_istft = librosa.istft(S)

    Wave-plot the results

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(nrows=3, sharex=True, sharey=True)
    >>> librosa.display.waveshow(y, sr=sr, color='b', ax=ax[0])
    >>> ax[0].set(title='Original', xlabel=None)
    >>> ax[0].label_outer()
    >>> librosa.display.waveshow(y_inv, sr=sr, color='g', ax=ax[1])
    >>> ax[1].set(title='Griffin-Lim reconstruction', xlabel=None)
    >>> ax[1].label_outer()
    >>> librosa.display.waveshow(y_istft, sr=sr, color='r', ax=ax[2])
    >>> ax[2].set_title('Magnitude-only istft reconstruction')
    N)seedzUnsupported random_state=r   zGriffin-Lim with momentum=z+ > 1 can be unstable. Proceed with caution!r	   r+  r   z"griffinlim() called with momentum=z < 0rD   r   r  rB   r   zinit=z must either None or 'random'r   )r3   r4   r5   r6   r7   r9   r:   )ra   r  default_rngrA  rY   RandomState	Generatorr   r^   r_   rS   r}  r   rg   r8   r   r   r   r`   r!   r    r   )r   r  r4   r5   r3   r6   r7   r8   r   r9   r  r  r  rnganglesrn  rebuilttprevinversers   s                       r   r-   r-   J
  sW   T ii##%	L#	&ii###6	L299#8#8")):M:M"N	O88HIJJ!|(
 3$ $	

 
AA(4PQQ }QWWR[1_% XXaggT^^AGG%<=F
))F
CxKKRUUSZZQWWZ-E!EGq		q	uTF*GHII GEG aKF6]!!

 !!	
 q	x1x<0E99F"&&.3&&!C H 
 
r   	rq   r   r3   r4   r   r5   r6   r7   r9   c        	            |3||dz  dz   |j                   d   k7  rd|j                   d   dz
  z  }||fS |t        d|       | t        d      t        j                  t	        | ||||||            |z  }||fS )a  Retrieve a magnitude spectrogram.

    This is primarily used in feature extraction functions that can operate on
    either audio time-series or spectrogram input.

    Parameters
    ----------
    y : None or np.ndarray
        If provided, an audio time series

    S : None or np.ndarray
        Spectrogram input, optional

    n_fft : int > 0
        STFT window size

    hop_length : int > 0
        STFT hop length

    power : float > 0
        Exponent for the magnitude spectrogram,
        e.g., 1 for energy, 2 for power, etc.

    win_length : int <= n_fft [scalar]
        Each frame of audio is windowed by ``window``.
        The window will be of length ``win_length`` and then padded
        with zeros to match ``n_fft``.

        If unspecified, defaults to ``win_length = n_fft``.

    window : string, tuple, number, function, or np.ndarray [shape=(n_fft,)]
        - a window specification (string, tuple, or number);
          see `scipy.signal.get_window`
        - a window function, such as `scipy.signal.windows.hann`
        - a vector or array of length ``n_fft``

        .. see also:: `filters.get_window`

    center : boolean
        - If ``True``, the signal ``y`` is padded so that frame
          ``t`` is centered at ``y[t * hop_length]``.
        - If ``False``, then frame ``t`` begins at ``y[t * hop_length]``

    pad_mode : string
        If ``center=True``, the padding mode to use at the edges of the signal.
        By default, STFT uses zero padding.

    Returns
    -------
    S_out : np.ndarray [dtype=np.float]
        - If ``S`` is provided as input, then ``S_out == S``
        - Else, ``S_out = |stft(y, ...)|**power``
    n_fft : int > 0
        - If ``S`` is provided, then ``n_fft`` is inferred from ``S``
        - Else, copied from input
    r	   r   rD   z)Unable to compute spectrogram with n_fft=z6Input signal must be provided to compute a spectrogram)r3   r4   r5   r7   r6   r9   )rS   r   ra   r   r    r  s	            r   _spectrogramr  /  s    H 	}=EQJNaggbk9q)E0 e8O+ = #LUG!TUU9 H  FF))!!%
  	
 e8Or   )rq   
np.ndarrayr3   rY   r4   Optional[int]r5   r  r6   r   r7   boolr8   Optional[DTypeLike]r9   r   r:   Optional[np.ndarray]returnr  )r   r  r4   r  r5   r  r3   r  r6   r   r7   r  r8   r  r   r  r:   r  r  r  )	r   Nr0   NNr1   TNr2   )rq   r  r   r   r   r  r3   rY   r4   r  r5   r  r6   r   r7   r  r8   r  r9   r   r  Tuple[np.ndarray, np.ndarray]) rq   r  r   r   r   r  r3   rY   r4   r  r5   r  r6   r   r7   r  r   r  r   r  r   Union[float, Callable]r   r  r   r  r8   r  r9   r   r  z)Tuple[np.ndarray, np.ndarray, np.ndarray])r   r  r   r   r  r  )
r   r  r   r   r4   r  r3   r  r  r  )rq   r  r   r   r5   rY   r4   r  r7   r  r   r   r9   r   r   strr   r  r  r   r  r  )
r   r   r!  r  r"  r   r#  Optional[float]r  np.floating[Any])
r   z_SequenceLike[_ComplexLike_co]r!  r  r"  r   r#  r  r  r  )
r   z"_ScalarOrSequence[_ComplexLike_co]r!  r  r"  r   r#  r  r  #Union[np.floating[Any], np.ndarray])r8  r   r!  r   r  r  )r8  r  r!  r   r  r  )r8  z Union[_FloatLike_co, np.ndarray]r!  r   r  r  )
r   r  rP  r  rL  r  r  r   r  r  )rq   r  rS  r   rT  r  rL  r  rU  r   rV  r   rX   rY   r  r  ) r   r  r   r   r4   rY   rk  r   rl  r   r   r   rm  r   rn  r   ro  r  rp  rY   r!  r  rX   rY   rq  r  rr  r  rs  zLiteral[False]r  r  ) r   r  r   r   r4   rY   rk  r   rl  r   r   r   rm  r   rn  r   ro  r  rp  rY   r!  r  rX   rY   rq  r  rr  r  rs  zLiteral[True]r  r  ) r   r  r   r   r4   rY   rk  r   rl  r   r   r   rm  r   rn  r   ro  r  rp  rY   r!  r  rX   rY   rq  r  rr  r  rs  r  r  z0Union[np.ndarray, Tuple[np.ndarray, np.ndarray]])r   r  r  rY   r4   r  r5   r  r3   r  r6   r   r7   r  r8   r  r   r  r9   r   r  r   r  zOptional[str]r  z@Optional[Union[int, np.random.RandomState, np.random.Generator]]r  r  )rq   r  r   r  r3   r  r4   r  r   r   r5   r  r6   r   r7   r  r9   r   r  zTuple[np.ndarray, int])B__doc__
__future__r   r^   numpyra   r  scipy.ndimagescipy.signalscipy.interpolatenumbar    r   r~   r   audior   _cacher
   r   util.exceptionsr   filtersr   r   r   numpy.typingr   typingr   r   r   r   r   r   r   typing_extensionsr   _typingr   r   r   r   r   r   r   __all__r    r!   r   r   r   r$   r"   r%   r#   r'   r(   r)   r*   r&   r+   r,   r-   r  r   r   r   <module>r     s   ( "             , 5 & " H H H %  $ R  $ $ !%' $OO O 	O
 O O O O O 
O O Od
 R !% $ !%  $hh h 	h
 h h h h h 
h h hV d$>  >$ " $ $ !%'``` ` 	`
 ` ` ` ` ` ` #`J " $ $ !%'rrr r 	r
 r r r r r r #rp " $ $ !%(,!%'!oo 	o 	o
 o o o o o o o &o o o o  !o" /#od	 /0 BR !%ll l 	l
 l l^ R  $#{{ 	{ 	{
 { { { { { { { { {| 
 #&! 
  	
   
 
 #&!% 
  	
   
 
 #&!) 
  	
  ) 
 R #&"|)| 
 | 	|
 | )| |~ 
 
 
 	 
 
  
 	 
 
 
* 
 )	 
 RHK , ,4 
 #&! 
  	
   
 
 #&!% 
  	
   
 
 #&!) 
  	
  ) 
 R #&"B)B 
 B 	B
 B )B BJ 
 
 
 	 
 
 
 
 	 
 
 
* 
 )	 
 RLO 0 04 R;>EE *E58EJMEE EP R CC C 	C
 C C C C C CL 
  #!" #! 	 	
     
   
   	  !" # 
* 
  #!" 	 	
     
   
   	  !" ## 
* 
  #!"! 	 	
     
   
   	  !" 6# 
* R   $"#!II 	I 	I
 I I I I 
I I I 
I I I 	I  !I" 6#I I^  $ $ !% '" 	!bb b 	b
 b b b b b b b b bb" #bN #" # $ '__ _ 	_
 _ _ _ _ _ _ _r   