Debian Codegraph @

src/libsphinxbase/feat/lda.c

raw
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 <[email protected]>
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;
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", ldafile);
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 for (i = 0; argname[i]; i++) {
87 if (strcmp(argname[i], "version") == 0) {
88 if (strcmp(argval[i], MATRIX_FILE_VERSION) != 0)
89 E_WARN("%s: Version mismatch: %s, expecting %s\n",
90 ldafile, argval[i], MATRIX_FILE_VERSION);
91 }
92 }
93
94 bio_hdrarg_free(argname, argval);
95 argname = argval = NULL;
96
97 chksum = 0;
98
99 if (feat->lda)
100 ckd_free_3d((void ***)feat->lda);
101
102 {
103 /* Use a temporary variable to avoid strict-aliasing problems. */
104 void ***outlda;
105
106 if (bio_fread_3d(&outlda, sizeof(float32),
107 &feat->n_lda, &m, &n,
108 fh, byteswap, &chksum) < 0) {
109 E_ERROR_SYSTEM("%s: bio_fread_3d(lda) failed\n", ldafile);
110 fclose(fh);
111 return -1;
112 }
113 feat->lda = (void *)outlda;
114 }
115 fclose(fh);
116
117 #ifdef FIXED_POINT
118 /* FIXME: This is a fragile hack that depends on mfcc_t and
119 * float32 being the same size (which they are, but...) */
120 for (i = 0; i < feat->n_lda * m * n; ++i) {
121 feat->lda[0][0][i] = FLOAT2MFCC(((float *)feat->lda[0][0])[i]);
122 }
123 #endif
124
125 /* Note that SphinxTrain stores the eigenvectors as row vectors. */
126 if (n != feat->stream_len[0])
127 E_FATAL("LDA matrix dimension %d doesn't match feature stream size %d\n", n, feat->stream_len[0]);
128
129 /* Override dim from file if it is 0 or greater than m. */
130 if (dim > m || dim <= 0) {
131 dim = m;
132 }
133 feat->out_dim = dim;
134
135 return 0;
136 }
137
138 void
139 feat_lda_transform(feat_t *fcb, mfcc_t ***inout_feat, uint32 nfr)
140 {
141 mfcc_t *tmp;
142 uint32 i, j, k;
143
144 tmp = ckd_calloc(fcb->stream_len[0], sizeof(mfcc_t));
145 for (i = 0; i < nfr; ++i) {
146 /* Do the matrix multiplication inline here since fcb->lda
147 * is transposed (eigenvectors in rows not columns). */
148 /* FIXME: In the future we ought to use the BLAS. */
149 memset(tmp, 0, sizeof(mfcc_t) * fcb->stream_len[0]);
150 for (j = 0; j < feat_dimension(fcb); ++j) {
151 for (k = 0; k < fcb->stream_len[0]; ++k) {
152 tmp[j] += MFCCMUL(inout_feat[i][0][k], fcb->lda[0][j][k]);
153 }
154 }
155 memcpy(inout_feat[i][0], tmp, fcb->stream_len[0] * sizeof(mfcc_t));
156 }
157 ckd_free(tmp);
158 }