o
    ?e_(                     @   s   d 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 ddlmZ dd ZedddgdejdddZdddZedddgdejdddZdS )zDiscrete Cosine Transform ops.    N)dtypes)ops)
smart_cond)tensor_shape)	array_ops)math_ops)fft_ops)dispatch)	tf_exportc                 C   s   |dkr
t d| |dur|dk rtd|dvrtd|dkr<|dkr*td	| jd dur<| jd d
k r<td|dvrFtd| dS )z>Checks that DCT/IDCT arguments are compatible and well formed.zaxis must be -1. Got: %sN   z&n should be a positive integer or Noner            z-Types I, II, III and IV (I)DCT are supported.orthoz2Normalization is not supported for the Type-I DCT.r   z9Type-I DCT requires the dimension to be greater than one.)Nr   z8Unknown normalization. Expected None or 'ortho', got: %s)NotImplementedError
ValueErrorshape)Zinput_tensorZdct_typenaxisnorm r   e/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/ops/signal/dct_ops.py_validate_dct_arguments   s$   r   z
signal.dctzspectral.dct)v1r   r   c                 C   s"   t | |||| t| |||||S )aE  Computes the 1D [Discrete Cosine Transform (DCT)][dct] of `input`.

  Types I, II, III and IV are supported.
  Type I is implemented using a length `2N` padded `tf.signal.rfft`.
  Type II is implemented using a length `2N` padded `tf.signal.rfft`, as
   described here: [Type 2 DCT using 2N FFT padded (Makhoul)]
   (https://dsp.stackexchange.com/a/10606).
  Type III is a fairly straightforward inverse of Type II
   (i.e. using a length `2N` padded `tf.signal.irfft`).
   Type IV is calculated through 2N length DCT2 of padded signal and
  picking the odd indices.

  @compatibility(scipy)
  Equivalent to [scipy.fftpack.dct]
   (https://docs.scipy.org/doc/scipy-1.4.0/reference/generated/scipy.fftpack.dct.html)
   for Type-I, Type-II, Type-III and Type-IV DCT.
  @end_compatibility

  Args:
    input: A `[..., samples]` `float32`/`float64` `Tensor` containing the
      signals to take the DCT of.
    type: The DCT type to perform. Must be 1, 2, 3 or 4.
    n: The length of the transform. If length is less than sequence length,
      only the first n elements of the sequence are considered for the DCT.
      If n is greater than the sequence length, zeros are padded and then
      the DCT is computed as usual.
    axis: For future expansion. The axis to compute the DCT along. Must be `-1`.
    norm: The normalization to apply. `None` for no normalization or `'ortho'`
      for orthonormal normalization.
    name: An optional name for the operation.

  Returns:
    A `[..., samples]` `float32`/`float64` `Tensor` containing the DCT of
    `input`.

  Raises:
    ValueError: If `type` is not `1`, `2`, `3` or `4`, `axis` is
      not `-1`, `n` is not `None` or greater than 0,
      or `norm` is not `None` or `'ortho'`.
    ValueError: If `type` is `1` and `norm` is `ortho`.

  [dct]: https://en.wikipedia.org/wiki/Discrete_cosine_transform
  r   _dct_internal)inputtyper   r   r   namer   r   r   dct2   s   .r!   c              
      s  t |d gu t   t jd jd}t jd p%t d durB fdd} fdd	}tk|| t jd pPt d }	t	
|	 j}
|d
kr}tj  ddddf gdd}t	t|}|W  d   S |dkrdt	t	|t	|
 tj d |
  }t	tj d|	 gddd|	f | }|dkrdt	|
 }|td }tjt|dd|	d
 gg|d}||9 }|W  d   S |dkrC|dkrt	|
}|td }tjt|dd|	d
 gg|d} |9  n |
9  dt	t	|t	|
tj d |
  }t	tj|t	 | d|	 gddd|	f }|W  d   S |dkrvt dd|	 |dd}|dd
ddf }|dkrm|tdt	|
 9 }|W  d   S W d   dS 1 sw   Y  dS )a  Computes the 1D Discrete Cosine Transform (DCT) of `input`.

  This internal version of `dct` does not perform any validation and accepts a
  dynamic value for `n` in the form of a rank 0 tensor.

  Args:
    input: A `[..., samples]` `float32`/`float64` `Tensor` containing the
      signals to take the DCT of.
    type: The DCT type to perform. Must be 1, 2, 3 or 4.
    n: The length of the transform. If length is less than sequence length,
      only the first n elements of the sequence are considered for the DCT.
      If n is greater than the sequence length, zeros are padded and then
      the DCT is computed as usual. Can be an int or rank 0 tensor.
    axis: For future expansion. The axis to compute the DCT along. Must be `-1`.
    norm: The normalization to apply. `None` for no normalization or `'ortho'`
      for orthonormal normalization.
    name: An optional name for the operation.

  Returns:
    A `[..., samples]` `float32`/`float64` `Tensor` containing the DCT of
    `input`.
  r!   g        dtyper   Nc                      s    ddf S )N.r   r   r   )r   r   r   r   truncate_input   s   z%_dct_internal.<locals>.truncate_inputc                     sN   t  j} dd t| D } || d  d< tj|tjd}tj |dS )Nc                 S   s   g | ]}d d gqS )r   r   ).0_r   r   r   
<listcomp>   s    z4_dct_internal.<locals>.pad_input.<locals>.<listcomp>r   r"   )Zpaddings)	lenr   range_opsconvert_to_tensor_dtypesZint32
_array_opspad)Zrankpaddingr   r   Zseq_lenr   r   	pad_input   s
   
z _dct_internal.<locals>.pad_inputr   .r   )r   r   g       @g      ?)Z
fft_lengthr   )Zconstant_valuesr   r   )r   r   r   r   )r*   Z
name_scoper+   r#   r   Zdimension_valuer   r-   r   	_math_opscastconcatrealr   Zrfftexpcomplexr)   _mathpiZrsqrtsqrtr.   Zexpand_dimsZirfftr   )r   r   r   r   r   r    zeror$   r1   Zaxis_dimZaxis_dim_floatZ
dct1_inputZdct1scaleZdct2Zn1Zn2weightsZdct3Zdct4r   r0   r   r   d   s   
 


5




L
L$r   zsignal.idctzspectral.idctc                 C   s6   t | |||| ddddd| }t| |||||dS )a  Computes the 1D [Inverse Discrete Cosine Transform (DCT)][idct] of `input`.

  Currently Types I, II, III, IV are supported. Type III is the inverse of
  Type II, and vice versa.

  Note that you must re-normalize by 1/(2n) to obtain an inverse if `norm` is
  not `'ortho'`. That is:
  `signal == idct(dct(signal)) * 0.5 / signal.shape[-1]`.
  When `norm='ortho'`, we have:
  `signal == idct(dct(signal, norm='ortho'), norm='ortho')`.

  @compatibility(scipy)
  Equivalent to [scipy.fftpack.idct]
   (https://docs.scipy.org/doc/scipy-1.4.0/reference/generated/scipy.fftpack.idct.html)
   for Type-I, Type-II, Type-III and Type-IV DCT.
  @end_compatibility

  Args:
    input: A `[..., samples]` `float32`/`float64` `Tensor` containing the
      signals to take the DCT of.
    type: The IDCT type to perform. Must be 1, 2, 3 or 4.
    n: For future expansion. The length of the transform. Must be `None`.
    axis: For future expansion. The axis to compute the DCT along. Must be `-1`.
    norm: The normalization to apply. `None` for no normalization or `'ortho'`
      for orthonormal normalization.
    name: An optional name for the operation.

  Returns:
    A `[..., samples]` `float32`/`float64` `Tensor` containing the IDCT of
    `input`.

  Raises:
    ValueError: If `type` is not `1`, `2` or `3`, `n` is not `None, `axis` is
      not `-1`, or `norm` is not `None` or `'ortho'`.

  [idct]:
  https://en.wikipedia.org/wiki/Discrete_cosine_transform#Inverse_transforms
  r   r   r   r   r   )r   r   r   r   r    r   )r   r   r   r   r   r    Zinverse_typer   r   r   idct   s
   )r?   )r   Nr   NN)__doc__mathr9   Ztensorflow.python.frameworkr   r,   r   r*   r   r   Ztensorflow.python.opsr   r-   r   r3   Ztensorflow.python.ops.signalr   Ztensorflow.python.utilr	   Z tensorflow.python.util.tf_exportr
   r   Zadd_dispatch_supportr!   r   r?   r   r   r   r   <module>   s&   
0p