File: tdct2.c

package info (click to toggle)
codec2 0.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 113,072 kB
  • sloc: ansic: 412,877; python: 4,004; sh: 1,540; objc: 817; asm: 683; makefile: 588
file content (176 lines) | stat: -rw-r--r-- 4,372 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*---------------------------------------------------------------------------*\

  FILE........: tdct2.c
  AUTHOR......: Phil Ayres
  DATE CREATED: July 2017

 * Unit test for DCT & DCT2 functions
 * 
\*---------------------------------------------------------------------------*/

/*
  Copyright David Rowe 2017

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.

 */


#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>

#include "t_helpers.h"
#include "dct2.h"

int main()
{
    int i;
    int j;

    test("dct");

    int N = 4;

    codec2_dct_cfg dct_cfg = dct_config(N);

    float y[] = {1, 2, 3, 4};
    float expect_dct[] = {20, -6.30864406, 0, -0.44834153};
    float res_dct[N];

    dct(dct_cfg, N, y, res_dct);


    for (i = 0; i < N; i++) {
        //printf("Result = %f : %f\n", res_dct[i].real, res_dct[i].imag);
        //printf("Expected = %f\n", expect_dct[i]);
        if (abs(expect_dct[i] - res_dct[i]) > 0.00001)
            test_failed_f(expect_dct[i], res_dct[i]);
    }

    dct_cfg_free(dct_cfg);

    test("idct");

    codec2_dct_cfg idct_cfg = idct_config(N);

    float *expect_idct = y;
    float res_idct[N];

    idct(idct_cfg, N, res_dct, res_idct);


    for (i = 0; i < N; i++) {
        //printf("Result = %f : %f\n", res_dct[i].real, res_dct[i].imag);
        //printf("Expected = %f\n", expect_dct[i]);
        if (abs(expect_idct[i] - res_idct[i]) > 0.00001)
            test_failed_f(expect_idct[i], res_idct[i]);
    }


    dct_cfg_free(idct_cfg);

    test("dct2");

    int M = 3;

    codec2_dct_cfg dct_cfg_n = dct_config(N);
    codec2_dct_cfg dct_cfg_m = dct_config(M);


    float expect_dct2[3][4] = {
        { 180, -26.76530997, 8.48528137, 1.90215201},
        { 3.46410162, -9.60123774, -7.34846923, -3.97696289},
        { -66, 5.5432772, 4.24264069, 2.29610059}
    };

    float y2[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {3, 1, 2, 3}
    };


    float res_dct2[M][N];
    dct2(dct_cfg_m, dct_cfg_n, M, N, y2, res_dct2);

    /*
        printf("Result\n");
        for (i = 0; i < M; i++) {
            for (j = 0; j < N; j++) {
                printf("%f ", res_dct2[i][j]);
            
                //if (abs(expect_dct2[i][j] - res_dct2[i][j]) > 0.00001)
                  //  test_failed_f(expect_dct2[i][j], res_dct2[i][j]);
            }
            printf("\n");
        }
        printf("Expected\n");
        for (i = 0; i < M; i++) {
            for (j = 0; j < N; j++) {
                printf("%f ", expect_dct2[i][j]);
            
                //if (abs(expect_dct2[i][j] - res_dct2[i][j]) > 0.00001)
                  //  test_failed_f(expect_dct2[i][j], res_dct2[i][j]);
            }
            printf("\n");
        }
     */
    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {

            if (abs(expect_dct2[i][j] - res_dct2[i][j]) > 0.00001)
                test_failed_f(expect_dct2[i][j], res_dct2[i][j]);
        }

    }



    dct_cfg_free(dct_cfg_m);
    dct_cfg_free(dct_cfg_n);
    test("idct2");

    codec2_dct_cfg idct_cfg_n = idct_config(N);
    codec2_dct_cfg idct_cfg_m = idct_config(M);


    float expect_idct2[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {3, 1, 2, 3}
    };


    float res_idct2[M][N];
    idct2(idct_cfg_m, idct_cfg_n, M, N, res_dct2, res_idct2);

    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {
            //printf("Result = %f \n", res_idct2[i][j]);
            //printf("Expected = %f\n", expect_idct2[i][j]);
            if (abs(expect_idct2[i][j] - res_idct2[i][j]) > 0.00001)
                test_failed_f(expect_idct2[i][j], res_idct2[i][j]);
        }
    }

    dct_cfg_free(idct_cfg_m);
    dct_cfg_free(idct_cfg_n);


    printf("OK!\n");
}