Using Discrete Cosine Transform (DCT) Functions

Introduction

The vDSP API provides the vDSP_DCT_CreateSetup and vDSP_DCT_Execute functions. They implement DCT capabilities using the following model:

For an example of DCT functions in use, see vDSP Examples.

Note the integration with the DFT family of functions – single-precision DCT setups have the same type as a DFT setup, and can be shared with DFT setups; and the DestroySetup function from the DFT family is used to destroy DCT setups.

Creating and Using DCT Setups

A DCT setup contains all the information needed to perform a single-precision DCT operation, except the pointers to input and output data vectors. It may include information about memory allocations made by previously created setups.

This is a preparation step to be done when a program is starting, or is starting some new phase (e.g., when a communication channel is opened). It should never be done during real-time processing. The setup routine is slow and is called only once to prepare data that can be used many times.

DCT setups can be shared with other DCT setups (as well as single-precision DFT setups). This occurs when you pass a previously created setup in the Previous parameter of a CreateSetup call that creates another setup. When setups are shared, vDSP may be able to share memory amongst them, leading to better efficiency. Sharing any setup of a group of setups that share data will result in a new setup sharing data with all of the group.

Creating a setup

  1. Use the vDSP_DCT_CreateSetup function.

  2. Pass in any existing setup that you want the new setup to share with, or pass NULL to create a new setup independent of any previous setups.

  3. Specify the length of the transform, i.e. the number of elements to process. Only certain lengths are implemented: they are given by

    f * 2n, where f is 1, 3, 5, or 15 and n >= 4

  4. Specify the type of the transform. At present, the supported DCT types are II and III (which are mutual inverses, up to scaling) and IV (which is its own inverse). These are specified with symbol names vDSP_DCT_II, vDSP_DCT_III, and vDSP_DCT_IV.

Executing a DCT setup

Once you have created all the setups needed for a particular phase of your program, you can execute them in any desired order.

A DCT setup does its work when it is passed into the vDSP_DCT_Execute function. It fully specifies the desired transform, leaving only the data pointers to be provided to vDSP_DCT_Execute. The setup can be used repeatedly as many times as desired, changing the data pointers each time.

When vDSP_DCT_Execute is called with a setup returned from vDSP_DCT_CreateSetup, it performs one of the following calculations:

If Type is vDSP_DCT_II:
  For 0 <= k < N,
    Or[k] = sum(Ir[j] * cos(k * (j+1/2) * pi / N, 0 <= j < N)
 
If Type is vDSP_DCT_III:
  For 0 <= k < N,
    Or[k] = Ir[0]/2 + sum(Ir[j] * cos((k+1/2) * j * pi / N), 1 <= j < N)
 
If Type is vDSP_DCT_IV:
  For 0 <= k < N,
    Or[k] = sum(Ir[j] * cos((k+1/2) * (j+1/2) * pi / N, 0 <= j < N)
 

Where

  • N is the length given in the setup

  • h is the array of real numbers passed to vDSP_DCT_Execute in Input

  • H is the array of real numbers stored by vDSP_DCT_Execute in the array passed to it in Output.

Destroying a DCT Setup

When a DCT setup is no longer needed, it should be destroyed with the vDSP_DFT_DestroySetup function. This allows vDSP to release memory associated with the setup (and not needed by other setups or for other purposes).