SphinxBase 5prealpha
fe_warp_affine.c
1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/* ====================================================================
3 * Copyright (c) 2006 Carnegie Mellon University. All rights
4 * reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
21 *
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * ====================================================================
35 *
36 */
37/*********************************************************************
38 *
39 * File: fe_warp_affine.c
40 *
41 * Description:
42 * Warp the frequency axis according to an affine function, i.e.:
43 *
44 * w' = a * w + b
45 *
46 *********************************************************************/
47
48/* static char rcsid[] = "@(#)$Id: fe_warp_affine.c,v 1.2 2006/02/17 00:31:34 egouvea Exp $"; */
49
50#include <stdio.h>
51#include <stdlib.h>
52#include <math.h>
53#include <string.h>
54
55#ifdef _MSC_VER
56#pragma warning (disable: 4996)
57#endif
58
59#include "sphinxbase/strfuncs.h"
60#include "sphinxbase/err.h"
61
62#include "fe_warp.h"
63#include "fe_warp_affine.h"
64
65#define N_PARAM 2
66#define YES 1
67#define NO 0
68
69/*
70 * params[0] : a
71 * params[1] : b
72 */
73static float params[N_PARAM] = { 1.0f, 0.0f };
74static int32 is_neutral = YES;
75static char p_str[256] = "";
76static float nyquist_frequency = 0.0f;
77
78
79const char *
80fe_warp_affine_doc()
81{
82 return "affine :== < w' = a * x + b >";
83}
84
85uint32
86fe_warp_affine_id()
87{
88 return FE_WARP_ID_AFFINE;
89}
90
91uint32
92fe_warp_affine_n_param()
93{
94 return N_PARAM;
95}
96
97void
98fe_warp_affine_set_parameters(char const *param_str, float sampling_rate)
99{
100 char *tok;
101 char *seps = " \t";
102 char temp_param_str[256];
103 int param_index = 0;
104
105 nyquist_frequency = sampling_rate / 2;
106 if (param_str == NULL) {
107 is_neutral = YES;
108 return;
109 }
110 /* The new parameters are the same as the current ones, so do nothing. */
111 if (strcmp(param_str, p_str) == 0) {
112 return;
113 }
114 is_neutral = NO;
115 strcpy(temp_param_str, param_str);
116 memset(params, 0, N_PARAM * sizeof(float));
117 strcpy(p_str, param_str);
118 /* FIXME: strtok() is not re-entrant... */
119 tok = strtok(temp_param_str, seps);
120 while (tok != NULL) {
121 params[param_index++] = (float) atof_c(tok);
122 tok = strtok(NULL, seps);
123 if (param_index >= N_PARAM) {
124 break;
125 }
126 }
127 if (tok != NULL) {
128 E_INFO
129 ("Affine warping takes up to two arguments, %s ignored.\n",
130 tok);
131 }
132 if (params[0] == 0) {
133 is_neutral = YES;
134 E_INFO
135 ("Affine warping cannot have slope zero, warping not applied.\n");
136 }
137}
138
139float
140fe_warp_affine_warped_to_unwarped(float nonlinear)
141{
142 if (is_neutral) {
143 return nonlinear;
144 }
145 else {
146 /* linear = (nonlinear - b) / a */
147 float temp = nonlinear - params[1];
148 temp /= params[0];
149 if (temp > nyquist_frequency) {
150 E_WARN
151 ("Warp factor %g results in frequency (%.1f) higher than Nyquist (%.1f)\n",
152 params[0], temp, nyquist_frequency);
153 }
154 return temp;
155 }
156}
157
158float
159fe_warp_affine_unwarped_to_warped(float linear)
160{
161 if (is_neutral) {
162 return linear;
163 }
164 else {
165 /* nonlinear = a * linear - b */
166 float temp = linear * params[0];
167 temp += params[1];
168 return temp;
169 }
170}
171
172void
173fe_warp_affine_print(const char *label)
174{
175 uint32 i;
176
177 for (i = 0; i < N_PARAM; i++) {
178 printf("%s[%04u]: %6.3f ", label, i, params[i]);
179 }
180 printf("\n");
181}
Implementation of logging routines.
#define E_INFO(...)
Print logging information to standard error stream.
Definition: err.h:114
#define E_WARN(...)
Print warning message to error log.
Definition: err.h:109
Miscellaneous useful string functions.
SPHINXBASE_EXPORT double atof_c(char const *str)
Locale independent version of atof().
Definition: strfuncs.c:55