SphinxBase  5prealpha
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  */
72 static float params[N_PARAM] = { 1.0f };
73 static int32 is_neutral = YES;
74 static char p_str[256] = "";
75 static float nyquist_frequency = 0.0f;
76 
77 
78 const char *
79 fe_warp_inverse_linear_doc()
80 {
81  return "inverse_linear :== < w' = x / a >";
82 }
83 
84 uint32
85 fe_warp_inverse_linear_id()
86 {
87  return FE_WARP_ID_INVERSE_LINEAR;
88 }
89 
90 uint32
91 fe_warp_inverse_linear_n_param()
92 {
93  return N_PARAM;
94 }
95 
96 void
97 fe_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 
138 float
139 fe_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 
156 float
157 fe_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 
169 void
170 fe_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 }
Miscellaneous useful string functions.
#define E_INFO(...)
Print logging information to standard error stream.
Definition: err.h:114
SPHINXBASE_EXPORT double atof_c(char const *str)
Locale independent version of atof().
Definition: strfuncs.c:55
Implementation of logging routines.
#define E_WARN(...)
Print warning message to error log.
Definition: err.h:109