B
    )`#                 @   s   d Z ddlmZ ddlmZ ddlm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 ZedddgdejdddZedddgdejdddZdS )zDiscrete Cosine Transform ops.    )absolute_import)division)print_functionN)dtypes)ops)tensor_shape)	array_ops)math_ops)fft_ops)dispatch)	tf_exportc             C   s   |dkrt d| |dk	r,|dk r,td|dkr<td|dkrx|dkrTtd	| jd dk	rx| jd d
k rxtd|dkrt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   S/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow/python/ops/signal/dct_ops.py_validate_dct_arguments    s    r   z
signal.dctzspectral.dct)Zv1r   r   c          
   C   s  t | |||| t|d| g t| } tjd| jd}t| jd pZt| d }|dk	r||kr~| dd|f } nNt	| j}dd	 t
|D }	|| |	|d
  d
< tj|	tjd}	tj| |	d} t| jd pt| d }
t|
| j}|d
kr6tj| | ddddf gdd}tt|}|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 }|S |dkr|dkrBt|}|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 }|S |dkrt| dd|
 |dd}|dd
ddf }|dkr|tdt| 9 }|S W dQ R X d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
  dctg        )dtyper   N.r   c             S   s   g | ]}d d gqS )r   r   ).0_r   r   r   
<listcomp>p   s    zdct.<locals>.<listcomp>r   )Zpaddings)r   r   g       @g      ?)Z
fft_lengthr   )Zconstant_valuesr   r   )typer   r   r   )r   _opsZ
name_scopeZconvert_to_tensorr   r   Zdimension_valuer   
_array_opslenrange_dtypesint32pad	_math_opscastconcatrealr
   Zrfftexpcomplex_mathpiZrsqrtsqrtZexpand_dimsZirfftr   )inputr"   r   r   r   nameZzeroZseq_lenZrankpaddingZaxis_dimZaxis_dim_floatZ
dct1_inputZdct1ZscaleZdct2Zn1Zn2weightsZdct3Zdct4r   r   r   r   5   s|    .


 
"






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   r4   )r   r   )r3   r"   r   r   r   r4   Zinverse_typer   r   r   idct   s    )r7   )r   Nr   NN)r   Nr   NN)__doc__
__future__r   r   r   mathr0   Ztensorflow.python.frameworkr   r'   r   r#   r   Ztensorflow.python.opsr   r$   r	   r*   Ztensorflow.python.ops.signalr
   Ztensorflow.python.utilr   Z tensorflow.python.util.tf_exportr   r   Zadd_dispatch_supportr   r7   r   r   r   r   <module>   s(    