SphinxBase  0.6
lda.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  * lda.c -- Read and apply LDA matrices to features.
39  *
40  * Author: David Huggins-Daines <dhuggins@cs.cmu.edu>
41  */
42 
43 #include <assert.h>
44 #include <string.h>
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48 
49 #ifdef _MSC_VER
50 #pragma warning (disable: 4018)
51 #endif
52 
53 #include "sphinxbase/feat.h"
54 #include "sphinxbase/ckd_alloc.h"
55 #include "sphinxbase/bio.h"
56 #include "sphinxbase/err.h"
57 
58 #define MATRIX_FILE_VERSION "0.1"
59 
60 int32
61 feat_read_lda(feat_t *feat, const char *ldafile, int32 dim)
62 {
63  FILE *fh;
64  int32 byteswap, chksum_present;
65  uint32 chksum, i, m, n;
66  char **argname, **argval;
67 
68  assert(feat);
69  if (feat->n_stream != 1) {
70  E_ERROR("LDA incompatible with multi-stream features (n_stream = %d)\n",
71  feat->n_stream);
72  return -1;
73  }
74 
75  if ((fh = fopen(ldafile, "rb")) == NULL) {
76  E_ERROR_SYSTEM("Failed to open transform file '%s' for reading: %s\n", ldafile, strerror(errno));
77  return -1;
78  }
79 
80  if (bio_readhdr(fh, &argname, &argval, &byteswap) < 0) {
81  E_ERROR("Failed to read header from transform file '%s'\n", ldafile);
82  fclose(fh);
83  return -1;
84  }
85 
86  chksum_present = 0;
87  for (i = 0; argname[i]; i++) {
88  if (strcmp(argname[i], "version") == 0) {
89  if (strcmp(argval[i], MATRIX_FILE_VERSION) != 0)
90  E_WARN("%s: Version mismatch: %s, expecting %s\n",
91  ldafile, argval[i], MATRIX_FILE_VERSION);
92  }
93  else if (strcmp(argname[i], "chksum0") == 0) {
94  chksum_present = 1; /* Ignore the associated value */
95  }
96  }
97 
98  bio_hdrarg_free(argname, argval);
99  argname = argval = NULL;
100 
101  chksum = 0;
102 
103  if (feat->lda)
104  ckd_free_3d((void ***)feat->lda);
105 
106  {
107  /* Use a temporary variable to avoid strict-aliasing problems. */
108  void ***outlda;
109 
110  if (bio_fread_3d(&outlda, sizeof(float32),
111  &feat->n_lda, &m, &n,
112  fh, byteswap, &chksum) < 0) {
113  E_ERROR_SYSTEM("%s: bio_fread_3d(lda) failed\n", ldafile);
114  fclose(fh);
115  return -1;
116  }
117  feat->lda = (void *)outlda;
118  }
119  fclose(fh);
120 
121 #ifdef FIXED_POINT
122  /* FIXME: This is a fragile hack that depends on mfcc_t and
123  * float32 being the same size (which they are, but...) */
124  for (i = 0; i < feat->n_lda * m * n; ++i) {
125  feat->lda[0][0][i] = FLOAT2MFCC(((float *)feat->lda[0][0])[i]);
126  }
127 #endif
128 
129  /* Note that SphinxTrain stores the eigenvectors as row vectors. */
130  if (n != feat->stream_len[0])
131  E_FATAL("LDA matrix dimension %d doesn't match feature stream size %d\n", n, feat->stream_len[0]);
132 
133  /* Override dim from file if it is 0 or greater than m. */
134  if (dim > m || dim <= 0) {
135  dim = m;
136  }
137  feat->out_dim = dim;
138 
139  return 0;
140 }
141 
142 void
143 feat_lda_transform(feat_t *fcb, mfcc_t ***inout_feat, uint32 nfr)
144 {
145  mfcc_t *tmp;
146  uint32 i, j, k;
147 
148  tmp = ckd_calloc(fcb->stream_len[0], sizeof(mfcc_t));
149  for (i = 0; i < nfr; ++i) {
150  /* Do the matrix multiplication inline here since fcb->lda
151  * is transposed (eigenvectors in rows not columns). */
152  /* FIXME: In the future we ought to use the BLAS. */
153  memset(tmp, 0, sizeof(mfcc_t) * fcb->stream_len[0]);
154  for (j = 0; j < feat_dimension(fcb); ++j) {
155  for (k = 0; k < fcb->stream_len[0]; ++k) {
156  tmp[j] += MFCCMUL(inout_feat[i][0][k], fcb->lda[0][j][k]);
157  }
158  }
159  memcpy(inout_feat[i][0], tmp, fcb->stream_len[0] * sizeof(mfcc_t));
160  }
161  ckd_free(tmp);
162 }