SphinxBase 0.6
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}
182
183/*
184 * Log record. Maintained by RCS.
185 *
186 * $Log: fe_warp_affine.c,v $
187 * Revision 1.2 2006/02/17 00:31:34 egouvea
188 * Removed switch -melwarp. Changed the default for window length to
189 * 0.025625 from 0.256 (so that a window at 16kHz sampling rate has
190 * exactly 410 samples). Cleaned up include's. Replaced some E_FATAL()
191 * with E_WARN() and return.
192 *
193 * Revision 1.1 2006/02/16 00:18:26 egouvea
194 * Implemented flexible warping function. The user can specify at run
195 * time which of several shapes they want to use. Currently implemented
196 * are an affine function (y = ax + b), an inverse linear (y = a/x) and a
197 * piecewise linear (y = ax, up to a frequency F, and then it "breaks" so
198 * Nyquist frequency matches in both scales.
199 *
200 * Added two switches, -warp_type and -warp_params. The first specifies
201 * the type, which valid values:
202 *
203 * -inverse or inverse_linear
204 * -linear or affine
205 * -piecewise or piecewise_linear
206 *
207 * The inverse_linear is the same as implemented by EHT. The -mel_warp
208 * switch was kept for compatibility (maybe remove it in the
209 * future?). The code is compatible with EHT's changes: cepstra created
210 * from code after his changes should be the same as now. Scripts that
211 * worked with his changes should work now without changes. Tested a few
212 * cases, same results.
213 *
214 */
Implementation of logging routines.
#define E_WARN
Print warning information to standard error stream.
Definition err.h:164
#define E_INFO
Print logging information to standard error stream.
Definition err.h:147
Miscellaneous useful string functions.
SPHINXBASE_EXPORT double atof_c(char const *str)
Locale independent version of atof().
Definition strfuncs.c:56