• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

src/libsphinxbase/fe/fe_warp_piecewise_linear.c

00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /* ====================================================================
00003  * Copyright (c) 2006 Carnegie Mellon University.  All rights 
00004  * reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer. 
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  * This work was supported in part by funding from the Defense Advanced 
00019  * Research Projects Agency and the National Science Foundation of the 
00020  * United States of America, and the CMU Sphinx Speech Consortium.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
00023  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00024  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00025  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
00026  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * ====================================================================
00035  *
00036  */
00037 /*********************************************************************
00038  *
00039  * File: fe_warp_piecewise_linear.c
00040  * 
00041  * Description: 
00042  *
00043  *      Warp the frequency axis according to an piecewise linear
00044  *      function. The function is linear up to a frequency F, where
00045  *      the slope changes so that the Nyquist frequency in the warped
00046  *      axis maps to the Nyquist frequency in the unwarped.
00047  *
00048  *              w' = a * w, w < F
00049  *              w' = a' * w + b, W > F
00050  *              w'(0) = 0
00051  *              w'(F) = F
00052  *              w'(Nyq) = Nyq
00053  *      
00054  *********************************************************************/
00055 
00056 /* static char rcsid[] = "@(#)$Id: fe_warp_piecewise_linear.c,v 1.2 2006/02/17 00:31:34 egouvea Exp $"; */
00057 
00058 #include <stdio.h>
00059 #include <stdlib.h>
00060 #include <math.h>
00061 #include <string.h>
00062 
00063 #ifdef _MSC_VER
00064 #pragma warning (disable: 4996)
00065 #endif
00066 
00067 #include "strfuncs.h"
00068 #include "err.h"
00069 #include "fe_warp.h"
00070 #include "fe_warp_piecewise_linear.h"
00071 
00072 #define N_PARAM         2
00073 #define YES             1
00074 #define NO              0
00075 
00076 /*
00077  * params[0] : a
00078  * params[1] : F (the non-differentiable point)
00079  */
00080 static float params[N_PARAM] = { 1.0f, 6800.0f };
00081 static float final_piece[2];
00082 static int32 is_neutral = YES;
00083 static char p_str[256] = "";
00084 static float nyquist_frequency = 0.0f;
00085 
00086 
00087 const char *
00088 fe_warp_piecewise_linear_doc()
00089 {
00090     return "piecewise_linear :== < w' = a * w, w < F >";
00091 }
00092 
00093 uint32
00094 fe_warp_piecewise_linear_id()
00095 {
00096     return FE_WARP_ID_PIECEWISE_LINEAR;
00097 }
00098 
00099 uint32
00100 fe_warp_piecewise_linear_n_param()
00101 {
00102     return N_PARAM;
00103 }
00104 
00105 void
00106 fe_warp_piecewise_linear_set_parameters(char const *param_str,
00107                                         float sampling_rate)
00108 {
00109     char *tok;
00110     char *seps = " \t";
00111     char temp_param_str[256];
00112     int param_index = 0;
00113 
00114     nyquist_frequency = sampling_rate / 2;
00115     if (param_str == NULL) {
00116         is_neutral = YES;
00117         return;
00118     }
00119     /* The new parameters are the same as the current ones, so do nothing. */
00120     if (strcmp(param_str, p_str) == 0) {
00121         return;
00122     }
00123     is_neutral = NO;
00124     strcpy(temp_param_str, param_str);
00125     memset(params, 0, N_PARAM * sizeof(float));
00126     memset(final_piece, 0, 2 * sizeof(float));
00127     strcpy(p_str, param_str);
00128     /* FIXME: strtok() is not re-entrant... */
00129     tok = strtok(temp_param_str, seps);
00130     while (tok != NULL) {
00131         params[param_index++] = (float) atof_c(tok);
00132         tok = strtok(NULL, seps);
00133         if (param_index >= N_PARAM) {
00134             break;
00135         }
00136     }
00137     if (tok != NULL) {
00138         E_INFO
00139             ("Piecewise linear warping takes up to two arguments, %s ignored.\n",
00140              tok);
00141     }
00142     if (params[1] < sampling_rate) {
00143         /* Precompute these. These are the coefficients of a
00144          * straight line that contains the points (F, aF) and (N,
00145          * N), where a = params[0], F = params[1], N = Nyquist
00146          * frequency.
00147          */
00148         if (params[1] == 0) {
00149             params[1] = sampling_rate * 0.85f;
00150         }
00151         final_piece[0] =
00152             (nyquist_frequency -
00153              params[0] * params[1]) / (nyquist_frequency - params[1]);
00154         final_piece[1] =
00155             nyquist_frequency * params[1] * (params[0] -
00156                                          1.0f) / (nyquist_frequency -
00157                                                   params[1]);
00158     }
00159     else {
00160         memset(final_piece, 0, 2 * sizeof(float));
00161     }
00162     if (params[0] == 0) {
00163         is_neutral = YES;
00164         E_INFO
00165             ("Piecewise linear warping cannot have slope zero, warping not applied.\n");
00166     }
00167 }
00168 
00169 float
00170 fe_warp_piecewise_linear_warped_to_unwarped(float nonlinear)
00171 {
00172     if (is_neutral) {
00173         return nonlinear;
00174     }
00175     else {
00176         /* linear = (nonlinear - b) / a */
00177         float temp;
00178         if (nonlinear < params[0] * params[1]) {
00179             temp = nonlinear / params[0];
00180         }
00181         else {
00182             temp = nonlinear - final_piece[1];
00183             temp /= final_piece[0];
00184         }
00185         if (temp > nyquist_frequency) {
00186             E_WARN
00187                 ("Warp factor %g results in frequency (%.1f) higher than Nyquist (%.1f)\n",
00188                  params[0], temp, nyquist_frequency);
00189         }
00190         return temp;
00191     }
00192 }
00193 
00194 float
00195 fe_warp_piecewise_linear_unwarped_to_warped(float linear)
00196 {
00197     if (is_neutral) {
00198         return linear;
00199     }
00200     else {
00201         float temp;
00202         /* nonlinear = a * linear - b */
00203         if (linear < params[1]) {
00204             temp = linear * params[0];
00205         }
00206         else {
00207             temp = final_piece[0] * linear + final_piece[1];
00208         }
00209         return temp;
00210     }
00211 }
00212 
00213 void
00214 fe_warp_piecewise_linear_print(const char *label)
00215 {
00216     uint32 i;
00217 
00218     for (i = 0; i < N_PARAM; i++) {
00219         printf("%s[%04u]: %6.3f ", label, i, params[i]);
00220     }
00221     printf("\n");
00222 }
00223 
00224 /*
00225  * Log record.  Maintained by RCS.
00226  *
00227  * $Log: fe_warp_piecewise_linear.c,v $
00228  * Revision 1.2  2006/02/17 00:31:34  egouvea
00229  * Removed switch -melwarp. Changed the default for window length to
00230  * 0.025625 from 0.256 (so that a window at 16kHz sampling rate has
00231  * exactly 410 samples). Cleaned up include's. Replaced some E_FATAL()
00232  * with E_WARN() and return.
00233  *
00234  * Revision 1.1  2006/02/16 00:18:26  egouvea
00235  * Implemented flexible warping function. The user can specify at run
00236  * time which of several shapes they want to use. Currently implemented
00237  * are an affine function (y = ax + b), an inverse linear (y = a/x) and a
00238  * piecewise linear (y = ax, up to a frequency F, and then it "breaks" so
00239  * Nyquist frequency matches in both scales.
00240  *
00241  * Added two switches, -warp_type and -warp_params. The first specifies
00242  * the type, which valid values:
00243  *
00244  * -inverse or inverse_linear
00245  * -linear or affine
00246  * -piecewise or piecewise_linear
00247  *
00248  * The inverse_linear is the same as implemented by EHT. The -mel_warp
00249  * switch was kept for compatibility (maybe remove it in the
00250  * future?). The code is compatible with EHT's changes: cepstra created
00251  * from code after his changes should be the same as now. Scripts that
00252  * worked with his changes should work now without changes. Tested a few
00253  * cases, same results.
00254  *
00255  */

Generated on Tue Aug 17 2010 for SphinxBase by  doxygen 1.7.1