summaryrefslogtreecommitdiff
path: root/gst-libs/gst/resample/test.c
blob: 44d19a651743754ba5114942314d21db1cfea893 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>

#include <resample.h>

#define AMP 16000
#define I_RATE 48000
#define O_RATE 44100
//#define O_RATE 24000

//#define test_func(x) 1
//#define test_func(x) sin(2*M_PI*(x)*10)
//#define test_func(x) sin(2*M_PI*(x)*(x)*1000)
#define test_func(x) sin(2*M_PI*(x)*(x)*12000)

short i_buf[I_RATE*2*2];
short o_buf[O_RATE*2*2];

static int i_offset;
static int o_offset;

FILE *out;

void test_res1(void);
void test_res2(void);
void test_res3(void);
void test_res4(void);
void test_res5(void);
void test_res6(void);
void test_res7(void);

int main(int argc,char *argv[])
{
	out = fopen("out","w");

	test_res7();

	return 0;
}

void *get_buffer(void *priv, unsigned int size)
{
	void *ret;
	ret = ((void *)o_buf) + o_offset;
	o_offset += size;
	return ret;
}

struct timeval start_time;
void start_timer(void)
{
	gettimeofday(&start_time,NULL);
	//printf("start %ld.%06ld\n",start_time.tv_sec,start_time.tv_usec);
}

void end_timer(void)
{
	struct timeval end_time;
	double diff;

	gettimeofday(&end_time,NULL);
	//printf("end %ld.%06ld\n",end_time.tv_sec,end_time.tv_usec);
	diff = (end_time.tv_sec - start_time.tv_sec) +
		1e-6*(end_time.tv_usec - start_time.tv_usec);

	printf("time %g\n",diff);

}

void test_res1(void)
{
	resample_t *r;
	int i;
	double sum10k,sum22k;
	double f;
	int n10k,n22k;
	double x;

	for(i=0;i<I_RATE;i++){
		i_buf[i*2+0] = rint(AMP * test_func((double)i/I_RATE));
		//i_buf[i*2+1] = rint(AMP * test_func((double)i/I_RATE));
		i_buf[i*2+1] = (i<1000)?AMP:0;
	}

	r = malloc(sizeof(resample_t));
	memset(r,0,sizeof(resample_t));

	r->i_rate = I_RATE;
	r->o_rate = O_RATE;
	//r->method = RESAMPLE_SINC_SLOW;
	r->method = RESAMPLE_SINC;
	r->channels = 2;
	//r->verbose = 1;
	r->filter_length = 64;
	r->get_buffer = get_buffer;

	resample_init(r);

	start_timer();
#define blocked
#ifdef blocked
	for(i=0;i+256<I_RATE;i+=256){
		resample_scale(r,i_buf+i*2,256*2*2);
	}
	if(I_RATE-i){
		resample_scale(r,i_buf+i*2,(I_RATE-i)*2*2);
	}
#else
	resample_scale(r,i_buf,I_RATE*2*2);
#endif
	end_timer();

	for(i=0;i<O_RATE;i++){
		f = AMP*test_func((double)i/O_RATE);
		//f = rint(AMP*test_func((double)i/O_RATE));
		fprintf(out,"%d %d %d %g %g\n",i,
			o_buf[2*i+0],o_buf[2*i+1],
			f,o_buf[2*i+0]-f);
	}

	sum10k=0;
	sum22k=0;
	n10k=0;
	n22k=0;
	for(i=0;i<O_RATE;i++){
		f = AMP*test_func((double)i/O_RATE);
		//f = rint(AMP*test_func((double)i/O_RATE));
		x = o_buf[2*i+0]-f;
		if(((0.5*i)/O_RATE*I_RATE)<10000){
			sum10k += x*x;
			n10k++;
		}
		if(((0.5*i)/O_RATE*I_RATE)<22050){
			sum22k += x*x;
			n22k++;
		}
	}
	printf("average error 10k=%g 22k=%g\n",
		sqrt(sum10k/n10k),
		sqrt(sum22k/n22k));
}


void test_res2(void)
{
	functable_t *t;
	int i;
	double x;
	double f1,f2;

	t = malloc(sizeof(*t));
	memset(t,0,sizeof(*t));

	t->start = -50.0;
	t->offset = 1;
	t->len = 100;

	t->func_x = functable_sinc;
	t->func_dx = functable_dsinc;

	functable_init(t);

	for(i=0;i<1000;i++){
		x = -50.0 + 0.1 * i;
		f1 = functable_sinc(NULL,x);
		f2 = functable_eval(t,x);
		fprintf(out,"%d %g %g %g\n",i,f1,f2,f1-f2);
	}
}

void test_res3(void)
{
	functable_t *t;
	int i;
	double x;
	double f1,f2;
	int n = 1;

	t = malloc(sizeof(*t));
	memset(t,0,sizeof(*t));

	t->start = -50.0;
	t->offset = 1.0 / n;
	t->len = 100 * n;

	t->func_x = functable_sinc;
	t->func_dx = functable_dsinc;

	t->func2_x = functable_window_std;
	t->func2_dx = functable_window_dstd;

	t->scale = 1.0;
	t->scale2 = 1.0 / (M_PI * 16);

	functable_init(t);

	for(i=0;i<1000 * n;i++){
		x = -50.0 + 0.1/n * i;
		f1 = functable_sinc(NULL,t->scale * x) *
			functable_window_std(NULL,t->scale2 * x);
		f2 = functable_eval(t,x);
		fprintf(out,"%d %g %g %g\n",i,f1,f2,f2-f1);
	}
}

double sinc_poly(double x)
{
#define INV3FAC 1.66666666666666666e-1
#define INV5FAC 8.33333333333333333e-3
#define INV7FAC 1.984126984e-4
#define INV9FAC 2.755731922e-6
#define INV11FAC 2.505210839e-8
	double x2 = x * x;

	return 1
		- x2 * INV3FAC
		+ x2 * x2 * INV5FAC
		- x2 * x2 * x2 * INV7FAC;
		//+ x2 * x2 * x2 * x2 * INV9FAC
		//- x2 * x2 * x2 * x2 * x2 * INV11FAC;
}

void test_res4(void)
{
	int i;
	double x,f1,f2;

	for(i=1;i<100;i++){
		x = 0.01 * i;
		f1 = 1 - sin(x)/x;
		f2 = 1 - sinc_poly(x);
	
		fprintf(out,"%g %.20g %.20g %.20g\n",x,f1,f2,f2-f1);
	}
}


void test_res5(void)
{
	int i;
	double sum;

	start_timer();
	sum = 0;
	for(i=0;i<I_RATE;i++){
		sum += i_buf[i*2];
	}
	end_timer();
	i_buf[0] = sum;
}


void short_to_double(double *d,short *x) { *d = *x; }
void short_to_float(float *f,short *x) { *f = *x; }
void float_to_double(double *f,float *x) { *f = *x; }
void double_to_short(short *f,double *x) { *f = *x; }

double res6_tmp[1000];

void test_res6(void)
{
	int i;

	for(i=0;i<I_RATE;i++){
		i_buf[i] = rint(AMP * test_func((double)i/I_RATE));
	}

	conv_double_short_ref(res6_tmp,i_buf,1000);
	for(i=0;i<1000;i++){
		res6_tmp[i] *= 3.0;
	}
	conv_short_double_ppcasm(o_buf,res6_tmp,1000);

	for(i=0;i<1000;i++){
		fprintf(out,"%d %d %g %d\n",i,i_buf[i],res6_tmp[i],o_buf[i]);
	}
}

void test_res7(void)
{
	resample_t *r;
	int i;
	double sum10k,sum22k;
	double f;
	int n10k,n22k;
	double x;

	for(i=0;i<I_RATE;i++){
		i_buf[i] = rint(AMP * test_func((double)i/I_RATE));
	}

	r = malloc(sizeof(resample_t));
	memset(r,0,sizeof(resample_t));

	r->i_rate = I_RATE;
	r->o_rate = O_RATE;
	//r->method = RESAMPLE_SINC_SLOW;
	r->method = RESAMPLE_SINC;
	r->channels = 1;
	//r->verbose = 1;
	r->filter_length = 64;
	r->get_buffer = get_buffer;

	resample_init(r);

	start_timer();
#define blocked
#ifdef blocked
	for(i=0;i+256<I_RATE;i+=256){
		resample_scale(r,i_buf+i,256*2);
	}
	if(I_RATE-i){
		resample_scale(r,i_buf+i,(I_RATE-i)*2);
	}
#else
	resample_scale(r,i_buf,I_RATE*2);
#endif
	end_timer();

	for(i=0;i<O_RATE;i++){
		f = AMP*test_func((double)i/O_RATE);
		//f = rint(AMP*test_func((double)i/O_RATE));
		fprintf(out,"%d %d %d %g %g\n",i,
			o_buf[i],0,
			f,o_buf[i]-f);
	}

	sum10k=0;
	sum22k=0;
	n10k=0;
	n22k=0;
	for(i=0;i<O_RATE;i++){
		f = AMP*test_func((double)i/O_RATE);
		//f = rint(AMP*test_func((double)i/O_RATE));
		x = o_buf[i]-f;
		if(((0.5*i)/O_RATE*I_RATE)<10000){
			sum10k += x*x;
			n10k++;
		}
		if(((0.5*i)/O_RATE*I_RATE)<22050){
			sum22k += x*x;
			n22k++;
		}
	}
	printf("average error 10k=%g 22k=%g\n",
		sqrt(sum10k/n10k),
		sqrt(sum22k/n22k));
}