-
Notifications
You must be signed in to change notification settings - Fork 3
/
wftest.cpp
executable file
·162 lines (109 loc) · 3.07 KB
/
wftest.cpp
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
// wftest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <math.h>
FILE *inf;
FILE *outf;
char buf[4096];
int i,j,k;
short last_val, this_val;
double interval,remainder,delta;
//int ints[1000];
//int errors[1000];
int samples;
double summ, average;
int max, min;
int nread;
short error_3150;
double freq,err1, err2;
double nanosec_per_sample;
char *filename = "wow16_001.wav";
//typedef
double sum_of_squares;
int good_samples;
int old_main(int argc, char* argv[])
{
int done = 0;
double proper_interval = 0.5 * 10e8/3150; // half a period of 3150 sampled
if(argc > 1)
filename = argv[1];
outf = fopen("intervals.wav","wb");
inf = fopen(filename, "rb");
if(inf == NULL){
printf("Can't open WAV file!\n");
return;
}
fread(&WavHeader,sizeof(WavHeader),1,inf);
if(WavHeader.numChannels != 1){
printf("Not a MONO wave file!\n");
exit(1);
}
nanosec_per_sample = 10e8 / WavHeader.sampleRate;
sum_of_squares = 0;
good_samples = 0;
WavHeader.sampleRate = 3150; // to write back the error wave
fwrite(&WavHeader,sizeof(WavHeader),1,outf);
fread(&this_val,2,1,inf);
interval = 0.0;
max = 0;
min = 2000000;
j=0;
samples = 0;
while(done == 0){
while(1){
last_val = this_val;
nread = fread(&this_val,2,1,inf);
if( nread < 1){
done = 1;
break;
}
if((this_val > 0) && (last_val < 0)){
delta = -last_val * nanosec_per_sample / (this_val - last_val);
interval += delta;
remainder = nanosec_per_sample - delta;
break;
}
if((this_val < 0) && (last_val > 0)){
delta = last_val * nanosec_per_sample / (last_val - this_val) ;
interval += delta;
remainder = nanosec_per_sample - delta;
break;
}
interval += nanosec_per_sample; // nS between samples
if(this_val == 0){
remainder = 0;
break;
}
}
if(!done){
last_val = this_val;
error_3150 = (short)(10 * (proper_interval - interval));
// for 1% will be 315
if((error_3150 < 600) && (error_3150 > -600)){
sum_of_squares += (proper_interval - interval)*(proper_interval - interval)/(proper_interval *proper_interval);
good_samples++;
fwrite(&error_3150,2,1,outf);
if(interval > max)
max = (int)interval;
if(interval < min)
min = (int)interval;
}
summ += (double) interval;
//ints[j++] = interval;
interval = remainder;
samples++;
}
}
average=summ/(double) samples;
freq = 1000000000 / average /2;
err1 = (max - min) * 100 / average;
//err2 = (average - min) * 10000 / average;
printf("freq=%.1f max=%d min=%d err=%.4f \n", freq, max, min, err1);
printf("max err = %.4f min err = %.4f RMS=%.4f\n", (max - average) *100 / average,
(average - min) * 100 / average, sqrt(sum_of_squares/good_samples) * 100 );
printf("\n");
fclose(outf);
fclose(inf);
// CDialog::OnOK();
}