SphinxBase 0.6
fe_warp_inverse_linear.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_inverse_linear.c
40 *
41 * Description:
42 * Warp the frequency axis according to an inverse_linear function, i.e.:
43 *
44 * w' = w / a
45 *
46 *********************************************************************/
47
48/* static char rcsid[] = "@(#)$Id: fe_warp_inverse_linear.c,v 1.3 2006/02/23 19:40:11 eht 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_inverse_linear.h"
64
65#define N_PARAM 1
66#define YES 1
67#define NO 0
68
69/*
70 * params[0] : a
71 */
72static float params[N_PARAM] = { 1.0f };
73static int32 is_neutral = YES;
74static char p_str[256] = "";
75static float nyquist_frequency = 0.0f;
76
77
78const char *
79fe_warp_inverse_linear_doc()
80{
81 return "inverse_linear :== < w' = x / a >";
82}
83
84uint32
85fe_warp_inverse_linear_id()
86{
87 return FE_WARP_ID_INVERSE_LINEAR;
88}
89
90uint32
91fe_warp_inverse_linear_n_param()
92{
93 return N_PARAM;
94}
95
96void
97fe_warp_inverse_linear_set_parameters(char const *param_str, float sampling_rate)
98{
99 char *tok;
100 char *seps = " \t";
101 char temp_param_str[256];
102 int param_index = 0;
103
104 nyquist_frequency = sampling_rate / 2;
105 if (param_str == NULL) {
106 is_neutral = YES;
107 return;
108 }
109 /* The new parameters are the same as the current ones, so do nothing. */
110 if (strcmp(param_str, p_str) == 0) {
111 return;
112 }
113 is_neutral = NO;
114 strcpy(temp_param_str, param_str);
115 memset(params, 0, N_PARAM * sizeof(float));
116 strcpy(p_str, param_str);
117 /* FIXME: strtok() is not re-entrant... */
118 tok = strtok(temp_param_str, seps);
119 while (tok != NULL) {
120 params[param_index++] = (float) atof_c(tok);
121 tok = strtok(NULL, seps);
122 if (param_index >= N_PARAM) {
123 break;
124 }
125 }
126 if (tok != NULL) {
127 E_INFO
128 ("Inverse linear warping takes only one argument, %s ignored.\n",
129 tok);
130 }
131 if (params[0] == 0) {
132 is_neutral = YES;
133 E_INFO
134 ("Inverse linear warping cannot have slope zero, warping not applied.\n");
135 }
136}
137
138float
139fe_warp_inverse_linear_warped_to_unwarped(float nonlinear)
140{
141 if (is_neutral) {
142 return nonlinear;
143 }
144 else {
145 /* linear = nonlinear * a */
146 float temp = nonlinear * params[0];
147 if (temp > nyquist_frequency) {
148 E_WARN
149 ("Warp factor %g results in frequency (%.1f) higher than Nyquist (%.1f)\n",
150 params[0], temp, nyquist_frequency);
151 }
152 return temp;
153 }
154}
155
156float
157fe_warp_inverse_linear_unwarped_to_warped(float linear)
158{
159 if (is_neutral) {
160 return linear;
161 }
162 else {
163 /* nonlinear = a / linear */
164 float temp = linear / params[0];
165 return temp;
166 }
167}
168
169void
170fe_warp_inverse_linear_print(const char *label)
171{
172 uint32 i;
173
174 for (i = 0; i < N_PARAM; i++) {
175 printf("%s[%04u]: %6.3f ", label, i, params[i]);
176 }
177 printf("\n");
178}
179
180/*
181 * Log record. Maintained by RCS.
182 *
183 * $Log: fe_warp_inverse_linear.c,v $
184 * Revision 1.3 2006/02/23 19:40:11 eht
185 * corrected the doc string for the inverse linear warp function.
186 *
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