Skip to content

bounds.realtransforms

This module implements discrete transforms for real signals:

The implementations relies on the FFT under-the-hood, with memory-saving tricks (borrowed from cupy).

The table belows lists all functions implemented in the module. In addition to these, wrappers of the form FUNCTYPE = partial(FUNC, type=TYPE) are defined. For example:

dct1 = partial(dct, type=1)
idct1 = partial(idct, type=1)
dctn1 = partial(dctn, type=1)

FUNCTION DESCRIPTION
dct

One-dimensional Discrete Cosine Transform (DCT)

dst

One-dimensional Discrete Sine Transform (DST)

idct

One-dimensional Inverse Discrete Cosine Transform (DCT)

idst

One-dimensional Inverse Discrete Sine Transform (DST)

dctn

N-dimensional Discrete Cosine Transform (IDCT)

dstn

N-dimensional Discrete Sine Transform (IDST)

idctn

N-dimensional Inverse Discrete Cosine Transform (IDCT)

idstn

N-dimensional Inverse Discrete Sine Transform (IDST)

```

dct

dct(x, dim=-1, norm='backward', type=2)

Return the Discrete Cosine Transform

Type IV not implemented

PARAMETER DESCRIPTION
x

The input tensor

TYPE: tensor

dim

Dimensions over which the DCT is computed. Default is the last one.

TYPE: int DEFAULT: -1

norm

Normalization mode. Default is "backward".

TYPE: (backward, ortho, forward) DEFAULT: "backward"

type

Type of the DCT. Default type is 2.

TYPE: int DEFAULT: 2

RETURNS DESCRIPTION
y

The transformed tensor.

TYPE: tensor

Source code in bounds/realtransforms.py
def dct(
    x: Tensor,
    dim: int = -1,
    norm: str = 'backward',
    type: int = 2,
) -> Tensor:
    """Return the Discrete Cosine Transform

    !!! warning "Type IV not implemented"

    Parameters
    ----------
    x : tensor
        The input tensor
    dim : int
        Dimensions over which the DCT is computed.
        Default is the last one.
    norm : {"backward", "ortho", "forward"}
        Normalization mode. Default is "backward".
    type: {1, 2, 3, 4}
        Type of the DCT. Default type is 2.

    Returns
    -------
    y : tensor
        The transformed tensor.

    """
    if dim is None:
        dim = -1
    if type in _IMPLEMENTED_TYPES:
        return DCTN.apply(x, type, dim, norm)
    else:
        raise ValueError('DCT only implemented for types I-IV')

idct

idct(x, dim=-1, norm='backward', type=2)

Return the Inverse Discrete Cosine Transform

Warning

Type IV not implemented

PARAMETER DESCRIPTION
x

The input tensor

TYPE: tensor

dim

Dimensions over which the DCT is computed. Default is the last one.

TYPE: int DEFAULT: -1

norm

Normalization mode. Default is "backward".

TYPE: (backward, ortho, forward) DEFAULT: "backward"

type

Type of the DCT. Default type is 2.

TYPE: int DEFAULT: 2

RETURNS DESCRIPTION
y

The transformed tensor.

TYPE: tensor

Source code in bounds/realtransforms.py
def idct(
    x: Tensor,
    dim: int = -1,
    norm: str = 'backward',
    type: int = 2,
) -> Tensor:
    """Return the Inverse Discrete Cosine Transform

    !!! warning
        Type IV not implemented

    Parameters
    ----------
    x : tensor
        The input tensor
    dim : int
        Dimensions over which the DCT is computed.
        Default is the last one.
    norm : {"backward", "ortho", "forward"}
        Normalization mode. Default is "backward".
    type: {1, 2, 3, 4}
        Type of the DCT. Default type is 2.

    Returns
    -------
    y : tensor
        The transformed tensor.

    """
    if dim is None:
        dim = -1
    norm = flipnorm[norm or "backward"]
    type = fliptype[type]
    return dct(x, dim, norm, type)

dst

dst(x, dim=-1, norm='backward', type=2)

Return the Discrete Sine Transform

Type IV not implemented

Warning

dst(..., norm="ortho") yields a different result than scipy and cupy for types 2 and 3. This is because their DST is not properly orthogonalized. Use norm="ortho_scipy" to get results matching their implementation.

PARAMETER DESCRIPTION
x

The input tensor

TYPE: tensor

dim

Dimensions over which the DCT is computed. Default is the last one.

TYPE: int DEFAULT: -1

norm

Normalization mode. Default is "backward".

TYPE: (backward, ortho, forward, ortho_scipy) DEFAULT: "backward"

type

Type of the DCT. Default type is 2.

TYPE: int DEFAULT: 2

RETURNS DESCRIPTION
y

The transformed tensor.

TYPE: tensor

Source code in bounds/realtransforms.py
def dst(
    x: Tensor,
    dim: int = -1,
    norm: str = 'backward',
    type: int = 2,
) -> Tensor:
    """Return the Discrete Sine Transform

    !!! warning "Type IV not implemented"

    !!! warning
        `dst(..., norm="ortho")` yields a different result than `scipy`
        and `cupy` for types 2 and 3. This is because their DST is not
        properly orthogonalized. Use `norm="ortho_scipy"` to get results
        matching their implementation.

    Parameters
    ----------
    x : tensor
        The input tensor
    dim : int
        Dimensions over which the DCT is computed.
        Default is the last one.
    norm : {"backward", "ortho", "forward", "ortho_scipy"}
        Normalization mode. Default is "backward".
    type: {1, 2, 3, 4}
        Type of the DCT. Default type is 2.

    Returns
    -------
    y : tensor
        The transformed tensor.

    """
    if dim is None:
        dim = -1
    if type in _IMPLEMENTED_TYPES:
        return DSTN.apply(x, type, dim, norm)
    else:
        raise ValueError('DST only implemented for types I-IV')

idst

idst(x, dim=-1, norm='backward', type=2)

Return the Inverse Discrete Sine Transform

Type IV not implemented

Warning

idst(..., norm="ortho") yields a different result than scipy and cupy for types 2 and 3. This is because their DST is not properly orthogonalized. Use norm="ortho_scipy" to get results matching their implementation.

PARAMETER DESCRIPTION
x

The input tensor

TYPE: tensor

dim

Dimensions over which the DCT is computed. Default is the last one.

TYPE: int DEFAULT: -1

norm

Normalization mode. Default is "backward".

TYPE: (backward, ortho, forward, ortho_scipy) DEFAULT: "backward"

type

Type of the DCT. Default type is 2.

TYPE: int DEFAULT: 2

RETURNS DESCRIPTION
y

The transformed tensor.

TYPE: tensor

Source code in bounds/realtransforms.py
def idst(
    x: Tensor,
    dim: int = -1,
    norm: str = 'backward',
    type: int = 2,
) -> Tensor:
    """Return the Inverse Discrete Sine Transform

    !!! warning "Type IV not implemented"

    !!! warning
        `idst(..., norm="ortho")` yields a different result than `scipy`
        and `cupy` for types 2 and 3. This is because their DST is not
        properly orthogonalized. Use `norm="ortho_scipy"` to get results
        matching their implementation.

    Parameters
    ----------
    x : tensor
        The input tensor
    dim : int
        Dimensions over which the DCT is computed.
        Default is the last one.
    norm : {"backward", "ortho", "forward", "ortho_scipy"}
        Normalization mode. Default is "backward".
    type: {1, 2, 3, 4}
        Type of the DCT. Default type is 2.

    Returns
    -------
    y : tensor
        The transformed tensor.

    """
    if dim is None:
        dim = -1
    norm = flipnorm[norm or "backward"]
    type = fliptype[type]
    return dst(x, dim, norm, type)

dctn

dctn(x, dim=None, norm='backward', type=2)

Return multidimensional Discrete Cosine Transform along the specified axes.

Type IV not implemented

PARAMETER DESCRIPTION
x

The input tensor

TYPE: tensor

dim

Dimensions over which the DCT is computed. If not given, all dimensions are used.

TYPE: [sequence of] int DEFAULT: None

norm

Normalization mode. Default is "backward".

TYPE: (backward, ortho, forward) DEFAULT: "backward"

type

Type of the DCT. Default type is 2.

TYPE: int DEFAULT: 2

RETURNS DESCRIPTION
y

The transformed tensor.

TYPE: tensor

Source code in bounds/realtransforms.py
def dctn(
    x: Tensor,
    dim: Optional[int] = None,
    norm: str = 'backward',
    type: int = 2,
) -> Tensor:
    """Return multidimensional Discrete Cosine Transform
    along the specified axes.

    !!! warning "Type IV not implemented"

    Parameters
    ----------
    x : tensor
        The input tensor
    dim : [sequence of] int
        Dimensions over which the DCT is computed.
        If not given, all dimensions are used.
    norm : {"backward", "ortho", "forward"}
        Normalization mode. Default is "backward".
    type: {1, 2, 3, 4}
        Type of the DCT. Default type is 2.

    Returns
    -------
    y : tensor
        The transformed tensor.

    """
    if dim is None:
        dim = list(range(x.ndim))
    if type in _IMPLEMENTED_TYPES:
        return DCTN.apply(x, type, dim, norm)
    else:
        raise ValueError('DCT only implemented for types I-IV')

idctn

idctn(x, dim=None, norm='backward', type=2)

Return multidimensional Inverse Discrete Cosine Transform along the specified axes.

Type IV not implemented

PARAMETER DESCRIPTION
x

The input tensor

TYPE: tensor

dim

Dimensions over which the DCT is computed. If not given, all dimensions are used.

TYPE: [sequence of] int DEFAULT: None

norm

Normalization mode. Default is "backward".

TYPE: (backward, ortho, forward) DEFAULT: "backward"

type

Type of the DCT. Default type is 2.

TYPE: int DEFAULT: 2

RETURNS DESCRIPTION
y

The transformed tensor.

TYPE: tensor

Source code in bounds/realtransforms.py
def idctn(
    x: Tensor,
    dim: Optional[int] = None,
    norm: str = 'backward',
    type: int = 2,
) -> Tensor:
    """Return multidimensional Inverse Discrete Cosine Transform
    along the specified axes.

    !!! warning "Type IV not implemented"

    Parameters
    ----------
    x : tensor
        The input tensor
    dim : [sequence of] int
        Dimensions over which the DCT is computed.
        If not given, all dimensions are used.
    norm : {"backward", "ortho", "forward"}
        Normalization mode. Default is "backward".
    type: {1, 2, 3, 4}
        Type of the DCT. Default type is 2.

    Returns
    -------
    y : tensor
        The transformed tensor.

    """
    if dim is None:
        dim = list(range(x.ndim))
    norm = flipnorm[norm or "backward"]
    type = fliptype[type]
    return dctn(x, dim, norm, type)

dstn

dstn(x, dim=None, norm='backward', type=2)

Return multidimensional Discrete Sine Transform along the specified axes.

Type IV not implemented

Warning

dstn(..., norm="ortho") yields a different result than scipy and cupy for types 2 and 3. This is because their DST is not properly orthogonalized. Use norm="ortho_scipy" to get results matching their implementation.

PARAMETER DESCRIPTION
x

The input tensor

TYPE: tensor

dim

Dimensions over which the DCT is computed. If not given, all dimensions are used.

TYPE: [sequence of] int DEFAULT: None

norm

Normalization mode. Default is "backward".

TYPE: (backward, ortho, forward, ortho_scipy) DEFAULT: "backward"

type

Type of the DCT. Default type is 2.

TYPE: int DEFAULT: 2

RETURNS DESCRIPTION
y

The transformed tensor.

TYPE: tensor

Source code in bounds/realtransforms.py
def dstn(
    x: Tensor,
    dim: Optional[int] = None,
    norm: str = 'backward',
    type: int = 2,
) -> Tensor:
    """Return multidimensional Discrete Sine Transform
    along the specified axes.

    !!! warning "Type IV not implemented"

    !!! warning
        `dstn(..., norm="ortho")` yields a different result than `scipy`
        and `cupy` for types 2 and 3. This is because their DST is not
        properly orthogonalized. Use `norm="ortho_scipy"` to get results
        matching their implementation.

    Parameters
    ----------
    x : tensor
        The input tensor
    dim : [sequence of] int
        Dimensions over which the DCT is computed.
        If not given, all dimensions are used.
    norm : {"backward", "ortho", "forward", "ortho_scipy"}
        Normalization mode. Default is "backward".
    type: {1, 2, 3, 4}
        Type of the DCT. Default type is 2.

    Returns
    -------
    y : tensor
        The transformed tensor.

    """
    if dim is None:
        dim = list(range(x.ndim))
    if type in _IMPLEMENTED_TYPES:
        return DSTN.apply(x, type, dim, norm)
    else:
        raise ValueError('DST only implemented for types I-IV')

idstn

idstn(x, dim=None, norm='backward', type=2)

Return multidimensional Inverse Discrete Sine Transform along the specified axes.

Type IV not implemented

Warning

idstn(..., norm="ortho") yields a different result than scipy and cupy for types 2 and 3. This is because their DST is not properly orthogonalized. Use norm="ortho_scipy" to get results matching their implementation.

PARAMETER DESCRIPTION
x

The input tensor

TYPE: tensor

dim

Dimensions over which the DCT is computed. If not given, all dimensions are used.

TYPE: [sequence of] int DEFAULT: None

norm

Normalization mode. Default is "backward".

TYPE: "backward", "ortho", "forward", "ortho_scipy DEFAULT: "backward"

type

Type of the DCT. Default type is 2.

TYPE: int DEFAULT: 2

RETURNS DESCRIPTION
y

The transformed tensor.

TYPE: tensor

Source code in bounds/realtransforms.py
def idstn(
    x: Tensor,
    dim: Optional[int] = None,
    norm: str = 'backward',
    type: int = 2,
) -> Tensor:
    """Return multidimensional Inverse Discrete Sine Transform
    along the specified axes.

    !!! warning "Type IV not implemented"

    !!! warning
        `idstn(..., norm="ortho")` yields a different result than `scipy`
        and `cupy` for types 2 and 3. This is because their DST is not
        properly orthogonalized. Use `norm="ortho_scipy"` to get results
        matching their implementation.

    Parameters
    ----------
    x : tensor
        The input tensor
    dim : [sequence of] int
        Dimensions over which the DCT is computed.
        If not given, all dimensions are used.
    norm : {"backward", "ortho", "forward", "ortho_scipy}
        Normalization mode. Default is "backward".
    type: {1, 2, 3, 4}
        Type of the DCT. Default type is 2.

    Returns
    -------
    y : tensor
        The transformed tensor.

    """
    if dim is None:
        dim = list(range(x.ndim))
    norm = flipnorm[norm or "backward"]
    type = fliptype[type]
    return dstn(x, dim, norm, type)