Skip to content

Cosine Transforms

psambit9791 edited this page Dec 18, 2023 · 7 revisions

Forward Cosine Transform

The DiscreteCosine and FastCosine classes decomposes a finite sequence of data points in terms of a sum of cosine functions of different frequencies. This is different from the Fourier transform which decomposes the input signal into both sine and cosine functions.

This library implements 4 of the 8 types of transforms - Type I - IV. Refer to this link for more details about each type - and their utlities.

The examples provided here use this signal:

$\sin(40\pi t) + \frac{1}{2}\sin(400\pi t)$

signal

CODE

STANDARD NORMALIZATION

If using DCT, we can use all types 1 to 4, while performing the transformation:

DiscreteCosine dct = new DiscreteCosine(signal); //signal is a double[]

// Type 1 
dct.transform(1);
// OR
dct.transform();
double[] outputType1 = dct.getOutput()

// Type 2 
dct.transform(2);
double[] outputType2 = dct.getOutput()

// Type 3
dct.transform(3);
double[] outputType3 = dct.getOutput()

// Type 4
dct.transform(4);
double[] outputType4 = dct.getOutput()

Fast Cosine Transform is a more efficient implementation but only exists for Type 1 Transform. This acts as a wrapper on top of Apache Math 3's implementation of FastCosineTransformer

FastCosine f1 = new FastCosine(signal); //signal is a double[]
f1.transform();
double[] out = f1.getOutput();

dft

ORTHOGONAL NORMALIZATION

If using DCT, we can use all types 1 to 4, while performing the transformation:

DiscreteCosine dct = new DiscreteCosine(signal, DiscreteCosine.Normalization.ORTHOGONAL); //signal is a double[]
dct.transform(1); // Type can be 1, 2, 3 or 4
double[] outputType1 = dct.getOutput()

Fast Cosine Transform is a more efficient implementation but only exists for Type 1 Transform. This acts as a wrapper on top of Apache Math 3's implementation of FastCosineTransformer

FastCosine f1 = new FastCosine(signal, FastCosine.Normalization.ORTHOGONAL); //signal is a double[]
f1.transform();
double[] out = f1.getOutput();

dft


Inverse Cosine Transform

Inverse Cosine Transforms are used to recover the original signal from a Cosine Transformed signal. Similar to Cosine Transform, JDSP provides InverseDiscreteCosine and InverseFastCosine; to be used with DiscreteCosine and FastCosine respectively.

If the normalisation is set to ORTHOGONAL, the output of the the inverse transform is in the same scale as the original signal. If not, the scale may change.

The examples provided here use this as the original signal:

$\sin(40\pi t) + \frac{1}{2}\sin(400\pi t)$

Example Usage

For this example, we will try and perform the forward and inverse transform to see how it operates and compare the outputs visually.

CODE

STANDARD NORMALIZATION

DiscreteCosine dct = new DiscreteCosine(original_signal); //original_signal is a double[]
dct.transform(1); // Type can be 1, 2, 3 or 4
double[] outputType1 = dct.getOutput()

InverseDiscreteCosine idct = new InverseDiscreteCosine(this.signal1);
idct.transform(1); // Must be the same as the type used for DCT
double[] recovered_signal = idct.getOutput();

If using InverseFastCosine, we can only use the Type 1 transform.

FastCosine f1 = new FastCosine(original_signal); //original_signal is a double[]
f1.transform();
double[] out = f1.getOutput();

InverseFastCosine if1 = new InverseFastCosine(out);
if1.transform();
double[] reconstructed = if1.getOutput();

dft

ORTHOGONAL NORMALIZATION

DiscreteCosine dct = new DiscreteCosine(original_signal, DiscreteCosine.Normalization.ORTHOGONAL); //original_signal is a double[]
dct.transform(1); // Type can be 1, 2, 3 or 4
double[] outputType1 = dct.getOutput()

InverseDiscreteCosine idct = new InverseDiscreteCosine(outputType1, InverseDiscreteCosine.Normalization.ORTHOGONAL);
idct.transform(1); // Must be the same as the type used for DCT
double[] recovered_signal = idct.getOutput();

If using InverseFastCosine, we can only use the Type 1 transform.

FastCosine f1 = new FastCosine(original_signal, FastCosine.Normalization.ORTHOGONAL); //original_signal is a double[]
f1.transform();
double[] out = f1.getOutput();

InverseFastCosine if1 = new InverseFastCosine(out, InverseFastCosine.Normalization.ORTHOGONAL);
if1.transform();
double[] reconstructed = if1.getOutput();

dft

Clone this wiki locally