B
    )`W%              	   @   s   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 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ZdZdddZdddZdd ZedejdddddejdfddZdS ) zmel conversion ops.    )absolute_import)division)print_function)dtypes)ops)tensor_util)	array_ops)math_ops)	shape_ops)dispatch)	tf_exportg     @g     @Nc          	   C   s<   t |d| g" t | } tt| t d  S Q R X dS )a6  Converts frequencies in `mel_values` from the mel scale to linear scale.

  Args:
    mel_values: A `Tensor` of frequencies in the mel scale.
    name: An optional name for the operation.

  Returns:
    A `Tensor` of the same shape and type as `mel_values` containing linear
    scale frequencies in Hertz.
  Zmel_to_hertzg      ?N)r   
name_scopeconvert_to_tensor_MEL_BREAK_FREQUENCY_HERTZr	   exp_MEL_HIGH_FREQUENCY_Q)Z
mel_valuesname r   S/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow/python/ops/signal/mel_ops.py_mel_to_hertz$   s    
r   c          	   C   s<   t |d| g" t | } ttd| t   S Q R X dS )a5  Converts frequencies in `frequencies_hertz` in Hertz to the mel scale.

  Args:
    frequencies_hertz: A `Tensor` of frequencies in Hertz.
    name: An optional name for the operation.

  Returns:
    A `Tensor` of the same shape and type of `frequencies_hertz` containing
    frequencies in the mel scale.
  Zhertz_to_melg      ?N)r   r   r   r   r	   logr   )Zfrequencies_hertzr   r   r   r   _hertz_to_mel6   s    
r   c             C   s   | dkrt d|  |dk r(t d| ||kr@t d||f t|tjs||dkr`t d| ||d kr|t d||f |jst d	| d
S )z1Checks the inputs to linear_to_mel_weight_matrix.r   z&num_mel_bins must be positive. Got: %sg        z.lower_edge_hertz must be non-negative. Got: %sz.lower_edge_hertz %.1f >= upper_edge_hertz %.1fz%sample_rate must be positive. Got: %s   zlupper_edge_hertz must not be larger than the Nyquist frequency (sample_rate / 2). Got %s for sample_rate: %sz,dtype must be a floating point type. Got: %sN)
ValueError
isinstancer   TensorZis_floating)num_mel_binssample_ratelower_edge_hertzupper_edge_hertzdtyper   r   r   _validate_argumentsG   s     r!   z"signal.linear_to_mel_weight_matrix      i@  g     @_@g     @c          	      sX  t |d@}t|t jr2t|}|dk	r2|}t |||| tj||dd}t j	||dd}t j	||dd}t 	d|}d}	|d	 }
t
||
||	d }tt|d}tjt
t|t| d
 ddd}t fddtj|dddD \}}}|| ||  }|| ||  }t|t||}tj||	dgddgg|dS Q R X dS )a
  Returns a matrix to warp linear scale spectrograms to the [mel scale][mel].

  Returns a weight matrix that can be used to re-weight a `Tensor` containing
  `num_spectrogram_bins` linearly sampled frequency information from
  `[0, sample_rate / 2]` into `num_mel_bins` frequency information from
  `[lower_edge_hertz, upper_edge_hertz]` on the [mel scale][mel].

  This function follows the [Hidden Markov Model Toolkit
  (HTK)](http://htk.eng.cam.ac.uk/) convention, defining the mel scale in
  terms of a frequency in hertz according to the following formula:

      $$	extrm{mel}(f) = 2595 * 	extrm{log}_{10}(1 + rac{f}{700})$$

  In the returned matrix, all the triangles (filterbanks) have a peak value
  of 1.0.

  For example, the returned matrix `A` can be used to right-multiply a
  spectrogram `S` of shape `[frames, num_spectrogram_bins]` of linear
  scale spectrum values (e.g. STFT magnitudes) to generate a "mel spectrogram"
  `M` of shape `[frames, num_mel_bins]`.

      # `S` has shape [frames, num_spectrogram_bins]
      # `M` has shape [frames, num_mel_bins]
      M = tf.matmul(S, A)

  The matrix can be used with `tf.tensordot` to convert an arbitrary rank
  `Tensor` of linear-scale spectral bins into the mel scale.

      # S has shape [..., num_spectrogram_bins].
      # M has shape [..., num_mel_bins].
      M = tf.tensordot(S, A, 1)

  Args:
    num_mel_bins: Python int. How many bands in the resulting mel spectrum.
    num_spectrogram_bins: An integer `Tensor`. How many bins there are in the
      source spectrogram data, which is understood to be `fft_size // 2 + 1`,
      i.e. the spectrogram only contains the nonredundant FFT bins.
    sample_rate: An integer or float `Tensor`. Samples per second of the input
      signal used to create the spectrogram. Used to figure out the frequencies
      corresponding to each spectrogram bin, which dictates how they are mapped
      into the mel scale.
    lower_edge_hertz: Python float. Lower bound on the frequencies to be
      included in the mel spectrum. This corresponds to the lower edge of the
      lowest triangular band.
    upper_edge_hertz: Python float. The desired top edge of the highest
      frequency band.
    dtype: The `DType` of the result matrix. Must be a floating point type.
    name: An optional name for the operation.

  Returns:
    A `Tensor` of shape `[num_spectrogram_bins, num_mel_bins]`.

  Raises:
    ValueError: If `num_mel_bins`/`num_spectrogram_bins`/`sample_rate` are not
      positive, `lower_edge_hertz` is negative, frequency edges are incorrectly
      ordered, `upper_edge_hertz` is larger than the Nyquist frequency.

  [mel]: https://en.wikipedia.org/wiki/Mel_scale
  linear_to_mel_weight_matrixNr   )r   r   r   g           g       @r      )Zframe_lengthZ
frame_stepc             3   s   | ]}t |d  gV  qdS )r%   N)r   Zreshape).0t)r   r   r   	<genexpr>   s   z.linear_to_mel_weight_matrix.<locals>.<genexpr>)Zaxisr   )r   r   r   r   r   Zconstant_valuer!   r	   castr   Zlinspacer   Zexpand_dimsr   r
   frametuplesplitmaximumZminimumpad)r   Znum_spectrogram_binsr   r   r   r    r   Zmaybe_const_valZzeroZbands_to_zeroZnyquist_hertzZlinear_frequenciesZspectrogram_bins_melZband_edges_melZlower_edge_melZ
center_melZupper_edge_melZlower_slopesZupper_slopesZmel_weights_matrixr   )r   r   r$   ]   sF    D




r$   )N)N)__doc__
__future__r   r   r   Ztensorflow.python.frameworkr   r   r   Ztensorflow.python.opsr   r	   Ztensorflow.python.ops.signalr
   Ztensorflow.python.utilr   Z tensorflow.python.util.tf_exportr   r   r   r   r   r!   Zadd_dispatch_supportfloat32r$   r   r   r   r   <module>   s2   

