⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp_book.c

📁 LastWave
💻 C
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------------*/void AddMolecule2Book(BOOK book, MOLECULE molecule){  /* checking the inputs */  CheckBook(book);  CheckMoleculeNotEmpty(molecule);  CheckTFContentCompat(book,GetMoleculeAtom(molecule,0,0));  // Case where we have to resize the book  if(book->size == book->sizeAlloc) {    if(book->sizeAlloc==0) SizeBook(book,MP_DEFAULT_BOOK_SIZE);    else SizeBook(book,2*book->sizeAlloc); // Nota : it may be a bad strategy to double at each time !  }    // Now we can append the molecule  book->molecules[book->size] = molecule;  book->size++;}void DeleteMoleculeFromBook(BOOK book,unsigned long rank) {  unsigned long n;  CheckBookNotEmpty(book);  if(!INRANGE(0,rank,book->size-1)) Errorf("DeleteMoleculeFromBook : rank %d is not in range [0 %d]",book->size-1);  book->molecules[rank] = DeleteMolecule(book->molecules[rank]);  // Piles up molecules to the left of the array  // TODO : replace the pile up with  // book->molecules[rank] = book->molecules[book->size-1]  // book->molecules[book->size-1] = NULL;  // book->size--;  for(n = rank; n < book->size-1; n++) {    book->molecules[n] = book->molecules[n+1];  }  book->molecules[book->size-1] = NULL;  book->size--;  // Note that we do NOT re-allocate to a smaller size}/* Get the n-th molecule , 0 <= n <= size-1 */MOLECULE GetBookMolecule(const BOOK book,unsigned long n){  // Some checkings  CheckBookNotEmpty(book);  if(n>=book->size)  Errorf("GetBookMolecule : Bad molecule number %d [%d %d]",n,0,book->size-1);  return(book->molecules[n]);}	/* ALLOCATION */void CheckBook(const BOOK book){  CheckTFContent(book);}void CheckBookNotEmpty(const BOOK book){  CheckBook(book);  if(book->size == 0)    Errorf("CheckBookNotEmpty : empty book (run the pursuit first)");}/* * The fields of a book */static char* nameDoc = "{[= <name>]} {Sets/Gets the name of a book}";static char* sizeDoc = "{} {Gets the number of &mol in a book.}";static char* sizeAllocDoc = "{[= <sizeAlloc>]} {Sets/Gets the allocation size for the array of &mol in a book. In case of a Set, <sizeAlloc> must be larger than book.size, else an error is generated. The previously allocated part is kept (book.size is not changed).}";/* * 'name' field */static void * GetNameBookV(BOOK book, void **arg){  /* Documentation */  if (book == NULL) return(nameDoc);  return(GetStrField(book->name,arg));}static void * SetNameBookV(BOOK book, void **arg){  /* doc */  if (book == NULL) return(nameDoc);  if (book->name==defaultName || book->name == NULL) {    book->name=CharAlloc(1);    book->name[0] = '\0';  }  return(SetStrField(&(book->name),arg));}  static void *GetSizeBookV(BOOK book, void **arg){  char *field = ARG_G_GetField(arg);    /* Documentation */  if (book == NULL) {    if(!strcmp(field,"size"))      return(sizeDoc);    if(!strcmp(field,"sizeAlloc")) return(sizeAllocDoc);  }  if(!strcmp(field,"size"))        return(GetIntField(book->size,arg));  if(!strcmp(field,"sizeAlloc"))   return(GetIntField(book->sizeAlloc,arg));}static void * SetSizeAllocBookV(BOOK book, void **arg){  int sizeAlloc;  /* doc */  if (book == NULL) return(sizeAllocDoc);  // Init for += syntax  sizeAlloc = book->sizeAlloc;  if(SetIntField(&sizeAlloc,arg,FieldSPositive)==NULL) return(NULL);  SizeBook(book,sizeAlloc);}  /* 'tfContent' parameters */static char *dxDoc = "{[= <dx>]} {Sets/Gets the abscissa step of the original signal of the book and all its atoms.}";static char *x0Doc = "{[= <x0>]} {Sets/Gets the first abscissa of the original signal of the book and all its atoms.}";void *SetDxBookV(BOOK book, void **arg){  unsigned long n;  MOLECULE molecule;  unsigned short k;  unsigned char channel;  ATOM atom;  /* Documentation */  if (book == NULL) return(dxDoc);  if(SetFloatField(&(book->dx),arg,FieldSPositive)==NULL) return(NULL);  /* Update the atoms dx */  for(n = 0; n < book->size; n++) {    molecule = GetBookMolecule(book,n);    for(channel = 0; channel < molecule->nChannels; channel++) {      for(k = 0; k < molecule->dim; k++) {	atom = GetMoleculeAtom(molecule,channel,k);	atom->dx = book->dx;      }    }  }  return(numType);} void *SetX0BookV(BOOK book, void **arg){  unsigned long n;  MOLECULE molecule;  unsigned short k;  unsigned char channel;  ATOM atom;  /* Documentation */  if (book == NULL) return(x0Doc);  if(SetFloatField(&(book->x0),arg,0)==NULL) return(NULL);  /* Update the atoms x0 */  for(n = 0; n < book->size; n++) {    molecule = GetBookMolecule(book,n);    for(channel = 0; channel < molecule->nChannels; channel++) {      for(k = 0; k < molecule->dim; k++) {	atom = GetMoleculeAtom(molecule,channel,k);	atom->x0 = book->x0;      }    }  }  return(numType);}/* * The extraction of signals that contain the fields of the molecules of a book */ static char* dimDoc        = "{} {Gets a &signal of size book.size containing the list of dimensions  of the molecules of a book (i.e. the number of atoms contained in each molecule). The dimension is larger than 1 only for books built using the Harmonic Matching Pursuit.}";static char* wcoeff2Doc    = "{} {Gets a &signal of size book.size containing the list of 'coeff2' of the molecule in a book.}";static char* windowSizeDoc = "{} {Gets a &signal of size book.size containing the list of 'windowSize' of the first atom of the molecules in a book.}";static char* windowShapeDoc = "{} {Gets a &listv of size book.size containing the list of 'windowShape' of the first atom of the molecules of a book."WindowShapeHelpString"}";static char* timeIdDoc     = "{} {Gets a &signal of size book.size containing the list of 'timeId' of the first atom of the molecules in a book..}";static char* timeDoc       = "{} {Gets a &signal of size book.size containing the list of 'time' of the first atom of the molecules in a book.}";static char* freqIdDoc     = "{} {Gets a &signal of size book.size containing the list of 'freqId' of the first atom of the molecules in a book.}";static char* freqDoc       = "{} {Gets a &signal of size book.size containing the list of 'freq' of the first atom of the molecules in a book.}";static char* chirpIdDoc    = "{} {Gets a &signal of size book.size containing the list of 'chirpId' of the first atom of the molecules in a book.}";static char* chirpDoc      = "{} {Gets a &signal of size book.size containing the list of 'chirp' of the first atom of the molecules in a book.}";static char* phaseDoc      = "{} {Gets a &signal of size book.size containing the list of 'phase' of the first atom of the molecules in a book.}";static char* acoeff2Doc    = "{} {Gets a &signal of size book.size containing the list of 'coeff2' of the first atom of the molecules in a book.}";static char* ggDoc         = "{} {Gets a &listv {real imag} of two &signal of size book.size containing the list of 'gg' of the first atom of the molecules in a book.}";void *GetMoleculeFieldBookV(BOOK book, void **arg){  char *field = ARG_G_GetField(arg);  SIGNAL signal = NULL;  SIGNAL signalIm = NULL;  LISTV lv = NULL;  unsigned long n;  ATOM atom;  LWFLOAT phase;  /* Documentation */  if (book == NULL) {    if(!strcmp(field,"dim")) return(dimDoc);    if(!strcmp(field,"wcoeff2")) return(wcoeff2Doc);    if(!strcmp(field,"windowSize")) return(windowSizeDoc);    if(!strcmp(field,"windowShape")) return(windowShapeDoc);    if(!strcmp(field,"timeId")) return(timeIdDoc);    if(!strcmp(field,"time")) return(timeDoc);    if(!strcmp(field,"freqId")) return(freqIdDoc);    if(!strcmp(field,"freq")) return(freqDoc);    if(!strcmp(field,"chirpId")) return(chirpIdDoc);    if(!strcmp(field,"chirp")) return(chirpDoc);    if(!strcmp(field,"phase")) return(phaseDoc);    if(!strcmp(field,"acoeff2")) return(acoeff2Doc);    if(!strcmp(field,"gg")) return(ggDoc);    Errorf("Weired : GetMoleculeFieldBooV : unknown field %s",field);  }  if(!strcmp(field,"windowShape")) {    lv = TNewListv();    for(n = 0; n < book->size; n++) {      AppendStr2Listv(lv,WindowShape2Name(GetMoleculeAtom(GetBookMolecule(book,n),0,0)->windowShape));    }    return(GetValueField(lv,arg));  }  signal = TNewSignal();  SizeSignal(signal,book->size,YSIG);  signal->dx = 1;  signal->x0 = 0;  if(!strcmp(field,"dim")) {    for(n = 0; n < book->size; n++) {      signal->Y[n] = GetBookMolecule(book,n)->dim;    }  }  if(!strcmp(field,"wcoeff2")) {    for(n = 0; n < book->size; n++) {      signal->Y[n] = GetBookMolecule(book,n)->coeff2;    }  }  if(!strcmp(field,"windowSize")) {    for(n = 0; n < book->size; n++) {      atom = GetMoleculeAtom(GetBookMolecule(book,n),0,0);      signal->Y[n] = atom->windowSize;    }  }  if(!strcmp(field,"timeId")) {    for(n = 0; n < book->size; n++) {      atom = GetMoleculeAtom(GetBookMolecule(book,n),0,0);       signal->Y[n] = atom->timeId;    }  }  if(!strcmp(field,"time")) {    for(n = 0; n < book->size; n++) {      atom = GetMoleculeAtom(GetBookMolecule(book,n),0,0);       signal->Y[n] = TimeId2Time(book,atom->timeId);    }  }  if(!strcmp(field,"freqId")) {    for(n = 0; n < book->size; n++) {      atom = GetMoleculeAtom(GetBookMolecule(book,n),0,0);       signal->Y[n] = atom->freqId;    }  }  if(!strcmp(field,"freq")) {    for(n = 0; n < book->size; n++) {      atom = GetMoleculeAtom(GetBookMolecule(book,n),0,0);       signal->Y[n] = FreqId2Freq(book,atom->freqId);    }  }  if(!strcmp(field,"chirpId")) {    for(n = 0; n < book->size; n++) {      atom = GetMoleculeAtom(GetBookMolecule(book,n),0,0);       signal->Y[n] = atom->chirpId;    }  }  if(!strcmp(field,"chirp")) {    for(n = 0; n < book->size; n++) {      atom = GetMoleculeAtom(GetBookMolecule(book,n),0,0);       signal->Y[n] = ChirpId2Chirp(book,atom->chirpId);    }  }  if(!strcmp(field,"phase")) {    for(n = 0; n < book->size; n++) {      atom = GetMoleculeAtom(GetBookMolecule(book,n),0,0);       phase = atan2(atom->sinPhase,atom->cosPhase)/(2*M_PI);      if (phase<0)     phase = 1+phase;      if(phase >= 1.0) phase = phase-1.0;      signal->Y[n] = phase;    }  }  if(!strcmp(field,"acoeff2")) {    for(n = 0; n < book->size; n++) {      atom = GetMoleculeAtom(GetBookMolecule(book,n),0,0);       signal->Y[n] = atom->coeff2;    }  }  if(!strcmp(field,"gg")) {    signalIm = TNewSignal();    SizeSignal(signalIm,book->size,YSIG);    signalIm->dx = 1;    signalIm->x0 = 0;    for(n = 0; n < book->size; n++) {      atom = GetMoleculeAtom(GetBookMolecule(book,n),0,0);       signal->Y[n] = atom->realGG;      signalIm->Y[n] = atom->imagGG;    }    lv = TNewListv();    AppendValue2Listv(lv,(VALUE)signal);    AppendValue2Listv(lv,(VALUE)signalIm);    return(GetValueField(lv,arg));  }  return(GetValueField(signal,arg));}  /* * The field list */struct field fieldsBook[] = {  "",GetBookV,SetBookV,GetOptionsBookV,GetExtractInfoBookV,    "sig",GetBookV,SetBookV,GetOptionsBookV,GetExtractInfoBookV,    "name",GetNameBookV,SetNameBookV,NULL,NULL,  // The TFContent fields  "dx",GetDxTFContentV,SetDxBookV,NULL,NULL,  "x0",GetX0TFContentV,SetX0BookV,NULL,NULL,  "signalSize",GetSignalSizeTFContentV,NULL,NULL,NULL,  "freqIdNyquist",GetFreqIdNyquistTFContentV,NULL,NULL,NULL,  // The array fields  "size",GetSizeBookV,NULL,NULL,NULL,  "sizeAlloc",GetSizeBookV,SetSizeAllocBookV,NULL,NULL,  // The molecule fields  "dim",GetMoleculeFieldBookV,NULL,NULL,NULL,  "wcoeff2",GetMoleculeFieldBookV,NULL,NULL,NULL,  "windowSize",GetMoleculeFieldBookV,NULL,NULL,NULL,  "windowShape",GetMoleculeFieldBookV,NULL,NULL,NULL,  "timeId",GetMoleculeFieldBookV,NULL,NULL,NULL,  "time",GetMoleculeFieldBookV,NULL,NULL,NULL,  "freqId",GetMoleculeFieldBookV,NULL,NULL,NULL,  "freq",GetMoleculeFieldBookV,NULL,NULL,NULL,  "chirpId",GetMoleculeFieldBookV,NULL,NULL,NULL,  "chirp",GetMoleculeFieldBookV,NULL,NULL,NULL,  "acoeff2",GetMoleculeFieldBookV,NULL,NULL,NULL,  "phase",GetMoleculeFieldBookV,NULL,NULL,NULL,#ifdef ATOM_ADVANCED  "gg",GetMoleculeFieldBookV,NULL,NULL,NULL,#endif // ATOM_ADVANCED  NULL, NULL, NULL, NULL, NULL};/* * The type structure for BOOK */TypeStruct tsBook = {  "{{{&book} {This type is the basic type for storing the result of Matching Pursuit decompositions as an array of &mol's. \n \- Operator + : book+molecule, appends a molecule at the end of the book.}}}",  /* Documentation */  &bookType,       /* The basic (unique) type name */  NULL,     /* The GetType function */                           DeleteBook,     /* The Delete function */  NewBook,     /* The New function */    CopyBook,       /* The copy function */  ClearBook,       /* The clear function */    ToStrBook,       /* String conversion */  ShortPrintBook,   /* The Print function : print the object when 'print' is called */  PrintInfoBook,   /* The PrintInfo function : called by 'info' */  NumExtractBook,              /* The NumExtract function : used to deal with syntax like 10a */     fieldsBook,      /* The list of fields */};/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -