FE_io.cpp
上传用户:italyroyal
上传日期:2013-05-06
资源大小:473k
文件大小:3k
- ///////////////////////////////////////////////////////////////////////////////
- // This is a part of the Feature program.
- // Version: 1.0
- // Date: February 22, 2003
- // Programmer: Oh-Wook Kwon
- // Copyright(c) 2003 Oh-Wook Kwon. All rights reserved. owkwon@ucsd.edu
- ///////////////////////////////////////////////////////////////////////////////
- #include "StdAfx.h"
- #include "FE_feature.h"
- int Fe::ad_read(FILE *fp, short *buf, int n)
- {
- int num_data;
- if(!buf) return 0;
- num_data = FREAD(buf,sizeof(short),n,fp);
- return(num_data);
- }
- int Fe::ad_write(FILE *fp, short *buf, int n)
- {
- int num_data;
- if(!buf) return 0;
- num_data = FWRITE(buf,sizeof(short),n,fp);
- return(num_data);
- }
- int Fe::read_feature_basic(FeMatrix<float>& feat, int dim, FILE *fi)
- {
- int l;
- if(feat.size()==0) return 0;
- for (l=0; ; l++){
- if(FREAD(&feat[l][0], sizeof(float), dim, fi)==0)
- return(l);
- }
- return l;
- }
- int Fe::write_feature_basic(FeMatrix<float>& feat, int num_frames, int dim, FILE *fo)
- {
- if(feat.size()==0) return 0;
- for (int l=0; l < num_frames; l++){
- FWRITE(&feat[l][0], sizeof(float), dim, fo);
- }
- return 1;
- }
- int Fe::write_feature_vectors(FILE *fo, FeMatrix<float>& feat, int* pTag, const char* featname)
- {
- int num_frames = feat.size();
- if(num_frames == 0) return 0;
- int dim = feat[0].size();
- int nsave = dim;
- if(pTag){
- nsave = 0;
- for(int i=0;i<dim;i++)
- if(pTag[i]) nsave++;
- }
- fprintf(fo,"#DATA %sn", ((featname) ? featname : ""));
- for (int n=0; n<num_frames; n++){
- fprintf(fo, "%f %d ",(float)(n*m_shiftSizeMs/1000.0),nsave);
- for (int j=0; j<dim; j++) {
- if(pTag && pTag[j]) fprintf(fo, " %f ",feat[n][j]);
- }
- fprintf(fo, "n");
- }
- return(1);
- }
- int Fe::FREAD(void *ptr, int size, int nitems, FILE *stream)
- {
- assert(size==1 || size==2 || size==4 || size==8);
- char c,*p,*q;
- int i,j,n;
- n=fread(ptr,size,nitems,stream);
- if(m_swapByte){
- for(i=0;i<size/2;i++){
- for(j=0,p=(char*)ptr+i,q=(char*)ptr+(size-1-i);j<n;j++,p+=size,q+=size){
- c=(*p);
- (*p)=(*q);
- (*q)=c;
- }
- }
- }
- return(n);
- }
- int Fe::FWRITE(void *ptr, int size, int nitems, FILE *stream)
- {
- assert(size==1 || size==2 || size==4 || size==8);
- char c,*p,*q;
- int i,j,n;
- if(m_swapByte){
- vector<char> tmp;
- tmp.resize(size*nitems);
- for(i=0,p=(char*)ptr;i<(size*nitems);i++,p++) tmp[i]=(*p);
- for(i=0;i<size/2;i++){
- for(j=0,p=&tmp[0]+i,q=&tmp[0]+(size-1-i);j<nitems;j++,p+=size,q+=size){
- c=(*p);
- (*p)=(*q);
- (*q)=c;
- }
- }
- n=fwrite(&tmp[0],size,nitems,stream);
- }
- else{
- n=fwrite(ptr,size,nitems,stream);
- }
- return(n);
- }