Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

fft.dct

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.

How it works

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.

Syntax

scipy.fft.dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, orthogonalize=None)

Parameters

NameTypeDescription
xarray_likeInput array.
type{1, 2, 3, 4}, optionalDCT variant; one of 1, 2, 3, or 4. Defaults to 2.
nint, optionalTransform length. Input is truncated or zero-padded to this length; defaults to the selected axis length.
axisint, optionalAxis along which to compute the transform; defaults to the last axis.
norm{"backward", "ortho", "forward"}, optionalNormalization mode; accepts "backward", "ortho", or "forward".
overwrite_xbool, optionalWhether the input contents may be overwritten.
workersint, optionalMaximum number of workers for parallel computation.
orthogonalizebool, optionalWhether to orthogonalize the transform when supported.

Returns

ndarray of real: the transformed input array.

Key points

  • The function converts an input sequence into discrete cosine-transform coefficients.
  • Four transform variants are available through the type argument.
  • Without an explicit length, the transform uses the size of the chosen input axis.
  • The axis argument determines the dimension processed for array inputs.
  • Scaling is selected with the norm argument.
  • The workers argument limits parallel resources used by the computation.
  • The result is the transformed input represented as a real-valued NumPy array.

Common mistakes

  • Requesting a shorter transform length discards input values beyond that length.
  • Requesting a longer transform length extends the input with zeros before transforming.
  • Omitting type selects the type-II transform, not type-I, III, or IV.
  • For multidimensional arrays, omitting axis transforms each slice along the final dimension.

Example: Compute a type-II DCT

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.

When to use it

Cosine-basis analysis

Convert spatial or sampled-domain data into cosine coefficients for compact representation or analysis.

Axis-specific array transforms

Apply a DCT to rows, columns, or another dimension of multidimensional numerical data.

Parallel numerical processing

Run larger transforms with bounded parallel resources.

Interview questions

QWhat does 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.

QHow do 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.

QWhich arguments control multidimensional operation and resource usage?

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.idct
  • scipy.fft.dst
  • scipy.fft.fft
  • scipy.fft.rfft

Source: scipy.fft.dct. Checked 2026-10-15.