Home | History | Annotate | Download | only in clib
      1 /*---------------------------------------------------------------------------*
      2  *  cnorm_tr.c  *
      3  *                                                                           *
      4  *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
      5  *                                                                           *
      6  *  Licensed under the Apache License, Version 2.0 (the 'License');          *
      7  *  you may not use this file except in compliance with the License.         *
      8  *                                                                           *
      9  *  You may obtain a copy of the License at                                  *
     10  *      http://www.apache.org/licenses/LICENSE-2.0                           *
     11  *                                                                           *
     12  *  Unless required by applicable law or agreed to in writing, software      *
     13  *  distributed under the License is distributed on an 'AS IS' BASIS,        *
     14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
     15  *  See the License for the specific language governing permissions and      *
     16  *  limitations under the License.                                           *
     17  *                                                                           *
     18  *---------------------------------------------------------------------------*/
     19 
     20 
     21 #ifndef _RTT
     22 #include <stdio.h>
     23 #endif
     24 #include <stdlib.h>
     25 #include <math.h>
     26 #include <string.h>
     27 #include <assert.h>
     28 
     29 #include "channorm.h"
     30 #include "prelib.h"
     31 #ifndef _RTT
     32 #include "duk_io.h"
     33 #endif
     34 #include "portable.h"
     35 
     36 #define DEBUG   0
     37 
     38 #define ESTIMATE_PERIOD  -1
     39 #define BACK_ESTIMATE_PERIOD    1000
     40 #define ESTIMATE_PERCENTILE 50
     41 
     42 
     43 static const char cnorm_tr[] = "$Id: cnorm_tr.c,v 1.4.10.6 2007/10/15 18:06:24 dahan Exp $";
     44 
     45 norm_info *create_channel_normalization()
     46 {
     47   norm_info *channorm;
     48 
     49   channorm = (norm_info *) CALLOC_CLR(1, sizeof(norm_info), "clib.channorm");
     50   return (channorm);
     51 }
     52 
     53 void destroy_channel_normalization(norm_info *channorm)
     54 {
     55   ASSERT(channorm);
     56   FREE((char *)channorm);
     57   return;
     58 }
     59 
     60 void apply_channel_normalization_in_imelda(norm_info *channorm,
     61     imeldata *outframe, imeldata *frame,
     62     int dimen)
     63 {
     64   int ii;
     65 
     66   ASSERT(channorm);
     67   ASSERT(frame);
     68   ASSERT(outframe);
     69   ASSERT(dimen <= channorm->dim);
     70   for (ii = 0; ii < dimen; ii++)
     71     outframe[ii] = MAKEBYTE(frame[ii] + channorm->imelda_adjust[ii]);
     72   return;
     73 }
     74 
     75 void estimate_normalization_parameters(norm_info *channorm,
     76                                        spect_dist_info **chandata, int dimen)
     77 {
     78   int ii, adjust;
     79 
     80   ASSERT(channorm);
     81   ASSERT(chandata);
     82   ASSERT(dimen <= channorm->dim);
     83   for (ii = 0; ii < dimen; ii++)
     84     if (chandata[ii])
     85     {
     86       evaluate_parameters(chandata[ii]);
     87       /*  The additive expression is due to
     88       **  the normalization taking place before the
     89       **  utterance object is created
     90       */
     91       adjust = mean_normalize_data(chandata[ii], 0);
     92       /*     channorm->adjust[ii]= adjust; */
     93 #if USE_MEDIAN
     94       shift_distribution_counts(chandata[ii], adjust);
     95 #endif
     96       shift_parameters(chandata[ii], adjust);
     97 #if NORM_IN_IMELDA
     98       channorm->imelda_adjust[ii] += adjust;
     99 #else
    100       channorm->adjust[ii] += adjust;
    101 #endif
    102     }
    103 #if NORM_IN_IMELDA
    104   channorm->adj_valid = True;
    105 #if DEBUG
    106   log_report("NORM IML: ");
    107   for (ii = 0; ii < channorm->dim; ii++)
    108     log_report("%d ", channorm->imelda_adjust[ii]);
    109   log_report("\n");
    110 #endif
    111 #else
    112   channorm->adj_valid = False;
    113 #if DEBUG
    114   log_report("NORM ADJ: ");
    115   for (ii = 0; ii < channorm->dim; ii++)
    116     log_report("%d ", channorm->adjust[ii]);
    117   log_report("\n");
    118 #endif
    119 #endif
    120   return;
    121 }
    122 
    123 void setup_channel_normalization(norm_info *channorm,
    124                                  spect_dist_info **chandata, int dimen,
    125                                  int forget_factor)
    126 {
    127   int ii;
    128 
    129   ASSERT(channorm);
    130   ASSERT(chandata);
    131   for (ii = 0; ii < dimen; ii++)
    132   {
    133 #if MODEL_BASED || 1
    134     chandata[ii] = create_spectrum_distribution(
    135                      128, 128,
    136                      0, 255, forget_factor, ESTIMATE_PERIOD,
    137                      ESTIMATE_PERCENTILE, 10);
    138 #else
    139     chandata[ii] = create_spectrum_distribution(
    140                      channorm->chan_tgt[ii], channorm->chan_init[ii],
    141                      0, 511, forget_factor, ESTIMATE_PERIOD,
    142                      ESTIMATE_PERCENTILE, 10);
    143 #endif
    144     channorm->adjust[ii] = channorm->target[ii]
    145                            - channorm->init[ii];
    146   }
    147   channorm->adj_valid = False;
    148   return;
    149 }
    150 
    151 void clear_channel_normalization(spect_dist_info **chandata, int dimen)
    152 {
    153   int ii;
    154 
    155   ASSERT(chandata);
    156   for (ii = 0; ii < dimen; ii++)
    157     if (chandata[ii])
    158     {
    159       destroy_spectrum_distribution(chandata[ii]);
    160       chandata[ii] = NULL;
    161     }
    162   return;
    163 }
    164 
    165 void setup_ambient_estimation(spect_dist_info **backchan, int dimen,
    166                               int forget_factor)
    167 {
    168   int ii;
    169 
    170   ASSERT(backchan);
    171   for (ii = 0; ii < dimen; ii++)
    172     backchan[ii] = create_spectrum_distribution(
    173                      0, 0, 0, 255, forget_factor, BACK_ESTIMATE_PERIOD,
    174                      ESTIMATE_PERCENTILE, 10);
    175   return;
    176 }
    177 
    178 void clear_ambient_estimation(spect_dist_info **backchan, int dimen)
    179 {
    180   int ii;
    181 
    182   ASSERT(backchan);
    183 
    184   for (ii = 0; ii < dimen; ii++)
    185     if (backchan[ii])
    186     {
    187       destroy_spectrum_distribution(backchan[ii]);
    188       backchan[ii] = NULL;
    189     }
    190   return;
    191 }
    192 
    193