fftw_dct.c
3.77 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <fftw3.h>
#if DCT_TEST_PRECISION == 1
typedef float float_prec;
#define PF "%.7f"
#define FFTW_PLAN fftwf_plan
#define FFTW_MALLOC fftwf_malloc
#define FFTW_FREE fftwf_free
#define FFTW_PLAN_CREATE fftwf_plan_r2r_1d
#define FFTW_EXECUTE fftwf_execute
#define FFTW_DESTROY_PLAN fftwf_destroy_plan
#define FFTW_CLEANUP fftwf_cleanup
#elif DCT_TEST_PRECISION == 2
typedef double float_prec;
#define PF "%.18f"
#define FFTW_PLAN fftw_plan
#define FFTW_MALLOC fftw_malloc
#define FFTW_FREE fftw_free
#define FFTW_PLAN_CREATE fftw_plan_r2r_1d
#define FFTW_EXECUTE fftw_execute
#define FFTW_DESTROY_PLAN fftw_destroy_plan
#define FFTW_CLEANUP fftw_cleanup
#elif DCT_TEST_PRECISION == 3
typedef long double float_prec;
#define PF "%.18Lf"
#define FFTW_PLAN fftwl_plan
#define FFTW_MALLOC fftwl_malloc
#define FFTW_FREE fftwl_free
#define FFTW_PLAN_CREATE fftwl_plan_r2r_1d
#define FFTW_EXECUTE fftwl_execute
#define FFTW_DESTROY_PLAN fftwl_destroy_plan
#define FFTW_CLEANUP fftwl_cleanup
#else
#error DCT_TEST_PRECISION must be a number 1-3
#endif
enum type {
DCT_I = 1,
DCT_II = 2,
DCT_III = 3,
DCT_IV = 4,
DST_I = 5,
DST_II = 6,
DST_III = 7,
DST_IV = 8,
};
int gen(int type, int sz)
{
float_prec *a, *b;
FFTW_PLAN p;
int i, tp;
a = FFTW_MALLOC(sizeof(*a) * sz);
if (a == NULL) {
fprintf(stderr, "failure\n");
exit(EXIT_FAILURE);
}
b = FFTW_MALLOC(sizeof(*b) * sz);
if (b == NULL) {
fprintf(stderr, "failure\n");
exit(EXIT_FAILURE);
}
switch(type) {
case DCT_I:
tp = FFTW_REDFT00;
break;
case DCT_II:
tp = FFTW_REDFT10;
break;
case DCT_III:
tp = FFTW_REDFT01;
break;
case DCT_IV:
tp = FFTW_REDFT11;
break;
case DST_I:
tp = FFTW_RODFT00;
break;
case DST_II:
tp = FFTW_RODFT10;
break;
case DST_III:
tp = FFTW_RODFT01;
break;
case DST_IV:
tp = FFTW_RODFT11;
break;
default:
fprintf(stderr, "unknown type\n");
exit(EXIT_FAILURE);
}
switch(type) {
case DCT_I:
case DCT_II:
case DCT_III:
case DCT_IV:
for(i=0; i < sz; ++i) {
a[i] = i;
}
break;
case DST_I:
case DST_II:
case DST_III:
case DST_IV:
/* TODO: what should we do for dst's?*/
for(i=0; i < sz; ++i) {
a[i] = i;
}
break;
default:
fprintf(stderr, "unknown type\n");
exit(EXIT_FAILURE);
}
p = FFTW_PLAN_CREATE(sz, a, b, tp, FFTW_ESTIMATE);
FFTW_EXECUTE(p);
FFTW_DESTROY_PLAN(p);
for(i=0; i < sz; ++i) {
printf(PF"\n", b[i]);
}
FFTW_FREE(b);
FFTW_FREE(a);
return 0;
}
int main(int argc, char* argv[])
{
int n, tp;
if (argc < 3) {
fprintf(stderr, "missing argument: program type n\n");
exit(EXIT_FAILURE);
}
tp = atoi(argv[1]);
n = atoi(argv[2]);
gen(tp, n);
FFTW_CLEANUP();
return 0;
}