\doxysection{lda.\+c}
\label{lda_8c_source}\index{src/libsphinxbase/feat/lda.c@{src/libsphinxbase/feat/lda.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 2006\ Carnegie\ Mellon\ University.\ \ All\ rights}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ \textcolor{comment}{/*}}
\DoxyCodeLine{00038\ \textcolor{comment}{\ *\ lda.c\ -\/-\/\ Read\ and\ apply\ LDA\ matrices\ to\ features.}}
\DoxyCodeLine{00039\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00040\ \textcolor{comment}{\ *\ Author:\ David\ Huggins-\/Daines\ <dhuggins@cs.cmu.edu>}}
\DoxyCodeLine{00041\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00042\ }
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ <assert.h>}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00045\ \textcolor{preprocessor}{\#ifdef\ HAVE\_CONFIG\_H}}
\DoxyCodeLine{00046\ \textcolor{preprocessor}{\#include\ <config.h>}}
\DoxyCodeLine{00047\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00048\ }
\DoxyCodeLine{00049\ \textcolor{preprocessor}{\#ifdef\ \_MSC\_VER}}
\DoxyCodeLine{00050\ \textcolor{preprocessor}{\#pragma\ warning\ (disable:\ 4018)}}
\DoxyCodeLine{00051\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00052\ }
\DoxyCodeLine{00053\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/feat.h"{}}}
\DoxyCodeLine{00054\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00055\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/bio.h"{}}}
\DoxyCodeLine{00056\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/err.h"{}}}
\DoxyCodeLine{00057\ }
\DoxyCodeLine{00058\ \textcolor{preprocessor}{\#define\ MATRIX\_FILE\_VERSION\ "{}0.1"{}}}
\DoxyCodeLine{00059\ }
\DoxyCodeLine{00060\ int32}
\DoxyCodeLine{00061\ feat\_read\_lda(feat\_t\ *feat,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *ldafile,\ int32\ dim)}
\DoxyCodeLine{00062\ \{}
\DoxyCodeLine{00063\ \ \ \ \ FILE\ *fh;}
\DoxyCodeLine{00064\ \ \ \ \ int32\ byteswap;}
\DoxyCodeLine{00065\ \ \ \ \ uint32\ chksum,\ i,\ m,\ n;}
\DoxyCodeLine{00066\ \ \ \ \ \textcolor{keywordtype}{char}\ **argname,\ **argval;}
\DoxyCodeLine{00067\ }
\DoxyCodeLine{00068\ \ \ \ \ assert(feat);}
\DoxyCodeLine{00069\ \ \ \ \ \textcolor{keywordflow}{if}\ (feat-\/>n\_stream\ !=\ 1)\ \{}
\DoxyCodeLine{00070\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}LDA\ incompatible\ with\ multi-\/stream\ features\ (n\_stream\ =\ \%d)\(\backslash\)n"{}},}
\DoxyCodeLine{00071\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ feat-\/>n\_stream);}
\DoxyCodeLine{00072\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00073\ \ \ \ \ \}}
\DoxyCodeLine{00074\ }
\DoxyCodeLine{00075\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fh\ =\ fopen(ldafile,\ \textcolor{stringliteral}{"{}rb"{}}))\ ==\ NULL)\ \{}
\DoxyCodeLine{00076\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ open\ transform\ file\ '\%s'\ for\ reading"{}},\ ldafile);}
\DoxyCodeLine{00077\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00078\ \ \ \ \ \}}
\DoxyCodeLine{00079\ }
\DoxyCodeLine{00080\ \ \ \ \ \textcolor{keywordflow}{if}\ (bio\_readhdr(fh,\ \&argname,\ \&argval,\ \&byteswap)\ <\ 0)\ \{}
\DoxyCodeLine{00081\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ read\ header\ from\ transform\ file\ '\%s'\(\backslash\)n"{}},\ ldafile);}
\DoxyCodeLine{00082\ \ \ \ \ \ \ \ \ fclose(fh);}
\DoxyCodeLine{00083\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00084\ \ \ \ \ \}}
\DoxyCodeLine{00085\ }
\DoxyCodeLine{00086\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ argname[i];\ i++)\ \{}
\DoxyCodeLine{00087\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (strcmp(argname[i],\ \textcolor{stringliteral}{"{}version"{}})\ ==\ 0)\ \{}
\DoxyCodeLine{00088\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (strcmp(argval[i],\ MATRIX\_FILE\_VERSION)\ !=\ 0)}
\DoxyCodeLine{00089\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_WARN(\textcolor{stringliteral}{"{}\%s:\ Version\ mismatch:\ \%s,\ expecting\ \%s\(\backslash\)n"{}},}
\DoxyCodeLine{00090\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ldafile,\ argval[i],\ MATRIX\_FILE\_VERSION);}
\DoxyCodeLine{00091\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00092\ \ \ \ \ \}}
\DoxyCodeLine{00093\ }
\DoxyCodeLine{00094\ \ \ \ \ bio\_hdrarg\_free(argname,\ argval);}
\DoxyCodeLine{00095\ \ \ \ \ argname\ =\ argval\ =\ NULL;}
\DoxyCodeLine{00096\ }
\DoxyCodeLine{00097\ \ \ \ \ chksum\ =\ 0;}
\DoxyCodeLine{00098\ }
\DoxyCodeLine{00099\ \ \ \ \ \textcolor{keywordflow}{if}\ (feat-\/>lda)}
\DoxyCodeLine{00100\ \ \ \ \ \ \ \ \ ckd\_free\_3d((\textcolor{keywordtype}{void}\ ***)feat-\/>lda);}
\DoxyCodeLine{00101\ }
\DoxyCodeLine{00102\ \ \ \ \ \{}
\DoxyCodeLine{00103\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Use\ a\ temporary\ variable\ to\ avoid\ strict-\/aliasing\ problems.\ */}}
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{void}\ ***outlda;}
\DoxyCodeLine{00105\ }
\DoxyCodeLine{00106\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (bio\_fread\_3d(\&outlda,\ \textcolor{keyword}{sizeof}(float32),}
\DoxyCodeLine{00107\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \&feat-\/>n\_lda,\ \&m,\ \&n,}
\DoxyCodeLine{00108\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fh,\ byteswap,\ \&chksum)\ <\ 0)\ \{}
\DoxyCodeLine{00109\ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}\%s:\ bio\_fread\_3d(lda)\ failed\(\backslash\)n"{}},\ ldafile);}
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ \ \ \ \ fclose(fh);}
\DoxyCodeLine{00111\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00112\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00113\ \ \ \ \ \ \ \ \ feat-\/>lda\ =\ (\textcolor{keywordtype}{void}\ *)outlda;}
\DoxyCodeLine{00114\ \ \ \ \ \}}
\DoxyCodeLine{00115\ \ \ \ \ fclose(fh);}
\DoxyCodeLine{00116\ \ \ \ \ }
\DoxyCodeLine{00117\ \textcolor{preprocessor}{\#ifdef\ FIXED\_POINT}}
\DoxyCodeLine{00118\ \ \ \ \ \textcolor{comment}{/*\ FIXME:\ This\ is\ a\ fragile\ hack\ that\ depends\ on\ mfcc\_t\ and}}
\DoxyCodeLine{00119\ \textcolor{comment}{\ \ \ \ \ *\ float32\ being\ the\ same\ size\ (which\ they\ are,\ but...)\ */}}
\DoxyCodeLine{00120\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ feat-\/>n\_lda\ *\ m\ *\ n;\ ++i)\ \{}
\DoxyCodeLine{00121\ \ \ \ \ \ \ \ \ feat-\/>lda[0][0][i]\ =\ FLOAT2MFCC(((\textcolor{keywordtype}{float}\ *)feat-\/>lda[0][0])[i]);}
\DoxyCodeLine{00122\ \ \ \ \ \}}
\DoxyCodeLine{00123\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00124\ }
\DoxyCodeLine{00125\ \ \ \ \ \textcolor{comment}{/*\ Note\ that\ SphinxTrain\ stores\ the\ eigenvectors\ as\ row\ vectors.\ */}}
\DoxyCodeLine{00126\ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ !=\ feat-\/>stream\_len[0])}
\DoxyCodeLine{00127\ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}LDA\ matrix\ dimension\ \%d\ doesn't\ match\ feature\ stream\ size\ \%d\(\backslash\)n"{}},\ n,\ feat-\/>stream\_len[0]);}
\DoxyCodeLine{00128\ \ \ \ \ }
\DoxyCodeLine{00129\ \ \ \ \ \textcolor{comment}{/*\ Override\ dim\ from\ file\ if\ it\ is\ 0\ or\ greater\ than\ m.\ */}}
\DoxyCodeLine{00130\ \ \ \ \ \textcolor{keywordflow}{if}\ (dim\ >\ m\ ||\ dim\ <=\ 0)\ \{}
\DoxyCodeLine{00131\ \ \ \ \ \ \ \ \ dim\ =\ m;}
\DoxyCodeLine{00132\ \ \ \ \ \}}
\DoxyCodeLine{00133\ \ \ \ \ feat-\/>out\_dim\ =\ dim;}
\DoxyCodeLine{00134\ }
\DoxyCodeLine{00135\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00136\ \}}
\DoxyCodeLine{00137\ }
\DoxyCodeLine{00138\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00139\ feat\_lda\_transform(feat\_t\ *fcb,\ mfcc\_t\ ***inout\_feat,\ uint32\ nfr)}
\DoxyCodeLine{00140\ \{}
\DoxyCodeLine{00141\ \ \ \ \ mfcc\_t\ *tmp;}
\DoxyCodeLine{00142\ \ \ \ \ uint32\ i,\ j,\ k;}
\DoxyCodeLine{00143\ }
\DoxyCodeLine{00144\ \ \ \ \ tmp\ =\ ckd\_calloc(fcb-\/>stream\_len[0],\ \textcolor{keyword}{sizeof}(mfcc\_t));}
\DoxyCodeLine{00145\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ nfr;\ ++i)\ \{}
\DoxyCodeLine{00146\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Do\ the\ matrix\ multiplication\ inline\ here\ since\ fcb-\/>lda}}
\DoxyCodeLine{00147\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ is\ transposed\ (eigenvectors\ in\ rows\ not\ columns).\ */}}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ FIXME:\ In\ the\ future\ we\ ought\ to\ use\ the\ BLAS.\ */}}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ memset(tmp,\ 0,\ \textcolor{keyword}{sizeof}(mfcc\_t)\ *\ fcb-\/>stream\_len[0]);}
\DoxyCodeLine{00150\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (j\ =\ 0;\ j\ <\ feat\_dimension(fcb);\ ++j)\ \{}
\DoxyCodeLine{00151\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (k\ =\ 0;\ k\ <\ fcb-\/>stream\_len[0];\ ++k)\ \{}
\DoxyCodeLine{00152\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tmp[j]\ +=\ MFCCMUL(inout\_feat[i][0][k],\ fcb-\/>lda[0][j][k]);}
\DoxyCodeLine{00153\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00154\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00155\ \ \ \ \ \ \ \ \ memcpy(inout\_feat[i][0],\ tmp,\ fcb-\/>stream\_len[0]\ *\ \textcolor{keyword}{sizeof}(mfcc\_t));}
\DoxyCodeLine{00156\ \ \ \ \ \}}
\DoxyCodeLine{00157\ \ \ \ \ ckd\_free(tmp);}
\DoxyCodeLine{00158\ \}}

\end{DoxyCode}
