comparison bqvec/test/TestVectorOps.cpp @ 366:5d0a2ebb4d17

Bring dependent libraries in to repo
author Chris Cannam
date Fri, 24 Jun 2016 14:47:45 +0100
parents
children af71cbdab621
comparison
equal deleted inserted replaced
365:112766f4c34b 366:5d0a2ebb4d17
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 #include "VectorOpsComplex.h"
4
5 #include <iostream>
6 #include <cstdlib>
7
8 #include <time.h>
9
10 using namespace std;
11
12 namespace breakfastquay {
13
14 namespace Test {
15
16 #ifdef _WIN32
17 #define drand48() (-1+2*((float)rand())/RAND_MAX)
18 #endif
19
20 bool
21 testMultiply()
22 {
23 cerr << "testVectorOps: testing v_multiply complex" << endl;
24
25 const int N = 1024;
26 turbot_complex_sample_t target[N];
27 turbot_complex_sample_t src1[N];
28 turbot_complex_sample_t src2[N];
29
30 for (int i = 0; i < N; ++i) {
31 src1[i].re = drand48();
32 src1[i].im = drand48();
33 src2[i].re = drand48();
34 src2[i].im = drand48();
35 }
36
37 turbot_sample_t mean, first, last, total = 0;
38 for (int i = 0; i < N; ++i) {
39 turbot_complex_sample_t result;
40 c_multiply(result, src1[i], src2[i]);
41 if (i == 0) first = result.re;
42 if (i == N-1) last = result.im;
43 total += result.re;
44 total += result.im;
45 }
46 mean = total / (N*2);
47 cerr << "Naive method: mean = " << mean << ", first = " << first
48 << ", last = " << last << endl;
49
50 v_multiply(target, src1, src2, N);
51 total = 0;
52
53 for (int i = 0; i < N; ++i) {
54 if (i == 0) first = target[i].re;
55 if (i == N-1) last = target[i].im;
56 total += target[i].re;
57 total += target[i].im;
58 }
59 mean = total / (N*2);
60 cerr << "v_multiply: mean = " << mean << ", first = " << first
61 << ", last = " << last << endl;
62
63 int iterations = 50000;
64 cerr << "Iterations: " << iterations << endl;
65
66 cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl;
67 float divisor = float(CLOCKS_PER_SEC) / 1000.f;
68
69 clock_t start = clock();
70
71 for (int j = 0; j < iterations; ++j) {
72 for (int i = 0; i < N; ++i) {
73 c_multiply(target[i], src1[i], src2[i]);
74 }
75 }
76
77 clock_t end = clock();
78
79 cerr << "Time for naive method: " << float(end - start)/divisor << endl;
80
81 start = clock();
82
83 for (int j = 0; j < iterations; ++j) {
84 v_multiply(target, src1, src2, N);
85 }
86
87 end = clock();
88
89 cerr << "Time for v_multiply: " << float(end - start)/divisor << endl;
90
91 return true;
92 }
93
94 bool
95 testPolarToCart()
96 {
97 cerr << "testVectorOps: testing v_polar_to_cartesian" << endl;
98
99 const int N = 1024;
100 turbot_complex_sample_t target[N];
101 turbot_sample_t mag[N];
102 turbot_sample_t phase[N];
103
104 for (int i = 0; i < N; ++i) {
105 mag[i] = drand48();
106 phase[i] = (drand48() * M_PI * 2) - M_PI;
107 }
108
109 turbot_sample_t mean, first, last, total = 0;
110 for (int i = 0; i < N; ++i) {
111 turbot_sample_t real = mag[i] * cos(phase[i]);
112 turbot_sample_t imag = mag[i] * sin(phase[i]);
113 if (i == 0) first = real;
114 if (i == N-1) last = imag;
115 total += real;
116 total += imag;
117 }
118 mean = total / (N*2);
119 cerr << "Naive method: mean = " << mean << ", first = " << first
120 << ", last = " << last << endl;
121
122 v_polar_to_cartesian(target, mag, phase, N);
123
124 total = 0;
125
126 for (int i = 0; i < N; ++i) {
127 if (i == 0) first = target[i].re;
128 if (i == N-1) last = target[i].im;
129 total += target[i].re;
130 total += target[i].im;
131 }
132 mean = total / (N*2);
133 cerr << "v_polar_to_cartesian: mean = " << mean << ", first = " << first
134 << ", last = " << last << endl;
135
136 int iterations = 10000;
137 cerr << "Iterations: " << iterations << endl;
138
139 cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl;
140 float divisor = float(CLOCKS_PER_SEC) / 1000.f;
141
142 clock_t start = clock();
143
144 for (int j = 0; j < iterations; ++j) {
145 for (int i = 0; i < N; ++i) {
146 target[i].re = mag[i] * cos(phase[i]);
147 target[i].im = mag[i] * sin(phase[i]);
148 }
149 }
150
151 clock_t end = clock();
152
153 cerr << "Time for naive method: " << float(end - start)/divisor << endl;
154
155 start = clock();
156
157 for (int j = 0; j < iterations; ++j) {
158 v_polar_to_cartesian(target, mag, phase, N);
159 }
160
161 end = clock();
162
163 cerr << "Time for v_polar_to_cartesian: " << float(end - start)/divisor << endl;
164
165 return true;
166 }
167
168 bool
169 testPolarToCartInterleaved()
170 {
171 cerr << "testVectorOps: testing v_polar_interleaved_to_cartesian" << endl;
172
173 const int N = 1024;
174 turbot_complex_sample_t target[N];
175 turbot_sample_t source[N*2];
176
177 for (int i = 0; i < N; ++i) {
178 source[i*2] = drand48();
179 source[i*2+1] = (drand48() * M_PI * 2) - M_PI;
180 }
181
182 turbot_sample_t mean, first, last, total = 0;
183 for (int i = 0; i < N; ++i) {
184 turbot_sample_t real = source[i*2] * cos(source[i*2+1]);
185 turbot_sample_t imag = source[i*2] * sin(source[i*2+1]);
186 if (i == 0) first = real;
187 if (i == N-1) last = imag;
188 total += real;
189 total += imag;
190 }
191 mean = total / (N*2);
192 cerr << "Naive method: mean = " << mean << ", first = " << first
193 << ", last = " << last << endl;
194
195 v_polar_interleaved_to_cartesian(target, source, N);
196
197 total = 0;
198
199 for (int i = 0; i < N; ++i) {
200 if (i == 0) first = target[i].re;
201 if (i == N-1) last = target[i].im;
202 total += target[i].re;
203 total += target[i].im;
204 }
205 mean = total / (N*2);
206 cerr << "v_polar_interleaved_to_cartesian: mean = " << mean << ", first = " << first
207 << ", last = " << last << endl;
208
209 int iterations = 10000;
210 cerr << "Iterations: " << iterations << endl;
211
212 cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl;
213 float divisor = float(CLOCKS_PER_SEC) / 1000.f;
214
215 clock_t start = clock();
216
217 for (int j = 0; j < iterations; ++j) {
218 for (int i = 0; i < N; ++i) {
219 target[i].re = source[i*2] * cos(source[i*2+1]);
220 target[i].im = source[i*2] * sin(source[i*2+1]);
221 }
222 }
223
224 clock_t end = clock();
225
226 cerr << "Time for naive method: " << float(end - start)/divisor << endl;
227
228 start = clock();
229
230 for (int j = 0; j < iterations; ++j) {
231 v_polar_interleaved_to_cartesian(target, source, N);
232 }
233
234 end = clock();
235
236 cerr << "Time for v_polar_interleaved_to_cartesian: " << float(end - start)/divisor << endl;
237
238 return true;
239 }
240
241 bool
242 testVectorOps()
243 {
244 if (!testMultiply()) return false;
245 if (!testPolarToCart()) return false;
246 if (!testPolarToCartInterleaved()) return false;
247
248 return true;
249 }
250
251 }
252
253 }
254