Cosine-basis analysis
Convert spatial or sampled-domain data into cosine coefficients for compact representation or analysis.
SciPy · Function
Computes a discrete cosine transform of a sequence or array along a selected axis. It supports four DCT types, configurable length and normalization, and optional parallel execution.
dct transforms real-valued samples along a chosen array axis into cosine-basis coefficients. Select the DCT variant with type, control the transform length with n, choose scaling with norm, and optionally control memory use or parallelism with the remaining arguments.
scipy.fft.dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, orthogonalize=None)| Name | Type | Description |
|---|---|---|
x | array_like | Input array. |
type | {1, 2, 3, 4}, optional | DCT variant; one of 1, 2, 3, or 4. Defaults to 2. |
n | int, optional | Transform length. Input is truncated or zero-padded to this length; defaults to the selected axis length. |
axis | int, optional | Axis along which to compute the transform; defaults to the last axis. |
norm | {"backward", "ortho", "forward"}, optional | Normalization mode; accepts "backward", "ortho", or "forward". |
overwrite_x | bool, optional | Whether the input contents may be overwritten. |
workers | int, optional | Maximum number of workers for parallel computation. |
orthogonalize | bool, optional | Whether to orthogonalize the transform when supported. |
ndarray of real: the transformed input array.
type argument.axis argument determines the dimension processed for array inputs.norm argument.workers argument limits parallel resources used by the computation.type selects the type-II transform, not type-I, III, or IV.axis transforms each slice along the final dimension.import numpy as np
from scipy.fft import dct
x = np.array([1.0, 2.0, 3.0, 4.0])
y = dct(x, type=2, norm="ortho")
print(y)Computes an orthonormally scaled type-II DCT of a one-dimensional NumPy array.
Convert spatial or sampled-domain data into cosine coefficients for compact representation or analysis.
Apply a DCT to rows, columns, or another dimension of multidimensional numerical data.
Run larger transforms with bounded parallel resources.
scipy.fft.dct compute, and what are its main defaults?The function returns the discrete cosine transform of an arbitrary-type sequence. It operates along axis, uses type II by default, and returns a real-valued NumPy array.
Follow-up: How would you change the output length? Pass n; values are truncated when it is smaller than the selected axis and zero-padded when it is larger.
type and norm affect a DCT?type chooses one of DCT types 1 through 4. norm controls normalization and accepts "backward", "ortho", or "forward"; its default is None in the signature shown by the documentation.
Follow-up: Why might norm="ortho" be useful? It requests orthogonal scaling rather than the default normalization convention.
axis selects the dimension transformed and defaults to -1. workers can specify the maximum number of workers for parallel computation, while overwrite_x permits the input contents to be destroyed when set to true.
Follow-up: What should be checked before using overwrite_x=True? Ensure the original input is no longer needed, because the documentation permits its contents to be destroyed.
scipy.fft.idctscipy.fft.dstscipy.fft.fftscipy.fft.rfft