53 #include "sphinxbase/byteorder.h"
54 #include "sphinxbase/fixpoint.h"
60 #include "fe_internal.h"
63 static const arg_t fe_args[] = {
64 waveform_to_cepstral_command_line_macro(),
65 { NULL, 0, NULL, NULL }
74 fe->sampling_rate = cmd_ln_float32_r(config,
"-samprate");
75 fe->frame_rate = (int16)cmd_ln_int32_r(config,
"-frate");
78 fe->seed = cmd_ln_int32_r(config,
"-seed");
80 #ifdef WORDS_BIGENDIAN
81 fe->swap = strcmp(
"big",
cmd_ln_str_r(config,
"-input_endian")) == 0 ? 0 : 1;
83 fe->swap = strcmp(
"little",
cmd_ln_str_r(config,
"-input_endian")) == 0 ? 0 : 1;
85 fe->window_length = cmd_ln_float32_r(config,
"-wlen");
86 fe->pre_emphasis_alpha = cmd_ln_float32_r(config,
"-alpha");
88 fe->num_cepstra = (uint8)cmd_ln_int32_r(config,
"-ncep");
89 fe->fft_size = (int16)cmd_ln_int32_r(config,
"-nfft");
92 for (j = fe->fft_size, fe->fft_order = 0; j > 1; j >>= 1, fe->fft_order++) {
93 if (((j % 2) != 0) || (fe->fft_size <= 0)) {
94 E_ERROR(
"fft: number of points must be a power of 2 (is %d)\n",
100 if (fe->fft_size < (
int)(fe->window_length * fe->sampling_rate)) {
101 E_ERROR(
"FFT: Number of points must be greater or equal to frame size (%d samples)\n",
102 (
int)(fe->window_length * fe->sampling_rate));
108 if (0 == strcmp(
cmd_ln_str_r(config,
"-transform"),
"dct"))
109 fe->transform = DCT_II;
110 else if (0 == strcmp(
cmd_ln_str_r(config,
"-transform"),
"legacy"))
111 fe->transform = LEGACY_DCT;
112 else if (0 == strcmp(
cmd_ln_str_r(config,
"-transform"),
"htk"))
113 fe->transform = DCT_HTK;
115 E_ERROR(
"Invalid transform type (values are 'dct', 'legacy', 'htk')\n");
120 fe->log_spec = RAW_LOG_SPEC;
122 fe->log_spec = SMOOTH_LOG_SPEC;
130 mel->sampling_rate = fe->sampling_rate;
131 mel->fft_size = fe->fft_size;
132 mel->num_cepstra = fe->num_cepstra;
133 mel->num_filters = cmd_ln_int32_r(config,
"-nfilt");
136 fe->feature_dimension = mel->num_filters;
138 fe->feature_dimension = fe->num_cepstra;
140 mel->upper_filt_freq = cmd_ln_float32_r(config,
"-upperf");
141 mel->lower_filt_freq = cmd_ln_float32_r(config,
"-lowerf");
146 mel->warp_params =
cmd_ln_str_r(config,
"-warp_params");
147 mel->lifter_val = cmd_ln_int32_r(config,
"-lifter");
152 if (fe_warp_set(mel, mel->warp_type) != FE_SUCCESS) {
153 E_ERROR(
"Failed to initialize the warping function.\n");
156 fe_warp_set_parameters(mel, mel->warp_params, mel->sampling_rate);
161 fe_print_current(
fe_t const *fe)
163 E_INFO(
"Current FE Parameters:\n");
164 E_INFO(
"\tSampling Rate: %f\n", fe->sampling_rate);
165 E_INFO(
"\tFrame Size: %d\n", fe->frame_size);
166 E_INFO(
"\tFrame Shift: %d\n", fe->frame_shift);
167 E_INFO(
"\tFFT Size: %d\n", fe->fft_size);
168 E_INFO(
"\tLower Frequency: %g\n",
169 fe->mel_fb->lower_filt_freq);
170 E_INFO(
"\tUpper Frequency: %g\n",
171 fe->mel_fb->upper_filt_freq);
172 E_INFO(
"\tNumber of filters: %d\n", fe->mel_fb->num_filters);
173 E_INFO(
"\tNumber of Overflow Samps: %d\n", fe->num_overflow_samps);
174 E_INFO(
"\tStart Utt Status: %d\n", fe->start_flag);
175 E_INFO(
"Will %sremove DC offset at frame level\n",
176 fe->remove_dc ?
"" :
"not ");
178 E_INFO(
"Will add dither to audio\n");
179 E_INFO(
"Dither seeded with %d\n", fe->seed);
182 E_INFO(
"Will not add dither to audio\n");
184 if (fe->mel_fb->lifter_val) {
185 E_INFO(
"Will apply sine-curve liftering, period %d\n",
186 fe->mel_fb->lifter_val);
188 E_INFO(
"Will %snormalize filters to unit area\n",
189 fe->mel_fb->unit_area ?
"" :
"not ");
190 E_INFO(
"Will %sround filter frequencies to DFT points\n",
191 fe->mel_fb->round_filters ?
"" :
"not ");
192 E_INFO(
"Will %suse double bandwidth in mel filter\n",
193 fe->mel_fb->doublewide ?
"" :
"not ");
211 if (fe_parse_general_params(config, fe) < 0) {
220 fe->frame_shift = (int32) (fe->sampling_rate / fe->frame_rate + 0.5);
221 fe->frame_size = (int32) (fe->window_length * fe->sampling_rate + 0.5);
223 fe->frame_counter = 0;
225 if (fe->frame_size > (fe->fft_size)) {
227 (
"Number of FFT points has to be a power of 2 higher than %d\n",
234 fe_init_dither(fe->seed);
237 fe->overflow_samps =
ckd_calloc(fe->frame_size,
sizeof(int16));
238 fe->hamming_window =
ckd_calloc(fe->frame_size/2,
sizeof(window_t));
241 fe_create_hamming(fe->hamming_window, fe->frame_size);
244 fe->mel_fb =
ckd_calloc(1,
sizeof(*fe->mel_fb));
247 fe_parse_melfb_params(config, fe, fe->mel_fb);
248 fe_build_melfilters(fe->mel_fb);
249 fe_compute_melcosine(fe->mel_fb);
253 fe->spch =
ckd_calloc(fe->frame_size,
sizeof(*fe->spch));
254 fe->frame =
ckd_calloc(fe->fft_size,
sizeof(*fe->frame));
255 fe->spec =
ckd_calloc(fe->fft_size,
sizeof(*fe->spec));
256 fe->mfspec =
ckd_calloc(fe->mel_fb->num_filters,
sizeof(*fe->mfspec));
259 fe->ccc =
ckd_calloc(fe->fft_size / 4,
sizeof(*fe->ccc));
260 fe->sss =
ckd_calloc(fe->fft_size / 4,
sizeof(*fe->sss));
261 fe_create_twiddle(fe);
264 fe_print_current(fe);
280 fe_get_config(
fe_t *fe)
286 fe_init_dither(int32 seed)
289 E_INFO(
"You are using the internal mechanism to generate the seed.\n");
297 E_INFO(
"You are using %d as the seed.\n", seed);
303 fe_start_utt(
fe_t * fe)
305 fe->num_overflow_samps = 0;
306 memset(fe->overflow_samps, 0, fe->frame_size *
sizeof(int16));
313 fe_get_output_size(
fe_t *fe)
315 return (
int)fe->feature_dimension;
319 fe_get_input_size(
fe_t *fe,
int *out_frame_shift,
323 *out_frame_shift = fe->frame_shift;
325 *out_frame_size = fe->frame_size;
329 fe_process_frame(
fe_t * fe, int16
const *spch, int32 nsamps, mfcc_t * fr_cep)
331 fe_read_frame(fe, spch, nsamps);
332 return fe_write_frame(fe, fr_cep);
336 fe_process_frames(
fe_t *fe,
337 int16
const **inout_spch,
338 size_t *inout_nsamps,
340 int32 *inout_nframes)
343 int outidx, i, n, n_overflow, orig_n_overflow;
344 int16
const *orig_spch;
348 if (buf_cep == NULL) {
349 if (*inout_nsamps + fe->num_overflow_samps < (
size_t)fe->frame_size)
353 + ((*inout_nsamps + fe->num_overflow_samps - fe->frame_size)
355 return *inout_nframes;
359 if (*inout_nsamps + fe->num_overflow_samps < (
size_t)fe->frame_size) {
360 if (*inout_nsamps > 0) {
362 memcpy(fe->overflow_samps + fe->num_overflow_samps,
363 *inout_spch, *inout_nsamps * (
sizeof(int16)));
364 fe->num_overflow_samps += *inout_nsamps;
366 *inout_spch += *inout_nsamps;
375 if (*inout_nframes < 1) {
381 orig_spch = *inout_spch;
382 orig_n_overflow = fe->num_overflow_samps;
385 + ((*inout_nsamps + fe->num_overflow_samps - fe->frame_size)
388 if (frame_count > *inout_nframes)
389 frame_count = *inout_nframes;
394 if (fe->num_overflow_samps) {
395 int offset = fe->frame_size - fe->num_overflow_samps;
398 memcpy(fe->overflow_samps + fe->num_overflow_samps,
399 *inout_spch, offset *
sizeof(**inout_spch));
400 fe_read_frame(fe, fe->overflow_samps, fe->frame_size);
401 assert(outidx < frame_count);
402 if ((n = fe_write_frame(fe, buf_cep[outidx])) < 0)
406 *inout_spch += offset;
407 *inout_nsamps -= offset;
408 fe->num_overflow_samps -= fe->frame_shift;
411 fe_read_frame(fe, *inout_spch, fe->frame_size);
412 assert(outidx < frame_count);
413 if ((n = fe_write_frame(fe, buf_cep[outidx])) < 0)
417 *inout_spch += fe->frame_size;
418 *inout_nsamps -= fe->frame_size;
422 for (i = 1; i < frame_count; ++i) {
423 assert(*inout_nsamps >= (
size_t)fe->frame_shift);
425 fe_shift_frame(fe, *inout_spch, fe->frame_shift);
426 assert(outidx < frame_count);
427 if ((n = fe_write_frame(fe, buf_cep[outidx])) < 0)
431 *inout_spch += fe->frame_shift;
432 *inout_nsamps -= fe->frame_shift;
434 if (fe->num_overflow_samps > 0)
435 fe->num_overflow_samps -= fe->frame_shift;
439 if (fe->num_overflow_samps <= 0) {
441 n_overflow = *inout_nsamps;
442 if (n_overflow > fe->frame_shift)
443 n_overflow = fe->frame_shift;
444 fe->num_overflow_samps = fe->frame_size - fe->frame_shift;
446 if (fe->num_overflow_samps > *inout_spch - orig_spch)
447 fe->num_overflow_samps = *inout_spch - orig_spch;
448 fe->num_overflow_samps += n_overflow;
449 if (fe->num_overflow_samps > 0) {
450 memcpy(fe->overflow_samps,
451 *inout_spch - (fe->frame_size - fe->frame_shift),
452 fe->num_overflow_samps *
sizeof(**inout_spch));
454 *inout_spch += n_overflow;
455 *inout_nsamps -= n_overflow;
461 memmove(fe->overflow_samps,
462 fe->overflow_samps + orig_n_overflow - fe->num_overflow_samps,
463 fe->num_overflow_samps *
sizeof(*fe->overflow_samps));
465 n_overflow = *inout_spch - orig_spch + *inout_nsamps;
466 if (n_overflow > fe->frame_size - fe->num_overflow_samps)
467 n_overflow = fe->frame_size - fe->num_overflow_samps;
468 memcpy(fe->overflow_samps + fe->num_overflow_samps,
469 orig_spch, n_overflow *
sizeof(*orig_spch));
470 fe->num_overflow_samps += n_overflow;
472 if (n_overflow > *inout_spch - orig_spch) {
473 n_overflow -= (*inout_spch - orig_spch);
474 *inout_spch += n_overflow;
475 *inout_nsamps -= n_overflow;
480 *inout_nframes = outidx;
485 fe_process_utt(
fe_t * fe, int16
const * spch,
size_t nsamps,
486 mfcc_t *** cep_block, int32 * nframes)
492 fe_process_frames(fe, NULL, &nsamps, NULL, nframes);
495 cep = (mfcc_t **)
ckd_calloc_2d(*nframes, fe->feature_dimension,
sizeof(**cep));
497 cep = (mfcc_t **)
ckd_calloc_2d(1, fe->feature_dimension,
sizeof(**cep));
499 rv = fe_process_frames(fe, &spch, &nsamps, cep, nframes);
507 fe_end_utt(
fe_t * fe, mfcc_t * cepvector, int32 * nframes)
510 if (fe->num_overflow_samps > 0) {
511 fe_read_frame(fe, fe->overflow_samps, fe->num_overflow_samps);
512 *nframes = fe_write_frame(fe, cepvector);
519 fe->num_overflow_samps = 0;
537 if (--fe->refcount > 0)
542 if (fe->mel_fb->mel_cosine)
543 fe_free_2d((
void *) fe->mel_fb->mel_cosine);
569 fe_mfcc_to_float(
fe_t * fe,
570 mfcc_t ** input, float32 ** output, int32 nframes)
575 if ((
void *) input == (
void *) output)
576 return nframes * fe->feature_dimension;
578 for (i = 0; i < nframes * fe->feature_dimension; ++i)
579 output[0][i] = MFCC2FLOAT(input[0][i]);
588 fe_float_to_mfcc(
fe_t * fe,
589 float32 ** input, mfcc_t ** output, int32 nframes)
594 if ((
void *) input == (
void *) output)
595 return nframes * fe->feature_dimension;
597 for (i = 0; i < nframes * fe->feature_dimension; ++i)
598 output[0][i] = FLOAT2MFCC(input[0][i]);
604 fe_logspec_to_mfcc(
fe_t * fe,
const mfcc_t * fr_spec, mfcc_t * fr_cep)
607 fe_spec2cep(fe, fr_spec, fr_cep);
612 powspec =
ckd_malloc(fe->mel_fb->num_filters *
sizeof(powspec_t));
613 for (i = 0; i < fe->mel_fb->num_filters; ++i)
614 powspec[i] = (powspec_t) fr_spec[i];
615 fe_spec2cep(fe, powspec, fr_cep);
622 fe_logspec_dct2(
fe_t * fe,
const mfcc_t * fr_spec, mfcc_t * fr_cep)
625 fe_dct2(fe, fr_spec, fr_cep, 0);
630 powspec =
ckd_malloc(fe->mel_fb->num_filters *
sizeof(powspec_t));
631 for (i = 0; i < fe->mel_fb->num_filters; ++i)
632 powspec[i] = (powspec_t) fr_spec[i];
633 fe_dct2(fe, powspec, fr_cep, 0);
640 fe_mfcc_dct3(
fe_t * fe,
const mfcc_t * fr_cep, mfcc_t * fr_spec)
643 fe_dct3(fe, fr_cep, fr_spec);
648 powspec =
ckd_malloc(fe->mel_fb->num_filters *
sizeof(powspec_t));
649 fe_dct3(fe, fr_cep, powspec);
650 for (i = 0; i < fe->mel_fb->num_filters; ++i)
651 fr_spec[i] = (mfcc_t) powspec[i];