44 #include "sphinxbase/fixpoint.h"
69 cmn_diff(int16
const *signal, int32 *out_diff,
int ndiff)
79 for (tscale = 0; tscale < 32; ++tscale)
80 if (ndiff & (1<<(31-tscale)))
88 for (t = 1; t < ndiff; ++t) {
89 uint32 dd, dshift, norm;
94 for (j = 0; j < ndiff; ++j) {
95 int diff = signal[j] - signal[t + j];
97 if (dd > (1UL<<tscale)) {
101 dd += (diff * diff) >> dshift;
105 if (dshift > cshift) {
106 cum += dd << (dshift-cshift);
109 cum += dd >> (cshift-dshift);
113 while (cum > (1UL<<tscale)) {
118 if (cum == 0) cum = 1;
120 norm = (t << tscale) / cum;
122 out_diff[t] = (int32)(((
long long)dd * norm)
123 >> (tscale - 15 + cshift - dshift));
131 float search_range,
int smooth_window)
136 pe->frame_size = frame_size;
139 pe->
wsize = smooth_window * 2 + 1;
171 thresholded_search(int32 *diff_window, fixed32 threshold,
int start,
int end)
177 for (i = start; i < end; ++i) {
178 int diff = diff_window[i];
180 if (diff < threshold) {
207 difflen = pe->frame_size / 2;
223 int wstart, wlen, half_wsize, i;
224 int best, best_diff, search_width, low_period, high_period;
226 half_wsize = (pe->
wsize-1)/2;
229 if (half_wsize == 0) {
239 if (pe->
endut == 0 && pe->
nfr < half_wsize + 1) {
253 wlen = pe->
wstart - wstart;
254 if (wlen < 0) wlen += pe->
wsize;
273 for (i = 0; i < wlen; ++i) {
281 if (diff < best_diff) {
294 *out_bestdiff = best_diff;
301 if (search_width == 0) search_width = 1;
302 low_period = best - search_width;
303 high_period = best + search_width;
304 if (low_period < 0) low_period = 0;
305 if (high_period > pe->frame_size / 2) high_period = pe->frame_size / 2;
309 low_period, high_period);
313 *out_period = (best > 65535) ? 65535 : best;
315 *out_bestdiff = (best_diff > 65535) ? 65535 : best_diff;