// ebscalc.h #ifndef __ebscalc_h #define __ebscalc_h extern "C" { #include "ebs.h" #include "ebshead.h" #include #include #include void bcopy(char*, char*, int); } #include "ebsclass.h" #include #define CALCMAXMEM 8 // in MegaByte (so much memory could be used) #define BLOCKSIZE 8192 // so many samples will be processed at one time #define MAXOPEN 30 // ebscalc should not open more the MAXOPEN files #define DEBUG #ifndef SEEK_SET #define SEEK_SET 0 #endif #ifndef SEEK_CUR #define SEEK_CUR 1 #endif #ifndef SEEK_END #define SEEK_END 2 #endif // BINDINGS #define EBS_NO_BINDING 0 #define EBS_BIND_TO_EBS_FILE 1 #define EBS_BIND_TO_TMP_FILE 2 #define EBS_BIND_TO_FUNCTION 3 #define EBS_BIND_TO_EVALUATION 4 #define EBS_BIND_TO_CURVE 5 #define EBS_BIND_TO_BUFFER 6 #define EBS_BIND_TO_TMP_CLASS 7 #define EBS_BIND_TO_FUNCTION_C 8 // CURVE TYPES #define EBS_IN_TIME 1 #define EBS_IN_FREQUENCE 2 // OPERATORS #define EBS_NO_OPERATOR 0 #define EBS_VECTOR_PLUS 1 #define EBS_VECTOR_MINUS 2 #define EBS_VECTOR_MULT 3 #define EBS_VECTOR_DIV 4 #define EBS_PLUS_VALUE 5 #define EBS_MINUS_VALUE 6 #define EBS_MULT_VALUE 7 #define EBS_DIV_VALUE 8 #define EBS_CONCATENATION 9 #define EBS_CONVOLUTION 10 #define EBS_CORRELATION 11 #define EBS_LINCORRELATION 12 #define EBS_FFT 13 #define EBS_CONVOLUTION_WITH_FREQUENCE 14 // this will start the evaluation of all needed curves int EvaluateAll(void); void InfoInCurvePointers(void); class EBScurve; // a chain of EBScurves struct curve_pointers { EBScurve *curve; curve_pointers *next_curve; }; class EBStmp { // will be later a leaf of the evaluation tree private: unsigned long Reference; // how many references there are curve_pointers *Dependent; // which curves depend on this tmp unsigned long NumOfSamples; // number of samples in curve unsigned long SamPosition; // which is the next sample to return short BindType; // where can be the data found? short CurveType; // in frequenz or in time (default: time) int IsFileOpen; // is the file allready opened char *FileName; // for tmp or ebs files FILE *FFile; // file opened with fopen EBSFile *EFile; // file opened with EBSclass unsigned long CutOffset; // if something is cut out // if a curve is bound to a EBS file unsigned long ChannelNumber; unsigned long SampleOffset; int OpenFile(void); // opens the file (ffile or ebs file) void AddCurve(EBScurve*); // all curves who depend on this one int RemoveCurve(EBScurve*); // returns 1 on success and 0 on fail int FileInTmpPointers(void); // if the local FileName is in a other EBStmp friend int EvaluateCurve(EBScurve*); friend void LoseDependency(EBScurve*); friend void KillDependency(EBScurve*); void JumpToFirstPosition(void); friend void ToOpenClose(void); friend EBScurve; public: EBStmp(void); // Constructor ~EBStmp(void); // Destructor // the next sample that is read will be the first one void RewindSample(void); // return first sample and rewind for GetNextSample short GetFirstSample(void); // get the next sample after last call (of n-th or first sample) short GetNextSample(void); // get the next complex number after last call Complex GetNextComplex(void); // get the n-th sample short GetSample(unsigned long); // close the temp or ebs file void CloseFile(void); }; // a chain of EBStmp struct tmp_pointers { EBStmp *tmp; tmp_pointers *next_tmp; }; void PutNextComplex(Complex, FILE*); void AddInTmpPointers(EBStmp*); typedef short (*GFun)(unsigned long); typedef Complex (*CFun)(unsigned long); class EBScurve { public: char *Description; private: int Evaluated; // are the points of the curve available double SampleRate; // must be set short Vmin, Vmax; // min, max values of curve // this is not used yet, but maybe later in a further version of ebs // double PhysMin; // Minimum in physical values // double PhysMax; // Maximum " // double ConvMin; // Minimum of the converter (AD) // double ConvMax; // Maximum " double Bias; // as physical value double BiasCalc; // for calculating double ScaleFactor; // out of units double ScaleFactorCalc; // for calculating char *SIUnit; // SI unit out of units int UnitSpec; // is a unit specified? unsigned long NumOfSamples; // number of samples in curve short CurveBindType; // to what is the curve bound? short CurveType; // in frequenz or in time (default: time) unsigned long Reference; // how many references there are curve_pointers *Dependent; // which other curves depend of this curve unsigned long ZeroOffset; // is used for response function (convolution) unsigned long Lag; // is used for the correlation of lags unsigned long FFTPos; // where to start the fft calculation unsigned long FFTSize; // size of the block to fft // if a curve is bound to a EBS file char *FileName; unsigned long ChannelNumber; unsigned long SampleOffset; // if a curve is bound to a function // this is a function which takes a unsigned long and returns a short back short (*BindFunction)(unsigned long); Complex (*BindFunctionC)(unsigned long); // if a curve is bound to a evulation out of other curves EBScurve *Curve1; EBScurve *Curve2; unsigned long Operator; // if a curve is bound to a EBStmp class EBStmp *Leaf; // bind to a tmp file int BindTo(char*); // cut out of a other curve unsigned long CutOffset; // where is the curve after evaluation bound to? EBSFile* EbsFile; unsigned long OutChannel; unsigned long OutLength; unsigned long OutOffset; // copy allmost all entries to a new curve friend void CopyCurve(EBScurve*, EBScurve*); EBScurve(const EBScurve& curve) { }; // copy not allowed void AddCurve(EBScurve*); // all curves who depend on this one int RemoveCurve(EBScurve*); // returns 1 on success and 0 on fail void ChangeBindings(EBScurve*, EBScurve*); // this->newcurve2/newcurve->this // the first curve is a simplified copy of the second one. friend void Simplify(EBScurve*, EBScurve*); friend void InsertInFilePointers(EBScurve*); friend void AppendFilePointers(EBScurve*); // determines the number of samples and the rate a curve will have // which is a evaluation out of two other curves friend void DetermineLengthRate(unsigned long&, double&, EBScurve*, EBScurve*); friend void ScaleBiasAdjust(EBScurve*); // writes a curve to an ebs file // the parameter (maxlength) is only used in this version of // ebs, because that it is not yet possible to have channels with // different lengths. so the length of the longest channel is given // with which each shorter channel can zero padded to that length. int WriteCurve(unsigned long); int CalcVector(FILE*, EBStmp*, EBStmp*); int CalcConc(FILE*, EBStmp*, EBStmp*); int CalcConv(FILE*, EBStmp*, EBStmp*); int CalcCorr(FILE*, EBStmp*, EBStmp*); int CalcLinCorr(FILE*, EBStmp*, EBStmp*); int CalcFFT(FILE*, EBStmp*); int CalcConvFreq(FILE*, EBStmp*, EBStmp*); // this curve does not longer depend on other curves friend void LoseDependency(EBScurve*); public: EBScurve(char *description = ""); // Constructor ~EBScurve(void); // Destructor // get / set Bias double GetBias(void) { return Bias; } void SetBias(double bias) { Bias = bias; } // get / set ScaleFactor double GetScaleFactor(void) { return ScaleFactor; } void SetScaleFactor(double factor) { ScaleFactor = factor; } // get / set SampleRate double GetSampleRate(void) { return SampleRate; } void SetSampleRate(double sam) { SampleRate = sam; } // get / set SI Unit char *GetSIUnit(void) { return SIUnit; } void SetSIUnit(char *); // get / set CurveType (Frequenz or Time) short GetCurveType(void) { return CurveType; } void SetCurveType(short type) { CurveType = type; } // get / set NumberOfSamples unsigned long GetNumberOfSamples(void) { return NumOfSamples; } void SetNumberOfSamples(unsigned long length) { NumOfSamples = length; } // several types of binding a curve to the samples // returns 0 on sucess, otherwise -1 // bind to an EBS file int BindTo(char*, unsigned long, unsigned long, unsigned long); // bind to a function (returns short and takes unsigned long) int BindTo(GFun); // bind to a complex function (returns complex, takes unsigned long). // see also Numerical Recipes in C, chapter 12 (p. 508). int BindTo(CFun); // output binding int Output(EBSFile*, unsigned long, unsigned long = 0, unsigned long = ~0); // creates for that curve a tmp class friend int EvaluateCurve(EBScurve*); // starts the evaluation for all curves friend int EvaluateAll(void); // creates a tmp file for the curve with the given samplerate friend EBStmp *ChangeSampleRate(EBScurve*, double); // following: operators on curves friend EBScurve& operator+(EBScurve&, double); friend EBScurve& operator-(EBScurve&, double); friend EBScurve& operator*(EBScurve&, double); friend EBScurve& operator/(EBScurve&, double); friend EBScurve& operator+(EBScurve&, EBScurve&); friend EBScurve& operator-(EBScurve&, EBScurve&); friend EBScurve& operator*(EBScurve&, EBScurve&); friend EBScurve& operator/(EBScurve&, EBScurve&); // overloading some operators EBScurve& operator=(EBScurve&); double operator[](unsigned long); // to cut out a part of a curve / offset, length friend EBScurve& Cut(EBScurve&, unsigned long, unsigned long=~0); // concats to curves friend EBScurve& Concat(EBScurve&, EBScurve&); // FFT of curve at position with size. // default: start position = 0, size = size of curve friend EBScurve& FFT(EBScurve&, unsigned long=0, unsigned long=~0); // correlates the two curves with each other. // the third parameter is the lag. if you are interested in the // correlation of lags as large as +-K. default for lags is half the // length of the longer one curve. friend EBScurve& Correlation(EBScurve&, EBScurve&, unsigned long = ~0); // lineare Pearson correlation // useful for spike matching. // first curve is the signal, the second one is the spike sample friend EBScurve& LinCorrelation(EBScurve&, EBScurve&); // the first curve will be convoluted with the second one // there is a additional paramter for the second curve: where // is the zero point in time for the response curve friend EBScurve& Convolution(EBScurve&, EBScurve&, unsigned long); // the first curve is in time the second one in frequence. the first curve // is fft-ed and multiplied with the second one, after the convolution // theorem. the result is transformed back to a curve in time. friend EBScurve& ConvolutionWithFrequence(EBScurve& curve1, EBScurve& curve2); // DEBUG function: prints a lot of information of a curve void info(void); }; #endif