summaryrefslogtreecommitdiff
path: root/tests/test6/test6.c
blob: e5a2a12b47683367962d3aa3c68bf632eb1e070c (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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include "compute.h"

typedef void (*cb_processdata_t)(int n, float *);


//#if 1 //(UGLY_IEEE754_FLOAT32_HACK :-)
/*
static inline float todB_a(const float *x){
	return (float)((*(int *)x)&0x7fffffff) * 7.17711438e-7f -764.6161886f;
}
*/
static inline float todB_a(const float *x){
	union {
		//int32_t i;
		int i;
		float f;
	} ix;
	ix.f = *x;
	ix.i = ix.i&0x7fffffff;
	return (float)(ix.i * 7.17711438e-7f -764.6161886f);
}
//#else

static inline float todB_a2(const float *x){
	return (*(x)==0?-400.f:logf(*(x)**(x))*4.34294480f);
}

//#endif
void test_todb_a() {
	float f, dbav[32]={0.f}, dbaf[32]={0.f};
	int i=0;
	for(f=1024000.f;f>1.f;f/=2.f) {
		dbaf[31-i]=f;
		dbav[31-i]=todB_a(&f);
		i++;
	}
	for (i=0;i<32;i++)
		printf("f==%f\tv==%f\n", dbaf[i], dbav[i]);

}

void dump_testfile() {
	int i,n;
	float f[256];
	FILE *fh=fopen("./test.raw", "r");
	if (fh==NULL) return;
	while ( (n=fread(f, sizeof(float), 256, fh)) > 0 ) {
		for(i=0;i<n;i++) {
			printf("%+.3f\t", f[i]);
		}
	}
	fclose(fh);
}

void parse_testfile(cb_processdata_t cb) {
	int n;
	float f[2048];
	FILE *fh=fopen("./test.raw", "r");
	if (fh==NULL) return;

	//n=128;
	n=512;
	while ( (n=fread(f, sizeof(float), n, fh)) > 0 ) {
		cb(n,f);
		//n=128+256*(rand()%7);
		n=512;
	}
	fclose(fh);
}

void process_mean_max(int n, float *f) {
	int i;
	float t, mean=0, max=0;
	for(i=0;i<n;i++) {
		t=fabs(f[i]);
		mean+=t;
		if (t>max) max=t;
	}
	mean/=n;

	printf("%+.3f %+.3f %4i\n", mean, max, n);
}

void process_level(int n, float *f) {
	int rate=24000;
	float level;

	level=compute_level(f, n, rate);
	printf("%+.3f %4i\n", level, n);
}

int main() {
	//test_todb_a();
	//dump_testfile();
	//parse_testfile(process_mean_max);
	parse_testfile(process_level);

	return 0;
}