Computes the 1D [Discrete Cosine Transform (DCT)][dct] of `input`. Currently only Types I, II and III 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
(input, type=2, n=None, axis=-1, norm=None, name=None)
| 50 | # TODO(rjryan): Implement `axis` parameter. |
| 51 | @tf_export("signal.dct", v1=["signal.dct", "spectral.dct"]) |
| 52 | def dct(input, type=2, n=None, axis=-1, norm=None, name=None): # pylint: disable=redefined-builtin |
| 53 | """Computes the 1D [Discrete Cosine Transform (DCT)][dct] of `input`. |
| 54 | |
| 55 | Currently only Types I, II and III are supported. |
| 56 | Type I is implemented using a length `2N` padded `tf.signal.rfft`. |
| 57 | Type II is implemented using a length `2N` padded `tf.signal.rfft`, as |
| 58 | described here: [Type 2 DCT using 2N FFT padded (Makhoul)](https://dsp.stackexchange.com/a/10606). |
| 59 | Type III is a fairly straightforward inverse of Type II |
| 60 | (i.e. using a length `2N` padded `tf.signal.irfft`). |
| 61 | |
| 62 | @compatibility(scipy) |
| 63 | Equivalent to [scipy.fftpack.dct](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.fftpack.dct.html) |
| 64 | for Type-I, Type-II and Type-III DCT. |
| 65 | @end_compatibility |
| 66 | |
| 67 | Args: |
| 68 | input: A `[..., samples]` `float32` `Tensor` containing the signals to |
| 69 | take the DCT of. |
| 70 | type: The DCT type to perform. Must be 1, 2 or 3. |
| 71 | n: The length of the transform. If length is less than sequence length, |
| 72 | only the first n elements of the sequence are considered for the DCT. |
| 73 | If n is greater than the sequence length, zeros are padded and then |
| 74 | the DCT is computed as usual. |
| 75 | axis: For future expansion. The axis to compute the DCT along. Must be `-1`. |
| 76 | norm: The normalization to apply. `None` for no normalization or `'ortho'` |
| 77 | for orthonormal normalization. |
| 78 | name: An optional name for the operation. |
| 79 | |
| 80 | Returns: |
| 81 | A `[..., samples]` `float32` `Tensor` containing the DCT of `input`. |
| 82 | |
| 83 | Raises: |
| 84 | ValueError: If `type` is not `1`, `2` or `3`, `axis` is |
| 85 | not `-1`, `n` is not `None` or greater than 0, |
| 86 | or `norm` is not `None` or `'ortho'`. |
| 87 | ValueError: If `type` is `1` and `norm` is `ortho`. |
| 88 | |
| 89 | [dct]: https://en.wikipedia.org/wiki/Discrete_cosine_transform |
| 90 | """ |
| 91 | _validate_dct_arguments(input, type, n, axis, norm) |
| 92 | with _ops.name_scope(name, "dct", [input]): |
| 93 | # We use the RFFT to compute the DCT and TensorFlow only supports float32 |
| 94 | # for FFTs at the moment. |
| 95 | input = _ops.convert_to_tensor(input, dtype=_dtypes.float32) |
| 96 | |
| 97 | seq_len = ( |
| 98 | tensor_shape.dimension_value(input.shape[-1]) or |
| 99 | _array_ops.shape(input)[-1]) |
| 100 | if n is not None: |
| 101 | if n <= seq_len: |
| 102 | input = input[..., 0:n] |
| 103 | else: |
| 104 | rank = len(input.shape) |
| 105 | padding = [[0, 0] for i in range(rank)] |
| 106 | padding[rank - 1][1] = n - seq_len |
| 107 | padding = _ops.convert_to_tensor(padding, dtype=_dtypes.int32) |
| 108 | input = _array_ops.pad(input, paddings=padding) |
| 109 |
no test coverage detected