dct#

ivy.dct(x, /, *, type=2, n=None, axis=-1, norm=None, out=None)[source]#

Compute the 1D Discrete Cosine Tranformation of a given signal.

Parameters:
  • x (Union[Array, NativeArray]) – The input signal.

  • type (Literal[1, 2, 3, 4]) – The type of the dct. Must be 1, 2, 3 or 4. (default: 2)

  • n (Optional[int]) – The lenght of the transform. If n is less than the input signal lenght, (default: None) then x is truncated, if n is larger then x is zero-padded.

  • axis (int) – The axis to compute the DCT along. (default: -1)

  • norm (Optional[Literal[‘ortho’]]) – The type of normalization to be applied. Must be either None or “ortho”. (default: None)

  • out (Optional[Union[Array, NativeArray]]) – optional output array, for writing the result to. (default: None)

Return type:

Union[Array, NativeArray]

Returns:

  • ret – Array containing the transformed input.

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([8, 16, 24, 32, 40, 48, 56, 64])
>>> ivy.dct(x, type=2, n=None, norm='ortho')
ivy.array([102., -51.5, 0., -5.39, 0., -1.61, 0., -0.406])
>>> x = ivy.array([[[8, 16, 24, 32], [40, 48, 56, 64]],
...                [[1,  2,  3,  4], [ 5,  6,  7,  8]]])
>>> ivy.dct(x, type=1, n=None, axis=0, norm=None)
ivy.array([[[ 9., 18., 27., 36.],
            [45., 54., 63., 72.]],
           [[ 7., 14., 21., 28.],
            [35., 42., 49., 56.]]])
>>> x = ivy.array([[ 8.1, 16.2, 24.3, 32.4],
...                [40.5, 48.6, 56.7, 64.8]])
>>> y = ivy.zeros((2, 4), dtype=ivy.float32)
>>> ivy.dct(x, type=1, n=None, norm=None, out=y)
>>> print(y)
ivy.array([[ 1.22e+02, -3.24e+01,  1.91e-06, -8.10e+00],
           [ 3.16e+02, -3.24e+01,  3.81e-06, -8.10e+00]])
>>> x = ivy.array([8., 16., 24., 32., 40., 48., 56., 64.])
>>> ivy.dct(x, type=4, n=None, norm=None, out=x)
>>> print(x)
ivy.array([ 279. , -280. ,  128. , -115. ,   83.7,  -79.5,   69.8,  -68.7])

With one ivy.Container input:

>>> x = ivy.Container(a=ivy.array([8, 16, 24, 32, 40, 48, 56, 64]),
...                   b=ivy.array([1,  2,  3,  4,  5,  6,  7,  8]))
>>> ivy.dct(x, type=3, n=None, norm='ortho')
{
    a: ivy.array([79.5, -70.4, 30., -23.6, 13.9, -10.1, 5.2, -1.95]),
    b: ivy.array([9.94, -8.8, 3.75, -2.95, 1.74, -1.26, 0.65, -0.244])
}

With multiple ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([8, 16, 24, 32, 40, 48, 56, 64]),
...                   b=ivy.array([1,  2,  3,  4,  5,  6,  7,  8]))
>>> container_n = ivy.Container(a=9, b=4)
>>> container_type = ivy.Container(a=2, b=1)
>>> container_norm = ivy.Container(a="ortho", b=None)
>>> ivy.dct(x, type=container_type, n=container_n, norm=container_norm)
{
    a: ivy.array([96., -28.2, -31.9, 22.9, -26., 19.8, -17., 10.9,
                -5.89]),
    b: ivy.array([15., -4., 0., -1.])
}
Array.dct(self, /, *, type=2, n=None, axis=-1, norm=None, out=None)#

ivy.Array instance method variant of ivy.dct. This method simply wraps the function, and so the docstring for ivy.dct also applies to this method with minimal changes.

Parameters:
  • self (Array) – The input signal.

  • type (Literal[1, 2, 3, 4]) – The type of the dct. Must be 1, 2, 3 or 4. (default: 2)

  • n (Optional[int]) – The lenght of the transform. If n is less than the input signal lenght, (default: None) then x is truncated, if n is larger than x is zero-padded.

  • norm (Optional[Literal[‘ortho’]]) – The type of normalization to be applied. Must be either None or “ortho”. (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type:

Array

Returns:

ret – Array containing the transformed input.

Examples

>>> x = ivy.array([8., 16., 24., 32., 40., 48., 56., 64.])
>>> x.dct(type=2, norm="ortho")
ivy.array([ 102.,  -51.5,   0.,  -5.39,   0.,  -1.61,   0., -0.406])
Container.dct(self, /, *, type=2, n=None, axis=-1, norm=None, out=None)#

ivy.Container instance method variant of ivy.dct. This method simply wraps the function, and so the docstring for ivy.dct also applies to this method with minimal changes.

Parameters:
  • self (Container) – Container with the input signals.

  • type (Literal[1, 2, 3, 4]) – The type of the dct. Must be 1, 2, 3 or 4. (default: 2)

  • n (Optional[int]) – The lenght of the transform. If n is less than the input signal lenght, (default: None) then x is truncated, if n is larger then x is zero-padded.

  • norm (Optional[Literal[‘ortho’]]) – The type of normalization to be applied. Must be either None or “ortho”. (default: None)

  • out (Optional[Container]) – optional output container, for writing the result to. (default: None)

Return type:

Container

Returns:

ret – The transformed input.

Examples

>>> x = ivy.Container(a=ivy.array([8, 16, 24, 32, 40, 48, 56, 64]),
...                   b=ivy.array([1,  2,  3,  4,  5,  6,  7,  8]))
>>> x.dct(type=2, norm='ortho')
{
    a: ivy.array([102., -51.5, 0., -5.39, 0., -1.61, 0.,
                -0.406]),
    b: ivy.array([12.7, -6.44, 0., -0.673, 0., -0.201, 0.,
                -0.0507])
}