📄 hquant.c
字号:
mean[s] = CreateVector(&dStack,swidth[s]); SequenceMean(seq[s],mean[s]); if (trace&T_MEAN) { printf("Stream %d",s); ShowVector(" global mean:\n",mean[s],20); } switch(ck){ case INVDIAGC: case DIAGC: cov[s].var = CreateVector(&dStack,swidth[s]); SequenceCov(seq[s],ck,cov[s],mean[s]); if(trace&T_MEAN) ShowVector("Global variance",cov[s].var,20); break; case FULLC: cov[s].inv = CreateTriMat(&dStack,swidth[s]); SequenceCov(seq[s],ck,cov[s],mean[s]); if(trace&T_MEAN) ShowTriMat("Global covariance",cov[s].inv,20,20); break; case NULLC: default: cov[s].var = NULL; if(trace&T_MEAN) printf("Using Euclidean distance\n"); break; }}/* ClusterVecs: apply required clustering to vectors in stream s */void ClusterVecs(Sequence *seq, int s){ switch(tType) { case binTree: cs[s] = TreeCluster(&cStack,seq[s],cbSizes[s],ck,ck,cov[s]); break; case linTree: cs[s] = FlatCluster(&cStack,seq[s],cbSizes[s],ck,ck,cov[s]); break; default: HError(2590,"ClusterVecs: Unsupported tree type %d",tType); } if(trace&T_CLUST) ShowClusterSet(cs[s]);}/* ---------------- Procedures to construct a VQ table ------------ *//* AddLinEntries: Construct linear VQ table from cluster set */VQNode AddLinEntries(ClusterSet *cs, int s){ int i,rid; VQNode n = NULL, lastN = NULL; Cluster *c; for (i=1,c=cs->cl+1; i<=cs->numClust; i++,c++) { rid = (i==cs->numClust)?0:i+1; if( globClustVar ) n = CreateVQNode(i,i,0,rid,c->vCtr,ck,cov[s]); else n = CreateVQNode(i,i,0,rid,c->vCtr,ck,c->cov); n->right = lastN; lastN = n; } return(n);}/* AddBinEntries: Construct binary tree VQ table from cluster set */ VQNode AddBinEntries(ClusterSet *cs, short nid, int s){ Cluster *c; VQNode n; Covariance* cptr; /* parent is leaf node so end recursion */ if(nid > cs->numClust) return(NULL); /* Create this node */ c = cs->cl + nid; /* cluster corresponding to this node */ if( globClustVar ) /* Output global covariance */ cptr = &(cov[s]); else cptr = &(c->cov); if (nid > cs->numClust/2) /* if a leaf node */ n = CreateVQNode(nid-(cs->numClust/2),nid,0,0,c->vCtr,ck,*cptr); else n = CreateVQNode(0,nid,2*nid,2*nid + 1,c->vCtr,ck,*cptr); /* tree-structured clusters stored as follows: 1 2 3 4 5 6 7 . . . . . . . . so lid = 2*nid, rid = 2*nid+1, leaves have nid > numClust/2 */ /* recursively create children */ n->left = AddBinEntries(cs, 2*nid, s); n->right = AddBinEntries(cs, 2*nid+1, s); return(n);}/* WriteVQTable: Create VQ table in memory then write to file */void WriteVQTable(ClusterSet *cs[], char *fn){ int s; VQTable vq; vq = CreateVQTab(fn,(short)info.tgtPK,tType,ck,swidth); for (s=1;s<=swidth[0];s++){ if (cs[s]->isTree) vq->tree[s] = AddBinEntries(cs[s],1,s); else vq->tree[s] = AddLinEntries(cs[s],s); if (trace&T_TOP) printf("[%d] ",cs[s]->numClust); } StoreVQTab(vq,fn); if (trace&T_TOP) printf("entries -> %s\n",fn); if(trace & T_TAB) PrintVQTab(vq);}/* ------------------------ Initialisation ----------------------- *//* CheckStreamWidths: check that user-specified stream widths make sense */void CheckStreamWidths(BufferInfo info){ int s, sum = 0; for(s=1; s<=swidth[0];s++){ if(swidth[s] > info.tgtVecSize) HError(2530,"CheckStreamWidths: Specified stream %d width [%d] is wider than data [%d]", s,swidth[s], info.tgtVecSize); if(widthSet && swidth[s] == 0 ) HError(2530,"CheckStreamWidths: Width for stream %d must be specified",s); sum += swidth[s]; } if(sum > info.tgtVecSize) HError(2530,"CheckStreamWidths: Specified stream widths are wider than data [%d]", info.tgtVecSize); for(; s<SMAX; s++) { if(cbSizes[s] != DEF_NCLUST) HError(-2530,"CheckStreamWidths: Codebook size set for non-existent stream %d",s); }}/* Initialise: set up global data storage */void Initialise(char *datafn){ ParmBuf pbuf; int s; Boolean eSep; CreateHeap(&iStack,"inBuf", MSTAK, 1, 0.5, 100000, LONG_MAX); CreateHeap(&dStack,"seqStack", MSTAK, 1, 0.5, 100000, LONG_MAX); CreateHeap(&cStack,"clustStack",MSTAK, 1, 0.5, 100000, LONG_MAX); /* Peek at first data file to get observation format */ if((pbuf = OpenBuffer(&iStack, datafn, 0, UNDEFF, FALSE_dup, FALSE_dup))==NULL) HError(2550,"Initialise: Config parameters invalid"); GetBufferInfo(pbuf, &info); CloseBuffer(pbuf); ResetHeap(&iStack); /* set/validate stream widths */ if(swidth[0] > 0) CheckStreamWidths(info); else ZeroStreamWidths(1,swidth); /* Create an observation to hold the input parameters */ SetStreamWidths(info.tgtPK,info.tgtVecSize,swidth,&eSep); obs = MakeObservation(&gstack,swidth,info.tgtPK,FALSE,eSep); if (segLab != NULL) segId = GetLabId(segLab,TRUE); /* Create sequences to hold all data*/ for (s=1;s<=swidth[0];s++) dSeq[s] = CreateSequence(&dStack,4096);}/* ------------------------- Load Data ----------------------------- *//* CheckData: check data file consistent with already loaded data */void CheckData(char *fn, BufferInfo newInfo) { if (newInfo.tgtVecSize!=info.tgtVecSize) HError(2531,"CheckData: Wrong vector size in %s [%d], should be [%d]", fn,newInfo.tgtVecSize,info.tgtVecSize); if (newInfo.tgtPK != info.tgtPK) HError(2531,"CheckData: Parm kind of %s differs from data already read",fn);}/* LoadFile: load whole file or segments and accumulate variance */void LoadFile(char *fn){ ParmBuf pbuf; BufferInfo info; char labfn[80]; Transcription *trans; long segStIdx,segEnIdx; int i,s,j,ncas,nObs=0; LLink p; if (segId == NULL) { /* load whole parameter file */ if((pbuf=OpenBuffer(&iStack, fn, 0, dff, FALSE_dup, FALSE_dup))==NULL) HError(2550,"LoadFile: Config parameters invalid"); GetBufferInfo(pbuf,&info); CheckData(fn,info); nObs = ObsInBuffer(pbuf); for (i=0; i<nObs; i++) { for(s=1;s<=swidth[0];s++) obs.fv[s] = CreateVector(&dStack,swidth[s]); ReadAsTable(pbuf,i,&obs); for(s=1;s<=swidth[0];s++) StoreItem(dSeq[s],(Ptr)obs.fv[s]); } CloseBuffer(pbuf); } else { /* load segment of parameter file */ MakeFN(fn,labDir,labExt,labfn); trans = LOpen(&iStack,labfn,lff); ncas = NumCases(trans->head,segId); if ( ncas > 0) { if((pbuf=OpenBuffer(&iStack, fn, 0, dff, FALSE_dup, FALSE_dup))==NULL) HError(2550,"LoadFile: Config parameters invalid"); GetBufferInfo(pbuf,&info); CheckData(fn,info); for (i=1,nObs=0; i<=ncas; i++) { p = GetCase(trans->head,segId,i); segStIdx= (long) (p->start/info.tgtSampRate); segEnIdx = (long) (p->end/info.tgtSampRate); if (trace&T_SEGS) printf(" loading seg %s [%ld->%ld]\n", segId->name,segStIdx,segEnIdx); if (segEnIdx >= ObsInBuffer(pbuf)) segEnIdx = ObsInBuffer(pbuf)-1; if (segEnIdx >= segStIdx) { for (j=segStIdx;j<=segEnIdx;j++) { /* SJY: The HInit code I copied this from had no */ /* SJY: CreateVector call here -- a bug? */ for(s=1;s<=swidth[0];s++) obs.fv[s] = CreateVector(&dStack,swidth[s]); ReadAsTable(pbuf,j,&obs); for(s=1;s<=swidth[0];s++) StoreItem(dSeq[s],(Ptr)obs.fv[s]); ++nObs; } } } CloseBuffer(pbuf); } } ResetHeap(&iStack); if (trace&T_LOAD) { printf(" %5d obs loaded from %s, streams: ",nObs,fn); for(s=1;s<=swidth[0];s++) printf("[%d]" ,swidth[s]); printf("\n"); fflush(stdout); } }/* ----------------------------------------------------------- *//* END: HQuant.c *//* ----------------------------------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -