📄 boxcount.c
字号:
/* Author: Rainer Hegger Last modified: Sep 3, 1999 */#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>#include <limits.h>#include "routines/tsa.h"#define WID_STR "Estimates the Renyi entropy of Qth order\n\t\using a partition instead of the Grassberger-Procaccia scheme"typedef struct { double *hist; void *ptr;} hliste;unsigned long LENGTH=ULONG_MAX,exclude=0;unsigned int column=1,MAXDIM=10,DELAY=1,EPSCOUNT=20;unsigned int verbosity=0xff;double Q=2.0,EPSMIN=1.e-3,EPSMAX=1.0;char *outfile=NULL;int epsi;unsigned long length;double EPSFAKTOR;double *histo;double *series;void show_options(char *progname){ what_i_do(progname,WID_STR); fprintf(stderr,"Usage: %s [Options]\n",progname); fprintf(stderr,"Options:\n"); fprintf(stderr,"\t-l # of datapoints [Default: whole file]\n"); fprintf(stderr,"\t-c column to read [Default: %u]\n",column); fprintf(stderr,"\t-x # of lines to ignore [Default: %lu]\n",exclude); fprintf(stderr,"\t-M maximal embedding dimension [Default: %u]\n",MAXDIM); fprintf(stderr,"\t-d delay [Default: %u]\n",DELAY); fprintf(stderr,"\t-Q order of the Renyi entropy [Default: %.1f]\n",Q); fprintf(stderr,"\t-e minimal epsilon (data is rescaled to [0:1])" " [Default: %.2e]\n",EPSMIN); fprintf(stderr,"\t-E maximal epsilon (data is rescaled to [0:1])" " [Default: %.2e]\n",EPSMAX); fprintf(stderr,"\t-# # of epsilons to use [Default: %u]\n",EPSCOUNT); fprintf(stderr,"\t-o output file name [Default: 'datafile'.box]\n"); fprintf(stderr,"\t-V verbosity level [Default: 1]\n\t\t" "0='only panic messages'\n\t\t" "1='+ input/output messages'\n"); fprintf(stderr,"\t-h show these options\n\n"); exit(0);}void scan_options(int n,char **in){ char *out; if ((out=check_option(in,n,'l','u')) != NULL) sscanf(out,"%lu",&LENGTH); if ((out=check_option(in,n,'c','u')) != NULL) sscanf(out,"%u",&column); if ((out=check_option(in,n,'x','u')) != NULL) sscanf(out,"%lu",&exclude); if ((out=check_option(in,n,'M','u')) != NULL) sscanf(out,"%u",&MAXDIM); if ((out=check_option(in,n,'d','u')) != NULL) sscanf(out,"%u",&DELAY); if ((out=check_option(in,n,'Q','f')) != NULL) sscanf(out,"%lf",&Q); if ((out=check_option(in,n,'e','f')) != NULL) sscanf(out,"%lf",&EPSMIN); if ((out=check_option(in,n,'E','f')) != NULL) sscanf(out,"%lf",&EPSMAX); if ((out=check_option(in,n,'#','u')) != NULL) sscanf(out,"%u",&EPSCOUNT); if ((out=check_option(in,n,'V','u')) != NULL) sscanf(out,"%u",&verbosity); if ((out=check_option(in,n,'o','s')) != NULL) outfile=out;}hliste *make_histo(void){ int i; hliste *element; check_alloc(element=(hliste*)malloc(sizeof(hliste))); element->ptr=NULL; check_alloc(element->hist=(double*)malloc(sizeof(double)*MAXDIM)); for (i=0;i<MAXDIM;i++) element->hist[i]=0.0; return element;}void next_dim(int dim,int n,unsigned int *first){ int i,which,d1=(dim-1)*DELAY; double epsinv,norm,p; unsigned int **act; int *found,hf; epsinv=(double)epsi; norm=(double)length; check_alloc(act=(unsigned int**)malloc(epsi*sizeof(int*))); check_alloc(found=(int*)malloc(epsi*sizeof(int))); for (i=0;i<epsi;i++) { found[i]=0; act[i]=NULL; } for (i=0;i<n;i++) { which=(int)(series[first[i]+d1]*epsinv); hf= ++found[which]; check_alloc(act[which]= realloc((unsigned int*)act[which],hf*sizeof(unsigned int))); act[which][hf-1]=first[i]; } for (i=0;i<epsi;i++) if (found[i]) { p=(double)(found[i])/(norm); if (Q == 1.0) histo[dim-1] -= p*log(p); else histo[dim-1] += pow(p,Q); } if (dim<MAXDIM) for (i=0;i<epsi;i++) if (found[i]) next_dim(dim+1,found[i],act[i]); for (i=0;i<epsi;i++) if (found[i]) free(act[i]); free(act); free(found);}void start_box(void){ int i,which; double epsinv,norm,p; unsigned int **act; int *found,hf; void next_dim(); epsinv=(double)epsi; norm=(double)length; check_alloc(act=(unsigned int**)malloc(epsi*sizeof(int*))); check_alloc(found=(int*)malloc(epsi*sizeof(int))); for (i=0;i<epsi;i++) { found[i]=0; act[i]=NULL; } for (i=0;i<length;i++) { which=(int)(series[i]*epsinv); hf= ++found[which]; check_alloc(act[which]= realloc((unsigned int*)act[which],hf*sizeof(unsigned int))); act[which][hf-1]=i; } for (i=0;i<epsi;i++) if (found[i]) { p=(double)(found[i])/(norm); if (Q == 1.0) histo[0] -= p*log(p); else histo[0] += pow(p,Q); } if (1<MAXDIM) for (i=0;i<epsi;i++) { if (found[i]) next_dim(2,found[i],act[i]); } for (i=0;i<epsi;i++) if (found[i]) free(act[i]); free(act); free(found);}int main(int argc,char **argv){ int i,j,k,count,epsi_old=0,epsi_test; void *root; hliste *histo_el; double *deps,heps; double min,interval; char *infile=NULL,stdi=0; FILE *fHq; if (scan_help(argc,argv)) show_options(argv[0]); scan_options(argc,argv);#ifndef OMIT_WHAT_I_DO if (verbosity&VER_INPUT) what_i_do(argv[0],WID_STR);#endif infile=search_datafile(argc,argv,&column,verbosity); if (infile == NULL) stdi=1; if (outfile == NULL) { if (!stdi) { check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1)); sprintf(outfile,"%s.box",infile); } else { check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1)); sprintf(outfile,"stdin.box"); } } test_outfile(outfile); series=(double*)get_series(infile,&LENGTH,exclude,column,verbosity); rescale_data(series,LENGTH,&min,&interval); for (i=0;i<LENGTH;i++) if (series[i] >= 1.0) series[i] -= EPSMIN/2.0; check_alloc(histo=(double*)malloc(sizeof(double)*MAXDIM)); check_alloc(deps=(double*)malloc(sizeof(double)*EPSCOUNT)); histo_el=make_histo(); root=histo_el; EPSFAKTOR=pow(EPSMAX/EPSMIN,1.0/(double)EPSCOUNT); length=LENGTH-(MAXDIM-1)*DELAY; heps=EPSMAX*EPSFAKTOR; for (k=0;k<EPSCOUNT;k++) { count++; for (i=0;i<MAXDIM;i++) histo[i]=0.0; do { heps /= EPSFAKTOR; epsi_test=(int)(1./heps); } while (epsi_test <= epsi_old); epsi=epsi_test; epsi_old=epsi; deps[k]=heps; start_box(); histo_el=root; while (histo_el->ptr != NULL) histo_el=histo_el->ptr; for (i=0;i<MAXDIM;i++) if (Q == 1.0) histo_el->hist[i]=histo[i]; else histo_el->hist[i]=log(histo[i])/(1.0-Q); histo_el->ptr=make_histo(); histo_el=histo_el->ptr; fHq=fopen(outfile,"w"); if (verbosity&VER_INPUT) fprintf(stderr,"Opened %s for writing\n",outfile); for (i=0;i<MAXDIM;i++) { fprintf(fHq,"#dim = %d\n",i+1); histo_el=root; for (j=0;j<=k;j++) { if (i == 0) fprintf(fHq,"%e %e %e %e\n",deps[j],interval*deps[j], histo_el->hist[i],histo_el->hist[i]); else fprintf(fHq,"%e %e %e %e\n",deps[j],interval*deps[j], histo_el->hist[i],histo_el->hist[i]-histo_el->hist[i-1]); histo_el=histo_el->ptr; } fprintf(fHq,"\n"); } fclose(fHq); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -