81 #define MAXSHORT 32767 88 static int zx_lft_N, zx_rht_N;
91 int n, j, k, N0 = 0, N1, N2, N_, q, lower_found = 0, score = 1, apply_bias;
92 int x_index, y_index, z_index;
93 int zx_rate = 0, zx_at_N0 = 0, prev_sign;
94 int seg1_zxs = 0, seg2_zxs = 0, total_zxs;
95 short prev_seg1, prev_seg2;
98 double xx = 0.0, yy = 0.0, zz = 0.0, xy = 0.0, yz = 0.0, xz = 0.0;
99 double max_cc = 0.0, coefficient, coeff_weight;
100 double xx_N, yy_N, xy_N, y1y1_N, xy1_N, yy1_N, beta;
101 LIST_ *sig_pks_hd, *sig_pks_tl, *sig_peak, *head, *tail;
103 sig_pks_hd = head =
NULL;
104 sig_pks_tl = tail =
NULL;
121 prev_seg1 = seg.
data[paras->
Nmax - paras->
Nmin] < 0 ? -1 : 1;
122 prev_seg2 = seg.
data[paras->
Nmax] < 0 ? -1 : 1;
123 for (j = 0; j < paras->
Nmin; j += paras->
L) {
125 x_index = paras->
Nmax - paras->
Nmin + j;
126 y_index = paras->
Nmax + j;
127 if (seg.
data[x_index] > x_max) x_max = seg.
data[x_index];
128 if (seg.
data[x_index] < x_min) x_min = seg.
data[x_index];
129 if (seg.
data[y_index] > y_max) y_max = seg.
data[y_index];
130 if (seg.
data[y_index] < y_min) y_min = seg.
data[y_index];
132 if (seg.
data[x_index] * prev_seg1 < 0) {
136 if (seg.
data[y_index] * prev_seg2 < 0) {
141 xx += (double) seg.
data[x_index] * seg.
data[x_index];
142 yy += (
double) seg.
data[y_index] * seg.
data[y_index];
143 xy += (double) seg.
data[x_index] * seg.
data[y_index];
146 if (abs (x_max) + abs (x_min) < 2 * paras->
Tsilent ||
147 abs (y_max) + abs (y_min) < 2 * paras->
Tsilent) {
148 for (q = 0; q < p_cc->
size; p_cc->
coeff[q++] = 0.0);
157 p_cc->
coeff[0] = p_status->
cc_max = xy / sqrt (xx) / sqrt (yy);
158 for (q = 1; q < p_cc->
size && q < paras->
L; p_cc->
coeff[q++] = 0.0);
159 total_zxs = seg1_zxs + seg2_zxs;
160 prev_sign = p_cc->
coeff[0] < 0.0 ? -1 : 1;
161 prev_seg1 = seg.
data[paras->
Nmax - paras->
Nmin] < 0 ? -1 : 1;
163 for (n = paras->
Nmin + paras->
L; n <= paras->Nmax; n += paras->
L,
165 x_index = paras->
Nmax - n;
166 y_index = paras->
Nmax + j;
168 if (seg.
data[x_index] * prev_seg1 < 0) {
172 if (seg.
data[y_index] * prev_seg2 < 0) {
177 xx += (double) seg.
data[x_index] * seg.
data[x_index];
178 yy += (
double) seg.
data[y_index] * seg.
data[y_index];
179 for (k = 0, xy = 0.0; k < n; k += paras->
L)
181 p_cc->
coeff[n - paras->
Nmin] = xy / sqrt (xx) / sqrt (yy);
185 for (q = n - paras->
Nmin + 1;
186 q < p_cc->size && q < n - paras->Nmin + paras->
L;
187 p_cc->
coeff[q++] = 0.0);
192 if (p_cc->
coeff[n - paras->
Nmin] * prev_sign < 0.0) {
197 if (N0 != 0 && zx_rate > zx_at_N0) {
202 if (apply_bias && n > zx_lft_N && n < zx_rht_N)
206 if (p_cc->
coeff[n - paras->
Nmin] > max_cc && total_zxs > 3 && lower_found) {
208 if (max_cc * coeff_weight >= p_status->
threshold) {
215 if (sig_pks_hd ==
NULL) {
223 sig_peak = sig_pks_hd;
224 while (sig_peak !=
NULL) {
226 for (j = 0; j < sig_peak->
N0; j++) {
227 y_index = paras->
Nmax + j;
228 z_index = paras->
Nmax + sig_peak->
N0 + j;
229 yy += (double) seg.
data[y_index] * seg.
data[y_index];
230 zz += (
double) seg.
data[z_index] * seg.
data[z_index];
231 yz += (double) seg.
data[y_index] * seg.
data[z_index];
233 if (yy == 0.0 || zz == 0.0)
236 coefficient = yz / sqrt (yy) / sqrt (zz);
237 if (apply_bias && sig_peak->
N0 > zx_lft_N && sig_peak->
N0 < zx_rht_N)
241 if (coefficient * coeff_weight >= p_status->
threshold) {
251 if (head ==
NULL) head = sig_pks_hd;
252 if (tail ==
NULL) tail = sig_pks_tl;
256 for (j = 0; j < tail->
N0; j++)
257 xx += (
double) seg.
data[paras->
Nmax - tail->
N0 + j] *
260 while (sig_peak !=
NULL) {
261 if (sig_peak->
score == score) {
263 for (j = 0; j < tail->
N0; j++) {
264 z_index = paras->
Nmax + sig_peak->
N0 + j;
265 xz += (double) seg.
data[paras->
Nmax - tail->
N0 + j] *
267 zz += (
double) seg.
data[z_index] * seg.
data[z_index];
269 coefficient = xz / sqrt (xx) / sqrt (zz);
270 if (sig_peak == head)
271 max_cc = coefficient;
272 else if (coefficient * paras->
Tdh > max_cc) {
274 max_cc = coefficient;
282 if ((tail == head && score == 1 && p_status->
v_uv !=
VOICED) ||
288 zx_lft_N = zx_rht_N = 0;
289 for (q = N0; q >= paras->
Nmin; q -= paras->
L)
290 if (p_cc->
coeff[q - paras->
Nmin] < 0.0) {
294 for (q = N0; q <= paras->
Nmax; q += paras->
L)
295 if (p_cc->
coeff[q - paras->
Nmin] < 0.0) {
300 if (N0 - paras->
L < paras->
Nmin) {
302 N2 = N0 + 2 * paras->
L;
304 else if (N0 + paras->
L > paras->
Nmax) {
305 N1 = N0 - 2 * paras->
L;
315 for (j = 0; j < N1; j++) {
316 x_index = paras->
Nmax - N1 + j;
317 y_index = paras->
Nmax + j;
318 xx += (double) seg.
data[x_index] * seg.
data[x_index];
319 xy += (
double) seg.
data[x_index] * seg.
data[y_index];
320 yy += (double) seg.
data[y_index] * seg.
data[y_index];
323 xy / sqrt (xx) / sqrt (yy);
325 for (n = N1 + 1; n <= N2; n++, j++) {
328 for (k = 0, xy = 0.0; k < n; k++)
330 p_cc->
coeff[n - paras->
Nmin] = xy / sqrt (xx) / sqrt (yy);
338 if (N0 - 1 < paras->
Nmin || N0 == N1) N_ = N0;
339 else if (N0 + 1 > paras->
Nmax || N0 == N2) N_ = N0 - 1;
345 xx_N = yy_N = xy_N = y1y1_N = xy1_N = yy1_N = 0.0;
346 for (j = 0; j < N_; j++) {
347 x_index = paras->
Nmax - N_ + j;
348 y_index = paras->
Nmax + j;
349 xx_N += (double) seg.
data[x_index] * seg.
data[x_index];
350 yy_N += (
double) seg.
data[y_index] * seg.
data[y_index];
351 xy_N += (double) seg.
data[x_index] * seg.
data[y_index];
352 y1y1_N += (
double) seg.
data[y_index + 1] * seg.
data[y_index + 1];
353 xy1_N += (double) seg.
data[x_index] * seg.
data[y_index + 1];
354 yy1_N += (
double) seg.
data[y_index] * seg.
data[y_index + 1];
356 beta = (xy1_N * yy_N - xy_N * yy1_N) /
357 (xy1_N * (yy_N - yy1_N) + xy_N * (y1y1_N - yy1_N));
362 else if (beta >= 1.0) {
367 p_status->
cc_max = ((1.0 - beta) * xy_N + beta * xy1_N) /
368 sqrt (xx_N * ((1.0 - beta) * (1.0 - beta) * yy_N +
369 2.0 * beta * (1.0 - beta) * yy1_N +
370 beta * beta * y1y1_N));
385 LIST_ *new_node, *last_node;
388 last_node = *p_list_tl;
389 new_node->
N0 = N_val;
390 new_node->
score = score_val;
392 if (*p_list_hd ==
NULL)
393 *p_list_hd = new_node;
396 *p_list_tl = new_node;
411 strcpy (prog,
"srpd");
412 fprintf (stderr,
"%s: ", prog);
415 fprintf (stderr,
"cannot write to output file");
418 fprintf (stderr,
"decimation factor not set");
421 fprintf (stderr,
"insufficient memory available");
427 fprintf (stderr,
"improper fseek () to reposition a stream");
430 fprintf (stderr,
"artificial frame length set out of range");
433 fprintf (stderr,
"maximum pitch frequency value (Hz) not set");
436 fprintf (stderr,
"minimum pitch frequency value (Hz) not set");
439 fprintf (stderr,
"usage: %s -i lpf_sample_file ", prog);
440 fprintf (stderr,
"-o pitch_file [options]\n");
441 fprintf (stderr,
"\nOptions {with default values}\n");
442 fprintf (stderr,
"-a form pitch_file in ascii format\n");
443 fprintf (stderr,
"-l 'lower pitch frequency limit' {%f (Hz)}\n",
445 fprintf (stderr,
"-u 'upper pitch frequency limit' {%f (Hz)}\n",
447 fprintf (stderr,
"-d 'decimation factor' {%d (samples)}\n",
449 fprintf (stderr,
"-n 'noise floor (abs. amplitude)' {%d}\n",
451 fprintf (stderr,
"-h 'unvoiced to voiced coeff threshold' {%f}\n",
453 fprintf (stderr,
"-m 'min. voiced to unvoiced coeff threshold' {%f}\n",
455 fprintf (stderr,
"-r 'voiced to unvoiced coeff threshold ratio' {%f}\n",
457 fprintf (stderr,
"-t 'anti pitch doubling/halving threshold' {%f}\n",
459 fprintf (stderr,
"-p perform peak tracking\n");
460 fprintf (stderr,
"-f 'sampling frequency' {%d (Hz)}\n",
DEFAULT_SF);
461 fprintf (stderr,
"-s 'frame shift' {%f (ms)}\n",
DEFAULT_SHIFT);
462 fprintf (stderr,
"-w 'artificial frame length' {%f (ms)}\n",
466 fprintf (stderr,
"noise floor set below minimum amplitude");
469 fprintf (stderr,
"attempt to set sampling frequency negative");
472 fprintf (stderr,
"frame shift set out of range");
475 fprintf (stderr,
"anti pitch doubling/halving threshold not set");
478 fprintf (stderr,
"unvoiced to voiced coeff threshold not set");
481 fprintf (stderr,
"voiced to unvoiced coeff threshold ratio not set");
484 fprintf (stderr,
"minimum voiced to unvoiced coeff threshold not set");
487 fprintf (stderr,
"undefined error, %u occurred", err_type);
490 fprintf (stderr,
"\n");
563 static int status =
BEGINNING, padding= -1, tracklen = 0;
565 int samples_read = 0;
566 long init_file_position, offset;
573 cout <<
"track len " << tracklen;
576 offset = (long) (p_seg->
length / 2 - paras->
Nmax) *
sizeof (short);
583 p_seg->
shift)) *
sizeof (short);
590 cout <<
"padding " << padding << endl;
593 else if (tracklen-- <= 0)
598 cout <<
"tl " << tracklen << endl;
601 init_file_position =
EST_ftell (voxfile);
602 offset = (long) (p_seg->
shift * sizeof (
short));
603 samples_read = fread ((
short *) p_seg->
data, sizeof (
short),
604 p_seg->
size, voxfile);
605 if (samples_read == p_seg->
size) {
629 static int status =
BEGINNING, padding = -1, tracklen = 0;
658 % p_seg->
shift == 0 ? 0 : 1);
663 else if (tracklen-- <= 0) {
676 offset = p_seg->
shift;
679 p_seg->
data[i] = sig.
a(i + wave_pos,0);
680 for ( ; i < p_seg->
size; ++i)
720 if (fprintf(outfile,
"%7g\n",status.
pitch_freq) != 8)
724 if (!fwrite ((
double *) &status.
pitch_freq, sizeof (
double), 1, outfile))
735 while (*p_list_hd !=
NULL) {
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
void error(error_flags err_type)
#define walloc(TYPE, SIZE)
EST_FilePos EST_ftell(FILE *fp)
void add_to_list(LIST_ **p_list_hd, LIST_ **p_list_tl, int N_val, int score_val)
ssize_t num_samples() const
return the number of samples in the waveform
void free_list(LIST_ **p_list_hd)
short & a(ssize_t i, ssize_t channel=0)
#define DEFAULT_MAX_PITCH
void initialise_structures(struct Srpd_Op *p_par, SEGMENT_ *p_seg, CROSS_CORR_ *p_cc)
void initialise_status(struct Srpd_Op *paras, STATUS_ *p_status)
void initialise_parameters(struct Srpd_Op *p_par)
#define DEFAULT_MIN_PITCH
int read_next_wave_segment(EST_Wave &sig, Srpd_Op *paras, SEGMENT_ *p_seg)
void super_resolution_pda(struct Srpd_Op *paras, SEGMENT_ seg, CROSS_CORR_ *p_cc, STATUS_ *p_status)
int EST_fseek(FILE *fp, EST_FilePos offset, int whence)
#define DEFAULT_TMAX_RATIO
void end_structure_use(SEGMENT_ *p_seg, CROSS_CORR_ *p_cc)
#define DEFAULT_DECIMATION
void write_track(STATUS_ status, struct Srpd_Op paras, FILE *outfile)
int read_next_segment(FILE *voxfile, struct Srpd_Op *paras, SEGMENT_ *p_seg)