
    AVh                         d 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  ed      e	j                  dd
              Zy	)z0Mel-Frequency Cepstral Coefficients (MFCCs) ops.    )ops)	array_ops)math_ops)dct_ops)dispatch)	tf_exportz&signal.mfccs_from_log_mel_spectrogramsNc                 6   t        j                  |d| g      5  t        j                  |       } | j                  j                  rY| j                  j
                  d   j                  6| j                  j
                  d   j                  }|dk(  r&t        d| z        t        j                  |       d   }t        j                  | d      }|t        j                  t        j                  ||j                        dz        z  cddd       S # 1 sw Y   yxY w)	a{	  Computes [MFCCs][mfcc] of `log_mel_spectrograms`.

  Implemented with GPU-compatible ops and supports gradients.

  [Mel-Frequency Cepstral Coefficient (MFCC)][mfcc] calculation consists of
  taking the DCT-II of a log-magnitude mel-scale spectrogram. [HTK][htk]'s MFCCs
  use a particular scaling of the DCT-II which is almost orthogonal
  normalization. We follow this convention.

  All `num_mel_bins` MFCCs are returned and it is up to the caller to select
  a subset of the MFCCs based on their application. For example, it is typical
  to only use the first few for speech recognition, as this results in
  an approximately pitch-invariant representation of the signal.

  For example:

  ```python
  batch_size, num_samples, sample_rate = 32, 32000, 16000.0
  # A Tensor of [batch_size, num_samples] mono PCM samples in the range [-1, 1].
  pcm = tf.random.normal([batch_size, num_samples], dtype=tf.float32)

  # A 1024-point STFT with frames of 64 ms and 75% overlap.
  stfts = tf.signal.stft(pcm, frame_length=1024, frame_step=256,
                         fft_length=1024)
  spectrograms = tf.abs(stfts)

  # Warp the linear scale spectrograms into the mel-scale.
  num_spectrogram_bins = stfts.shape[-1].value
  lower_edge_hertz, upper_edge_hertz, num_mel_bins = 80.0, 7600.0, 80
  linear_to_mel_weight_matrix = tf.signal.linear_to_mel_weight_matrix(
    num_mel_bins, num_spectrogram_bins, sample_rate, lower_edge_hertz,
    upper_edge_hertz)
  mel_spectrograms = tf.tensordot(
    spectrograms, linear_to_mel_weight_matrix, 1)
  mel_spectrograms.set_shape(spectrograms.shape[:-1].concatenate(
    linear_to_mel_weight_matrix.shape[-1:]))

  # Compute a stabilized log to get log-magnitude mel-scale spectrograms.
  log_mel_spectrograms = tf.math.log(mel_spectrograms + 1e-6)

  # Compute MFCCs from log_mel_spectrograms and take the first 13.
  mfccs = tf.signal.mfccs_from_log_mel_spectrograms(
    log_mel_spectrograms)[..., :13]
  ```

  Args:
    log_mel_spectrograms: A `[..., num_mel_bins]` `float32`/`float64` `Tensor`
      of log-magnitude mel-scale spectrograms.
    name: An optional name for the operation.
  Returns:
    A `[..., num_mel_bins]` `float32`/`float64` `Tensor` of the MFCCs of
    `log_mel_spectrograms`.

  Raises:
    ValueError: If `num_mel_bins` is not positive.

  [mfcc]: https://en.wikipedia.org/wiki/Mel-frequency_cepstrum
  [htk]: https://en.wikipedia.org/wiki/HTK_(software)
  mfccs_from_log_mel_spectrogramsNr   z&num_mel_bins must be positive. Got: %s   )typeg       @)r   
name_scopeconvert_to_tensorshapendimsdimsvalue
ValueErrorr   r   dctr   rsqrtcastdtype)log_mel_spectrogramsnamenum_mel_binsdct2s       U/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/signal/mfcc_ops.pyr
   r
      s    | ~~d=+,. 7 001EF""((""''+11=)//44R8>>l		A-. / 	/ __%9:2>l;;+!4D(..lDJJ/#57 7'7 7 7s   C,DD)N)__doc__tensorflow.python.frameworkr   tensorflow.python.opsr   r   tensorflow.python.ops.signalr   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   add_dispatch_supportr
        r   <module>r'      sF    7 + + * 0 + 6 34	P7  5P7r&   