[SciPy-dev] DCT naming conventions ?

David Cournapeau cournape@gmail....
Mon Jan 19 02:39:25 CST 2009

On Mon, Jan 19, 2009 at 4:46 AM, Tom Grydeland <tom.grydeland@gmail.com> wrote:
> On Sun, Jan 18, 2009 at 6:04 PM, william ratcliff
> <william.ratcliff@gmail.com> wrote:
>> I like your arguments idea,
>> dct(...,type=number)
>> idct(...,type=number).
> +1
>> maybe the type could default to 2 for dct and idct (with the implementation
>> of idct for type 2 being dct of type3).  This would make life easier for
>> users.
> +1
> Also, this naming makes it possible to give the 2D and
> higher-dimensionality DCTs names analogously to the FFT functions.

Ok, I implemented the above scheme for DCT I/II/III, the docstring is:

def dct(x, type=2, n=None, axis=-1, norm=None):
    Return the Discrete Cosine Transform of arbitrary type sequence x.

    x : array-like
        input array.
    type : {1, 2, 3}
        type of the DCT (see Notes).
    n : int, optional
        Length of the transform.
    axis : int, optional
        axis over which to compute the transform.
    norm : {None, 'ortho'}
        normalization mode (see Notes).

    y : real ndarray

    For a single dimension array x, dct(x, norm='ortho') is equal to matlab

    There are theoretically 8 types of the DCT, only the first 3 types are
    implemented in scipy. 'The' DCT generally refers to DCT type 2, and 'the'
    Inverse DCT generally refers to DCT type 3.

    type I
    There are several definitions of the DCT-I; we use the following (for

    for 0 <= k < N,

        y[k] = x[0] + (-1)**k x[N-1] + 2 * sum x[n]*cos(pi*k*n/(N-1))

    type II
    There are several definitions of the DCT-II; we use the following (for

        y[k] = 2* sum x[n]*cos(pi*k*(2n+1)/(2*N)), 0 <= k < N.

    If norm='ortho', y[k] is multiplied by a scaling factor f:

        f = sqrt(1/(4*N)) if k = 0
        f = sqrt(1/(2*N)) otherwise

    Which makes the corresponding matrix of coefficients orthonormal (OO' = Id).

    type III
    There are several definitions, we use the following (norm=None):

        y[k] = x[0] + 2 * sum x[n]*cos(pi*(k+0.5)*n/N), 0 <= k < N.

    Or (norm='ortho'), for 0 <= k < N:

        y[k] = x[0] / sqrt(N) + sqrt(1/N) * sum x[n]*cos(pi*(k+0.5)*n/N)

    The (unnormalized) DCT-III is the inverse of the (unnormalized) DCT-II, up
    to a factor 2*N. The orthonormalized DCT-III is exactly the inverse of the
    orthonormalized DCT-II.



    'A Fast Cosine Transform in One and Two Dimensions', by J. Makhoul, in IEEE
    Transactions on acoustics, speech and signal processing.

More information about the Scipy-dev mailing list