tsa/0000775002356700235670000000000012264575422012066 5ustar schloeglschloegltsa/src/0000775002356700235670000000000012264575422012655 5ustar schloeglschloegltsa/src/Makefile0000664002356700235670000001116012264575422014314 0ustar schloeglschloegl#################################################### # Copyright 2010,2011,2012 Alois Schloegl # This is part of the TSA-toolbox - a statistics and machine learning toolbox for data with and without missing values. # http://pub.ist.ac.at/~schloegl/matlab/tsa/ #################################################### ### modify directories according to your needs # Define non-default octave-version # Octave - global install (e.g. from debian package) # OCTAVE_VERSION= # Better alternative: define an OCTAVE_VERSION bash variable (or in .bashrc or .profile) # OCTAVE_VERSION=-3.6.3 # Matlab configuration # MATLABDIR = /usr/local/MATLAB/R2010b # comment the following line if you use MATLAB on 32-bit operating system MEX_OPTION += -largeArrayDims # Mingw crosscompiler: available at http://www.nongnu.org/mingw-cross-env/ CROSS = $(HOME)/src/mxe.github.schloegl/usr/bin/i686-pc-mingw32 CROSS64 = $(HOME)/src/mxe.github.schloegl/usr/bin/x86_64-w64-mingw32 # include directory for Win32-Matlab include W32MAT_INC = -I$(HOME)/bin/win32/Matlab/R2010b/extern/include/ W64MAT_INC = -I$(HOME)/bin/win64/Matlab/R2010b/extern/include/ # path to GNUMEX libraries, available from here http://sourceforge.net/projects/gnumex/ GNUMEX = $(HOME)/bin/win32/gnumex GNUMEX64 = $(HOME)/bin/win64/gnumex # building gnumex64 was difficult, these hints were quite useful: # http://sourceforge.net/mailarchive/forum.php?thread_name=AANLkTinZvxgC9ezp2P3UCX_a7TAUYuVsp2U40MQUV6qr%40mail.gmail.com&forum_name=gnumex-users # Instead of building "mex shortpath.c" and "mex uigetpath.c", I used empty m-functions within argout=argin; #################################################### W32MAT_INC += -I$(subst /usr/bin/,/usr/,$(CROSS))/include/ W64MAT_INC += -I$(subst /usr/bin/,/usr/,$(CROSS64))/include/ LDLIBS_W32 = $(subst /usr/bin/,/usr/,$(CROSS))/lib/ LDLIBS_W64 = $(subst /usr/bin/,/usr/,$(CROSS64))/lib/ W32_LIBS = $(LDLIBS_W32)liblapack.a W64_LIBS = $(LDLIBS_W64)liblapack.a W32_LIBS += $(LDLIBS_W32)libblas.a W64_LIBS += $(LDLIBS_W64)libblas.a CC = gcc CXX = g++ CFLAGS = -fopenmp -Wall -Wextra -Wconversion -O2 -fPIC OCTMEX = mkoctfile$(OCTAVE_VERSION) --mex RM = rm MEX_OPTION += -lgomp CC\#$(CXX) CXX\#$(CXX) CFLAGS\#"$(CFLAGS)" CXXFLAGS\#"$(CFLAGS)" MATMEX = $(MATLABDIR)/bin/mex $(MEX_OPTION) PROGS = covm_mex.mex sumskipnan_mex.mex # ### per default only the mex-files for octave are built mex4o octave: $(PROGS) ### Matlab configuration - search for a matlab directory if not defined above ifndef MATLABDIR ifneq ('',$(shell ls -1 /usr/local/MATLAB)) # use oldest, typically mex-files a compatible with newer Matlab versions MATLABDIR=$(shell ls -dt1 /usr/local/MATLAB/R* |tail -1) # alternatively, use latest matlab version #MATLABDIR=$(shell ls -dt1 /usr/local/MATLAB/R* |head -1) endif endif ### if the MATLABDIR has been found or defined ifneq ('',MATLABDIR) ifneq ('',$(lshell ls -1 $(MATLABDIR))) MEX_EXT = $(shell $(MATLABDIR)/bin/mexext) mex4m matlab: $(patsubst %.mex, %.$(MEX_EXT), $(PROGS)) endif endif mexw32 win32: $(patsubst %.mex, %.mexw32, $(PROGS)) mexw64 win64: $(patsubst %.mex, %.mexw64, $(PROGS)) all: octave win32 win64 mex4m clean: -$(RM) *.o *.obj *.o64 core octave-core *.oct *~ *.mex* ######################################################### # Octave, MATLAB on Linux ######################################################### %.oct: %.cc mkoctfile$(OCTAVE_VERSION) "$<" %.mex: %.cpp $(OCTMEX) "$<" -lgomp -llapack -lblas ## Octave %.$(MEX_EXT): %.cpp $(MATMEX) "$<" -lgomp -llapack -lblas ## Matlab ######################################################### # MATLAB/WIN32 ######################################################### %.obj: %.cpp $(CROSS)-$(CXX) -fopenmp -c -DMATLAB_MEX_FILE -x c++ -o "$@" $(W32MAT_INC) -O2 -DMX_COMPAT_32 "$<" %.obj: %.c $(CROSS)-$(CXX) -fopenmp -c -DMATLAB_MEX_FILE -x c++ -o "$@" $(W32MAT_INC) -O2 -DMX_COMPAT_32 "$<" %.mexw32: %.obj $(CROSS)-$(CXX) -shared $(GNUMEX)/mex.def -o "$@" -L$(GNUMEX) -s "$<" -llibmx -llibmex -llibmat -lcholmod -lgomp -lpthread -L$(LDLIBS_W32) -lblas -llapack ######################################################### # MATLAB/WIN64 ######################################################### ## ToDO: fix OpenMP support: currently -fopenmp causes Matlab to crash %.o64: %.cpp $(CROSS64)-$(CXX) -c -DMATLAB_MEX_FILE -x c++ -o "$@" $(W64MAT_INC) -O2 "$<" %.o64: %.c $(CROSS64)-$(CXX) -c -DMATLAB_MEX_FILE -x c++ -o "$@" $(W64MAT_INC) -O2 "$<" %.mexw64: %.o64 $(CROSS64)-$(CXX) -shared $(GNUMEX64)/mex.def -o "$@" -L$(GNUMEX64) -s "$<" -llibmx -llibmex -llibmat -lcholmod -lgomp -lpthread -L$(LDLIBS_W64) -lblas -llapack tsa/src/covm_mex.cpp0000664002356700235670000004612512264575422015206 0ustar schloeglschloegl/* //------------------------------------------------------------------- // C-MEX implementation of COVM - this function is part of the NaN-toolbox. // // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, see . // // // covm: in-product of matrices, NaN are skipped. // usage: // [cc,nn] = covm_mex(X,Y,flag,W); // // Input: // - X: // - Y: [optional], if empty, Y=X; // - flag: if not empty, it is set to 1 if some NaN was observed // - W: weight vector to compute weighted correlation // // Output: // - CC = X' * sparse(diag(W)) * Y while NaN's are skipped // - NN = real(~isnan(X)')*sparse(diag(W))*real(~isnan(Y)) count of valid (non-NaN) elements // computed more efficiently // // $Id: covm_mex.cpp 8223 2011-04-20 09:16:06Z schloegl $ // Copyright (C) 2009,2010,2011 Alois Schloegl // This function is part of the NaN-toolbox // http://pub.ist.ac.at/~schloegl/matlab/NaN/ // //------------------------------------------------------------------- */ #ifdef __GNUC__ #include #endif #include #include "mex.h" /*#define NO_FLAG*/ void mexFunction(int POutputCount, mxArray* POutput[], int PInputCount, const mxArray *PInputs[]) { double *X0=NULL, *Y0=NULL, *W=NULL; double *CC; double *NN = NULL; size_t rX,cX,rY,cY; size_t i; char flag_isNaN = 0; int ACC_LEVEL; /*********** check input arguments *****************/ // check for proper number of input and output arguments if ((PInputCount <= 0) || (PInputCount > 5)) { mexPrintf("usage: [CC,NN] = covm_mex(X [,Y [,flag [,W [,'E']]]])\n\n"); mexPrintf("Do not use COVM_MEX directly, use COVM instead. \n"); /* mexPrintf("\nCOVM_MEX computes the covariance matrix of real matrices and skips NaN's\n"); mexPrintf("\t[CC,NN] = covm_mex(...)\n\t\t computes CC=X'*Y, NN contains the number of not-NaN elements\n"); mexPrintf("\t\t CC./NN is the unbiased covariance matrix\n"); mexPrintf("\t... = covm_mex(X,Y,...)\n\t\t computes CC=X'*sparse(diag(W))*Y, number of rows of X and Y must match\n"); mexPrintf("\t... = covm_mex(X,[], ...)\n\t\t computes CC=X'*sparse(diag(W))*X\n"); mexPrintf("\t... = covm_mex(...,flag,...)\n\t\t if flag is not empty, it is set to 1 if some NaN occured in X or Y\n"); mexPrintf("\t... = covm_mex(...,W)\n\t\t W to compute weighted covariance, number of elements must match the number of rows of X\n"); mexPrintf("\t\t if isempty(W), all weights are 1\n"); mexPrintf("\t[CC,NN]=covm_mex(X,Y,flag,W)\n"); */ return; } if (POutputCount > 2) mexErrMsgTxt("covm.MEX has 1 to 2 output arguments."); // get 1st argument if(mxIsDouble(PInputs[0]) && !mxIsComplex(PInputs[0])) X0 = mxGetPr(PInputs[0]); else mexErrMsgTxt("First argument must be REAL/DOUBLE."); rX = mxGetM(PInputs[0]); cX = mxGetN(PInputs[0]); // get 2nd argument if (PInputCount > 1) { if (!mxGetNumberOfElements(PInputs[1])) ; // Y0 = NULL; else if (mxIsDouble(PInputs[1]) && !mxIsComplex(PInputs[1])) Y0 = mxGetPr(PInputs[1]); else mexErrMsgTxt("Second argument must be REAL/DOUBLE."); } // get weight vector for weighted sumskipnan if (PInputCount > 3) { // get 4th argument size_t nW = mxGetNumberOfElements(PInputs[3]); if (!nW) ; else if (nW == rX) W = mxGetPr(PInputs[3]); else mexErrMsgTxt("number of elements in W must match numbers of rows in X"); } #ifdef __GNUC__ ACC_LEVEL = 0; { mxArray *LEVEL = NULL; int s = mexCallMATLAB(1, &LEVEL, 0, NULL, "flag_accuracy_level"); if (!s) { ACC_LEVEL = (int) mxGetScalar(LEVEL); } mxDestroyArray(LEVEL); } // mexPrintf("Accuracy Level=%i\n",ACC_LEVEL); #endif if (Y0==NULL) { Y0 = X0; rY = rX; cY = cX; } else { rY = mxGetM(PInputs[1]); cY = mxGetN(PInputs[1]); } if (rX != rY) mexErrMsgTxt("number of rows in X and Y do not match"); /*********** create output arguments *****************/ POutput[0] = mxCreateDoubleMatrix(cX, cY, mxREAL); CC = mxGetPr(POutput[0]); if (POutputCount > 1) { POutput[1] = mxCreateDoubleMatrix(cX, cY, mxREAL); NN = mxGetPr(POutput[1]); } /*********** compute covariance *****************/ #if 0 /*------ version 1 --------------------- this solution is slower than the alternative solution below for transposed matrices, this might be faster. */ for (k=0; k 2) && mxGetNumberOfElements(PInputs[2])) { // set FLAG_NANS_OCCURED switch (mxGetClassID(PInputs[2])) { case mxDOUBLE_CLASS: *(double*)mxGetData(PInputs[2]) = 1.0; break; case mxSINGLE_CLASS: *(float*)mxGetData(PInputs[2]) = 1.0; break; case mxLOGICAL_CLASS: case mxCHAR_CLASS: case mxINT8_CLASS: case mxUINT8_CLASS: *(char*)mxGetData(PInputs[2]) = 1; break; #ifdef __GNUC__ case mxINT16_CLASS: case mxUINT16_CLASS: *(uint16_t*)mxGetData(PInputs[2]) = 1; break; case mxINT32_CLASS: case mxUINT32_CLASS: *(uint32_t*)mxGetData(PInputs[2])= 1; break; case mxINT64_CLASS: case mxUINT64_CLASS: *(uint64_t*)mxGetData(PInputs[2]) = 1; break; case mxFUNCTION_CLASS: case mxUNKNOWN_CLASS: case mxCELL_CLASS: case mxSTRUCT_CLASS: #endif default: mexPrintf("Type of 3rd input argument cannot be used to return status of NaN occurence."); } } #endif #endif } tsa/src/sumskipnan_mex.cpp0000664002356700235670000006045212264575422016431 0ustar schloeglschloegl //------------------------------------------------------------------- // C-MEX implementation of SUMSKIPNAN - this function is part of the NaN-toolbox. // // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, see . // // // sumskipnan: sums all non-NaN values // usage: // [o,count,SSQ] = sumskipnan_mex(x,DIM,flag,W); // // SUMSKIPNAN uses two techniques to reduce errors: // 1) long double (80bit) instead of 64-bit double is used internally // 2) The Kahan Summation formula is used to reduce the error margin from N*eps to 2*eps // The latter is only implemented in case of stride=1 (column vectors only, summation along 1st dimension). // // Input: // - x data array // - DIM (optional) dimension to sum // - flag (optional) is actually an output argument telling whether some NaN was observed // - W (optional) weight vector to compute weighted sum (default 1) // // Output: // - o (weighted) sum along dimension DIM // - count of valid elements // - sums of squares // // // $Id: sumskipnan_mex.cpp 12492 2014-01-10 13:34:15Z schloegl $ // Copyright (C) 2009,2010,2011 Alois Schloegl // This function is part of the NaN-toolbox // http://pub.ist.ac.at/~schloegl/matlab/NaN/ // //------------------------------------------------------------------- #include #include #include "mex.h" inline int __sumskipnan2w__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W); inline int __sumskipnan3w__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W); inline int __sumskipnan2wr__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W); inline int __sumskipnan3wr__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W); inline int __sumskipnan2we__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W); inline int __sumskipnan3we__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W); inline int __sumskipnan2wer__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W); inline int __sumskipnan3wer__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W); //#define NO_FLAG #ifdef tmwtypes_h #if (MX_API_VER<=0x07020000) typedef int mwSize; #endif #endif void mexFunction(int POutputCount, mxArray* POutput[], int PInputCount, const mxArray *PInputs[]) { const mwSize *SZ; double* LInput; double* LOutputSum; double* LOutputCount; double* LOutputSum2; long double* LongOutputSum = NULL; long double* LongOutputCount = NULL; long double* LongOutputSum2 = NULL; double x; double* W = NULL; // weight vector mwSize DIM = 0; mwSize D1, D2, D3; // NN; // mwSize ND, ND2; // number of dimensions: input, output mwSize ix0, ix1, ix2; // index to input and output mwSize j, l; // running indices mwSize *SZ2; // size of output char flag_isNaN = 0; // check for proper number of input and output arguments if ((PInputCount <= 0) || (PInputCount > 4)) mexErrMsgTxt("SUMSKIPNAN.MEX requires between 1 and 4 arguments."); if (POutputCount > 4) mexErrMsgTxt("SUMSKIPNAN.MEX has 1 to 3 output arguments."); // get 1st argument if(mxIsDouble(PInputs[0]) && !mxIsComplex(PInputs[0])) LInput = mxGetPr(PInputs[0]); else mexErrMsgTxt("First argument must be REAL/DOUBLE."); // get 2nd argument if (PInputCount > 1) { switch (mxGetNumberOfElements(PInputs[1])) { case 0: x = 0.0; // accept empty element break; case 1: x = (mxIsNumeric(PInputs[1]) ? mxGetScalar(PInputs[1]) : -1.0); break; default:x = -1.0; // invalid } if ((x < 0) || (x > 65535) || (x != floor(x))) mexErrMsgTxt("Error SUMSKIPNAN.MEX: DIM-argument must be a positive integer scalar"); DIM = (unsigned)floor(x); } // get size ND = mxGetNumberOfDimensions(PInputs[0]); // NN = mxGetNumberOfElements(PInputs[0]); SZ = mxGetDimensions(PInputs[0]); // if DIM==0 (undefined), look for first dimension with more than 1 element. for (j = 0; (DIM < 1) && (j < ND); j++) if (SZ[j]>1) DIM = j+1; if (DIM < 1) DIM=1; // in case DIM is still undefined ND2 = (ND>DIM ? ND : DIM); // number of dimensions of output SZ2 = (mwSize*)mxCalloc(ND2, sizeof(mwSize)); // allocate memory for output size for (j=0; j ND, add extra elements 1 SZ2[j] = 1; for (j=0, D1=1; j 3) { if (!mxGetNumberOfElements(PInputs[3])) ; // empty weight vector - no weighting else if (mxGetNumberOfElements(PInputs[3])==D2) W = mxGetPr(PInputs[3]); else mexErrMsgTxt("Error SUMSKIPNAN.MEX: length of weight vector does not match size of dimension"); } int ACC_LEVEL = 0; { mxArray *LEVEL = NULL; int s = mexCallMATLAB(1, &LEVEL, 0, NULL, "flag_accuracy_level"); if (!s) { ACC_LEVEL = (int) mxGetScalar(LEVEL); if ((D1>1) && (ACC_LEVEL>2)) mexWarnMsgTxt("Warning: Kahan summation not supported with stride > 1 !"); } mxDestroyArray(LEVEL); } // mexPrintf("Accuracy Level=%i\n",ACC_LEVEL); // create outputs #define TYP mxDOUBLE_CLASS POutput[0] = mxCreateNumericArray(ND2, SZ2, TYP, mxREAL); LOutputSum = mxGetPr(POutput[0]); if (D1!=1 && D2>0) LongOutputSum = (long double*) mxCalloc(D1*D3,sizeof(long double)); if (POutputCount >= 2) { POutput[1] = mxCreateNumericArray(ND2, SZ2, TYP, mxREAL); LOutputCount = mxGetPr(POutput[1]); if (D1!=1 && D2>0) LongOutputCount = (long double*) mxCalloc(D1*D3,sizeof(long double)); } if (POutputCount >= 3) { POutput[2] = mxCreateNumericArray(ND2, SZ2, TYP, mxREAL); LOutputSum2 = mxGetPr(POutput[2]); if (D1!=1 && D2>0) LongOutputSum2 = (long double*) mxCalloc(D1*D3,sizeof(long double)); } mxFree(SZ2); if (!D1 || !D2 || !D3) // zero size array ; // do nothing else if (D1==1) { if (ACC_LEVEL<1) { // double accuray, naive summation, error = N*2^-52 switch (POutputCount) { case 0: case 1: #pragma omp parallel for schedule(dynamic) for (l = 0; l DIM for (l = 0; l DIM for (l = 0; l DIM for (l = 0; l 2) && mxGetNumberOfElements(PInputs[2])) { // set FLAG_NANS_OCCURED switch (mxGetClassID(PInputs[2])) { case mxLOGICAL_CLASS: case mxCHAR_CLASS: case mxINT8_CLASS: case mxUINT8_CLASS: *(uint8_t*)mxGetData(PInputs[2]) = 1; break; case mxDOUBLE_CLASS: *(double*)mxGetData(PInputs[2]) = 1.0; break; case mxSINGLE_CLASS: *(float*)mxGetData(PInputs[2]) = 1.0; break; case mxINT16_CLASS: case mxUINT16_CLASS: *(uint16_t*)mxGetData(PInputs[2]) = 1; break; case mxINT32_CLASS: case mxUINT32_CLASS: *(uint32_t*)mxGetData(PInputs[2])= 1; break; case mxINT64_CLASS: case mxUINT64_CLASS: *(uint64_t*)mxGetData(PInputs[2]) = 1; break; case mxFUNCTION_CLASS: case mxUNKNOWN_CLASS: case mxCELL_CLASS: case mxSTRUCT_CLASS: default: mexPrintf("Type of 3rd input argument not supported."); } } #endif } #define stride 1 inline int __sumskipnan2w__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W) { long double sum=0; char flag=0; // LOOP along dimension DIM double *end = data + stride*Ni; if (W) { // with weight vector long double count = 0.0; do { long double x = *data; if (!isnan(x)) { count += *W; sum += *W*x; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 W++; } while (data < end); *No = count; } else { // w/o weight vector size_t countI = 0; do { long double x = *data; if (!isnan(x)) { countI++; sum += x; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 } while (data < end); *No = (double)countI; } #ifndef NO_FLAG if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; #endif *s = sum; } inline int __sumskipnan3w__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W) { long double sum=0; long double msq=0; char flag=0; // LOOP along dimension DIM double *end = data + stride*Ni; if (W) { // with weight vector long double count = 0.0; do { long double x = *data; if (!isnan(x)) { count += *W; long double t = *W*x; sum += t; msq += x*t; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 W++; } while (data < end); *No = count; } else { // w/o weight vector size_t countI = 0; do { long double x = *data; if (!isnan(x)) { countI++; sum += x; msq += x*x; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 } while (data < end); *No = (double)countI; } #ifndef NO_FLAG if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; #endif *s = sum; *s2 = msq; } inline int __sumskipnan2wr__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W) { double sum=0; char flag=0; // LOOP along dimension DIM double *end = data + stride*Ni; if (W) { // with weight vector double count = 0.0; do { double x = *data; if (!isnan(x)) { count += *W; sum += *W*x; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 W++; } while (data < end); *No = count; } else { // w/o weight vector size_t countI = 0; do { double x = *data; if (!isnan(x)) { countI++; sum += x; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 } while (data < end); *No = (double)countI; } #ifndef NO_FLAG if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; #endif *s = sum; } inline int __sumskipnan3wr__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W) { double sum=0; double msq=0; char flag=0; // LOOP along dimension DIM double *end = data + stride*Ni; if (W) { // with weight vector double count = 0.0; do { double x = *data; if (!isnan(x)) { count += *W; double t = *W*x; sum += t; msq += x*t; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 W++; } while (data < end); *No = count; } else { // w/o weight vector size_t countI = 0; do { double x = *data; if (!isnan(x)) { countI++; sum += x; msq += x*x; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 } while (data < end); *No = (double)countI; } #ifndef NO_FLAG if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; #endif *s = sum; *s2 = msq; } /*************************************** using Kahan's summation formula [1] this gives more accurate results while the computational effort within the loop is about 4x as high First tests show a penalty of about 40% in terms of computational time. [1] David Goldberg, What Every Computer Scientist Should Know About Floating-Point Arithmetic ACM Computing Surveys, Vol 23, No 1, March 1991. ****************************************/ inline int __sumskipnan2we__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W) { long double sum=0; char flag=0; // LOOP along dimension DIM double *end = data + stride*Ni; if (W) { // with weight vector long double count = 0.0; long double rc=0.0, rn=0.0; do { long double x = *data; long double t,y; if (!isnan(x)) { //count += *W; [1] y = *W-rn; t = count+y; rn= (t-count)-y; count= t; //sum += *W*x; [1] y = *W*x-rc; t = sum+y; rc= (t-sum)-y; sum= t; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 W++; } while (data < end); *No = count; } else { // w/o weight vector size_t countI = 0; long double rc=0.0; do { long double x = *data; long double t,y; if (!isnan(x)) { countI++; // sum += x; [1] y = x-rc; t = sum+y; rc= (t-sum)-y; sum= t; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 } while (data < end); *No = (double)countI; } #ifndef NO_FLAG if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; #endif *s = sum; } inline int __sumskipnan3we__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W) { long double sum=0; long double msq=0; char flag=0; // LOOP along dimension DIM double *end = data + stride*Ni; if (W) { // with weight vector long double count = 0.0; long double rc=0.0, rn=0.0, rq=0.0; do { long double x = *data; long double t,y; if (!isnan(x)) { //count += *W; [1] y = *W-rn; t = count+y; rn= (t-count)-y; count= t; long double w = *W*x; //sum += *W*x; [1] y = *W*x-rc; t = sum+y; rc= (t-sum)-y; sum= t; // msq += x*w; y = w*x-rq; t = msq+y; rq= (t-msq)-y; msq= t; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 W++; } while (data < end); *No = count; } else { // w/o weight vector size_t countI = 0; long double rc=0.0, rq=0.0; do { long double x = *data; long double t,y; if (!isnan(x)) { countI++; //sum += x; [1] y = x-rc; t = sum+y; rc= (t-sum)-y; sum= t; // msq += x*x; y = x*x-rq; t = msq+y; rq= (t-msq)-y; msq= t; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 } while (data < end); *No = (double)countI; } #ifndef NO_FLAG if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; #endif *s = sum; *s2 = msq; } inline int __sumskipnan2wer__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W) { double sum=0; char flag=0; // LOOP along dimension DIM double *end = data + stride*Ni; if (W) { // with weight vector double count = 0.0; double rc=0.0, rn=0.0; do { double x = *data; double t,y; if (!isnan(x)) { //count += *W; [1] y = *W-rn; t = count+y; rn= (t-count)-y; count= t; //sum += *W*x; [1] y = *W*x-rc; t = sum+y; rc= (t-sum)-y; sum= t; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 W++; } while (data < end); *No = count; } else { // w/o weight vector size_t countI = 0; double rc=0.0; do { double x = *data; double t,y; if (!isnan(x)) { countI++; // sum += x; [1] y = x-rc; t = sum+y; rc= (t-sum)-y; sum= t; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 } while (data < end); *No = (double)countI; } #ifndef NO_FLAG if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; #endif *s = sum; } inline int __sumskipnan3wer__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W) { double sum=0; double msq=0; char flag=0; // LOOP along dimension DIM double *end = data + stride*Ni; if (W) { // with weight vector double count = 0.0; double rc=0.0, rn=0.0, rq=0.0; do { double x = *data; double t,y; if (!isnan(x)) { //count += *W; [1] y = *W-rn; t = count+y; rn= (t-count)-y; count= t; double w = *W*x; //sum += *W*x; [1] y = *W*x-rc; t = sum+y; rc= (t-sum)-y; sum= t; // msq += x*w; y = w*x-rq; t = msq+y; rq= (t-msq)-y; msq= t; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 W++; } while (data < end); *No = count; } else { // w/o weight vector size_t countI = 0; double rc=0.0, rq=0.0; do { double x = *data; double t,y; if (!isnan(x)) { countI++; //sum += x; [1] y = x-rc; t = sum+y; rc= (t-sum)-y; sum= t; // msq += x*x; y = x*x-rq; t = msq+y; rq= (t-msq)-y; msq= t; } #ifndef NO_FLAG else flag = 1; #endif data++; // stride=1 } while (data < end); *No = (double)countI; } #ifndef NO_FLAG if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; #endif *s = sum; *s2 = msq; } tsa/VERSION0000664002356700235670000000013712264575422013137 0ustar schloeglschloegl# TSA-toolbox http://pub.ist.ac.at/~schloegl/matlab/tsa # Version: 4.2.7 # Date: 2014-01-12 tsa/DESCRIPTION0000664002356700235670000000045112264575422013574 0ustar schloeglschloeglName: TSA Version: 4.2.7 Date: 2014-01-12 Author: Alois Schloegl Maintainer: Alois Schloegl Title: The TSA-toolbox Description: A toolbox for Time Series Analysis . Depends: octave (> 2.9.0) License: GPL version 3 or later Url: http://pub.ist.ac.at/~schloegl/matlab/tsa tsa/INDEX0000664002356700235670000000127310625373341012656 0ustar schloeglschloegltsa >> Time Series Analysis Univariate (stationary) analysis acovf acorf biacovf bispec durlev lattice rmle pacf parcor invest0 invest1 selmo selmo2 histo histo2 histo3 hup ucp y2res ar_spa detrend flix Multivariate stationary analysis mvar mvfilter mvfreqz arfit2 histo4 Adaptive (time-varying) analysis aar aarmam adim amarma mvaar Conversions between forms ac2poly ac2rc ar2rc rc2ar poly2ac poly2ar poly2rc rc2ac rc2poly ar2poly Utility functions arcext sinvest1 sbispec flag_implicit_samplerate Test suites tsademo bisdemo invfdemo tsa/COPYING0000664002356700235670000010451311441754133013117 0ustar schloeglschloegl GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . tsa/doc/0000775002356700235670000000000012264575422012633 5ustar schloeglschloegltsa/doc/README.TXT0000664002356700235670000000375312263773327014204 0ustar schloeglschloeglTSA (Time Series Analysis) Toolbox 4.2 ======================================= The TSA toolbox is useful for analysing Time Series. The methods are based on stochastic concepts and maximum entropy methods. The programs should be compatible to Matlab as well as Octave. It includes - Stochastic Signal processing - Autoregressive Model Identification - adaptive autoregressive modelling using Kalman filtering - multivariate autoregressive modelling - maximum entropy spectral estimation - matched (inverse) filter design - Histogram analysis - Calcution of the entropy of a time series - Non-linear analysis (3rd order statistics) - Test for UnitCircle- and Hurwitz- Polynomials - multiple signal processing - Several criteria (AIC, BIC, FPE, MDL, SBC, CAT, PHI) for model order selection an autoregressive model are included. - Fast algorithms are used - missing values (encoded as NaN's) are considered Furthermore includes the toolbox a fast version of the Yule-Walker method for estimating Autoregressive parameters, Autocorrelation Function (ACF), Partial autocorrelation (PARCOR), reflection coefficients and some other useful staff. Demo programs can be started with "demo" or "demotsa". % This library is free software; you can redistribute it and/or % modify it under the terms of the GNU Library General Public % License as published by the Free Software Foundation; either % Version 3 of the License, or (at your option) any later version. % % This library is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU % Library General Public License for more details. % % You should have received a copy of the GNU Library General Public % License along with this library; if not, see % . Copyright (C) 1996-2003,2010,2012,2014 by Alois Schloegl E-Mail: alois.schloegl@ist.ac.at WWW: http://pub.ist.ac.at/~schloegl/matlab/tsa/ tsa/inst/0000775002356700235670000000000012264575422013043 5ustar schloeglschloegltsa/inst/mvfreqz.m0000664002356700235670000002341712244747160014720 0ustar schloeglschloeglfunction [S,h,PDC,COH,DTF,DC,pCOH,dDTF,ffDTF, pCOH2, PDCF, coh,GGC,Af,GPDC,GGC2, DCOH]=mvfreqz(B,A,C,N,Fs) % MVFREQZ multivariate frequency response % [S,h,PDC,COH,DTF,DC,pCOH,dDTF,ffDTF,pCOH2,PDCF,coh,GGC,Af,GPDC,GGC2,DCOH] = mvfreqz(B,A,C,f,Fs) % [...] = mvfreqz(B,A,C,N,Fs) % % INPUT: % ======= % A, B multivariate polynomials defining the transfer function % % a0*Y(n) = b0*X(n) + b1*X(n-1) + ... + bq*X(n-q) % - a1*Y(n-1) - ... - ap*Y(:,n-p) % % A=[a0,a1,a2,...,ap] and B=[b0,b1,b2,...,bq] must be matrices of % size Mx((p+1)*M) and Mx((q+1)*M), respectively. % % C is the covariance of the input noise X (i.e. D'*D if D is the mixing matrix) % N if scalar, N is the number of frequencies % if N is a vector, N are the designated frequencies. % Fs sampling rate [default 2*pi] % % A,B,C and D can by obtained from a multivariate time series % through the following commands: % [AR,RC,PE] = mvar(Y,P); % M = size(AR,1); % number of channels % A = [eye(M),-AR]; % B = eye(M); % C = PE(:,M*P+1:M*(P+1)); % % Fs sampling rate in [Hz] % (N number of frequencies for computing the spectrum, this will become OBSOLETE), % f vector of frequencies (in [Hz]) % % % OUTPUT: % ======= % S power spectrum % h transfer functions, abs(h.^2) is the non-normalized DTF [11] % PDC partial directed coherence [2] % DC directed coupling [13] % COH coherency (complex coherence) [5] % DTF directed transfer function [3,13] % pCOH partial coherence % dDTF direct Directed Transfer function % ffDTF full frequency Directed Transfer Function % pCOH2 partial coherence - alternative method % GGC a modified version of Geweke's Granger Causality [Geweke 1982] % !!! it uses a Multivariate AR model, and computes the bivariate GGC as in [Bressler et al 2007]. % This is not the same as using bivariate AR models and GGC as in [Bressler et al 2007] % Af Frequency transform of A(z), abs(Af.^2) is the non-normalized PDC [11] % PDCF Partial Directed Coherence Factor [2] % GPDC Generalized Partial Directed Coherence [9,10] % DCOH directed coherence or Generalized DTF (GDTF) [12] (equ. 11a) % % see also: FREQZ, MVFILTER, MVAR % % REFERENCE(S): % [1] H. Liang et al. Neurocomputing, 32-33, pp.891-896, 2000. % [2] L.A. Baccala and K. Samashima, Biol. Cybern. 84,463-474, 2001. % [3] A. Korzeniewska, et al. Journal of Neuroscience Methods, 125, 195-207, 2003. % [4] Piotr J. Franaszczuk, Ph.D. and Gregory K. Bergey, M.D. % Fast Algorithm for Computation of Partial Coherences From Vector Autoregressive Model Coefficients % World Congress 2000, Chicago. % [5] Nolte G, Bai O, Wheaton L, Mari Z, Vorbach S, Hallett M. % Identifying true brain interaction from EEG data using the imaginary part of coherency. % Clin Neurophysiol. 2004 Oct;115(10):2292-307. % [6] Schlogl A., Supp G. % Analyzing event-related EEG data with multivariate autoregressive parameters. % (Eds.) C. Neuper and W. Klimesch, % Progress in Brain Research: Event-related Dynamics of Brain Oscillations. % Analysis of dynamics of brain oscillations: methodological advances. Elsevier. % Progress in Brain Research 159, 2006, p. 135 - 147 % [7] Bressler S.L., Richter C.G., Chen Y., Ding M. (2007) % Cortical fuctional network organization from autoregressive modelling of loal field potential oscillations. % Statistics in Medicine, doi: 10.1002/sim.2935 % [8] Geweke J., 1982 % J.Am.Stat.Assoc., 77, 304-313. % [9] L.A. Baccala, D.Y. Takahashi, K. Sameshima. (2006) % Generalized Partial Directed Coherence. % Submitted to XVI Congresso Brasileiro de Automatica, Salvador, Bahia. % [10] L.A. Baccala, D.Y. Takahashi, K. Sameshima. % Computer Intensive Testing for the Influence Between Time Series, % Eds. B. Schelter, M. Winterhalder, J. Timmer: % Handbook of Time Series Analysis - Recent Theoretical Developments and Applications % Wiley, p.413, 2006. % [11] M. Eichler % On the evaluation of informatino flow in multivariate systems by the directed transfer function % Biol. Cybern. 94: 469-482, 2006. % [12] L. Faes, S. Erla, and G. Nollo, (2012) % Measuring Connectivity in Linear Multivariate Processes: Definitions, Interpretation, and Practical Analysis % Computational and Mathematical Methods in Medicine Volume 2012 (2012), Article ID 140513, 18 pages % doi:10.1155/2012/140513 % [13] Maciej Kaminski, Mingzhou Ding, Wilson A. Truccolo, Steven L. Bressler % Evaluating causal relations in neural systems: Granger causality, % directed transfer function and statistical assessment of significance. % Biol. Cybern. 85, 145-157 (2001) % % $Id: mvfreqz.m 12366 2013-11-25 22:25:52Z schloegl $ % Copyright (C) 1996-2008 by Alois Schloegl % Copyright (C) 2013 Martin Billinger % This is part of the TSA-toolbox. See also % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % http://octave.sourceforge.net/ % http://biosig.sourceforge.net/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [K1,K2] = size(A); p = K2/K1-1; %a=ones(1,p+1); [K1,K2] = size(B); q = K2/K1-1; %b=ones(1,q+1); if nargin<3 C = eye(K1,K1); end; if nargin<5, Fs= 1; end; if nargin<4, N = 512; f = (0:N-1)*(Fs/(2*N)); end; if all(size(N)==1), fprintf(1,'Warning MVFREQZ: The forth input argument N is a scalar, this is ambigous.\n'); fprintf(1,' In the past, N was used to indicate the number of spectral lines. This might change.\n'); fprintf(1,' In future versions, it will indicate the spectral line.\n'); f = (0:N-1)*(Fs/(2*N)); else f = N; end; N = length(f); s = exp(i*2*pi*f/Fs); z = i*2*pi/Fs; h=zeros(K1,K1,N); Af=zeros(K1,K1,N); g=zeros(K1,K1,N); S=zeros(K1,K1,N); S1=zeros(K1,K1,N); DTF=zeros(K1,K1,N); COH=zeros(K1,K1,N); %COH2=zeros(K1,K1,N); PDC=zeros(K1,K1,N); %PDC3=zeros(K1,K1,N); PDCF = zeros(K1,K1,N); pCOH = zeros(K1,K1,N); GGC=zeros(K1,K1,N); GGC2=zeros(K1,K1,N); DCOH=zeros(K1,K1,N); invC=inv(C); tmp1=zeros(1,K1); tmp2=zeros(1,K1); M = zeros(K1,K1,N); detG = zeros(N,1); %D = sqrtm(C); %iD= inv(D); ddc2 = diag(diag(C).^(-1/2)); ddc2i = diag(diag(C).^(1/2)); for n=1:N, atmp = zeros(K1); for k = 1:p+1, atmp = atmp + A(:,k*K1+(1-K1:0))*exp(z*(k-1)*f(n)); end; % compensation of instantaneous correlation % atmp = iD*atmp*D; btmp = zeros(K1); for k = 1:q+1, btmp = btmp + B(:,k*K1+(1-K1:0))*exp(z*(k-1)*f(n)); end; h(:,:,n) = atmp\btmp; Af(:,:,n) = atmp/btmp; S(:,:,n) = h(:,:,n)*C*h(:,:,n)'/Fs; S1(:,:,n) = h(:,:,n)*h(:,:,n)'; ctmp = ddc2*atmp; %% used for GPDC dtmp = h(:,:,n) * ddc2i; %% used for directed coherence (DCOH) for k1 = 1:K1, tmp = squeeze(atmp(:,k1)); tmp1(k1) = sqrt(tmp'*tmp); tmp2(k1) = sqrt(tmp'*invC*tmp); %tmp = squeeze(atmp(k1,:)'); %tmp3(k1) = sqrt(tmp'*tmp); tmp = squeeze(ctmp(:,k1)); tmp3(k1) = sqrt(tmp'*tmp); tmp = dtmp(k1,:); tmp4(k1) = sqrt(tmp*tmp'); end; PDCF(:,:,n) = abs(atmp)./tmp2(ones(1,K1),:); PDC(:,:,n) = abs(atmp)./tmp1(ones(1,K1),:); GPDC(:,:,n) = abs(ctmp)./tmp3(ones(1,K1),:); %PDC3(:,:,n) = abs(atmp)./tmp3(:,ones(1,K1)); DCOH(:,:,n) = abs(dtmp) ./ tmp4(ones(1,K1),:)'; g = atmp/btmp; G(:,:,n) = g'*invC*g; detG(n) = det(G(:,:,n)); end; if nargout<4, return; end; %%%%% directed transfer function for k1=1:K1; DEN=sum(abs(h(k1,:,:)).^2,2); for k2=1:K2; %COH2(k1,k2,:) = abs(S(k1,k2,:).^2)./(abs(S(k1,k1,:).*S(k2,k2,:))); COH(k1,k2,:) = (S(k1,k2,:))./sqrt(abs(S(k1,k1,:).*S(k2,k2,:))); coh(k1,k2,:) = (S1(k1,k2,:))./sqrt(abs(S1(k1,k1,:).*S1(k2,k2,:))); %DTF(k1,k2,:) = sqrt(abs(h(k1,k2,:).^2))./DEN; DTF(k1,k2,:) = abs(h(k1,k2,:))./sqrt(DEN); ffDTF(k1,k2,:) = abs(h(k1,k2,:))./sqrt(sum(DEN,3)); pCOH2(k1,k2,:) = abs(G(k1,k2,:).^2)./(G(k1,k1,:).*G(k2,k2,:)); %M(k2,k1,:) = ((-1)^(k1+k2))*squeeze(G(k1,k2,:))./detG; % oder ist M = G? end; end; dDTF = pCOH2.*ffDTF; if nargout<6, return; end; DC = zeros(K1); for k = 1:p, DC = DC + A(:,k*K1+(1:K1)).^2; end; if nargout<13, return; end; for k1=1:K1; for k2=1:K2; % Bivariate Granger Causality (similar to Bressler et al. 2007. ) GGC(k1,k2,:) = ((C(k1,k1)*C(k2,k2)-C(k1,k2)^2)/C(k2,k2))*real(h(k1,k2,:).*conj(h(k1,k2,:)))./abs(S(k2,k2,:)); %GGC2(k1,k2,:) = -log(1-((C(k1,k1)*C(k2,k2)-C(k1,k2)^2)/C(k2,k2))*real(h(k1,k2,:).*conj(h(k1,k2,:)))./S(k2,k2,:)); end; end; return; if nargout<7, return; end; for k1=1:K1; for k2=1:K2; M(k2,k1,:) = ((-1)^(k1+k2))*squeeze(G(k1,k2,:))./detG; % oder ist M = G? end; end; for k1=1:K1; for k2=1:K2; pCOH(k1,k2,:) = abs(M(k1,k2,:).^2)./(M(k1,k1,:).*M(k2,k2,:)); end; end; tsa/inst/invest1.m0000664002356700235670000001074612115040716014606 0ustar schloeglschloeglfunction [AutoCov,AutoCorr,ARPMX,E,C,s]=invest1(Y,Pmax,D); % First Investigation of a signal (time series) - interactive % [AutoCov,AutoCorr,ARPMX,E,CRITERIA,MOPS]=invest1(Y,Pmax,show); % % Y time series % Pmax maximal order (optional) % show optional; if given the parameters are shown % % AutoCov Autocorrelation % AutoCorr normalized Autocorrelation % PartACF Partial Autocorrelation % E Error function E(p) % CRITERIA curves of the various (see below) criteria, % MOPS=[optFPE optAIC optBIC optSBC optMDL optCAT optPHI]; % optimal model order according to various criteria % % FPE Final Prediction Error (Kay, 1987) % AIC Akaike Information Criterion (Marple, 1987) % BIC Bayesian Akaike Information Criterion (Wei, 1994) % SBC Schwartz's Bayesian Criterion (Wei, 1994) % MDL Minimal Description length Criterion (Marple, 1987) % CAT Parzen's CAT Criterion (Wei, 1994) % PHI Phi criterion (Pukkila et al. 1988) % minE order where E is minimal % % REFERENCES: % P.J. Brockwell and R.A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % S. Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % C.E. Shannon and W. Weaver "The mathematical theory of communication" University of Illinois Press, Urbana 1949 (reprint 1963). % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % optFPE order where FPE is minimal % optAIC order where AIC is minimal % optBIC order where BIC is minimal % optSBC order where SBC is minimal % optMDL order where MDL is minimal % optCAT order where CAT is minimal % optPHI order where PHI is minimal % optRC2 max reflection coefficient larger than std-error % $Id: invest1.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008,2010 by Alois Schloegl % This is part of the TSA-toolbox. See also % http://biosig-consulting.com/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . N=length(Y); [nr,nc]=size(Y); if nc==1 Y=transpose(Y); nc=nr; nr=1; end; if nargin<2 Pmax=min([100 nc/3]); end; if exist('OCTAVE_VERSION'), fprintf(2,'Warning INVEST1: DIFF-based optimization not possible\n'); %%% missing DIM-argument in DIFF.M else %tmp=Y-mean(Y,2)*ones(1,nc); RMS(:,1) = mean(Y.^2,2); Dmax = min(Pmax,5); for k = 1:Dmax, RMS(:,k+1) = mean(diff(Y,k,2).^2,2); end; [tmp, orderDIFF] = min(RMS,[],2); % show a nice histogram h = histo3(orderDIFF-1); X = 0:Dmax; H = zeros(1,Dmax+1); for k=1:length(h.X), H(find(X==h.X(k)))=h.H(k); end; %X = 0:Dmax; H = zeros(1,Dmax+1); for k=1:length(x), H(find(X==x(k)))=h(k); end; bar(X,H); drawnow; if nargin>2 oD=0; else oD=input('Which order should be used for differentiating [default=0] ?: '); end; if oD>0 Y=diff(Y,oD,2); end; end; [AutoCov, AutoCorr, ARPMX, E, NC] = invest0(Y,Pmax); [FPE,AIC,BIC,SBC,MDL,CATcrit,PHI,optFPE,optAIC,optBIC,optSBC,optMDL,optCAT,optPHI,s,C] = selmo(E,NC); if 0, optRC2=zeros(nr+1,1); for k=0:nr, if k>0 optRC2(k+1)=max(find(abs(ARPMX(k,(1:Pmax).*(2:Pmax+1)/2))*sqrt(size(Y,2))>1)); else optRC2(k+1)=max(find(mean(abs(ARPMX(:,(1:Pmax).*(2:Pmax+1)/2))*sqrt(size(Y,2)),2)>1)); end; end; %GERSCH=min(find(rc.^2<(0.05/1.05))); s=[s optRC2]; end; %CRITERIA=([FPE;AIC;BIC;SBC;MDL;CATcrit;PHI])'; MOPS = s(1:size(s,1),:); %[optFPE optAIC optBIC optSBC optMDL optCAT optPHI]; if nargin==3, if size(ARPMX,2)==2*Pmax, %invest1(eeg8s,30,'s'); AR=ARPMX(:,1:Pmax); RC=ARPMX(:,Pmax+1:2*Pmax); else AR=ARPMX(:,Pmax/2*(Pmax-1)+(1:Pmax)); RC=ARPMX(:,(1:Pmax).*(2:Pmax+1)/2); end; oo=optBIC; sinvest1; end; tsa/inst/histo.m0000664002356700235670000000422312115040716014334 0ustar schloeglschloeglfunction [H,X]=histo(Y,Mode) % HISTO calculates histogram for each column % [H,X] = HISTO(Y,Mode) % % Mode % 'rows' : frequency of each row % '1x' : single bin-values % 'nx' : separate bin-values for each column % X are the bin-values % H is the frequency of occurence of value X % % HISTO(Y) with no output arguments: % plots the histogram bar(X,H) % % more histogram-based results can be obtained by HIST2RES2 % % see also: HISTO, HISTO2, HISTO3, HISTO4 % % REFERENCE(S): % C.E. Shannon and W. Weaver "The mathematical theory of communication" University of Illinois Press, Urbana 1949 (reprint 1963). % $Id: histo.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2002,2008 by Alois Schloegl % This is part of the TSA-toolbox % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if nargin<2, Mode='1x'; end; Mode=lower(Mode); if strcmp(Mode,'rows') R = histo4(Y); elseif strcmp(Mode,'column') R = histo4(Y'); R.X = R.X'; elseif strcmp(Mode,'1x') R = histo3(Y); elseif strcmp(Mode,'nx') R = histo2(Y); end; H = R.H; X = R.X; if nargout == 0, if any(size(X)==1), if exist('OCTAVE_VERSION')<5, bar(R.X,R.H,'stacked'); else bar(R.X,R.H); end else warning('2-dim X-values not supported\n') %bar3(R.X,R.H); end; end; tsa/inst/durlev.m0000664002356700235670000001044212115040716014507 0ustar schloeglschloeglfunction [MX,res,arg3] = durlev(AutoCov); % function [AR,RC,PE] = durlev(ACF); % function [MX,PE] = durlev(ACF); % estimates AR(p) model parameter by solving the % Yule-Walker with the Durbin-Levinson recursion % for multiple channels % INPUT: % ACF Autocorrelation function from lag=[0:p] % % OUTPUT % AR autoregressive model parameter % RC reflection coefficients (= -PARCOR coefficients) % PE remaining error variance % MX transformation matrix between ARP and RC (Attention: needs O(p^2) memory) % AR(:,K) = MX(:,K*(K-1)/2+(1:K)); % RC(:,K) = MX(:,(1:K).*(2:K+1)/2); % % All input and output parameters are organized in rows, one row % corresponds to the parameters of one channel % % see also ACOVF ACORF AR2RC RC2AR LATTICE % % REFERENCES: % Levinson N. (1947) "The Wiener RMS(root-mean-square) error criterion in filter design and prediction." J. Math. Phys., 25, pp.261-278. % Durbin J. (1960) "The fitting of time series models." Rev. Int. Stat. Inst. vol 28., pp 233-244. % P.J. Brockwell and R. A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % S. Haykin "Adaptive Filter Theory" 3rd ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % $Id: durlev.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . % Inititialization [lr,lc]=size(AutoCov); res=[AutoCov(:,1), zeros(lr,lc-1)]; d=zeros(lr,1); if nargout<3 % needs O(p^2) memory MX=zeros(lr,lc*(lc-1)/2); idx=0; idx1=0; % Durbin-Levinson Algorithm for K=1:lc-1, %idx=K*(K-1)/2; %see below % for L=1:lr, d(L)=arp(L,1:K-1)*transpose(AutoCov(L,K:-1:2));end; % Matlab 4.x, Octave % d=sum(MX(:,idx+(1:K-1)).*AutoCov(:,K:-1:2),2); % Matlab 5.x MX(:,idx+K)=(AutoCov(:,K+1)-sum(MX(:,idx1+(1:K-1)).*AutoCov(:,K:-1:2),2))./res(:,K); %rc(:,K)=arp(:,K); %if K>1 %for compatibility with OCTAVE 2.0.13 MX(:,idx+(1:K-1))=MX(:,idx1+(1:K-1))-MX(:,(idx+K)*ones(K-1,1)).*MX(:,idx1+(K-1:-1:1)); %end; % for L=1:lr, d(L)=MX(L,idx+(1:K))*(AutoCov(L,K+1:-1:2).');end; % Matlab 4.x, Octave % d=sum(MX(:,idx+(1:K)).*AutoCov(:,K+1:-1:2),2); % Matlab 5.x res(:,K+1) = res(:,K).*(1-abs(MX(:,idx+K)).^2); idx1=idx; idx=idx+K; end; %arp=MX(:,K*(K-1)/2+(1:K)); %rc =MX(:,(1:K).*(2:K+1)/2); else % needs O(p) memory arp=zeros(lr,lc-1); rc=zeros(lr,lc-1); % Durbin-Levinson Algorithm for K=1:lc-1, % for L=1:lr, d(L)=arp(L,1:K-1)*transpose(AutoCov(L,K:-1:2));end; % Matlab 4.x, Octave % d=sum(arp(:,1:K-1).*AutoCov(:,K:-1:2),2); % Matlab 5.x arp(:,K) = (AutoCov(:,K+1)-sum(arp(:,1:K-1).*AutoCov(:,K:-1:2),2))./res(:,K); % Yule-Walker rc(:,K) = arp(:,K); %if K>1 %for compatibility with OCTAVE 2.0.13 arp(:,1:K-1)=arp(:,1:K-1)-arp(:,K*ones(K-1,1)).*arp(:,K-1:-1:1); %end; %for L=1:lr, d(L)=arp(L,1:K)*(AutoCov(L,K+1:-1:2).');end; % Matlab 4.x, Octave % d=sum(arp(:,1:K).*AutoCov(:,K+1:-1:2),2); % Matlab 5.x res(:,K+1) = res(:,K).*(1-abs(arp(:,K)).^2); end; % assign output arguments arg3=res; res=rc; MX=arp; end; %if tsa/inst/ar2rc.m0000664002356700235670000000606212115040716014222 0ustar schloeglschloeglfunction [MX,res,arg3] = ar2rc(ar); % converts autoregressive parameters into reflection coefficients % with the Durbin-Levinson recursion for multiple channels % function [AR,RC,PE] = ar2rc(AR); % function [MX,PE] = ar2rc(AR); % % INPUT: % AR autoregressive model parameter % % OUTPUT % AR autoregressive model parameter % RC reflection coefficients (= -PARCOR coefficients) % PE remaining error variance (relative to PE(1)=1) % MX transformation matrix between ARP and RC (Attention: needs O(p^2) memory) % AR = MX(:,K*(K-1)/2+(1:K)); % RC = MX(:,(1:K).*(2:K+1)/2); % % All input and output parameters are organized in rows, one row % corresponds to the parameters of one channel % % see also ACOVF ACORF DURLEV RC2AR % % REFERENCES: % P.J. Brockwell and R. A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % S. Haykin "Adaptive Filter Theory" 3rd ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % $Id: ar2rc.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . % Inititialization [lr,lc]=size(ar); res=[ones(lr,1) zeros(lr,lc)]; if nargout<3 % needs O(p^2) memory MX=zeros(lr,lc*(lc+1)/2); MX(:,lc*(lc-1)/2+(1:lc))=ar; % Durbin-Levinson Algorithm idx=lc*(lc-1)/2; for K=lc:-1:2; %idx=K*(K-1)/2; %see below MX(:,(K-2)*(K-1)/2+(1:K-1)) = (MX(:,idx+(1:K-1)) + MX(:,(idx+K)*ones(K-1,1)).*MX(:,idx+(K-1:-1:1)))./((ones(lr,1)-abs(MX(:,idx+K)).^2)*ones(1,K-1)); idx=idx-K+1; end; for K=1:lc idx=K*(K-1)/2; %see below res(:,K+1) = res(:,K).*(1-abs(MX(:,idx+K)).^2); end; %arp=MX(:,K*(K-1)/2+(1:K)); %rc =MX(:,(1:K).*(2:K+1)/2); else % needs O(p) memory %ar=zeros(lr,lc); rc=zeros(lr,lc); rc(:,lc)=ar(:,lc); MX=ar; % assign output % Durbin-Levinson Algorithm for K=lc-1:-1:1, ar(:,1:K)=(ar(:,1:K)+ar(:,(K+1)*ones(K,1)).*ar(:,K:-1:1))./((ones(lr,1)-abs(ar(:,K+1)).^2)*ones(1,K)); rc(:,K)=ar(:,K); end; for K=1:lc, res(:,K+1) = res(:,K) .* (1-abs(ar(:,K)).^2); end; % assign output arguments arg3=res; res=rc; %MX=ar; end; %if tsa/inst/rc2ar.m0000664002356700235670000000651712115040716014227 0ustar schloeglschloeglfunction [MX,res,arg3,acf] = rc2ar(rc); % converts reflection coefficients into autoregressive parameters % uses the Durbin-Levinson recursion for multiple channels % function [AR,RC,PE,ACF] = rc2ar(RC); % function [MX,PE] = rc2ar(RC); % % INPUT: % RC reflection coefficients % % OUTPUT % AR autoregressive model parameter % RC reflection coefficients (= -PARCOR coefficients) % PE remaining error variance (relative to PE(1)=1) % MX transformation matrix between ARP and RC (Attention: needs O(p^2) memory) % arp=MX(:,K*(K-1)/2+(1:K)); % rc =MX(:,(1:K).*(2:K+1)/2); % % All input and output parameters are organized in rows, one row % corresponds to the parameters of one channel % % see also ACOVF ACORF DURLEV AR2RC % % REFERENCES: % P.J. Brockwell and R. A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % S. Haykin "Adaptive Filter Theory" 3rd ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % $Id: rc2ar.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (c) 1996-2002,2007,2008 by Alois Schloegl % This function is part of the TSA-toolbox % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . % Inititialization [lr,lc]=size(rc); res=[ones(lr,1) zeros(lr,lc)]; if nargout<3 % needs O(p^2) memory MX=zeros(lr,lc*(lc+1)/2); idx=0; % Durbin-Levinson Algorithm for K=1:lc, MX(:,idx+K)=rc(:,K);%(AutoCov(:,K+1)-d)./res(:,K); %rc(:,K)=arp(:,K); if K>1 %for compatibility with OCTAVE 2.0.13 MX(:,idx+(1:K-1))=MX(:,(K-2)*(K-1)/2+(1:K-1))-MX(:,(idx+K)*ones(K-1,1)).*MX(:,(K-2)*(K-1)/2+(K-1:-1:1)); end; res(:,K+1) = res(:,K).*(1-abs(MX(:,idx+K)).^2); idx=idx+K; end; %arp=MX(:,K*(K-1)/2+(1:K)); %rc =MX(:,(1:K).*(2:K+1)/2); ACF=cumprod(ones(lr,lr)-rc.^2,2); else % needs O(p) memory ar=zeros(lr,lc); acf=[ones(lr,1),zeros(lr,lc)]; %rc=RC; %zeros(lr,lc-1); % Durbin-Levinson Algorithm for K=1:lc, acf(:,K) = -sum(acf(:,K:-1:1).*ar(:,1:K),2); ar(:,K) = rc(:,K); if K>1, %for compatibility with OCTAVE 2.0.13 ar(:,1:K-1) = ar(:,1:K-1) - ar(:,K*ones(K-1,1)) .* ar(:,K-1:-1:1); end; res(:,K+1) = res(:,K) .* (1-abs(ar(:,K)).^2); end; ACF=cumprod(ones(lr,lc)-rc.^2,2); % assign output arguments arg3=res; res=rc; MX=ar; end; %if tsa/inst/aar.m0000664002356700235670000002612712115040716013760 0ustar schloeglschloeglfunction [a,e,REV,TOC,CPUTIME,ESU] = aar(y, Mode, arg3, arg4, arg5, arg6, arg7, arg8, arg9); % Calculates adaptive autoregressive (AAR) and adaptive autoregressive moving average estimates (AARMA) % of real-valued data series using Kalman filter algorithm. % [a,e,REV] = aar(y, mode, MOP, UC, a0, A, W, V); % % The AAR process is described as following % y(k) - a(k,1)*y(t-1) -...- a(k,p)*y(t-p) = e(k); % The AARMA process is described as following % y(k) - a(k,1)*y(t-1) -...- a(k,p)*y(t-p) = e(k) + b(k,1)*e(t-1) + ... + b(k,q)*e(t-q); % % Input: % y Signal (AR-Process) % Mode is a two-element vector [aMode, vMode], % aMode determines 1 (out of 12) methods for updating the co-variance matrix (see also [1]) % vMode determines 1 (out of 7) methods for estimating the innovation variance (see also [1]) % aMode=1, vmode=2 is the RLS algorithm as used in [2] % aMode=-1, LMS algorithm (signal normalized) % aMode=-2, LMS algorithm with adaptive normalization % % MOP model order, default [10,0] % MOP=[p] AAR(p) model. p AR parameters % MOP=[p,q] AARMA(p,q) model, p AR parameters and q MA coefficients % UC Update Coefficient, default 0 % a0 Initial AAR parameters [a(0,1), a(0,2), ..., a(0,p),b(0,1),b(0,2), ..., b(0,q)] % (row vector with p+q elements, default zeros(1,p) ) % A Initial Covariance matrix (positive definite pxp-matrix, default eye(p)) % W system noise (required for aMode==0) % V observation noise (required for vMode==0) % % Output: % a AAR(MA) estimates [a(k,1), a(k,2), ..., a(k,p),b(k,1),b(k,2), ..., b(k,q] % e error process (Adaptively filtered process) % REV relative error variance MSE/MSY % % % Hint: % The mean square (prediction) error of different variants is useful for determining the free parameters (Mode, MOP, UC) % % REFERENCE(S): % [1] A. Schloegl (2000), The electroencephalogram and the adaptive autoregressive model: theory and applications. % ISBN 3-8265-7640-3 Shaker Verlag, Aachen, Germany. % % More references can be found at % http://pub.ist.ac.at/~schloegl/publications/ % % $Id: aar.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2003 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [nc,nr]=size(y); %if nc=2 p=MOP(1); q=MOP(2); MOP=p+q; end; if nargin<4 UC=0; else UC= arg4; end; a0=zeros(1,MOP); A0=eye(MOP); if nargin>4, if all(size(arg5)==([1,1]*(MOP+1))); % extended covariance matrix of AAR parameters a0 = arg5(1,2:size(arg5,2)); A0 = arg5(2:size(arg5,1),2:size(arg5,2)) - a0'*a0; else a0 = arg5; if nargin>5 A0 = arg6; end; end; end; if nargin<7, W = []; else W = arg7; end; if all(size(W)==MOP), if aMode ~= 0, fprintf(1,'aMode should be 0, because W is given.\n'); end; elseif isempty(W), if aMode == 0, fprintf(1,'aMode must be non-zero, because W is not given.\n'); end; elseif any(size(W)~=MOP), fprintf(1,'size of W does not fit. It must be %i x %i.\n',MOP,MOP); return; end; if nargin<8, V0 = []; else V0 = arg8; end; if all(size(V0)==nr), if vMode ~= 0, fprintf(1,'vMode should be 0, because V is given.\n'); end; elseif isempty(V0), if aMode == 0, fprintf(1,'vMode must be non-zero, because V is not given.\n'); end; else fprintf(1,'size of V does not fit. It must be 1x1.\n'); return; end; % if nargin<7 TH=3; else TH = arg7; end; % TH=TH*var(y); % TH=TH*mean(detrend(y,0).^2); MSY=mean(detrend(y,0).^2); e=zeros(nc,1); Q=zeros(nc,1); V=zeros(nc,1); T=zeros(nc,1); %DET=zeros(nc,1); SPUR=zeros(nc,1); ESU=zeros(nc,1); a=a0(ones(nc,1),:); %a=zeros(nc,MOP); %b=zeros(nc,q); mu=1-UC; % Patomaeki 1995 lambda=(1-UC); % Schloegl 1996 arc=poly((1-UC*2)*[1;1]);b0=sum(arc); % Whale forgettting factor for Mode=258,(Bianci et al. 1997) dW=UC/MOP*eye(MOP); % Schloegl %------------------------------------------------ % First Iteration %------------------------------------------------ Y=zeros(MOP,1); C=zeros(MOP); %X=zeros(q,1); at=a0; A=A0; E=y(1); e(1)=E; if ~isempty(V0) V(1) = V0; else V(1) = (1-UC) + UC*E*E; end; ESU(1) = 1; %Y'*A*Y; A1=zeros(MOP);A2=A1; tic;CPUTIME=cputime; %------------------------------------------------ % Update Equations %------------------------------------------------ T0=2; for t=T0:nc, %Y=[y(t-1); Y(1:p-1); E ; Y(p+1:MOP-1)] if t<=p Y(1:t-1)=y(t-1:-1:1); % Autoregressive else Y(1:p)=y(t-1:-1:t-p); end; if t<=q Y(p+(1:t-1))=e(t-1:-1:1); % Moving Average else Y(p+1:MOP)=e(t-1:-1:t-q); end; % Prediction Error E = y(t) - a(t-1,:)*Y; e(t) = E; E2=E*E; AY=A*Y; esu=Y'*AY; ESU(t)=esu; if isnan(E), a(t,:)=a(t-1,:); else V(t) = V(t-1)*(1-UC)+UC*E2; if aMode == -1, % LMS % V(t) = V(t-1)*(1-UC)+UC*E2; a(t,:)=a(t-1,:) + (UC/MSY)*E*Y'; elseif aMode == -2, % LMS with adaptive estimation of the variance a(t,:)=a(t-1,:) + UC/V(t)*E*Y'; else % Kalman filtering (including RLS) if vMode==0, %eMode==4 Q(t) = (esu + V0); elseif vMode==1, %eMode==4 Q(t) = (esu + V(t)); elseif vMode==2, %eMode==2 Q(t) = (esu + 1); elseif vMode==3, %eMode==3 Q(t) = (esu + lambda); elseif vMode==4, %eMode==1 Q(t) = (esu + V(t-1)); elseif vMode==5, %eMode==6 if E2>esu V(t)=(1-UC)*V(t-1)+UC*(E2-esu); else V(t)=V(t-1); end; Q(t) = (esu + V(t)); elseif vMode==6, %eMode==7 if E2>esu V(t)=(1-UC)*V(t-1)+UC*(E2-esu); else V(t)=V(t-1); end; Q(t) = (esu + V(t-1)); elseif vMode==7, %eMode==8 Q(t) = esu; end; k = AY / Q(t); % Kalman Gain a(t,:) = a(t-1,:) + k'*E; if aMode==0, %AMode=0 A = A - k*AY' + W; % Schloegl et al. 2003 elseif aMode==1, %AMode=1 A = (1+UC)*(A - k*AY'); % Schloegl et al. 1997 elseif aMode==2, %AMode=11 A = A - k*AY'; A = A + sum(diag(A))*dW; elseif aMode==3, %AMode=5 A = A - k*AY' + sum(diag(A))*dW; elseif aMode==4, %AMode=6 A = A - k*AY' + UC*eye(MOP); % Schloegl 1998 elseif aMode==5, %AMode=2 A = A - k*AY' + UC*UC*eye(MOP); elseif aMode==6, %AMode=2 T(t)=(1-UC)*T(t-1)+UC*(E2-Q(t))/(Y'*Y); A=A*V(t-1)/Q(t); if T(t)>0 A=A+T(t)*eye(MOP); end; elseif aMode==7, %AMode=6 T(t)=(1-UC)*T(t-1)+UC*(E2-Q(t))/(Y'*Y); A=A*V(t)/Q(t); if T(t)>0 A=A+T(t)*eye(MOP); end; elseif aMode==8, %AMode=5 Q_wo = (Y'*C*Y + V(t-1)); C=A-k*AY'; T(t)=(1-UC)*T(t-1)+UC*(E2-Q_wo)/(Y'*Y); if T(t)>0 A=C+T(t)*eye(MOP); else A=C; end; elseif aMode==9, %AMode=3 A = A - (1+UC)*k*AY'; A = A + sum(diag(A))*dW; elseif aMode==10, %AMode=7 A = A - (1+UC)*k*AY' + sum(diag(A))*dW; elseif aMode==11, %AMode=8 A = A - (1+UC)*k*AY' + UC*eye(MOP); % Schloegl 1998 elseif aMode==12, %AMode=4 A = A - (1+UC)*k*AY' + UC*UC*eye(MOP); elseif aMode==13 A = A - k*AY' + UC*diag(diag(A)); elseif aMode==14 A = A - k*AY' + (UC*UC)*diag(diag(A)); end; end; end; end; %a=a(end,:); TOC = toc; CPUTIME = cputime - CPUTIME; %REV = (e'*e)/(y'*y); REV = mean(e.*e)./mean(y.*y); tsa/inst/ar_spa.m0000664002356700235670000000733212115040716014457 0ustar schloeglschloeglfunction [w,A,B,R,P,F,ip] = ar_spa(ARP,nhz,E); % AR_SPA decomposes an AR-spectrum into its compontents % [w,A,B,R,P,F,ip] = ar_spa(AR,fs,E); % % INPUT: % AR autoregressive parameters % fs sampling rate, provide w and B in [Hz], if not given the result is in radians % E noise level (mean square), gives A and F in units of E, if not given as relative amplitude % % OUTPUT % w center frequency % A Amplitude % B bandwidth % - less important output parameters - % R residual % P poles % ip number of complex conjugate poles % real(F) power, absolute values are obtained by multiplying with noise variance E(p+1) % imag(F) assymetry, - " - % % All input and output parameters are organized in rows, one row % corresponds to the parameters of one channel % % see also ACOVF ACORF DURLEV IDURLEV PARCOR YUWA % % REFERENCES: % [1] Zetterberg L.H. (1969) Estimation of parameter for linear difference equation with application to EEG analysis. Math. Biosci., 5, 227-275. % [2] Isaksson A. and Wennberg, A. (1975) Visual evaluation and computer analysis of the EEG - A comparison. Electroenceph. clin. Neurophysiol., 38: 79-86. % [3] G. Florian and G. Pfurtscheller (1994) Autoregressive model based spectral analysis with application to EEG. IIG - Report Series, University of Technolgy Graz, Austria. % $Id: ar_spa.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2003 by Alois Schloegl % This is part of the TSA-toolbox see also: % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % http://octave.sf.net/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [NTR,pp]=size(ARP); R=zeros(size(ARP)); P=zeros(size(ARP)); w=zeros(size(ARP)); A=zeros(size(ARP)); B=zeros(size(ARP)); F=zeros(size(ARP)); F1 = F; for k = 1:NTR, %if ~mod(k,100),k, end; [r,p,tmp] = residue(1,[1 -ARP(k,:)]); [tmp,idx] = sort(-abs(r)); R(k,:) = r(idx)'; % Residual, P(k,:) = p(idx)'; % Poles %r(k,:)=roots([1 -ARP(k,:)])'; w(k,:) = angle(p(idx)'); % center frequency (in [radians]) A(k,:) = 1./abs(polyval([1 -ARP(k,:)],exp(i*w(k,:)))); % Amplitude %A(k,:) = freqz(1,[1 -ARP(k,:)],w(k,:)); % Amplitude %A2(k,:) = abs(r)'./abs(exp(i*w(k,:))-r'); % Amplitude B(k,:) = -log(abs(p(idx)')); % Bandwidth if nargout < 6, elseif 0, F(k,:) = (1+sign(imag(r(idx)')))./(polyval([-ARP(k,pp-1:-1:1).*(1:pp-1) pp],1./p(idx).').*polyval([-ARP(k,pp:-1:1) 1],p(idx).')); elseif 1; a3 = polyval([-ARP(k,pp-1:-1:1).*(1:pp-1), pp],1./p(idx).'); a = polyval([-ARP(k,pp:-1:1) 1],p(idx).'); %F(k,:) = (1+(imag(P(k,:))~=0))./(a.*a3); F(k,:) = (1+sign(imag(P(k,:))))./(a.*a3); end; end; A = A.*sqrt(E(:,ones(1,pp))/(2*pi*nhz)); if nargin>1, if size(nhz,1)==1, nhz = nhz(ones(NTR,1),:); end; w = w.*nhz(:,ones(1,pp))/(2*pi); B = B.*nhz(:,ones(1,pp))/(2*pi); end; if nargin>2, F = F.*E(:,ones(1,pp)); F1 = F1.*E(:,ones(1,pp)); end; ip = sum(imag(P)~=0,2)/2; return; np(:,1) = sum(imag(P')==0)'; % number of real poles np(:,2) = pp-np(:,1); % number of imaginary poles tsa/inst/aarmam.m0000664002356700235670000001744012115040716014451 0ustar schloeglschloeglfunction [z,e,REV,ESU,V,Z,SPUR] = aarmam(y, Mode, MOP, UC, z0, Z0, V0, W); % Estimating Adaptive AutoRegressive-Moving-Average-and-mean model (includes mean term) % % !! This function is obsolete and is replaced by AMARMA % % [z,E,REV,ESU,V,Z,SPUR] = aarmam(y, mode, MOP, UC, z0, Z0, V0, W); % Estimates AAR parameters with Kalman filter algorithm % y(t) = sum_i(a_i(t)*y(t-i)) + m(t) + e(t) + sum_i(b_i(t)*e(t-i)) % % State space model % z(t) = G*z(t-1) + w(t) w(t)=N(0,W) % y(t) = H*z(t) + v(t) v(t)=N(0,V) % % G = I, % z = [m(t),a_1(t-1),..,a_p(t-p),b_1(t-1),...,b_q(t-q)]; % H = [1,y(t-1),..,y(t-p),e(t-1),...,e(t-q)]; % W = E{(z(t)-G*z(t-1))*(z(t)-G*z(t-1))'} % V = E{(y(t)-H*z(t-1))*(y(t)-H*z(t-1))'} % % % Input: % y Signal (AR-Process) % Mode determines the type of algorithm % % MOP Model order [m,p,q], default [0,10,0] % m=1 includes the mean term, m=0 does not. % p and q must be positive integers % it is recommended to set q=0. % UC Update Coefficient, default 0 % z0 Initial state vector % Z0 Initial Covariance matrix % % Output: % z AR-Parameter % E error process (Adaptively filtered process) % REV relative error variance MSE/MSY % % REFERENCE(S): % [1] A. Schloegl (2000), The electroencephalogram and the adaptive autoregressive model: theory and applications. % ISBN 3-8265-7640-3 Shaker Verlag, Aachen, Germany. % % More references can be found at % http://pub.ist.ac.at/~schloegl/publications/ % $Id: aarmam.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008,2012 by Alois Schloegl % This is part of the TSA-toolbox. See also % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % http://octave.sourceforge.net/ % http://biosig.sourceforge.net/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . %#realonly %#inbounds warning('AARMAM is obsolete. Use AMARMA instead!') [nc,nr]=size(y); if nargin<2 Mode=0; elseif ischar(Mode) Mode=bin2dec(Mode); elseif isnan(Mode) return; end; if nargin<3 MOP=[0,10,0]; end; if length(MOP)==0, m=0;p=10; q=0; MOP=p; elseif length(MOP)==1, m=0;p=MOP(1); q=0; MOP=p; elseif length(MOP)==2, fprintf(1,'Error AMARMA: MOP is ambiguos\n'); elseif length(MOP)>2, m=MOP(1); p=MOP(2); q=MOP(3);MOP=m+p+q; end; if prod(size(Mode))>1 aMode=Mode(1); eMode=Mode(2); end; %fprintf(1,['a' int2str(aMode) 'e' int2str(eMode) ' ']); e=zeros(nc,1); V=zeros(nc,1);V(1)=V0; T=zeros(nc,1); ESU=zeros(nc,1)+nan; SPUR=zeros(nc,1)+nan; z=z0(ones(nc,1),:); arc=poly((1-UC*2)*[1;1]);b0=sum(arc); % Whale forgetting factor for Mode=258,(Bianci et al. 1997) dW=UC/MOP*eye(MOP); % Schloegl %------------------------------------------------ % First Iteration %------------------------------------------------ H = zeros(MOP,1); if m, H(1) = 1;%M0; if m~=1, fprintf(2,'Warning AARMAM: m must be 0 or 1\n'); return; end; end; if (p<0) || (q<0) || (round(p)~=p) || (round(q)~=q), fprintf(2,'Error AARMAM: p and q must be positive integers\n'); return; end; E = 0; Z = Z0; zt= z0; A1 = zeros(MOP); A2 = A1; y_1=0; %------------------------------------------------ % Update Equations %------------------------------------------------ for t=1:nc, % make measurement matrix if 0, if t>1, y_1 = y(t-1); end; H=[1; y_1; H(m+(1:p-1)'); E(1:min(1,q-1)) ; H(p+m+(1:q-1)')]; % shift y and e else % this seem to be slightly faster if t<=p, H(m+(1:t-1)) = y(t-1:-1:1); % Autoregressive else H(m+(1:p)) = y(t-1:-1:t-p); end; if t<=q, H(m+p+(1:t-1)) = e(t-1:-1:1); % Moving Average else H(m+p+(1:q)) = e(t-1:-1:t-q); end; end; % Prediction Error E = y(t) - zt*H; e(t) = E; if ~isnan(E), E2 = E*E; AY = Z*H; ESU(t) = H'*AY; if eMode==1 V0 = V(t-1); V(t) = V0*(1-UC)+UC*E2; elseif eMode==2 V0 = 1; V(t) = V0; %V(t-1)*(1-UC)+UC*E2; elseif eMode==3 V0 = 1-UC; V(t) = V0; %(t-1)*(1-UC)+UC*E2; elseif eMode==4 V0 = V0*(1-UC)+UC*E2; V(t) = V0; elseif eMode==5 V(t)=V0; %V0 = V0; elseif eMode==6 if E2>ESU(t), V0=(1-UC)*V0+UC*(E2-ESU(t)); end; V(t)=V0; elseif eMode==7 V0=V(t); if E2>ESU(t) V(t) = (1-UC)*V0+UC*(E2-ESU(t)); else V(t) = V0; end; elseif eMode==8 V0=0; V(t) = V0; % (t-1)*(1-UC)+UC*E2; end; k = AY / (ESU(t) + V0); % Kalman Gain zt = zt + k'*E; %z(t,:) = zt; if aMode==2 T(t)=(1-UC)*T(t-1)+UC*(E2-Q(t))/(H'*H); % Roberts I 1998 Z=Z*V(t-1)/Q(t); if T(t)>0 W=T(t)*eye(MOP); else W=zeros(MOP);end; elseif aMode==5 Q_wo = (H'*C*H + V(t-1)); % Roberts II 1998 T(t)=(1-UC)*T(t-1)+UC*(E2-Q_wo)/(H'*H); if T(t)>0 W=T(t)*eye(MOP); else W=zeros(MOP); end; elseif aMode==6 T(t)=(1-UC)*T(t-1)+UC*(E2-Q(t))/(H'*H); Z=Z*V(t)/Q(t); if T(t)>0 W=T(t)*eye(MOP); else W=zeros(MOP); end; elseif aMode==11 %Z = Z - k*AY'; W = sum(diag(Z))*dW; elseif aMode==12 W = UC*UC*eye(MOP); elseif aMode==13 W = UC*diag(diag(Z)); elseif aMode==14 W = (UC*UC)*diag(diag(Z)); elseif aMode==15 W = sum(diag(Z))*dW; elseif aMode==16 W = UC*eye(MOP); % Schloegl 1998 %elseif aMode==17 %W=W; end; Z = Z - k*AY'; % Schloegl 1998 else V(t) = V0; end; if any(any(isnan(W))), W=UC*Z;end; z(t,:) = zt; Z = Z + W; % Schloegl 1998 SPUR(t) = trace(Z); end; REV = mean(e.*e)/mean(y.*y); if any(~isfinite(Z(:))), REV=inf; end; tsa/inst/lattice.m0000664002356700235670000001174712115040716014644 0ustar schloeglschloegl function [MX,PE,arg3] = lattice(Y,lc,Mode); % Estimates AR(p) model parameter with lattice algorithm (Burg 1968) % for multiple channels. % If you have the NaN-tools, LATTICE.M can handle missing values (NaN), % % [...] = lattice(y [,Pmax [,Mode]]); % % [AR,RC,PE] = lattice(...); % [MX,PE] = lattice(...); % % INPUT: % y signal (one per row), can contain missing values (encoded as NaN) % Pmax max. model order (default size(y,2)-1)) % Mode 'BURG' (default) Burg algorithm % 'GEOL' geometric lattice % % OUTPUT % AR autoregressive model parameter % RC reflection coefficients (= -PARCOR coefficients) % PE remaining error variance % MX transformation matrix between ARP and RC (Attention: needs O(p^2) memory) % AR(:,K) = MX(:, K*(K-1)/2+(1:K)); = MX(:,sum(1:K-1)+(1:K)); % RC(:,K) = MX(:,cumsum(1:K)); = MX(:,(1:K).*(2:K+1)/2); % % All input and output parameters are organized in rows, one row % corresponds to the parameters of one channel % % see also ACOVF ACORF AR2RC RC2AR DURLEV SUMSKIPNAN % % REFERENCE(S): % J.P. Burg, "Maximum Entropy Spectral Analysis" Proc. 37th Meeting of the Society of Exp. Geophysiscists, Oklahoma City, OK 1967 % J.P. Burg, "Maximum Entropy Spectral Analysis" PhD-thesis, Dept. of Geophysics, Stanford University, Stanford, CA. 1975. % P.J. Brockwell and R. A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % S. Haykin "Adaptive Filter Theory" 3rd ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % $Id: lattice.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2002,2008,2010 by Alois Schloegl % This is part of the TSA-toolbox. See also % http://biosig-consulting.com/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if nargin<3, Mode='BURG'; else Mode=upper(Mode(1:4));end; BURG=~strcmp(Mode,'GEOL'); % Inititialization [lr,N]=size(Y); if nargin<2, lc=N-1; end; F=Y; B=Y; [DEN,nn] = sumskipnan((Y.*Y),2); PE = [DEN./nn,zeros(lr,lc)]; if nargout<3 % needs O(p^2) memory MX = zeros(lr,lc*(lc+1)/2); idx= 0; % Durbin-Levinson Algorithm for K=1:lc, [TMP,nn] = sumskipnan(F(:,K+1:N).*B(:,1:N-K),2); MX(:,idx+K) = TMP./DEN; %Burg if K>1, %for compatibility with OCTAVE 2.0.13 MX(:,idx+(1:K-1))=MX(:,(K-2)*(K-1)/2+(1:K-1))-MX(:,(idx+K)*ones(K-1,1)).*MX(:,(K-2)*(K-1)/2+(K-1:-1:1)); end; tmp = F(:,K+1:N) - MX(:,(idx+K)*ones(1,N-K)).*B(:,1:N-K); B(:,1:N-K) = B(:,1:N-K) - MX(:,(idx+K)*ones(1,N-K)).*F(:,K+1:N); F(:,K+1:N) = tmp; [PE(:,K+1),nn] = sumskipnan([F(:,K+1:N).^2,B(:,1:N-K).^2],2); if ~BURG, [f,nf] = sumskipnan(F(:,K+1:N).^2,2); [b,nb] = sumskipnan(B(:,1:N-K).^2,2); DEN = sqrt(b.*f); else DEN = PE(:,K+1); end; idx=idx+K; PE(:,K+1) = PE(:,K+1)./nn; % estimate of covariance end; else % needs O(p) memory arp=zeros(lr,lc-1); rc=zeros(lr,lc-1); % Durbin-Levinson Algorithm for K=1:lc, [TMP,nn] = sumskipnan(F(:,K+1:N).*B(:,1:N-K),2); arp(:,K) = TMP./DEN; %Burg rc(:,K) = arp(:,K); if K>1, % for compatibility with OCTAVE 2.0.13 arp(:,1:K-1) = arp(:,1:K-1) - arp(:,K*ones(K-1,1)).*arp(:,K-1:-1:1); end; tmp = F(:,K+1:N) - rc(:,K*ones(1,N-K)).*B(:,1:N-K); B(:,1:N-K) = B(:,1:N-K) - rc(:,K*ones(1,N-K)).*F(:,K+1:N); F(:,K+1:N) = tmp; [PE(:,K+1),nn] = sumskipnan([F(:,K+1:N).^2,B(:,1:N-K).^2],2); if ~BURG, [f,nf] = sumskipnan(F(:,K+1:N).^2,2); [b,nb] = sumskipnan(B(:,1:N-K).^2,2); DEN = sqrt(b.*f); else DEN = PE(:,K+1); end; PE(:,K+1) = PE(:,K+1)./nn; % estimate of covariance end; % assign output arguments arg3=PE; PE=rc; MX=arp; end; %if tsa/inst/covm.m0000664002356700235670000001560012264575422014167 0ustar schloeglschloeglfunction [CC,NN] = covm(X,Y,Mode,W) % COVM generates covariance matrix % X and Y can contain missing values encoded with NaN. % NaN's are skipped, NaN do not result in a NaN output. % The output gives NaN only if there are insufficient input data % % COVM(X,Mode); % calculates the (auto-)correlation matrix of X % COVM(X,Y,Mode); % calculates the crosscorrelation between X and Y % COVM(...,W); % weighted crosscorrelation % % Mode = 'M' minimum or standard mode [default] % C = X'*X; or X'*Y correlation matrix % % Mode = 'E' extended mode % C = [1 X]'*[1 X]; % l is a matching column of 1's % C is additive, i.e. it can be applied to subsequent blocks and summed up afterwards % the mean (or sum) is stored on the 1st row and column of C % % Mode = 'D' or 'D0' detrended mode % the mean of X (and Y) is removed. If combined with extended mode (Mode='DE'), % the mean (or sum) is stored in the 1st row and column of C. % The default scaling is factor (N-1). % Mode = 'D1' is the same as 'D' but uses N for scaling. % % C = covm(...); % C is the scaled by N in Mode M and by (N-1) in mode D. % [C,N] = covm(...); % C is not scaled, provides the scaling factor N % C./N gives the scaled version. % % see also: DECOVM, XCOVF % $Id: covm.m 9032 2011-11-08 20:25:36Z schloegl $ % Copyright (C) 2000-2005,2009 by Alois Schloegl % This function is part of the NaN-toolbox % http://pub.ist.ac.at/~schloegl/matlab/NaN/ % This program is free software; you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation; either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program; If not, see . global FLAG_NANS_OCCURED; if nargin<3, W = []; if nargin==2, if isnumeric(Y), Mode='M'; else Mode=Y; Y=[]; end; elseif nargin==1, Mode = 'M'; Y = []; elseif nargin==0, error('Missing argument(s)'); end; elseif (nargin==3) && isnumeric(Y) && ~isnumeric(Mode); W = []; elseif (nargin==3) && ~isnumeric(Y) && isnumeric(Mode); W = Mode; Mode = Y; Y = []; elseif (nargin==4) && ~isnumeric(Mode) && isnumeric(Y); ; %% ok else error('invalid input arguments'); end; Mode = upper(Mode); [r1,c1]=size(X); if ~isempty(Y) [r2,c2]=size(Y); if r1~=r2, error('X and Y must have the same number of observations (rows).'); end; else [r2,c2]=size(X); end; persistent mexFLAG2; persistent mexFLAG; if isempty(mexFLAG2) mexFLAG2 = exist('covm_mex','file'); end; if isempty(mexFLAG) mexFLAG = exist('sumskipnan_mex','file'); end; if ~isempty(W) W = W(:); if (r1~=numel(W)) error('Error COVM: size of weight vector does not fit number of rows'); end; %w = spdiags(W(:),0,numel(W),numel(W)); %nn = sum(W(:)); nn = sum(W); else nn = r1; end; if mexFLAG2 && mexFLAG && ~isempty(W), %% the mex-functions here are much slower than the m-scripts below %% however, the mex-functions support weighting of samples. if isempty(FLAG_NANS_OCCURED), %% mex-files require that FLAG_NANS_OCCURED is not empty, %% otherwise, the status of NAN occurence can not be returned. FLAG_NANS_OCCURED = logical(0); % default value end; if any(Mode=='D') || any(Mode=='E'), [S1,N1] = sumskipnan(X,1,W); if ~isempty(Y) [S2,N2] = sumskipnan(Y,1,W); else S2 = S1; N2 = N1; end; if any(Mode=='D'), % detrending mode X = X - ones(r1,1)*(S1./N1); if ~isempty(Y) Y = Y - ones(r1,1)*(S2./N2); end; end; end; [CC,NN] = covm_mex(real(X), real(Y), FLAG_NANS_OCCURED, W); %% complex matrices if ~isreal(X) && ~isreal(Y) [iCC,inn] = covm_mex(imag(X), imag(Y), FLAG_NANS_OCCURED, W); CC = CC + iCC; end; if isempty(Y) Y = X; end; if ~isreal(X) [iCC,inn] = covm_mex(imag(X), real(Y), FLAG_NANS_OCCURED, W); CC = CC - i*iCC; end; if ~isreal(Y) [iCC,inn] = covm_mex(real(X), imag(Y), FLAG_NANS_OCCURED, W); CC = CC + i*iCC; end; if any(Mode=='D') && ~any(Mode=='1'), % 'D1' NN = max(NN-1,0); end; if any(Mode=='E'), % extended mode NN = [nn, N2; N1', NN]; CC = [nn, S2; S1', CC]; end; elseif ~isempty(W), error('Error COVM: weighted COVM requires sumskipnan_mex and covm_mex but it is not available'); %% weighted covm without mex-file support %% this part is not working. elseif ~isempty(Y), if (~any(Mode=='D') && ~any(Mode=='E')), % if Mode == M NN = real(X==X)'*real(Y==Y); FLAG_NANS_OCCURED = any(NN(:) % This is part of the TSA-toolbox. See also % http://biosig-consulting.com/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if nargin<3, Mode='biased'; end; [lr,lc] = size(Z); MISSES = sum(isnan(Z)')'; if any(MISSES); % missing values M = real(~isnan(Z)); Z(isnan(Z))=0; end; if (nargin == 1) KMAX = lc-1; elseif (KMAX >= lc-1) KMAX = lc-1; end; ACF = zeros(lr,KMAX+1); if nargin>3, % for testing, use arg4 for comparing the methods, elseif (KMAX*KMAX > lc*log2(lc)) % & isempty(MISSES); Mode2 = 1; elseif (10*KMAX > lc); Mode2 = 3; else Mode2 = 4; end; %%%%% ESTIMATION of non-normalized ACF %%%%% % the following algorithms gve equivalent results, however, the computational effort is different, % depending on lr,lc and KMAX, a different algorithm is most efficient. if Mode2==1; % KMAX*KMAX > lc*log(lc); % O(n.logn)+O(K²) tmp = fft(Z',2^nextpow2(size(Z,2))*2); tmp = ifft(tmp.*conj(tmp)); ACF = tmp(1:KMAX+1,:)'; if ~any(any(imag(Z))), ACF=real(ACF); end; % should not be neccessary, unfortunately it is. elseif Mode2==3; % (10*KMAX > lc) % O(n*K) % use fast Built-in filter function for L = 1:lr, acf = filter(Z(L,lc:-1:1),1,Z(L,:)); ACF(L,:)= acf(lc:-1:lc-KMAX); end; else Mode2==4; % O(n*K) for L = 1:lr, for K = 0:KMAX, ACF(L,K+1) = Z(L,1:lc-K) * Z(L,1+K:lc)'; end; end; end; %%%%% GET number of elements used for estimating ACF - is needed for normalizing ACF %%%%% if any(MISSES), % the following algorithms gve equivalent results, however, the computational effort is different, % depending on lr,lc and KMAX, a different algorithm is most efficient. if Mode2==1; % KMAX*KMAX > lc*log(lc); % O(n.logn)+O(K²) tmp = fft(M',2^nextpow2(size(M,2))*2); tmp = ifft(tmp.*conj(tmp)); NN = tmp(1:KMAX+1,:)'; if ~any(any(imag(M))), NN=real(NN); end; % should not be neccessary, unfortunately it is. elseif Mode2==3; % (10*KMAX > lc) % O(n*K) % use fast Built-in filter function for L = 1:lr, acf = filter(M(L,lc:-1:1),1,M(L,:)); NN(L,:)= acf(lc:-1:lc-KMAX); end; else Mode2==4; % O(n*K) for L = 1:lr, for K = 0:KMAX, NN(L,K+1) = M(L,1:lc-K) * M(L,1+K:lc)'; end; end; end; else NN = (ones(lr,1)*(lc:-1:lc-KMAX)); end; if strcmp(Mode,'biased') if ~any(MISSES), ACF=ACF/lc; else %ACF=ACF./((lc-MISSES)*ones(1,KMAX+1)); ACF=ACF./max(NN + ones(lr,1)*(0:KMAX),0); end; elseif strcmp(Mode,'unbiased') ACF=ACF./NN; %if ~any(MISSES), % ACF=ACF./(ones(lr,1)*(lc:-1:lc-KMAX)); %else % ACF=ACF./((lc-MISSES)*ones(1,KMAX+1) - ones(lr,1)*(0:KMAX)); %end; elseif strcmp(Mode,'coeff') %ACF = ACF ./ ACF(:,ones(1,KMAX+1)) .* ((lc-MISSES)*ones(1,KMAX+1)); ACF = ACF./NN; ACF = ACF./(ACF(:,1)*ones(1,size(ACF,2))); else end; tsa/inst/parcor.m0000664002356700235670000000403712115040716014477 0ustar schloeglschloeglfunction [PARCOR,ARP, PE] = parcor(AutoCov); % estimates partial autocorrelation coefficients % Multiple channels can be used (one per row). % % [PARCOR, AR, PE] = parcor(AutoCov); % calculates Partial autocorrelation, autoregressive coefficients and residual error variance from the Autocorrelation function. % % [PARCOR] = parcor(acovf(x,p)); % calculates the Partial Autocorrelation coefficients of the data series x up to order p % % INPUT: % AutoCov Autocorrelation function for lag=0:P % % OUTPUT % AR autoregressive model parameter % PARCOR partial correlation coefficients (= -reflection coefficients) % PE remaining error variance % % All input and output parameters are organized in rows, one row % corresponds to the parameters of one channel. % The PARCOR coefficients are the negative reflection coefficients. % A significance test is implemented in PACF. % % see also: PACF ACOVF ACORF DURLEV AC2RC % % REFERENCES: % P.J. Brockwell and R.A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % S. Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % $Id: parcor.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1997-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [ARP,PARCOR,PE] = durlev(AutoCov); PARCOR=-PARCOR; tsa/inst/sbispec.m0000664002356700235670000000225012115040716014634 0ustar schloeglschloeglfunction sbispec(BISPEC) % SBISPEC show BISPECTRUM % $Id: sbispec.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1997, 1998, 2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [s1,s2]=size(BISPEC); t1=(0:s1-1)/max(s1); t2=(0:s2/2-1)/max(s2); tmp=tril(NaN*ones(s1,s2),-1); BISPEC=BISPEC+tmp+rot90(tmp); BISPEC=BISPEC(1:s1/2,:); subplot(211); mesh(t1,t2,abs(BISPEC)); title('Bispectrum - mesh plot'); subplot(212); contour(t1,t2,abs(BISPEC)); %contour(t1,t2,log(abs(BISPEC))); title('Bispectrum - contour plot'); tsa/inst/ac2rc.m0000664002356700235670000000254712115040716014207 0ustar schloeglschloeglfunction [RC,efinal] = ac2rc(AC); % converts the autocorrelation function into reflection coefficients % [RC,r0] = ac2rc(r) % % see also ACOVF ACORF AR2RC RC2AR DURLEV AC2POLY, POLY2RC, RC2POLY, RC2AC, AC2RC, POLY2AC % % $Id: ac2rc.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if all(size(AC))>1, fprintf(2,'Error AC2RC: "r" must be a vector\n'); return; end; mfilename='AC2RC'; if ~exist('durlev','file') fprintf(2,'Error %s: DURLEV.M not found. \n Download TSA toolbox from http://pub.ist.ac.at/~schloegl/matlab/tsa/\n',mfilename); return; end; [AR,RC,PE] = durlev(AC(:).'); RC=-RC; efinal=AC(1); tsa/inst/flix.m0000664002356700235670000000352512115040716014154 0ustar schloeglschloeglfunction Y=flix(D,x) % floating point index - interpolates data in case of non-integer indices % % Y=flix(D,x) % FLIX returns Y=D(x) if x is an integer % otherwise D(x) is interpolated from the neighbors D(ceil(x)) and D(floor(x)) % % Applications: % (1) discrete Dataseries can be upsampled to higher sampling rate % (2) transformation of non-equidistant samples to equidistant samples % (3) [Q]=flix(sort(D),q*(length(D)+1)) calculates the q-quantile of data series D % % FLIX(D,x) is the same as INTERP1(D,X,'linear'); Therefore, FLIX might % become obsolete in future. % % see also: HIST2RES, Y2RES, PLOTCDF, INTERP1 % $Id: flix.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) by 2001-2005,2008 Alois Schloegl % This is part of the TSA-toolbox see also: % http://www.dpmi.tu-graz.ac.at/schloegl/matlab/tsa/ % http://octave.sf.net/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . D = D(:); Y = x; k1 = ((x >= 1) & (x <= size(D,1))); Y(~k1) = NaN; k = x - floor(x); % distance to next sample ix = ~k & k1; % find integer indices Y(ix) = D(x(ix)); % put integer indices ix = k & k1; % find non-integer indices Y(ix) = D(floor(x(ix))).*(1-k(ix)) + D(ceil(x(ix))).*k(ix); tsa/inst/tsademo.m0000664002356700235670000000247612115040716014652 0ustar schloeglschloegl% TSADEMO demonstrates INVEST1 on EEG data % $Id: tsademo.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if exist('OCTAVE_VERSION')>5; load -force eeg8s.mat elseif 1 load eeg8s.mat else [FileName, PathName]=uigetfile('eeg8s.mat','load demo data EEG8S.MAT'); load([PathName FileName],'eeg8s'); end; s = eeg8s'; Pmax=100; [AutoCov,AutoCorr,ARPMX,E,CRITERIA,MOPS]=invest1(s,Pmax,'s'); if size(ARPMX,2)==2*Pmax, %invest1(eeg8s,30,'s'); AR=ARPMX(:,1:Pmax); RC=ARPMX(:,Pmax+1:2*Pmax); else AR=ARPMX(:,Pmax/2*(Pmax-1)+(1:Pmax)); RC=ARPMX(:,(1:Pmax).*(2:Pmax+1)/2); end; tsa/inst/bisdemo.m0000664002356700235670000000227312115040716014633 0ustar schloeglschloegl% BISDEMO (script) Shows BISPECTRUM of eeg8s.mat % $Id: bisdemo.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1997, 1998,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . load eeg8s.mat; [BISPEC,BICOV,ACF]=bispec(eeg8s,30); [s1,s2]=size(BISPEC); t1=(1:s1)/max(s1); t2=(1:s2)/max(s2); subplot(211); mesh(t1,t2,abs(BISPEC)); title('Bispectrum - mesh plot'); subplot(212); if exist('OCTAVE_VERSION')>5 contour(abs(BISPEC),10,t1,t2); else contour(t1,t2,abs(BISPEC),10); end; title('Bispectrum - contour plot'); tsa/inst/content.m0000664002356700235670000001157112115040716014664 0ustar schloeglschloegl% Time Series Analysis (Ver 3.10) % Schloegl A. (1996-2003,2008) Time Series Analysis - A Toolbox for the use with Matlab. % WWW: http://pub.ist.ac.at/~schloegl/matlab/tsa/ % % $Id: content.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2003,2008 by Alois Schloegl % % Time Series Analysis - a toolbox for the use with Matlab % aar adaptive autoregressive estimator % acovf (*) Autocovariance function % acorf (acf) (*) autocorrelation function % pacf (*) partial autocorrelation function, includes signifcance test and confidence interval % parcor (*) partial autocorrelation function % biacovf biautocovariance function (3rd order cumulant) % bispec Bi-spectrum % durlev (*) solves Yule-Walker equation - converts ACOVF into AR parameters % lattice (*) calcultes AR parameters with lattice method % lpc (*) calculates the prediction coefficients form a given time series % invest0 (*) a prior investigation (used by invest1) % invest1 (*) investigates signal (useful for 1st evaluation of the data) % selmo (*) Select Order of Autoregressive model using different criteria % histo (*) histogram % hup (*) test Hurwitz polynomials % ucp (*) test Unit Circle Polynomials % y2res (*) computes mean, variance, skewness, kurtosis, entropy, etc. from data series % ar_spa (*) spectral analysis based on the autoregressive model % detrend (*) removes trend, can handle missing values, non-equidistant sampled data % flix floating index, interpolates data for non-interger indices % quantiles calculates quantiles % % Multivariate analysis (planned in future) % mvar multivariate (vector) autoregressive estimation % mvfilter multivariate filter % arfit2 provides compatibility to ARFIT [Schneider and Neumaier, 2001] % Conversions between Autocorrelation (AC), Autoregressive parameters (AR), % prediction polynom (POLY) and Reflection coefficient (RC) % ac2poly (*) transforms autocorrelation into prediction polynom % ac2rc (*) transforms autocorrelation into reflexion coefficients % ar2rc (*) transforms autoregressive parameters into reflection coefficients % rc2ar (*) transforms reflection coefficients into autoregressive parameters % poly2ac (*) transforms polynom to autocorrelation % poly2ar (*) transforms polynom to AR % poly2rc (*) % rc2ac (*) % rc2poly (*) % ar2poly (*) % % Utility functions % sinvest1 shows the parameter calculated by INVEST1 % % Test suites % tsademo demonstrates INVEST1 on EEG data % invfdemo demonstration of matched, inverse filtering % bisdemo demonstrates bispectral estimation % % (*) indicates univariate analysis of multiple data series (each in a row) can be processed. % (-) indicates that these functions will be removed in future % % REFERENCES (sources): % http://www.itl.nist.gov/ % http://mathworld.wolfram.com/ % P.J. Brockwell and R.A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % O. Foellinger "Lineare Abtastsysteme", Oldenburg Verlag, Muenchen, 1986. % F. Gausch "Systemtechnik", Textbook, University of Technology Graz, 1993. % M.S. Grewal and A.P. Andrews "Kalman Filtering" Prentice Hall, 1993. % S. Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996. % E.I. Jury "Theory and Application of the z-Transform Method", Robert E. Krieger Publishing Co., 1973. % M.S. Kay "Modern Spectal Estimation" Prentice Hall, 1988. % Ch. Langraf and G. Schneider "Elemente der Regeltechnik", Springer Verlag, 1970. % S.L. Marple "Digital Spetral Analysis with Applications" Prentice Hall, 1987. % C.L. Nikias and A.P. Petropulu "Higher-Order Spectra Analysis" Prentice Hall, 1993. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % T. Schneider and A. Neumaier "Algorithm 808: ARFIT - a matlab package for the estimation of parameters and eigenmodes of multivariate autoregressive models" % ACM Transactions on Mathematical software, 27(Mar), 58-65. % C.E. Shannon and W. Weaver "The mathematical theory of communication" University of Illinois Press, Urbana 1949 (reprint 1963). % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % % % REFERENCES (applications): % [1] A. Schlögl, B. Kemp, T. Penzel, D. Kunz, S.-L. Himanen,A. Värri, G. Dorffner, G. Pfurtscheller. % Quality Control of polysomnographic Sleep Data by Histogram and Entropy Analysis. % Clin. Neurophysiol. 1999, Dec; 110(12): 2165 - 2170. % [2] Penzel T, Kemp B, Klösch G, Schlögl A, Hasan J, Varri A, Korhonen I. % Acquisition of biomedical signals databases % IEEE Engineering in Medicine and Biology Magazine 2001, 20(3): 25-32 % % Features: % - Multiple Signal Processing % - Efficient algorithms % - Model order selection tools % - higher (3rd) order analysis % - Maximum entropy spectral estimation % - can deal with missing values (NaN's) tsa/inst/y2res.m0000664002356700235670000000737012115040716014260 0ustar schloeglschloeglfunction [R]=y2res(Y) % Y2RES evaluates basic statistics of a data series % % R = y2res(y) % several statistics are estimated from each column of y % % OUTPUT: % R.N number of samples, NaNs are not counted % R.SUM sum of samples % R.MEAN mean % R.STD standard deviation % R.VAR variance % R.Max Maximum % R.Min Minimum % ... and many more including: % MEDIAN, Quartiles, Variance, standard error of the mean (SEM), % Coefficient of Variation, Quantization (QUANT), TRIMEAN, SKEWNESS, % KURTOSIS, Root-Mean-Square (RMS), ENTROPY % % $Id: y2res.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2005,2008 by Alois Schloegl % This is part of the TSA-toolbox % http://octave.sourceforge.net/ % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [R.SUM, R.N, R.SSQ] = sumskipnan(Y,1); %R.S3P = sumskipnan(Y.^3,1); R.S4P = sumskipnan(Y.^4,1); %R.S5P = sumskipnan(Y.^5,1); R.MEAN = R.SUM./R.N; R.MSQ = R.SSQ./R.N; R.RMS = sqrt(R.MSQ); R.SSQ0 = R.SSQ-R.SUM.*R.MEAN; % sum square of mean removed if 1,%flag_implicit_unbiased_estim, n1 = max(R.N-1,0); % in case of n=0 and n=1, the (biased) variance, STD and STE are INF else n1 = R.N; end; R.VAR = R.SSQ0./n1; % variance (unbiased) R.STD = sqrt(R.VAR); % standard deviation R.SEM = sqrt(R.SSQ0./(R.N.*n1)); % standard error of the mean R.SEV = sqrt(n1.*(n1.*R.S4P./R.N+(R.N.^2-2*R.N+3).*(R.SSQ./R.N).^2)./(R.N.^3)); % standard error of the variance R.Coefficient_of_variation = R.STD./R.MEAN; R.CM2 = R.SSQ0./n1; R.Max = max(Y,[],1); R.Min = min(Y,[],1); %R.NormEntropy = log2(sqrt(2*pi*exp(1)))+log2(R.STD); Q0500=repmat(nan,1,size(Y,2)); Q0250=Q0500; Q0750=Q0500; %MODE=Q0500; for k = 1:size(Y,2), tmp = sort(Y(:,k)); Q0250(k) = flix(tmp,R.N(k)/4 + 0.75); Q0500(k) = flix(tmp,R.N(k)/2 + 0.50); Q0750(k) = flix(tmp,R.N(k)*3/4 + 0.25); tmp = diff(tmp); pdf = diff([0; find(tmp>0); R.N(k)])/R.N(k); % empirical probability distribution R.ENTROPY(k) = -sumskipnan(pdf.*log(pdf)); tmp = tmp(find(tmp)); q = min(tmp); qerror = 0; if isempty(q), q = NaN; else tmp = tmp/q; qerror = max(abs(tmp-round(tmp))); end; R.QUANT(k) = q; R.Qerror(k) = qerror; end; if any(R.Qerror*1e6>R.QUANT) warning('(Y2RES) Quantization might not be equidistant') end; R.MEDIAN = Q0500; R.Quartiles = [Q0250; Q0750]; % R.IQR = H_spread = [Q0750 - Q0250]; R.TRIMEAN = [Q0250 + 2*Q0500 + Q0750]/4; Y = Y - repmat(R.MEAN,size(Y)./size(R.MEAN)); R.CM3 = sumskipnan(Y.^3,1)./n1; R.CM4 = sumskipnan(Y.^4,1)./n1; %R.CM5 = sumskipnan(Y.^5,1)./n1; R.SKEWNESS = R.CM3./(R.STD.^3); R.KURTOSIS = R.CM4./(R.VAR.^2)-3; %R.Skewness.Fisher = (R.CM3)./(R.STD.^3); %%% same as R.SKEWNESS %R.Skewness.Pearson_Mode = (R.MEAN-R.MODE)./R.STD; %R.Skewness.Pearson_coeff1 = (3*R.MEAN-R.MODE)./R.STD; R.Skewness.Pearson_coeff2 = (3*R.MEAN-R.MEDIAN)./R.STD; R.Skewness.Bowley = (Q0750+Q0250 - 2*Q0500)./(Q0750-Q0250); % quartile skewness coefficient R.datatype = 'STAT Level 4'; tsa/inst/histo2.m0000664002356700235670000000563312115040716014424 0ustar schloeglschloeglfunction R = histo2(Y, W) % HISTO2 calculates histogram for multiple columns with separate bin values % for each data column. % % R = HISTO2(Y) % R = HISTO2(Y, W) % Y data % W weight vector containing weights of each sample, % number of rows of Y and W must match. % default W=[] indicates that each sample is weighted with 1. % % R = HISTO(...) % R is a struct with th fields % R.X the bin-values, bin-values are computed separately for each % data column, thus R.X is a matrix, each column contains the % the bin values of for each data column, unused elements are indicated with NaN. % In order to have common bin values, use HISTO3. % R.H is the frequency of occurence of value X % R.N are the number of valid (not NaN) samples (i.e. sum of weights) % % more histogram-based results can be obtained by HIST2RES2 % % see also: HISTO, HISTO2, HISTO3, HISTO4 % % REFERENCE(S): % C.E. Shannon and W. Weaver "The mathematical theory of communication" University of Illinois Press, Urbana 1949 (reprint 1963). % $Id: histo2.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2002,2008,2011 by Alois Schloegl % This is part of the TSA-toolbox % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . %%%%% check input arguments %%%%% [yr,yc] = size(Y); if nargin < 2, W = []; end; if ~isempty(W) && (yr ~= numel(W)), error('number of rows of Y does not match number of elements in W'); end; %%%%% identify all possible X's and generate overall Histogram %%%%% N = sum(~isnan(Y), 1); NN = N; if isempty(W) sY = sort(Y,1); else [sY, idx] = sort(Y,1); W = cumsum(W(idx)); %% W becomes cumulative sum end; [ix,iy] = find( diff(sY, [], 1) > 0); nn0 = 0; for k = 1:yc, tmp = [ix(iy==k); N(k)]; nn1 = length(tmp); if isempty(W) H(1:nn1,k) = [tmp(1); diff(tmp)]; else %%% Note that W is the cumulative sum H(1:nn1,k) = [W(tmp(1),k); diff(W(tmp,k))]; NN(k) = W(N(k), k); end; X(1:nn1, k) = sY(tmp, k); if k==1; nn0 = nn1; elseif nn1 < nn0, H (1+nn1:nn0, k) = NaN; X (1+nn1:nn0, k) = NaN; elseif nn1 > nn0, H (1+nn0:nn1, 1:k-1) = NaN; X (1+nn0:nn1, 1:k-1) = NaN; nn0 = nn1; end; end; R.datatype = 'HISTOGRAM'; R.H = H; R.X = X; R.N = NN; tsa/inst/ucp.m0000664002356700235670000000316012115040716013774 0ustar schloeglschloeglfunction b=ucp(c) % UCP(C) tests if the polynomial C is a Unit-Circle-Polynomial. % It tests if all roots lie inside the unit circle like % B=ucp(C) does the same as % B=all(abs(roots(C))<1) but much faster. % The Algorithm is based on the Jury-Scheme. % C are the elements of the Polynomial % C(1)*X^N + ... + C(N)*X + C(N+1). % % REFERENCES: % O. Foellinger "Lineare Abtastsysteme", Oldenburg Verlag, Muenchen, 1986. % F. Gausch "Systemtechnik", Textbook, University of Technology Graz, 1993. % $Id: ucp.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-1999,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [lr,lc] = size(c); % JURY-Scheme b=ones(lr,1); lambda=zeros(lr,1); while (lc > 1), lambda = c(:,lc)./c(:,1); % disp([lc,size(lambda), sum(b),toc]); % ratio must be less then 1 b = b & (abs(lambda) < 1); % and reduced polynomial must be a UCP, too. c(:,1:lc-1) = c(:,1:lc-1) - lambda(:,ones(1,lc-1)).*c(:,lc:-1:2); lc = lc-1; end; tsa/inst/flag_implicit_samplerate.m0000664002356700235670000000415512115040716020232 0ustar schloeglschloeglfunction DIM=flag_implicit_samplerate(i) % The use of FLAG_IMPLICIT_SAMPLERATE is in experimental state. % FLAG_IMPLICIT_SAMPLERATE might even become obsolete. % Do not use it. % FLAG_IMPLICIT_SAMPLERATE sets and gets default mode for handling NaNs % The default DIM argument is stored in the global variable FLAG_implicit_samplerate % The idea is that the DIM-argument is not necessary. This might enable % more readable code. % % flag_implicit_samplerate(0) % calculation along first non-singleton dimension % flag_implicit_samplerate(1) % calculation along columns % flag_implicit_samplerate(2) % calculation along rows % % DIM = flag_implicit_samplerate() % gets default mode % % flag_implicit_samplerate(DIM) % sets default DIM % % DIM = flag_implicit_samplerate(DIM) % gets and sets DIM % % features: % - compatible to Matlab and Octave % % see also: SINVEST1 % $Id: flag_implicit_samplerate.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 2000-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . global FLAG_implicit_samplerate; %%% check whether FLAG was already defined if exist('FLAG_implicit_samplerate')~=1, FLAG_implicit_samplerate = 1; end; if isempty(FLAG_implicit_samplerate), FLAG_implicit_samplerate = 1; end; if nargin>0, fprintf(2,'Warning: FLAG_IMPLICIT_SAMPLERATE is in an experimental state\n'); fprintf(2,'It might become obsolete.\n'); FLAG_implicit_samplerate = i; end; DIM = FLAG_implicit_samplerate; tsa/inst/poly2ac.m0000664002356700235670000000320412115040716014555 0ustar schloeglschloeglfunction ACF=poly2ac(a,efinal) % converts an AR polynomial into an autocorrelation sequence % [R] = poly2ac(a [,efinal] ); % % see also ACOVF ACORF AR2RC RC2AR DURLEV AC2POLY, POLY2RC, RC2POLY, RC2AC, AC2RC, POLY2AC % % $Id: poly2ac.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . fprintf(2,'ERROR: POLY2AC does not work yet. Sorry\n'); return; mfilename='POLY2AC'; if ~exist('rc2ar','file') fprintf(2,'Error %s: RC2AR.M not found. \n Download TSA toolbox from http://pub.ist.ac.at/~schloegl/matlab/tsa/\n',mfilename); return; end; [AR,RC,PE] = ar2rc(poly2ar(a)); %[AR,RC,PE,ACF] = rc2ar(RC); if nargin<2, efinal=PE(:,size(PE,2)); end; ACF=zeros(size(a)); ACF(:,1) = 1; for k=2:size(a,2), ACF(:,k)=sum(AR(:,1:k-1).*ACF(:,k-(1:k-1)),2); end; R0=(sum(AR(:,1:k-1).*ACF(:,2:size(ACF,2)),2)+1).*efinal; %PE(:,size(PE,2)); ACF = ACF.*R0(:,ones(1,size(ACF,2))); %ACF=ACF*efinal*PE(1)/PE(length(PE)); tsa/inst/selmo2.m0000664002356700235670000000417412115040716014414 0ustar schloeglschloeglfunction X = selmo2(y,Pmax); % SELMO2 - model order selection for univariate and multivariate % autoregressive models % % X = selmo(y,Pmax); % % y data series % Pmax maximum model order % X.A, X.B, X.C parameters of AR model % X.OPT... various optimization criteria % % see also: SELMO, MVAR, % $Id: selmo2.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 2007 by Alois Schloegl % This is part of the TSA-toolbox. See also % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % http://octave.sourceforge.net/ % http://biosig.sourceforge.net/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [M,N]=size(y); if M>N, y=y'; end; [M,N]=size(y); % Univariate AR [AutoCov, AutoCorr, ARPMX, E, NC] = invest0(y,Pmax); [FPE,AIC,BIC,SBC,MDL,CATcrit,PHI,optFPE,optAIC,optBIC,optSBC,optMDL,optCAT,optPHI,s,C] = selmo(E,NC); % AIC for MVAR model [AR,RC,PE] = mvar(y',Pmax); E2 = repmat(NaN,1,Pmax); for k = 0:Pmax, S = PE(:,k*M+(1:M)); E2(k+1) = trace(S); %%%% FIX ME %%%%% this does not seem right because it depends on the scaling of y AIC_MV(k+1) = 2*log(det(S))+2*k*M*M/N; % Ding et al. 2000 refering to Akaike 1974 end; X.A = [eye(M),-AR]; X.B = eye(M); X.C = S; X.datatype = 'MVAR'; X.MV.AIC = AIC_MV; X.UV.FPE = FPE; X.UV.AIC = AIC; X.UV.BIC = BIC; X.UV.MDL = MDL; X.UV.CAT = CATcrit; X.UV.PHI = PHI; X.OPT.FPE = optFPE; X.OPT.AIC = optAIC; X.OPT.BIC = optBIC; X.OPT.MDL = optMDL; X.OPT.CAT = optCAT; X.OPT.PHI = optPHI; [tmp,X.OPT.MVAIC] = min(AIC_MV); tsa/inst/amarma.m0000664002356700235670000001762212115045313014451 0ustar schloeglschloeglfunction [z,e,REV,ESU,V,Z,SPUR] = amarma(y, Mode, MOP, UC, z0, Z0, V0, W); % Adaptive Mean-AutoRegressive-Moving-Average model estimation % [z,e,ESU,REV,V,Z,SPUR] = amarma(y, mode, MOP, UC, z0, Z0, V0, W); % % Estimates model parameters (mean and AR) with Kalman filter algorithm % y(t) = sum_i(a(i,t)*y(t-i)) + mu(t) + e(t) % or the more general adaptive mean-autoregressive-moving-avarage parameters % y(t) = sum_i(a(i,t)*y(t-i)) + mu(t) + e(t) + sum_i(b(i,t)*e(t-i)) % % State space model: % z(t)=G*z(t-1) + w(t) w(t)=N(0,W) % y(t)=H*z(t) + v(t) v(t)=N(0,V) % % G = I, (identity matrix) % z = [mu(t)/(1-sum_i(a(i,t))),a_1(t-1),..,a_p(t-p),b_1(t-1),...,b_q(t-q)]; % H = [1,y(t-1),..,y(t-p),e(t-1),...,e(t-q)]; % W = E{(z(t)-G*z(t-1))*(z(t)-G*z(t-1))'} % V = E{(y(t)-H*z(t-1))*(y(t)-H*z(t-1))'} % v = e % % Input: % y Signal (AR-Process) % Mode % [0,0] uses V0 and W % % MOP Model order [m,p,q], default [0,10,0] % m=1 includes the mean term, m=0 does not. % p and q must be positive integers % it is recommended to set q=0 (i.e. no moving average part) % because the optimization problem for ARMA models is % non-linear and can have local optima. % UC Update Coefficient, default 0 % z0 Initial state vector % Z0 Initial Covariance matrix % % Output: % z mean-autoregressive-moving-average-parameter % mu(t) = z(t,1:m) adaptive mean % a(t,:) = z(t,m+[1:p]) adaptive autoregressive parameters % b(t,:) = z(t,m+p+[1:q]) adaptive moving average parameters % e error process (Adaptively filtered process) % REV relative error variance MSE/MSY % % % see also: AAR % % REFERENCE(S): % [1] A. Schlögl (2000), The electroencephalogram and the adaptive autoregressive model: theory and applications. % ISBN 3-8265-7640-3 Shaker Verlag, Aachen, Germany. % [2] Schlögl A, Lee FY, Bischof H, Pfurtscheller G % Characterization of Four-Class Motor Imagery EEG Data for the BCI-Competition 2005. % Journal of neural engineering 2 (2005) 4, S. L14-L22 % [3] A. Schlögl , J. Fortin, W. Habenbacher, M. Akay. % Adaptive mean and trend removal of heart rate variability using Kalman filtering % Proceedings of the 23rd Annual International Conference of the IEEE Engineering in Medicine and Biology Society, % 25-28 Oct. 2001, Paper #1383, ISBN 0-7803-7213-1. % % More references can be found at % http://pub.ist.ac.at/~schloegl/publications/ % $Id: amarma.m 11694 2013-03-04 07:18:35Z schloegl $ % Copyright (C) 1998-2002,2005,2006,2007,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [nc,nr]=size(y); if nargin<2 Mode=0; elseif isnan(Mode) return; end; if nargin<3, MOP=[0,10,0]; end; if nargin<8, W = nan ; end; if length(MOP)==0, m=0;p=10; q=0; MOP=p; elseif length(MOP)==1, m=0;p=MOP(1); q=0; MOP=p; elseif length(MOP)==2, fprintf(1,'Error AMARMA: MOP is ambiguos\n'); elseif length(MOP)>2, m=MOP(1); p=MOP(2); q=MOP(3);MOP=m+p+q; end; if prod(size(Mode))>1 aMode=Mode(1); eMode=Mode(2); end; %fprintf(1,['a' int2str(aMode) 'e' int2str(eMode) ' ']); e = zeros(nc,1); V = zeros(nc,1);V(1)=V0; T = zeros(nc,1); ESU = zeros(nc,1)+nan; SPUR = zeros(nc,1)+nan; z = z0(ones(nc,1),:); dW = UC/MOP*eye(MOP); % Schloegl %------------------------------------------------ % First Iteration %------------------------------------------------ H = zeros(MOP,1); if m, %M0 = z0(1)/(1-sum(z0(2:p+1))); %transformierter Mittelwert H(1) = 1;%M0; %z0(1)= 1; end; Z = Z0; zt= z0; A1 = zeros(MOP); A2 = A1; %------------------------------------------------ % Update Equations %------------------------------------------------ for t=1:nc, %H=[y(t-1); H(1:p-1); E ; H(p+1:MOP-1)] if t<=p, H(m+(1:t-1)) = y(t-1:-1:1); %H(p)=mu0; % Autoregressive else H(m+(1:p)) = y(t-1:-1:t-p); %mu0]; end; if t<=q, H(m+p+(1:t-1)) = e(t-1:-1:1); % Moving Average else H(m+p+(1:q)) = e(t-1:-1:t-q); end; % Prediction Error E = y(t) - zt*H; e(t) = E; if ~isnan(E), E2 = E*E; AY = Z*H; % [zt, t, y(t), E,ESU(t),V(t),H,Z],pause, ESU(t) = H'*AY; if eMode==0 V(t) = V0; elseif eMode==1 V0 = V(t-1); V(t) = V0*(1-UC)+UC*E2; elseif eMode==2 V0 = 1; V(t) = V0; %V(t-1)*(1-UC)+UC*E2; elseif eMode==3 V0 = 1-UC; V(t) = V0; %(t-1)*(1-UC)+UC*E2; elseif eMode==4 V0 = V0*(1-UC)+UC*E2; V(t) = V0; elseif eMode==5 V(t)=V0; %V0 = V0; elseif eMode==6 if E2>ESU(t) V0=(1-UC)*V0+UC*(E2-ESU(t)); end; V(t)=V0; elseif eMode==7 V0=V(t); if E2>ESU(t) V(t) = (1-UC)*V0+UC*(E2-ESU(t)); else V(t) = V0; end; elseif eMode==8 V0=0; V(t) = V0; % (t-1)*(1-UC)+UC*E2; end; %[t,size(H),size(Z)] k = AY / (ESU(t) + V0); % Kalman Gain zt = zt + k'*E; %z(t,:) = zt; if aMode==0 %W = W; %nop % Schloegl et al. 2003 elseif aMode==2 T(t)=(1-UC)*T(t-1)+UC*(E2-Q(t))/(H'*H); % Roberts I 1998 Z=Z*V(t-1)/Q(t); if T(t)>0 W=T(t)*eye(MOP); else W=zeros(MOP);end; elseif aMode==5 Q_wo = (H'*C*H + V(t-1)); % Roberts II 1998 T(t)=(1-UC)*T(t-1)+UC*(E2-Q_wo)/(H'*H); if T(t)>0 W=T(t)*eye(MOP); else W=zeros(MOP); end; elseif aMode==6 T(t)=(1-UC)*T(t-1)+UC*(E2-Q(t))/(H'*H); Z=Z*V(t)/Q(t); if T(t)>0 W=T(t)*eye(MOP); else W=zeros(MOP); end; elseif aMode==11 %Z = Z - k*AY'; W = sum(diag(Z))*dW; elseif aMode==12 W = UC*UC*eye(MOP); elseif aMode==13 W = UC*diag(diag(Z)); elseif aMode==14 W = (UC*UC)*diag(diag(Z)); elseif aMode==15 W = sum(diag(Z))*dW; elseif aMode==16 W = UC*eye(MOP); % Schloegl 1998 elseif aMode==17 Z = 0.5*(Z+Z'); W = UC*Z; elseif aMode==18 W = 0.5*UC*(Z+Z'); %W=W; end; Z = Z - k*AY'; % Schloegl 1998 else V(t) = V0; end; if any(any(isnan(W))), W=UC*Z; end; z(t,:) = zt; Z = Z + W; % Schloegl 1998 SPUR(t)=trace(Z); end; if 0,m, z(:,1)=M0*z(:,1)./(1-sum(z(:,2:p),2)); end; REV = mean(e.*e)/mean(y.*y); if any(~isfinite(Z(:))), REV=inf; end; tsa/inst/rmle.m0000664002356700235670000000555012115040716014151 0ustar schloeglschloeglfunction [a,VAR,S,a_aux,b_aux,e_aux,MLE,pos] = rmle(arg1,arg2); % RMLE estimates AR Parameters using the Recursive Maximum Likelihood % Estimator according to [1] % % Use: [a,VAR]=rmle(x,p) % Input: % x is a column vector of data % p is the model order % Output: % a is a vector with the AR parameters of the recursive MLE % VAR is the excitation white noise variance estimate % % Reference(s): % [1] Kay S.M., Modern Spectral Analysis - Theory and Applications. % Prentice Hall, p. 232-233, 1988. % % $Id: rmle.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 2004 by Jose Luis Gutierrez % Grupo GENESIS - UTN - Argentina % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . x=arg1*1e-6; p=arg2; N=length(x); S=zeros(p+1,p+1); a_aux=zeros(p+1,p);, a_aux(1,:)=1; b_aux=ones(p+1,p); e_aux=zeros(p,1);, p_aux=zeros(p,1); MLE=zeros(3,1); pos=1; for i=0:p for j=0:p for n=0:N-1-i-j S(i+1,j+1)=S(i+1,j+1)+x(n+1+i)*x(n+1+j); end end end e0=S(1,1); c1=S(1,2); d1=S(2,2); coef3=1; coef2=((N-2)*c1)/((N-1)*d1); coef1=-(e0+N*d1)/((N-1)*d1); ti=-(N*c1)/((N-1)*d1); raices=roots([coef3 coef2 coef1 ti]); for o=1:3 if raices(o)>-1 && raices(o)<1 a_aux(2,1)=raices(o); b_aux(p+1,1)=raices(o); end end e_aux(1,1)=S(1,1)+2*a_aux(2,1)*S(1,2)+(a_aux(2,1)^2)*S(2,2); p_aux(1,1)=e_aux(1,1)/N; for k=2:p Ck=S(1:k,2:k+1); Dk=S(2:k+1,2:k+1); ck=a_aux(1:k,k-1)'*Ck*b_aux(p+1:-1:p+2-k,k-1); dk=b_aux(p+1:-1:p+2-k,k-1)'*Dk*b_aux(p+1:-1:p+2-k,k-1); coef3re=1; coef2re=((N-2*k)*ck)/((N-k)*dk); coef1re=-(k*e_aux(k-1,1)+N*dk)/((N-k)*dk); tire=-(N*ck)/((N-k)*dk); raices=roots([coef3re coef2re coef1re tire]); for o=1:3 if raices(o,1)>-1 && raices(o,1)<1 MLE(o,1)=((1-raices(o)^2)^(k/2))/(((e_aux(k-1)+2*ck*raices(o)+dk*(raices(o)^2))/N)^(N/2)); end end [C,I]=max(MLE); k_max=raices(I); for i=1:k-1 a_aux(i+1,k)=a_aux(i+1,k-1)+k_max*a_aux(k-i+1,k-1); end a_aux(k+1,k)=k_max; b_aux(p+1-k:p+1,k)=a_aux(1:k+1,k); e_aux(k,1)=e_aux(k-1,1)+2*ck*k_max+dk*k_max^2; p_aux(k,1)=e_aux(k,1)/N; end a=a_aux(:,p)'; VAR=p_aux(p)*1e12; tsa/inst/pacf.m0000664002356700235670000000414212115040716014117 0ustar schloeglschloeglfunction [PARCOR,sig,cil,ciu]= pacf(Z,KMAX); % Partial Autocorrelation function % [parcor,sig,cil,ciu] = pacf(Z,N); % % Input: % Z Signal, each row is analysed % N # of coefficients % Output: % parcor autocorrelation function % sig p-value for significance test % cil lower confidence interval % ciu upper confidence interval % % see also: DURLEV, LATTICE, AC2RC, AR2RC, % FLAG_IMPLICIT_SIGNIFICANCE % $Id: pacf.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1997-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [nr,nc] = size(Z); if nc % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if all(size(a))>1, fprintf(2,'Error poly2rc: "a" must be a vector\n'); return; end; a=a(:).'; mfilename='POLY2RC'; if ~exist('ar2rc','file') fprintf(2,'Error %s: AR2RC.M not found. \n Download TSA toolbox from http://pub.ist.ac.at/~schloegl/matlab/tsa/\n',mfilename); return; end; if nargin<2, efinal=1; end; [AR,RC,PE] = ar2rc(poly2ar(a)); if nargout>1, r0=efinal.*PE(:,1)./PE(:,size(PE,2)); end; tsa/inst/flag_implicit_skip_nan.m0000664002356700235670000000506312264575422017712 0ustar schloeglschloeglfunction FLAG = flag_implicit_skip_nan(i) % FLAG_IMPLICIT_SKIP_NAN sets and gets default mode for handling NaNs % 1 skips NaN's (the default mode if no mode is set) % 0 NaNs are propagated; input NaN's give NaN's at the output % % FLAG = flag_implicit_skip_nan() % gets current mode % % flag_implicit_skip_nan(FLAG) % sets mode % % prevFLAG = flag_implicit_skip_nan(nextFLAG) % gets previous set FLAG and sets FLAG for the future % flag_implicit_skip_nan(prevFLAG) % resets FLAG to previous mode % % It is used in: % SUMSKIPNAN, MEDIAN, QUANTILES, TRIMEAN % and affects many other functions like: % CENTER, KURTOSIS, MAD, MEAN, MOMENT, RMS, SEM, SKEWNESS, % STATISTIC, STD, VAR, ZSCORE etc. % % The mode is stored in the global variable FLAG_implicit_skip_nan % It is recommended to use flag_implicit_skip_nan(1) as default and % flag_implicit_skip_nan(0) should be used for exceptional cases only. % This feature might disappear without further notice, so you should really not % rely on it. % This program is free software; you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation; either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program; if not, write to the Free Software % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA % $Id: flag_implicit_skip_nan.m 8351 2011-06-24 17:35:07Z carandraug $ % Copyright (C) 2001-2003,2009 by Alois Schloegl % This function is part of the NaN-toolbox % http://pub.ist.ac.at/~schloegl/matlab/NaN/ persistent FLAG_implicit_skip_nan; %% if strcmp(version,'3.6'), FLAG_implicit_skip_nan=(1==1); end; %% hack for the use with Freemat3.6 %%% set DEFAULT value of FLAG if isempty(FLAG_implicit_skip_nan), FLAG_implicit_skip_nan = (1==1); %logical(1); % logical.m not available on 2.0.16 end; FLAG = FLAG_implicit_skip_nan; if nargin>0, FLAG_implicit_skip_nan = (i~=0); %logical(i); %logical.m not available in 2.0.16 if (~i) warning('flag_implicit_skipnan(0): You are warned!!! You have turned off skipping NaN in sumskipnan. This is not recommended. Make sure you really know what you do.') end; end; tsa/inst/rc2ac.m0000664002356700235670000000276112115040716014205 0ustar schloeglschloeglfunction ACF=rc2ac(RC,R0) % converts reflection coefficients to autocorrelation sequence % [R] = rc2ac(RC,R0); % % see also ACOVF ACORF AR2RC RC2AR DURLEV AC2POLY, POLY2RC, RC2POLY, RC2AC, AC2RC, POLY2AC % % $Id: rc2ac.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . fprintf(2,'ERROR: RC2AC does not work yet. Sorry\n'); return; if all(size(RC)>1), fprintf(2,'Error RC2AC: "K" must be a vector\n'); return; end; mfilename='RC2AC'; if ~exist('rc2ar','file') fprintf(2,'Error %s: RC2AR.M not found. \n Download TSA toolbox from http://pub.ist.ac.at/~schloegl/matlab/tsa/\n',mfilename); return; end; [AR,RC,PE] = rc2ar(RC(:).'); ACF=cumprod(ones(size(RC))-RC.^2,2); ACF = ACF./ACF(:,ones(1,size(ACF,2))); if nargin>1 ACF=ACF*R0; end; tsa/inst/arcext.m0000664002356700235670000000423512115040716014477 0ustar schloeglschloeglfunction [AR,RC] = arcext(MX,P); % ARCEXT extracts AR and RC of order P from Matrix MX % function [AR,RC] = arcext(MX,P); % % INPUT: % MX AR and RC matrix calculated by durlev % P model order (default maximum possible) % % OUTPUT % AR autoregressive model parameter % RC reflection coefficients (= -PARCOR coefficients) % % All input and output parameters are organized in rows, one row % corresponds to the parameters of one channel % % see also ACOVF ACORF DURLEV % % REFERENCES: % P.J. Brockwell and R. A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % S. Haykin "Adaptive Filter Theory" 3rd ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % $Id: arcext.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2003,2008,2012 by Alois Schloegl % This is part of the TSA-toolbox. See also % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % http://octave.sourceforge.net/ % http://biosig.sourceforge.net/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [lr,lc]=size(MX); if ~mod(sqrt(8*lc+1)-1,2) % full number of elements K = (sqrt(8*lc+1)-1)/2; elseif ~mod(lc,2), % compressed form of MX K = lc/2; else % invalid number of elements fprintf(2,'Warning ARCEXT: Number of elements is different than a triangular matrix\n'); end; if (K~=P) && (lc~=K*(K+1)/2), [AR,RC,PE]=rc2ar(MX(:,(1:P).*(2:P+1)/2)); else AR = MX(:,P*(P-1)/2+(1:P)); RC = MX(:,(1:P).*(2:P+1)/2); end; tsa/inst/mvar.m0000664002356700235670000011653412115040716014164 0ustar schloeglschloeglfunction [ARF,RCF,PE,DC,varargout] = mvar(Y, Pmax, Mode); % MVAR estimates parameters of the Multi-Variate AutoRegressive model % % Y(t) = Y(t-1) * A1 + ... + Y(t-p) * Ap + X(t); % whereas % Y(t) is a row vecter with M elements Y(t) = y(t,1:M) % % Several estimation algorithms are implemented, all estimators % can handle data with missing values encoded as NaNs. % % [AR,RC,PE] = mvar(Y, p); % [AR,RC,PE] = mvar(Y, p, Mode); % % with % AR = [A1, ..., Ap]; % % INPUT: % Y Multivariate data series % p Model order % Mode determines estimation algorithm % % OUTPUT: % AR multivariate autoregressive model parameter % RC reflection coefficients (= -PARCOR coefficients) % PE remaining error variances for increasing model order % PE(:,p*M+[1:M]) is the residual variance for model order p % % All input and output parameters are organized in columns, one column % corresponds to the parameters of one channel. % % Mode determines estimation algorithm. % 1: Correlation Function Estimation method using biased correlation function estimation method % also called the 'multichannel Yule-Walker' [1,2] % 6: Correlation Function Estimation method using unbiased correlation function estimation method % % 2: Partial Correlation Estimation: Vieira-Morf [2] using unbiased covariance estimates. % In [1] this mode was used and (incorrectly) denominated as Nutall-Strand. % Its the DEFAULT mode; according to [1] it provides the most accurate estimates. % 5: Partial Correlation Estimation: Vieira-Morf [2] using biased covariance estimates. % Yields similar results than Mode=2; % % 3: buggy: Partial Correlation Estimation: Nutall-Strand [2] (biased correlation function) % 9: Partial Correlation Estimation: Nutall-Strand [2] (biased correlation function) % 7: Partial Correlation Estimation: Nutall-Strand [2] (unbiased correlation function) % 8: Least Squares w/o nans in Y(t):Y(t-p) % 10: ARFIT [3] % 11: BURGV [4] % 13: modified BURGV - % 14: modified BURGV [4] % 15: Least Squares based on correlation matrix % 22: Modified Partial Correlation Estimation: Vieira-Morf [2,5] using unbiased covariance estimates. % 25: Modified Partial Correlation Estimation: Vieira-Morf [2,5] using biased covariance estimates. % % 90,91,96: Experimental versions % % Imputation methods: % 100+Mode: % 200+Mode: forward, past missing values are assumed zero % 300+Mode: backward, past missing values are assumed zero % 400+Mode: forward+backward, past missing values are assumed zero % 1200+Mode: forward, past missing values are replaced with predicted value % 1300+Mode: backward, 'past' missing values are replaced with predicted value % 1400+Mode: forward+backward, 'past' missing values are replaced with predicted value % 2200+Mode: forward, past missing values are replaced with predicted value + noise to prevent decaying % 2300+Mode: backward, past missing values are replaced with predicted value + noise to prevent decaying % 2400+Mode: forward+backward, past missing values are replaced with predicted value + noise to prevent decaying % % % % REFERENCES: % [1] A. Schloegl, Comparison of Multivariate Autoregressive Estimators. % Signal processing, Elsevier B.V. (in press). % available at http://dx.doi.org/doi:10.1016/j.sigpro.2005.11.007 % [2] S.L. Marple 'Digital Spectral Analysis with Applications' Prentice Hall, 1987. % [3] Schneider and Neumaier) % [4] Stijn de Waele, 2003 % [5] M. Morf, et al. Recursive Multichannel Maximum Entropy Spectral Estimation, % IEEE trans. GeoSci. Elec., 1978, Vol.GE-16, No.2, pp85-94. % % % A multivariate inverse filter can be realized with % [AR,RC,PE] = mvar(Y,P); % e = mvfilter([eye(size(AR,1)),-AR],eye(size(AR,1)),Y); % % see also: MVFILTER, MVFREQZ, COVM, SUMSKIPNAN, ARFIT2 % $Id: mvar.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2006,2011,2012 by Alois Schloegl % This is part of the TSA-toolbox. See also % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % http://octave.sourceforge.net/ % http://biosig.sourceforge.net/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . % Inititialization [N,M] = size(Y); if nargin<2, Pmax=max([N,M])-1; end; if iscell(Y) Pmax = min(max(N ,M ),Pmax); C = Y; end; if nargin<3, % according to [1], and other internal validations this is in many cases the best algorithm Mode=2; end; [C(:,1:M),n] = covm(Y,'M'); PE(:,1:M) = C(:,1:M)./n; if 0, elseif Mode==0; % this method is broken fprintf('Warning MVAR: Mode=0 is broken.\n') C(:,1:M) = C(:,1:M)/N; F = Y; B = Y; PEF = C(:,1:M); %?% PEF = PE(:,1:M); PEB = C(:,1:M); for K=1:Pmax, [D,n] = covm(Y(K+1:N,:),Y(1:N-K,:),'M'); D = D/N; ARF(:,K*M+(1-M:0)) = D/PEB; ARB(:,K*M+(1-M:0)) = D'/PEF; tmp = F(K+1:N,:) - B(1:N-K,:)*ARF(:,K*M+(1-M:0))'; B(1:N-K,:) = B(1:N-K,:) - F(K+1:N,:)*ARB(:,K*M+(1-M:0))'; F(K+1:N,:) = tmp; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)); PEF = [eye(M) - ARF(:,K*M+(1-M:0))*ARB(:,K*M+(1-M:0))]*PEF; PEB = [eye(M) - ARB(:,K*M+(1-M:0))*ARF(:,K*M+(1-M:0))]*PEB; PE(:,K*M+(1:M)) = PEF; end; elseif Mode==1, %%%%% Levinson-Wiggens-Robinson (LWR) algorithm using biased correlation function % ===== In [1,2] this algorithm is denoted 'Multichannel Yule-Walker' ===== % C(:,1:M) = C(:,1:M)/N; PEF = C(:,1:M); PEB = C(:,1:M); for K=1:Pmax, [C(:,K*M+(1:M)),n] = covm(Y(K+1:N,:),Y(1:N-K,:),'M'); C(:,K*M+(1:M)) = C(:,K*M+(1:M))/N; D = C(:,K*M+(1:M)); for L = 1:K-1, D = D - ARF(:,L*M+(1-M:0))*C(:,(K-L)*M+(1:M)); end; ARF(:,K*M+(1-M:0)) = D / PEB; ARB(:,K*M+(1-M:0)) = D'/ PEF; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)); PEF = [eye(M) - ARF(:,K*M+(1-M:0))*ARB(:,K*M+(1-M:0))]*PEF; PEB = [eye(M) - ARB(:,K*M+(1-M:0))*ARF(:,K*M+(1-M:0))]*PEB; PE(:,K*M+(1:M)) = PEF; end; elseif Mode==6, %%%%% Levinson-Wiggens-Robinson (LWR) algorithm using unbiased correlation function C(:,1:M) = C(:,1:M)/N; PEF = C(:,1:M); %?% PEF = PE(:,1:M); PEB = C(:,1:M); for K = 1:Pmax, [C(:,K*M+(1:M)),n] = covm(Y(K+1:N,:),Y(1:N-K,:),'M'); C(:,K*M+(1:M)) = C(:,K*M+(1:M))./n; %C{K+1} = C{K+1}/N; D = C(:,K*M+(1:M)); for L = 1:K-1, D = D - ARF(:,L*M+(1-M:0))*C(:,(K-L)*M+(1:M)); end; ARF(:,K*M+(1-M:0)) = D / PEB; ARB(:,K*M+(1-M:0)) = D'/ PEF; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)); PEF = [eye(M) - ARF(:,K*M+(1-M:0))*ARB(:,K*M+(1-M:0))]*PEF; PEB = [eye(M) - ARB(:,K*M+(1-M:0))*ARF(:,K*M+(1-M:0))]*PEB; PE(:,K*M+(1:M)) = PEF; end; elseif Mode==2 %%%%% Partial Correlation Estimation: Vieira-Morf Method [2] with unbiased covariance estimation %===== In [1] this algorithm is denoted 'Nutall-Strand with unbiased covariance' =====% %C(:,1:M) = C(:,1:M)/N; F = Y; B = Y; %PEF = C(:,1:M); %PEB = C(:,1:M); PEF = PE(:,1:M); PEB = PE(:,1:M); for K = 1:Pmax, [D,n] = covm(F(K+1:N,:),B(1:N-K,:),'M'); D = D./n; ARF(:,K*M+(1-M:0)) = D / PEB; ARB(:,K*M+(1-M:0)) = D'/ PEF; tmp = F(K+1:N,:) - B(1:N-K,:)*ARF(:,K*M+(1-M:0)).'; B(1:N-K,:) = B(1:N-K,:) - F(K+1:N,:)*ARB(:,K*M+(1-M:0)).'; F(K+1:N,:) = tmp; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)); [PEF,n] = covm(F(K+1:N,:),F(K+1:N,:),'M'); PEF = PEF./n; [PEB,n] = covm(B(1:N-K,:),B(1:N-K,:),'M'); PEB = PEB./n; PE(:,K*M+(1:M)) = PEF; end; elseif Mode==5 %%%%% Partial Correlation Estimation: Vieira-Morf Method [2] with biased covariance estimation %===== In [1] this algorithm is denoted 'Nutall-Strand with biased covariance' ===== % F = Y; B = Y; PEF = C(:,1:M); PEB = C(:,1:M); for K=1:Pmax, [D,n] = covm(F(K+1:N,:),B(1:N-K,:),'M'); %D=D/N; ARF(:,K*M+(1-M:0)) = D / PEB; ARB(:,K*M+(1-M:0)) = D'/ PEF; tmp = F(K+1:N,:) - B(1:N-K,:)*ARF(:,K*M+(1-M:0)).'; B(1:N-K,:) = B(1:N-K,:) - F(K+1:N,:)*ARB(:,K*M+(1-M:0)).'; F(K+1:N,:) = tmp; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)); [PEB,n] = covm(B(1:N-K,:),B(1:N-K,:),'M'); %PEB = D/N; [PEF,n] = covm(F(K+1:N,:),F(K+1:N,:),'M'); %PEF = D/N; %PE(:,K*M+(1:M)) = PEF; PE(:,K*M+(1:M)) = PEF./n; end; elseif 0, Mode==3 %%%%% Partial Correlation Estimation: Nutall-Strand Method [2] with biased covariance estimation %%% OBSOLETE because its buggy, use Mode=9 instead. % C(:,1:M) = C(:,1:M)/N; F = Y; B = Y; PEF = C(:,1:M); PEB = C(:,1:M); for K=1:Pmax, [D, n] = covm(F(K+1:N,:),B(1:N-K,:),'M'); D = D./N; ARF(:,K*M+(1-M:0)) = 2*D / (PEB+PEF); ARB(:,K*M+(1-M:0)) = 2*D'/ (PEF+PEB); tmp = F(K+1:N,:) - B(1:N-K,:)*ARF(:,K*M+(1-M:0)).'; B(1:N-K,:) = B(1:N-K,:) - F(K+1:N,:)*ARB(:,K*M+(1-M:0)).'; F(K+1:N,:) = tmp; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; %RCF{K} = ARF{K}; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); [PEF,n] = covm(F(K+1:N,:),F(K+1:N,:),'M'); PEF = PEF./N; [PEB,n] = covm(B(1:N-K,:),B(1:N-K,:),'M'); PEB = PEB./N; %PE(:,K*M+(1:M)) = PEF; PE(:,K*M+(1:M)) = PEF./n; end; elseif Mode==7 %%%%% Partial Correlation Estimation: Nutall-Strand Method [2] with unbiased covariance estimation F = Y; B = Y; PEF = PE(:,1:M); PEB = PE(:,1:M); for K = 1:Pmax, [D] = covm(F(K+1:N,:),B(1:N-K,:),'M'); %D = D./n; ARF(:,K*M+(1-M:0)) = 2*D / (PEB+PEF); ARB(:,K*M+(1-M:0)) = 2*D'/ (PEF+PEB); tmp = F(K+1:N,:) - B(1:N-K,:)*ARF(:,K*M+(1-M:0)).'; B(1:N-K,:) = B(1:N-K,:) - F(K+1:N,:)*ARB(:,K*M+(1-M:0)).'; F(K+1:N,:) = tmp; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; %RCF{K} = ARF{K}; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); [PEF] = covm(F(K+1:N,:),F(K+1:N,:),'M'); %PEF = PEF./n; [PEB] = covm(B(1:N-K,:),B(1:N-K,:),'M'); %PEB = PEB./n; %PE{K+1} = PEF; PE(:,K*M+(1:M)) = PEF; end; elseif any(Mode==[3,9]) %%%%% Partial Correlation Estimation: Nutall-Strand Method [2] with biased covariance estimation %% same as 3 but with fixed normalization F = Y; B = Y; PEF = C(:,1:M); PEB = C(:,1:M); for K=1:Pmax, [D, n] = covm(F(K+1:N,:),B(1:N-K,:),'M'); ARF(:,K*M+(1-M:0)) = 2*D / (PEB+PEF); ARB(:,K*M+(1-M:0)) = 2*D'/ (PEF+PEB); tmp = F(K+1:N,:) - B(1:N-K,:)*ARF(:,K*M+(1-M:0)).'; B(1:N-K,:) = B(1:N-K,:) - F(K+1:N,:)*ARB(:,K*M+(1-M:0)).'; F(K+1:N,:) = tmp; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; %RCF{K} = ARF{K}; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); [PEF,nf] = covm(F(K+1:N,:),F(K+1:N,:),'M'); [PEB,nb] = covm(B(1:N-K,:),B(1:N-K,:),'M'); %PE(:,K*M+(1:M)) = PEF; PE(:,K*M+(1:M)) = PEF./nf; end; elseif Mode==4, %%%%% Kay, not fixed yet. fprintf('Warning MVAR: Mode=4 is broken.\n') C(:,1:M) = C(:,1:M)/N; K = 1; [C(:,M+(1:M)),n] = covm(Y(2:N,:),Y(1:N-1,:)); C(:,M+(1:M)) = C(:,M+(1:M))/N; % biased estimate D = C(:,M+(1:M)); ARF(:,1:M) = C(:,1:M)\D; ARB(:,1:M) = C(:,1:M)\D'; RCF(:,1:M) = ARF(:,1:M); RCB(:,1:M) = ARB(:,1:M); PEF = C(:,1:M)*[eye(M) - ARB(:,1:M)*ARF(:,1:M)]; PEB = C(:,1:M)*[eye(M) - ARF(:,1:M)*ARB(:,1:M)]; for K=2:Pmax, [C(:,K*M+(1:M)),n] = covm(Y(K+1:N,:),Y(1:N-K,:),'M'); C(:,K*M+(1:M)) = C(:,K*M+(1:M)) / N; % biased estimate D = C(:,K*M+(1:M)); for L = 1:K-1, D = D - C(:,(K-L)*M+(1:M))*ARF(:,L*M+(1-M:0)); end; ARF(:,K*M+(1-M:0)) = PEB \ D; ARB(:,K*M+(1-M:0)) = PEF \ D'; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)) ; RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)) ; PEF = PEF*[eye(M) - ARB(:,K*M+(1-M:0)) *ARF(:,K*M+(1-M:0)) ]; PEB = PEB*[eye(M) - ARF(:,K*M+(1-M:0)) *ARB(:,K*M+(1-M:0)) ]; PE(:,K*M+(1:M)) = PEF; end; elseif Mode==8, %%%%% Least Squares ix = Pmax+1:N; y = repmat(NaN,N-Pmax,M*Pmax); for k = 1:Pmax, y(:,k*M+[1-M:0]) = Y(ix-k,:); end; ix2 = ~any(isnan([Y(ix,:),y]),2); ARF = Y(ix(ix2),:)'/y(ix2,:)'; PE = covm(Y(ix,:) - y*ARF'); RCF = zeros(M,M*Pmax); elseif Mode==10, %%%%% ARFIT try RCF = []; [w, ARF, PE] = arfit(Y, Pmax, Pmax, 'sbc', 'zero'); catch ARF = zeros(M,M*Pmax); RCF = ARF; end; elseif Mode==11, %%%%% de Waele 2003 %%% OBSOLETE warning('MVAR: mode=11 is obsolete use Mode 13 or 14!'); [pc,R0] = burgv(reshape(Y',[M,1,N]),Pmax); try, [ARF,ARB,Pf,Pb,RCF,RCB] = pc2parv(pc,R0); catch [RCF,RCB,Pf,Pb] = pc2rcv(pc,R0); [ARF,ARB,Pf,Pb] = pc2parv(pc,R0); end; ARF = -reshape(ARF(:,:,2:end),[M,M*Pmax]); RCF = -reshape(RCF(:,:,2:end),[M,M*Pmax]); PE = reshape(Pf,[M,M*(Pmax+1)]); elseif Mode==12, %%% OBSOLETE warning('MVAR: mode=12 is obsolete use Mode 13 or 14!'); % this is equivalent to Mode==11 [pc,R0] = burgv(reshape(Y',[M,1,N]),Pmax); [rcf,rcb,Pf,Pb] = pc2rcv(pc,R0); %%%%% Convert reflection coefficients RC to autoregressive parameters ARF = zeros(M,M*Pmax); ARB = zeros(M,M*Pmax); for K = 1:Pmax, ARF(:,K*M+(1-M:0)) = -rcf(:,:,K+1); ARB(:,K*M+(1-M:0)) = -rcb(:,:,K+1); for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; end; RCF = -reshape(rcf(:,:,2:end),[M,M*Pmax]); PE = reshape(Pf,[M,M*(Pmax+1)]); elseif Mode==13, % this is equivalent to Mode==11 but can deal with missing values %%%%%%%%%%% [pc,R0] = burgv_nan(reshape(Y',[M,1,N]),Pmax,1); % Copyright S. de Waele, March 2003 - modified Alois Schloegl, 2009 I = eye(M); sz = [M,M,Pmax+1]; pc= zeros(sz); pc(:,:,1) =I; K = zeros(sz); K(:,:,1) =I; Kb= zeros(sz); Kb(:,:,1) =I; P = zeros(sz); Pb= zeros(sz); %[P(:,:,1)]= covm(Y); [P(:,:,1)]= PE(:,1:M); % normalized Pb(:,:,1)= P(:,:,1); f = Y; b = Y; %the recursive algorithm for i = 1:Pmax, v = f(2:end,:); w = b(1:end-1,:); %% normalized, unbiased Rvv = covm(v); %Pfhat Rww = covm(w); %Pbhat Rvw = covm(v,w); %Pfbhat Rwv = covm(w,v); % = Rvw', written out for symmetry delta = lyap(Rvv*inv(P(:,:,i)),inv(Pb(:,:,i))*Rww,-2*Rvw); TsqrtS = chol( P(:,:,i))'; %square root M defined by: M=Tsqrt(M)*Tsqrt(M)' TsqrtSb= chol(Pb(:,:,i))'; pc(:,:,i+1) = inv(TsqrtS)*delta*inv(TsqrtSb)'; %The forward and backward reflection coefficient K(:,:,i+1) = -TsqrtS *pc(:,:,i+1) *inv(TsqrtSb); Kb(:,:,i+1)= -TsqrtSb*pc(:,:,i+1)'*inv(TsqrtS); %filtering the reflection coefficient out: f = (v'+ K(:,:,i+1)*w')'; b = (w'+Kb(:,:,i+1)*v')'; %The new R and Rb: %residual matrices P(:,:,i+1) = (I-TsqrtS *pc(:,:,i+1) *pc(:,:,i+1)'*inv(TsqrtS ))*P(:,:,i); Pb(:,:,i+1) = (I-TsqrtSb*pc(:,:,i+1)'*pc(:,:,i+1) *inv(TsqrtSb))*Pb(:,:,i); end %for i = 1:Pmax, R0 = PE(:,1:M); %% [rcf,rcb,Pf,Pb] = pc2rcv(pc,R0); rcf = zeros(sz); rcf(:,:,1) = I; Pf = zeros(sz); Pf(:,:,1) = R0; rcb = zeros(sz); rcb(:,:,1) = I; Pb = zeros(sz); Pb(:,:,1) = R0; for p = 1:Pmax, TsqrtPf = chol( Pf(:,:,p))'; %square root M defined by: M=Tsqrt(M)*Tsqrt(M)' TsqrtPb= chol(Pb(:,:,p))'; %reflection coefficients rcf(:,:,p+1) = -TsqrtPf *pc(:,:,p+1) *inv(TsqrtPb); rcb(:,:,p+1)= -TsqrtPb*pc(:,:,p+1)'*inv(TsqrtPf ); %residual matrices Pf(:,:,p+1) = (I-TsqrtPf *pc(:,:,p+1) *pc(:,:,p+1)'*inv(TsqrtPf ))*Pf(:,:,p); Pb(:,:,p+1) = (I-TsqrtPb*pc(:,:,p+1)'*pc(:,:,p+1) *inv(TsqrtPb))*Pb(:,:,p); end %for p = 2:order, %%%%%%%%%%%%%% end %%%%%% %%%%% Convert reflection coefficients RC to autoregressive parameters ARF = zeros(M,M*Pmax); ARB = zeros(M,M*Pmax); for K = 1:Pmax, ARF(:,K*M+(1-M:0)) = -rcf(:,:,K+1); ARB(:,K*M+(1-M:0)) = -rcb(:,:,K+1); for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; end; RCF = -reshape(rcf(:,:,2:end),[M,M*Pmax]); PE = reshape(Pf,[M,M*(Pmax+1)]); elseif Mode==14, % this is equivalent to Mode==11 but can deal with missing values %%%%%%%%%%%%%% [pc,R0] = burgv_nan(reshape(Y',[M,1,N]),Pmax,2); % Copyright S. de Waele, March 2003 - modified Alois Schloegl, 2009 I = eye(M); sz = [M,M,Pmax+1]; pc= zeros(sz); pc(:,:,1) =I; K = zeros(sz); K(:,:,1) =I; Kb= zeros(sz); Kb(:,:,1) =I; P = zeros(sz); Pb= zeros(sz); %[P(:,:,1),nn]= covm(Y); [P(:,:,1)]= C(:,1:M); %% no normalization Pb(:,:,1)= P(:,:,1); f = Y; b = Y; %the recursive algorithm for i = 1:Pmax, v = f(2:end,:); w = b(1:end-1,:); %% no normalization [Rvv,nn] = covm(v); %Pfhat [Rww,nn] = covm(w); %Pbhat [Rvw,nn] = covm(v,w); %Pfbhat [Rwv,nn] = covm(w,v); % = Rvw', written out for symmetry delta = lyap(Rvv*inv(P(:,:,i)),inv(Pb(:,:,i))*Rww,-2*Rvw); TsqrtS = chol( P(:,:,i))'; %square root M defined by: M=Tsqrt(M)*Tsqrt(M)' TsqrtSb= chol(Pb(:,:,i))'; pc(:,:,i+1) = inv(TsqrtS)*delta*inv(TsqrtSb)'; %The forward and backward reflection coefficient K(:,:,i+1) = -TsqrtS *pc(:,:,i+1) *inv(TsqrtSb); Kb(:,:,i+1)= -TsqrtSb*pc(:,:,i+1)'*inv(TsqrtS); %filtering the reflection coefficient out: f = (v'+ K(:,:,i+1)*w')'; b = (w'+Kb(:,:,i+1)*v')'; %The new R and Rb: %residual matrices P(:,:,i+1) = (I-TsqrtS *pc(:,:,i+1) *pc(:,:,i+1)'*inv(TsqrtS ))*P(:,:,i); Pb(:,:,i+1) = (I-TsqrtSb*pc(:,:,i+1)'*pc(:,:,i+1) *inv(TsqrtSb))*Pb(:,:,i); end %for i = 1:Pmax, R0 = PE(:,1:M); %%%%%%%%%% [rcf,rcb,Pf,Pb] = pc2rcv(pc,R0); sz = [M,M,Pmax+1]; rcf = zeros(sz); rcf(:,:,1) = I; Pf = zeros(sz); Pf(:,:,1) = R0; rcb = zeros(sz); rcb(:,:,1) = I; Pb = zeros(sz); Pb(:,:,1) = R0; for p = 1:Pmax, TsqrtPf = chol( Pf(:,:,p))'; %square root M defined by: M=Tsqrt(M)*Tsqrt(M)' TsqrtPb = chol(Pb(:,:,p))'; %reflection coefficients rcf(:,:,p+1)= -TsqrtPf *pc(:,:,p+1) *inv(TsqrtPb); rcb(:,:,p+1)= -TsqrtPb *pc(:,:,p+1)'*inv(TsqrtPf ); %residual matrices Pf(:,:,p+1) = (I-TsqrtPf *pc(:,:,p+1) *pc(:,:,p+1)'*inv(TsqrtPf))*Pf(:,:,p); Pb(:,:,p+1) = (I-TsqrtPb *pc(:,:,p+1)'*pc(:,:,p+1) *inv(TsqrtPb))*Pb(:,:,p); end %for p = 2:order, %%%%%%%%%%%%%% end %%%%%% %%%%% Convert reflection coefficients RC to autoregressive parameters ARF = zeros(M,M*Pmax); ARB = zeros(M,M*Pmax); for K = 1:Pmax, ARF(:,K*M+(1-M:0)) = -rcf(:,:,K+1); ARB(:,K*M+(1-M:0)) = -rcb(:,:,K+1); for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; end; RCF = -reshape(rcf(:,:,2:end),[M,M*Pmax]); PE = reshape(Pf,[M,M*(Pmax+1)]); elseif Mode==15, %%%%% Least Squares %% FIXME for K=1:Pmax, [C(:,K*M+(1:M)),n] = covm(Y(K+1:N,:),Y(1:N-K,:),'M'); % C(K*M+(1:M),:) = C(K*M+(1:M),:)/N; end; ARF = C(:,1:M)'/C(:,M+1:end)'; PE = covm(C(:,1:M)' - ARF * C(:,M+1:end)'); RCF = zeros(M, M*Pmax); elseif 0, %%%%% ARFIT and handling missing values % compute QR factorization for model of order pmax [R, scale] = arqr(Y, Pmax, 0); % select order of model popt = Pmax; % estimated optimum order np = M*Pmax; % number of parameter vectors of length m % decompose R for the optimal model order popt according to % % | R11 R12 | % R=| | % | 0 R22 | % R11 = R(1:np, 1:np); R12 = R(1:np, Pmax+1:Pmax+M); R22 = R(M*Pmax+1:Pmax+M, Pmax+1:Pmax+M); A = (R11\R12)'; % return covariance matrix dof = N-Pmax-M*Pmax; % number of block degrees of freedom PE = R22'*R22./dof; % bias-corrected estimate of covariance matrix try RCF = []; [w, ARF, PE] = arfit(Y, Pmax, Pmax, 'sbc', 'zero'); catch ARF = zeros(M,M*Pmax); RCF = ARF; end; elseif Mode==22 %%%%% Modified Partial Correlation Estimation: Vieira-Morf [2,5] using unbiased covariance estimates. %--------Initialization---------- F = Y; B = Y; [PEF, n] = covm(Y(2:N,:),'M'); PEF = PEF./n; [PEB, n] = covm(Y(1:N-1,:),'M'); PEB = PEB./n; %--------------------------------- for K = 1:Pmax, %---Update the estimated error covariances(15.89) in [2]--- [PEFhat,n] = covm(F(K+1:N,:),'M'); PEFhat = PEFhat./n; [PEBhat,n] = covm(B(1:N-K,:),'M'); PEBhat = PEBhat./n; [PEFBhat,n] = covm(F(K+1:N,:),B(1:N-K,:),'M'); PEFBhat = PEFBhat./n; %---Compute estimated normalized partial correlation matrix(15.88)in [2]--- Rho = inv(chol(PEFhat)')*PEFBhat*inv(chol(PEBhat)); %---Update forward and backward reflection coefficients (15.82) and (15.83) in [2]--- ARF(:,K*M+(1-M:0)) = chol(PEF)'*Rho*inv(chol(PEB)'); ARB(:,K*M+(1-M:0)) = chol(PEB)'*Rho'*inv(chol(PEF)'); %--------------------------------- tmp = F(K+1:N,:) - B(1:N-K,:)*ARF(:,K*M+(1-M:0)).'; B(1:N-K,:) = B(1:N-K,:) - F(K+1:N,:)*ARB(:,K*M+(1-M:0)).'; F(K+1:N,:) = tmp; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)); %---Update forward and backward error covariances (15.75) and (15.76) in [2]--- PEF = (eye(M)-ARF(:,K*M+(1-M:0))*ARB(:,K*M+(1-M:0)))*PEF; PEB = (eye(M)-ARB(:,K*M+(1-M:0))*ARF(:,K*M+(1-M:0)))*PEB; PE(:,K*M+(1:M)) = PEF; end elseif Mode==25 %%%%Modified Partial Correlation Estimation: Vieira-Morf [2,5] using biased covariance estimates. %--------Initialization---------- F = Y; B = Y; [PEF, n] = covm(Y(2:N,:),'M'); PEF = PEF./N; [PEB, n] = covm(Y(1:N-1,:),'M'); PEB = PEB./N; %--------------------------------- for K = 1:Pmax, %---Update the estimated error covariances(15.89) in [2]--- [PEFhat,n] = covm(F(K+1:N,:),'M'); PEFhat = PEFhat./N; [PEBhat,n] = covm(B(1:N-K,:),'M'); PEBhat = PEBhat./N; [PEFBhat,n] = covm(F(K+1:N,:),B(1:N-K,:),'M'); PEFBhat = PEFBhat./N; %---Compute estimated normalized partial correlation matrix(15.88)in [2]--- Rho = inv(chol(PEFhat)')*PEFBhat*inv(chol(PEBhat)); %---Update forward and backward reflection coefficients (15.82) and (15.83) in [2]--- ARF(:,K*M+(1-M:0)) = chol(PEF)'*Rho*inv(chol(PEB)'); ARB(:,K*M+(1-M:0)) = chol(PEB)'*Rho'*inv(chol(PEF)'); %--------------------------------- tmp = F(K+1:N,:) - B(1:N-K,:)*ARF(:,K*M+(1-M:0)).'; B(1:N-K,:) = B(1:N-K,:) - F(K+1:N,:)*ARB(:,K*M+(1-M:0)).'; F(K+1:N,:) = tmp; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)); %---Update forward and backward error covariances (15.75) and (15.76) in [2]--- PEF = (eye(M)-ARF(:,K*M+(1-M:0))*ARB(:,K*M+(1-M:0)))*PEF; PEB = (eye(M)-ARB(:,K*M+(1-M:0))*ARF(:,K*M+(1-M:0)))*PEB; PE(:,K*M+(1:M)) = PEF; end %%%%% EXPERIMENTAL VERSIONS %%%%% elseif Mode==90; % similar to MODE=0 %% not recommended C(:,1:M) = C(:,1:M)/N; F = Y; B = Y; PEF = PE(:,1:M); %CHANGED% PEB = PE(:,1:M); %CHANGED% for K=1:Pmax, [D,n] = covm(Y(K+1:N,:),Y(1:N-K,:),'M'); D = D/N; ARF(:,K*M+(1-M:0)) = D/PEB; ARB(:,K*M+(1-M:0)) = D'/PEF; tmp = F(K+1:N,:) - B(1:N-K,:)*ARF(:,K*M+(1-M:0))'; B(1:N-K,:) = B(1:N-K,:) - F(K+1:N,:)*ARB(:,K*M+(1-M:0))'; F(K+1:N,:) = tmp; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)); PEF = [eye(M) - ARF(:,K*M+(1-M:0))*ARB(:,K*M+(1-M:0))]*PEF; PEB = [eye(M) - ARB(:,K*M+(1-M:0))*ARF(:,K*M+(1-M:0))]*PEB; PE(:,K*M+(1:M)) = PEF; end; elseif Mode==91, %%%%% Levinson-Wiggens-Robinson (LWR) algorithm using biased correlation function % ===== In [1,2] this algorithm is denoted 'Multichannel Yule-Walker' ===== % % similar to MODE=1 %% not recommended C(:,1:M) = C(:,1:M)/N; PEF = PE(:,1:M); %CHANGED% PEB = PE(:,1:M); %CHANGED% for K=1:Pmax, [C(:,K*M+(1:M)),n] = covm(Y(K+1:N,:),Y(1:N-K,:),'M'); C(:,K*M+(1:M)) = C(:,K*M+(1:M))/N; D = C(:,K*M+(1:M)); for L = 1:K-1, D = D - ARF(:,L*M+(1-M:0))*C(:,(K-L)*M+(1:M)); end; ARF(:,K*M+(1-M:0)) = D / PEB; ARB(:,K*M+(1-M:0)) = D'/ PEF; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)); PEF = [eye(M) - ARF(:,K*M+(1-M:0))*ARB(:,K*M+(1-M:0))]*PEF; PEB = [eye(M) - ARB(:,K*M+(1-M:0))*ARF(:,K*M+(1-M:0))]*PEB; PE(:,K*M+(1:M)) = PEF; end; elseif Mode==96, %%%%% Levinson-Wiggens-Robinson (LWR) algorithm using unbiased correlation function % similar to MODE=6 %% not recommended C(:,1:M) = C(:,1:M)/N; PEF = PE(:,1:M); %CHANGED% PEB = PE(:,1:M); %CHANGED% for K = 1:Pmax, [C(:,K*M+(1:M)),n] = covm(Y(K+1:N,:),Y(1:N-K,:),'M'); C(:,K*M+(1:M)) = C(:,K*M+(1:M))./n; D = C(:,K*M+(1:M)); for L = 1:K-1, D = D - ARF(:,L*M+(1-M:0))*C(:,(K-L)*M+(1:M)); end; ARF(:,K*M+(1-M:0)) = D / PEB; ARB(:,K*M+(1-M:0)) = D'/ PEF; for L = 1:K-1, tmp = ARF(:,L*M+(1-M:0)) - ARF(:,K*M+(1-M:0))*ARB(:,(K-L)*M+(1-M:0)); ARB(:,(K-L)*M+(1-M:0)) = ARB(:,(K-L)*M+(1-M:0)) - ARB(:,K*M+(1-M:0))*ARF(:,L*M+(1-M:0)); ARF(:,L*M+(1-M:0)) = tmp; end; RCF(:,K*M+(1-M:0)) = ARF(:,K*M+(1-M:0)); RCB(:,K*M+(1-M:0)) = ARB(:,K*M+(1-M:0)); PEF = [eye(M) - ARF(:,K*M+(1-M:0))*ARB(:,K*M+(1-M:0))]*PEF; PEB = [eye(M) - ARB(:,K*M+(1-M:0))*ARF(:,K*M+(1-M:0))]*PEB; PE(:,K*M+(1:M)) = PEF; end; elseif Mode<100, fprintf('Warning MVAR: Mode=%i not supported\n',Mode); %%%%% IMPUTATION METHODS %%%%% else Mode0 = rem(Mode,100); if ((Mode0 >= 10) && (Mode0 < 20)), Mode0 = 1; end; if 0, elseif Mode>=2400, % forward and backward % assuming that past missing values are already IMPUTED with the prediction value + innovation process % no decaying [ARF,RCF,PE2] = mvar(Y, Pmax, Mode0); c = chol( PE2 (:, M*Pmax+(1:M))); Y1 = Y; Y1(1,isnan(Y1(1,:))) = 0; z = []; for k = 2:size(Y,1) [z1,z] = mvfilter(ARF,eye(M),Y1(k-1,:)',z); ix = isnan(Y1(k,:)); z1 = z1 + (randn(1,M)*c)'; Y1(k,ix) = z1(ix); end; Y2 = flipud(Y); [ARB,RCF,PE] = mvar(Y2, Pmax, Mode0); Y2(1,isnan(Y2(1,:))) = 0; z = []; for k = 2:size(Y2,1) [z2,z] = mvfilter(ARB,eye(M),Y2(k-1,:)',z); ix = isnan(Y(size(Y,1)-k+1,:)); z2 = z2 + (randn(1,M)*c)'; Y2(k,ix) = (z2(ix)' + Y2(k,ix))/2; end; Y2 = flipud(Y2); Z = (Y2+Y1)/2; Y(isnan(Y)) = Z(isnan(Y)); [ARF,RCF,PE] = mvar(Y, Pmax, rem(Mode,100)); elseif Mode>=2300, % backward prediction % assuming that past missing values are already IMPUTED with the prediction value + innovation process % no decaying Y = flipud(Y); [ARB,RCF,PE] = mvar(Y, Pmax, Mode0); c = chol(PE(:,M*Pmax+(1:M))); Y1 = Y; Y1(1,isnan(Y1(1,:))) = 0; z = []; for k = 2:size(Y,1) [z1,z] = mvfilter(ARB,eye(M),Y1(k-1,:)',z); ix = isnan(Y1(k,:)); z1 = z1 + (randn(1,M)*c)'; Y1(k,ix) = z1(ix); end; Y1 = flipud(Y1); [ARF,RCF,PE] = mvar(Y1, Pmax, rem(Mode,100)); elseif Mode>=2200, % forward predictions, % assuming that past missing values are already IMPUTED with the prediction value + innovation process % no decaying [ARF,RCF,PE] = mvar(Y, Pmax, Mode0); c = chol(PE(:,M*Pmax+(1:M))); Y1 = Y; Y1(1,isnan(Y1(1,:))) = 0; z = []; for k = 2:size(Y,1) [z1,z] = mvfilter(ARF,eye(M),Y1(k-1,:)',z); ix = isnan(Y1(k,:)); z1 = z1 + (randn(1,M)*c)'; Y1(k,ix) = z1(ix); end; [ARF,RCF,PE] = mvar(Y1, Pmax, rem(Mode,100)); elseif Mode>=1400, % forward and backward %assuming that past missing values are already IMPUTED with the prediction value [ARF,RCF,PE] = mvar(Y, Pmax, Mode0); Y1 = Y; Y1(1,isnan(Y1(1,:))) = 0; z = []; for k = 2:size(Y,1) [z1,z] = mvfilter(ARF,eye(M),Y1(k-1,:)',z); ix = isnan(Y1(k,:)); Y1(k,ix) = z1(ix); end; Y2 = flipud(Y); [ARB,RCF,PE] = mvar(Y2, Pmax, Mode0); Y2(1,isnan(Y2(1,:))) = 0; z = []; for k = 2:size(Y2,1) [z2,z] = mvfilter(ARB,eye(M),Y2(k-1,:)',z); ix = isnan(Y2(k,:)); Y2(k,ix) = z2(ix)'; end; Y2 = flipud(Y2); Z = (Y2+Y1)/2; Y(isnan(Y)) = Z(isnan(Y)); [ARF,RCF,PE] = mvar(Y, Pmax, rem(Mode,100)); elseif Mode>=1300, % backward prediction Y = flipud(Y); [ARB,RCF,PE] = mvar(Y, Pmax, Mode0); Y2 = Y; Y2(1,isnan(Y2(1,:))) = 0; z = []; for k = 2:size(Y,1) [z2,z] = mvfilter(ARB,eye(M),Y2(k-1,:)',z); ix = isnan(Y2(k,:)); Y2(k,ix) = z2(ix); end; Y2 = flipud(Y2); [ARF,RCF,PE] = mvar(Y2, Pmax, rem(Mode,100)); elseif Mode>=1200, % forward predictions, %assuming that past missing values are already IMPUTED with the prediction value [ARF,RCF,PE] = mvar(Y, Pmax, Mode0); Y1 = Y; Y1(1,isnan(Y1(1,:))) = 0; z = []; for k = 2:size(Y,1) [z1,z] = mvfilter(ARF,eye(M),Y1(k-1,:)',z); ix = isnan(Y1(k,:)); Y1(k,ix) = z1(ix); end; [ARF,RCF,PE] = mvar(Y1, Pmax, rem(Mode,100)); elseif Mode>=400, % forward and backward % assuming that 'past' missing values are ZERO [ARF,RCF,PE] = mvar(Y, Pmax, Mode0); Y1 = Y; Y1(isnan(Y)) = 0; Z1 = mvfilter([zeros(M),ARF],eye(M),Y1')'; Y1(isnan(Y)) = Z1(isnan(Y)); Y = flipud(Y); [ARB,RCF,PE] = mvar(Y, Pmax, Mode0); Y2 = Y; Y2(isnan(Y)) = 0; Z2 = mvfilter([zeros(M),ARB],eye(M),Y2')'; Y2(isnan(Y)) = Z2(isnan(Y)); Y2 = flipud(Y2); [ARF,RCF,PE] = mvar((Y1+Y2)/2, Pmax, rem(Mode,100)); elseif Mode>=300, % backward prediction % assuming that 'past' missing values are ZERO Y = flipud(Y); [ARB,RCF,PE] = mvar(Y, Pmax, Mode0); Y2 = Y; Y2(isnan(Y)) = 0; Z2 = mvfilter([zeros(M),ARB],eye(M),Y2')'; Y2(isnan(Y)) = Z2(isnan(Y)); Y2 = flipud(Y2); [ARF,RCF,PE] = mvar(Y2, Pmax, rem(Mode,100)); elseif Mode>=200, % forward predictions, assuming that past missing values are ZERO [ARF,RCF,PE] = mvar(Y, Pmax, Mode0); Y1 = Y; Y1(isnan(Y)) = 0; Z1 = mvfilter([zeros(M),ARF],eye(M),Y1')'; Y1(isnan(Y)) = Z1(isnan(Y)); [ARF,RCF,PE] = mvar(Y1, Pmax, rem(Mode,100)); elseif Mode>=100, [ARF,RCF,PE] = mvar(Y, Pmax, Mode0); Z1 = mvfilter(ARF,eye(M),Y')'; Z1 = [zeros(1,M); Z1(1:end-1,:)]; Y(isnan(Y)) = Z1(isnan(Y)); [ARF,RCF,PE] = mvar(Y, Pmax, rem(Mode,100)); end; end; if any(ARF(:)==inf), % Test for matrix division bug. % This bug was observed in LNX86-ML5.3, 6.1 and 6.5, but not in SGI-ML6.5, LNX86-ML6.5, Octave 2.1.35-40; Other platforms unknown. p = 3; FLAG_MATRIX_DIVISION_ERROR = ~all(all(isnan(repmat(0,p)/repmat(0,p)))) | ~all(all(isnan(repmat(nan,p)/repmat(nan,p)))); if FLAG_MATRIX_DIVISION_ERROR, %fprintf(2,'%%% Warning MVAR: Bug in Matrix-Division 0/0 and NaN/NaN yields INF instead of NAN. Workaround is applied.\n'); warning('MVAR: bug in Matrix-Division 0/0 and NaN/NaN yields INF instead of NAN. Workaround is applied.'); %%%%% Workaround ARF(ARF==inf)=NaN; RCF(RCF==inf)=NaN; end; end; %MAR = zeros(M,M*Pmax); DC = zeros(M); for K = 1:Pmax, % VAR{K+1} = -ARF(:,K*M+(1-M:0))'; DC = DC + ARF(:,K*M+(1-M:0))'.^2; %DC meausure [3] end; tsa/inst/invfdemo.m0000664002356700235670000000316412115040716015020 0ustar schloeglschloegl% invfdemo demonstrates Inverse Filtering % $Id: invfdemo.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1997-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . load eeg8s.mat; % load signal ly=length(eeg8s); Fs=128; %a=earpyw(eeg8s',11); % Calculates AR(11) parameters with Yule-Walker method a=lattice(eeg8s',11); % Calculates AR(11) parameters with Yule-Walker method % The AR parameters are the weight taps of IIR Filter isig=filter([1 -a],1,eeg8s); % Inverse filtering subplot(221); plot((1:ly)/Fs,eeg8s); title('Signal'); xlabel('t [sec]') subplot(223); plot((1:ly)/Fs,isig); xlabel('t [sec]') title('Inverse filtered process'); subplot(222); H=abs(fft(eeg8s,128)/ly).^2; plot(1:Fs,H); %plot([H mean(H)*ones(Fs,1)]); ylabel('S(f)') xlabel('f [Hz]') title('Spectrum of original signal'); subplot(224); H=abs(fft(isig,128)/ly).^2; plot(1:Fs,H); %plot([H mean(H)*ones(Fs,1)]); ylabel('S(f)') xlabel('f [Hz]') title('Spectrum of inverse filtered signal'); tsa/inst/acorf.m0000664002356700235670000000521112115040716014276 0ustar schloeglschloeglfunction [AUTOCOV,stderr,lpq,qpval] = acorf(Z,N); % Calculates autocorrelations for multiple data series. % Missing values in Z (NaN) are considered. % Also calculates Ljung-Box Q stats and p-values. % % [AutoCorr,stderr,lpq,qpval] = acorf(Z,N); % If mean should be removed use % [AutoCorr,stderr,lpq,qpval] = acorf(detrend(Z',0)',N); % If trend should be removed use % [AutoCorr,stderr,lpq,qpval] = acorf(detrend(Z')',N); % % INPUT % Z is data series for which autocorrelations are required % each in a row % N maximum lag % % OUTPUT % AutoCorr nr x N matrix of autocorrelations % stderr nr x N matrix of (approx) std errors % lpq nr x M matrix of Ljung-Box Q stats % qpval nr x N matrix of p-values on Q stats % % All input and output parameters are organized in rows, one row % corresponds to one series % % REFERENCES: % S. Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % J.S. Bendat and A.G.Persol "Random Data: Analysis and Measurement procedures", Wiley, 1986. % calculating lpq, stderr, qpval from % suggested by Philip Gray, University of New South Wales, % $Id: acorf.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [nr,nc] = size(Z); NC = sum(~isnan(Z),2); % missing values AUTOCOV = acovf(Z,N); AUTOCOV = AUTOCOV(:,2:N+1) ./ AUTOCOV(:,ones(1,N)); if nargout > 1 stderr = sqrt(1./NC)*ones(1,N); lpq = zeros(nr,N); qpval = zeros(nr,N); cum=zeros(nr,1); for k=1:N, cum = cum+AUTOCOV(:,k).*conj(AUTOCOV(:,k))./(NC-k); lpq(:,k) = NC.*(NC+2).*cum; % Ljung box Q for k lags %qpval(:,k) = 1 - chi2cdf(lpq(:,k),k); % p-value of Q stat qpval(:,k) = 1 - gammainc(lpq(:,k)/2,k/2); % replace chi2cdf by gammainc end; end; tsa/inst/detrend.m0000664002356700235670000001105412115040716014633 0ustar schloeglschloeglfunction [X,T]=detrend(t,X,p) % DETREND removes the trend from data, NaN's are considered as missing values % % DETREND is fully compatible to previous Matlab and Octave DETREND with the following features added: % - handles NaN's by assuming that these are missing values % - handles unequally spaced data % - second output parameter gives the trend of the data % - compatible to Matlab and Octave % % [...]=detrend([t,] X [,p]) % removes trend for unequally spaced data % t represents the time points % X(i) is the value at time t(i) % p must be a scalar % % [...]=detrend(X,0) % [...]=detrend(X,'constant') % removes the mean % % [...]=detrend(X,p) % removes polynomial of order p (default p=1) % % [...]=detrend(X,1) - default % [...]=detrend(X,'linear') % removes linear trend % % [X,T]=detrend(...) % % X is the detrended data % T is the removed trend % % see also: SUMSKIPNAN, ZSCORE % Copyright (C) 1995, 1996 Kurt Hornik % $Id: detrend.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 2001,2007 by Alois Schloegl % This function is part of the TSA-toolbox % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % Copyright (C) 1997, 1998, 2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if (nargin == 1) p = 1; X = t; t = []; elseif (nargin == 2) if strcmpi(X,'constant'), p = 0; X = t; t = []; elseif strcmpi(X,'linear'), p = 1; X = t; t = []; elseif ischar(X) error('unknown 2nd input argument'); elseif all(size(X)==1), p = X; X = t; t = []; else p = 1; end; elseif (nargin == 3) if ischar(X), warning('input arguments are not supported'); end; elseif (nargin > 3) fprintf (1,'usage: detrend (x [, p])\n'); end; % check data, must be in culomn order [m, n] = size (X); if (m == 1) X = X'; r=n; else r=m; end % check time scale if isempty(t), t = (1:r).'; % make time scale elseif ~all(size(t)==size(X)) t = t(:); end; % check dimension of t and X if ~all(size(X,1)==size(t,1)) fprintf (2,'detrend: size(t,1) must same as size(x,1) \n'); end; % check the order of the polynomial if (~(all(size(p)==1) & (p == round (p)) & (p >= 0))) fprintf (2,'detrend: p must be a nonnegative integer\n'); end if (nargout>1) , % needs more memory T = zeros(size(X))+nan; %T=repmat(nan,size(X)); % not supported by Octave 2.0.16 if (size(t,2)>1), % for multiple time scales for k=1:size(X,2), idx=find(~isnan(X(:,k))); b = (t(idx,k) * ones (1, p + 1)) .^ (ones (length(idx),1) * (0 : p)); T(idx,k) = b * (b \ X(idx,k)); end; else % if only one time scale is used b = (t * ones (1, p + 1)) .^ (ones (length(t),1) * (0 : p)); for k=1:size(X,2), idx=find(~isnan(X(:,k))); T(idx,k) = b(idx,:) * (b(idx,:) \ X(idx,k)); %X(idx,k) = X(idx,k) - T(idx,k); % 1st alternative implementation %X(:,k) = X(:,k) - T(:,k); % 2nd alternative end; end; X = X-T; % 3nd alternative if (m == 1) X = X'; T = T'; end else % needs less memory if (size(t,2)>1), % for multiple time scales for k = 1:size(X,2), idx = find(~isnan(X(:,k))); b = (t(idx,k) * ones (1, p + 1)) .^ (ones (length(idx),1) * (0 : p)); X(idx,k) = X(idx,k) - b * (b \ X(idx,k)); end; else % if only one time scale is used b = (t * ones (1, p + 1)) .^ (ones (length(t),1) * (0 : p)); for k = 1:size(X,2), idx = find(~isnan(X(:,k))); X(idx,k) = X(idx,k) - b(idx,:) * (b(idx,:) \ X(idx,k)); end; end; if (m == 1) X = X'; end end; tsa/inst/histo4.m0000664002356700235670000000600412115040716014417 0ustar schloeglschloeglfunction [R, tix] = histo4(Y, W) % HISTO4 calculates histogram of multidimensional data samples % and supports data compression % % R = HISTO4(Y) % R = HISTO4(Y, W) % Y data: on sample per row, each sample has with size(Y,2) elements % W weights of each sample (default: []) % W = [] indicates that each sample has equal weight % R is a struct with these fields: % R.X are the bin-values % R.H is the frequency of occurence of value X (weighted with W) % R.N are the total number of samples (or sum of W) % % HISTO4 might be useful for data compression, because % [R,tix] = histo4(Y) % is the compression step % R.X(tix,:) % is the decompression step % % The effort (in memory and speed) for compression is O(n*log(n)) % The effort (in memory and speed) for decompression is only O(n) % % see also: HISTO, HISTO2, HISTO3, HISTO4 % % REFERENCE(S): % C.E. Shannon and W. Weaver 'The mathematical theory of communication' University of Illinois Press, Urbana 1949 (reprint 1963). % $Id: histo4.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2005,2008,2009,2011 by Alois Schloegl % This is part of the TSA-toolbox % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . %%%%% check input arguments %%%%% [yr, yc] = size(Y); if nargin<2, W = []; end; if ~isempty(W) && (yr ~= numel(W)), error('number of rows of Y does not match number of elements in W'); end; %%%%% identify all possible X's and generate overall Histogram %%%%% [Y, idx] = sortrows(Y); d = diff(Y,[],1); ix = any( (~isnan(d) & (d~=0) ) | diff(isnan(Y),[],1), 2); tmp = [find(ix); yr]; R.X = Y(tmp,:); R.datatype = 'HISTOGRAM'; if isempty(W) R.H = [tmp(1); diff(tmp)]; R.N = yr; else W = cumsum(W(idx)); R.H = [W(tmp(1)); diff(W(tmp))]; R.N = W(end); end; %%%%% generate inverse index %%%%% if nargout>1, tix = cumsum([1;ix]); % rank cc = 1; tmp = sum(ix); if tmp < 2^8; tix = uint8(tix); cc = 8/1; elseif tmp < 2^16; tix = uint16(tix); cc = 8/2; elseif tmp < 2^32; tix = uint32(tix); cc = 8/4; end; [tmp, idx] = sort(idx); % inverse index tix = tix(idx); % inverse sort rank R.compressionratio = (prod(size(R.X)) + yr/cc) / (yr*yc); R.tix = tix; end; tsa/inst/histo3.m0000664002356700235670000001125612115040716014423 0ustar schloeglschloeglfunction [R, tix] = histo3(Y, W) % HISTO3 calculates histogram for multiple columns with common bin values % among all data columns, and can be useful for data compression. % % R = HISTO3(Y) % R = HISTO3(Y, W) % Y data % W weight vector containing weights of each sample, % number of rows of Y and W must match. % default W=[] indicates that each sample is weighted with 1. % R struct with these fields % R.X the bin-values, bin-values are equal for each channel % thus R.X is a column vector. If bin values should % be computed separately for each data column, use HISTO2 % R.H is the frequency of occurence of value X % R.N are the number of valid (not NaN) samples % % Data compression can be performed in this way % [R,tix] = histo3(Y) % is the compression step % % R.tix provides a compressed data representation. % R.compressionratio estimates the compression ratio % % R.X(tix) and R.X(R.tix) % reconstruct the orginal signal (decompression) % % The effort (in memory and speed) for compression is O(n*log(n)). % The effort (in memory and speed) for decompression is O(n) only. % % see also: HISTO, HISTO2, HISTO3, HISTO4 % % REFERENCE(S): % C.E. Shannon and W. Weaver "The mathematical theory of communication" University of Illinois Press, Urbana 1949 (reprint 1963). % $Id: histo3.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2002,2008,2011 by Alois Schloegl % This is part of the TSA-toolbox. See also % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % http://octave.sourceforge.net/ % http://biosig.sourceforge.net/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . %%%%% check input arguments %%%%% [yr,yc] = size(Y); if nargin < 2, W = []; end; if ~isempty(W) && (yr ~= numel(W)), error('number of rows of Y does not match number of elements in W'); end; %%%%% identify all possible X's and generate overall Histogram %%%%% [sY, idx] = sort(Y(:),1); [tmp,idx1] = sort(idx); % generate inverse index ix = diff(sY, [], 1) > 0; tmp = [find(ix); sum(~isnan(sY))]; R.datatype = 'HISTOGRAM'; R.X = sY(tmp); R.N = sum(~isnan(Y), 1); % generate inverse index if nargout>1, tix = cumsum([1; ix]); % rank tix = reshape(tix(idx1), yr, yc); % inverse sort rank cc = 1; tmp = sum(ix) + 1; if exist('OCTAVE_VERSION') >= 5, ; % NOP; no support for integer datatyp elseif tmp <= 2^8; tix = uint8(tix); cc = 8/1; elseif tmp <= 2^16; tix = uint16(tix); cc = 8/2; elseif tmp <= 2^32; tix = uint32(tix); cc = 8/4; end; R.compressionratio = (prod(size(R.X)) + (yr*yc)/cc) / (yr*yc); R.tix = tix; end; if yc==1, if isempty(W) R.H = [tmp(1); diff(tmp)]; else C = cumsum(W(idx)); % cumulative weights R.H = [C(tmp(1)); diff(C(tmp))]; end; return; elseif yc>1, % allocate memory H = zeros(size(R.X,1),yc); % scan each channel for k = 1:yc, if isempty(W) sY = sort(Y(:,k)); else [sY,ix] = sort(Y(:,k)); C = cumsum(W(ix)); end ix = find(diff(sY,[],1) > 0); if size(ix,1) > 0, tmp = [ix; R.N(k)]; else tmp = R.N(k); end; t = 0; j = 1; if isempty(W) for x = tmp', acc = sY(x); while R.X(j)~=acc, j=j+1; end; %j = find(sY(x)==R.X); % identify position on X H(j,k) = H(j,k) + (x-t); % add diff(tmp) t = x; end; else for x = tmp', acc = sY(x); while R.X(j)~=acc, j=j+1; end; %j = find(sY(x)==R.X); % identify position on X H(j,k) = H(j,k) + C(x)-t; % add diff(tmp) t = C(x); end; end; end; R.H = H; end; tsa/inst/adim.m0000664002356700235670000001012612115040716014117 0ustar schloeglschloeglfunction [IR, CC, D] = adim(U, UC, IR, CC, arg5); % ADIM adaptive information matrix. Estimates the inverse % correlation matrix in an adaptive way. % % [IR, CC] = adim(U, UC [, IR0 [, CC0]]); % U input data % UC update coefficient 0 < UC << 1 % IR0 initial information matrix % CC0 initial correlation matrix % IR information matrix (inverse correlation matrix) % CC correlation matrix % % The algorithm uses the Matrix Inversion Lemma, also known as % "Woodbury's identity", to obtain a recursive algorithm. % IR*CC - UC*I should be approx. zero. % % Reference(s): % [1] S. Haykin. Adaptive Filter Theory, Prentice Hall, Upper Saddle River, NJ, USA % pp. 565-567, Equ. (13.16), 1996. % $Id: adim.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2003 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [ur,p] = size(U); Mode_E = 1; %if nargin < 4, % m = zeros(size(U)+[0,Mode_E]); %end; if nargin<2, fprintf(2,'Error ADIM: missing update coefficient\n'); return; else if ~((UC > 0) & (UC <1)), fprintf(2,'Error ADIM: update coefficient not within range [0,1]\n'); return; end; if UC > 1/p, fprintf(2,'Warning ADIM: update coefficient should be smaller than 1/number_of_dimensions\n'); end; end; if nargin<3, IR = []; end; if nargin<4, CC = []; end; if nargin<5, arg5 = 6; end; if isempty(IR), IR = eye(p+Mode_E); end; if isempty(CC), CC = eye(p+Mode_E); end; D = zeros(ur,(p+Mode_E)^2); %D = zeros(ur,1); W = eye(p+Mode_E)*UC/(p+Mode_E); W2 = eye(p+Mode_E)*UC*UC/(p+Mode_E); for k = 1:ur, if ~Mode_E, % w/o mean term d = U(k,:); else % w/ mean term d = [1,U(k,:)]; end; if ~any(isnan(d)), CC = (1-UC)*CC + UC*(d'*d); %K = (1+UC)*IR*d'/(1+(1+UC)*d*IR*d'); v = IR*d'; %K = v/(1-UC+d*v); if arg5==0; % original version according to [1], can become unstable IR = (1+UC)*IR - (1+UC)/(1-UC+d*v)*v*v'; elseif arg5==1; % this provides IR*CC == I, this seems to be stable IR = IR - (1+UC)/(1-UC+d*v)*v*v' + sum(diag(IR))*W; elseif arg5==6; % DEFAULT: IR = ((1+UC)/2)*(IR+IR') - ((1+UC)/(1-UC+d*v))*v*v'; % more variants just for testing, do not use them. elseif arg5==2; IR = IR - (1+UC)/(1-UC+d*v)*v*v' + sum(diag(IR))*W2; elseif arg5==3; IR = IR - (1+UC)/(1-UC+d*v)*v*v' + eps*eye(p+Mode_E); elseif arg5==4; IR = (1+UC)*IR - (1+UC)/(1-UC+d*v)*v*v' + eps*eye(p+Mode_E); elseif arg5==5; IR = IR - (1+UC)/(1-UC+d*v)*v*v' + eps*eye(p+Mode_E); end; end; %D(k) = det(IR); D(k,:) = IR(:)'; end; IR = IR / UC; if Mode_E, IR(1,1) = IR(1,1) - 1; end; tsa/inst/hup.m0000664002356700235670000000340012115040716013776 0ustar schloeglschloeglfunction b=hup(C) %HUP(C) tests if the polynomial C is a Hurwitz-Polynomial. % It tests if all roots lie in the left half of the complex % plane % B=hup(C) is the same as % B=all(real(roots(c))<0) but much faster. % The Algorithm is based on the Routh-Scheme. % C are the elements of the Polynomial % C(1)*X^N + ... + C(N)*X + C(N+1). % % HUP2 works also for multiple polynomials, % each row a poly - Yet not tested % % REFERENCES: % F. Gausch "Systemtechnik", Textbook, University of Technology Graz, 1993. % Ch. Langraf and G. Schneider "Elemente der Regeltechnik", Springer Verlag, 1970. % $Id: hup.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (c) 1995-1998,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [lr,lc] = size(c); % Strip leading zeros and throw away. % not considered yet %d=(c(:,1)==0); % Trailing zeros mean there are roots at zero b=(c(:,lc)~=0); lambda=b; n=zeros(lc); if lc>3 n(4:2:lc,2:2:lc-2)=1; end; while lc>1 lambda(b)=c(b,1)./c(b,2); b = b & (lambda>=0) & (lambda % This is part of the TSA-toolbox. See also % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % http://octave.sourceforge.net/ % http://biosig.sourceforge.net/ % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . %%%%% checking of the input arguments was done the same way as ARFIT if (pmin ~= round(pmin) || pmax ~= round(pmax)) error('Order must be integer.'); end if (pmax < pmin) error('PMAX must be greater than or equal to PMIN.') end % set defaults and check for optional arguments if (nargin == 3) % no optional arguments => set default values mcor = 1; % fit intercept vector selector = 'sbc'; % use SBC as order selection criterion elseif (nargin == 4) % one optional argument if strcmp(selector, 'zero') mcor = 0; % no intercept vector to be fitted selector = 'sbc'; % default order selection else mcor = 1; % fit intercept vector end elseif (nargin == 5) % two optional arguments if strcmp(no_const, 'zero') mcor = 0; % no intercept vector to be fitted else error(['Bad argument. Usage: ', '[w,A,C,SBC,FPE,th]=AR(v,pmin,pmax,SELECTOR,''zero'')']) end end %%%%% Implementation of the MVAR estimation [N,M]=size(Y); if mcor, [m,N] = sumskipnan(Y,1); % calculate mean m = m./N; Y = Y - repmat(m,size(Y)./size(m)); % remove mean end; [MAR,RCF,PE] = mvar(Y, pmax, 2); % estimate MVAR(pmax) model N = min(N); %if 1;nargout>3; ne = N-mcor-(pmin:pmax); for p=pmin:pmax, % Get downdated logarithm of determinant logdp(p-pmin+1) = log(det(PE(:,p*M+(1:M))*(N-p-mcor))); end; % Schwarz's Bayesian Criterion sbc = logdp/M - log(ne) .* (1-(M*(pmin:pmax)+mcor)./ne); % logarithm of Akaike's Final Prediction Error fpe = logdp/M - log(ne.*(ne-M*(pmin:pmax)-mcor)./(ne+M*(pmin:pmax)+mcor)); % Modified Schwarz criterion (MSC): % msc(i) = logdp(i)/m - (log(ne) - 2.5) * (1 - 2.5*np(i)/(ne-np(i))); % get index iopt of order that minimizes the order selection % criterion specified by the variable selector if strcmpi(selector,'fpe'); [val, iopt] = min(fpe); else %if strcmpi(selector,'sbc'); [val, iopt] = min(sbc); end; % select order of model popt = pmin + iopt-1; % estimated optimum order if popt5, th=[]; fprintf(2,'Warning ARFIT2: output TH not defined\n'); end; tsa/inst/selmo.m0000664002356700235670000001377212115040716014336 0ustar schloeglschloeglfunction [FPE,AIC,BIC,SBC,MDL,CATcrit,PHI,optFPE,optAIC,optBIC,optSBC,optMDL,optCAT,optPHI,p,C]=selmo(e,NC); % Model order selection of an autoregrssive model % [FPE,AIC,BIC,SBC,MDL,CAT,PHI,optFPE,optAIC,optBIC,optSBC,optMDL,optCAT,optPHI]=selmo(E,N); % % E Error function E(p) % N length of the data set, that was used for calculating E(p) % show optional; if given the parameters are shown % % FPE Final Prediction Error (Kay 1987, Wei 1990, Priestley 1981 -> Akaike 1969) % AIC Akaike Information Criterion (Marple 1987, Wei 1990, Priestley 1981 -> Akaike 1974) % BIC Bayesian Akaike Information Criterion (Wei 1990, Priestley 1981 -> Akaike 1978,1979) % CAT Parzen's CAT Criterion (Wei 1994 -> Parzen 1974) % MDL Minimal Description length Criterion (Marple 1987 -> Rissanen 1978,83) % SBC Schwartz's Bayesian Criterion (Wei 1994; Schwartz 1978) % PHI Phi criterion (Pukkila et al. 1988, Hannan 1980 -> Hannan & Quinn, 1979) % HAR Haring G. (1975) % JEW Jenkins and Watts (1968) % % optFPE order where FPE is minimal % optAIC order where AIC is minimal % optBIC order where BIC is minimal % optSBC order where SBC is minimal % optMDL order where MDL is minimal % optCAT order where CAT is minimal % optPHI order where PHI is minimal % % usually is % AIC > FPE > *MDL* > PHI > SBC > CAT ~ BIC % % REFERENCES: % P.J. Brockwell and R.A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % S. Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % C.E. Shannon and W. Weaver "The mathematical theory of communication" University of Illinois Press, Urbana 1949 (reprint 1963). % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % Jenkins G.M. Watts D.G "Spectral Analysis and its applications", Holden-Day, 1968. % G. Haring "Über die Wahl der optimalen Modellordnung bei der Darstellung von stationären Zeitreihen mittels Autoregressivmodell als Basis der Analyse von EEG - Biosignalen mit Hilfe eines Digitalrechners", Habilitationschrift - Technische Universität Graz, Austria, 1975. % (1)"About selecting the optimal model at the representation of stationary time series by means of an autoregressive model as basis of the analysis of EEG - biosignals by means of a digital computer)" % % $Id: selmo.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1997-2002,2008,2012 by Alois Schloegl % This is part of the TSA-toolbox. See also % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % http://octave.sourceforge.net/ % http://biosig.sourceforge.net/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [lr,lc]=size(e); if (lr>1) && (lc>1), p=zeros(lr+1,9)+NaN; else p=zeros(1,9)+NaN; end; if nargin<2 NC=lc*ones(lr,1); NC=(lc-sum(isnan(e)')')*(NC=lc); % first part %end;% Pmax=min([100 N/3]); end; %if NC=lc); % first part else % NC=NC; end; M=lc-1; m=0:M; e = e./e(:,ones(1,lc)); for k=0:lr, if k>0, % E=e(k,:); N=NC(k); elseif lr>1 tmp = e;%(NC>0,:); tmp(isnan(tmp)) = 0; E = sum(tmp.*(NC*ones(1,lc)))/sum(NC); % weighted average, weigths correspond to number of valid (not missing) values N = sum(NC)./sum(NC>0); % corresponding number of values, else E = e; N = NC; end; FPE = E.*(N+m)./(N-m); %OK optFPE=find(FPE==min(FPE))-1; %optimal order if isempty(optFPE), optFPE=NaN; end; AIC = N*log(E)+2*m; %OK optAIC=find(AIC==min(AIC))-1; %optimal order if isempty(optAIC), optAIC=NaN; end; AIC4=N*log(E)+4*m; %OK optAIC4=find(AIC4==min(AIC4))-1; %optimal order if isempty(optAIC4), optAIC4=NaN; end; m=1:M; BIC=[ N*log(E(1)) N*log(E(m+1)) - (N-m).*log(1-m/N) + m*log(N) + m.*log(((E(1)./E(m+1))-1)./m)]; %BIC=[ N*log(E(1)) N*log(E(m+1)) - m + m*log(N) + m.*log(((E(1)./E(m+1))-1)./m)]; %m=0:M; BIC=N*log(E)+m*log(N); % Hannan, 1980 -> Akaike, 1977 and Rissanen 1978 optBIC=find(BIC==min(BIC))-1; %optimal order if isempty(optBIC), optBIC=NaN; end; HAR(2:lc)=-(N-m).*log((N-m).*E(m+1)./(N-m+1)./E(m)); HAR(1)=HAR(2); optHAR=min(find(HAR<=(min(HAR)+0.2)))-1; %optimal order % optHAR=find(HAR==min(HAR))-1; %optimal order if isempty(optHAR), optHAR=NaN; end; m=0:M; SBC = N*log(E)+m*log(N); optSBC=find(SBC==min(SBC))-1; %optimal order if isempty(optSBC), optSBC=NaN; end; MDL = N*log(E)+log(N)*m; optMDL=find(MDL==min(MDL))-1; %optimal order if isempty(optMDL), optMDL=NaN; end; m=0:M; %CATcrit= (cumsum(1./E(m+1))/N-1./E(m+1)); E1=N*E./(N-m); CATcrit= (cumsum(1./E1(m+1))/N-1./E1(m+1)); optCAT=find(CATcrit==min(CATcrit))-1; %optimal order if isempty(optCAT), optCAT=NaN; end; PHI = N*log(E)+2*log(log(N))*m; optPHI=find(PHI==min(PHI))-1; %optimal order if isempty(optPHI), optPHI=NaN; end; JEW = E.*(N-m)./(N-2*m-1); % Jenkins-Watt optJEW=find(JEW==min(JEW))-1; %optimal order if isempty(optJEW), optJEW=NaN; end; % in case more than 1 minimum is found, the smaller model order is returned; p(k+1,:) = [optFPE(1), optAIC(1), optBIC(1), optSBC(1), optCAT(1), optMDL(1), optPHI(1), optJEW(1), optHAR(1)]; end; C=[FPE;AIC;BIC;SBC;MDL;CATcrit;PHI;JEW;HAR(:)']'; tsa/inst/eeg8s.mat0000664002356700235670000002003210473652132014550 0ustar schloeglschloegleeg8sÀ¼ÔØÕv¦ç?Ô­ù!}ñ? Š S¡±¿ˆCÂÏl¦Õ?FƒV¾Ò㿘ºp´)"ã¿.4(Wý}å¿îÓ Ÿþê¿:€zø“^î?Ài[ =ÑŸ?¾C&ÆnC鿊B‹LÀö‚,„šÀ ß6­¤*Àœà¹û,ó¿p<Ñï6¶?øßÚ}BFÍ?·•°]³ø?K’ù J@VM§A61ã?â³Ô-dvó?jÇSÀÇäð?ôÕ(zGä¿0—–†!(¼¿Ð¶A)›¼?˜Ä”O ÛÀ¿ÜV0zØ¿NÍM`Q è¿ì|‘ÁXoö?ñELC-Ûù?¹àtdÇ–ù?rÁ•øm-þ?› „Ήû?½s\p­êú?¼´&—@¦Û M@¦oPº]㿈 D)å?°@Nš†¸?Jø#§yçç?7oìC‚;ð¿ÖO„â¾.î¿À\¬òÇHã¿sÿe½å³ñ¿Xï !€Å¿l>2¹âƒè?ÀŽ„x"?Ç¿³ÙdjÄî?üÛøøžµÓ?CÀ—êZ¤ý?y>ÃùD£ô? ¦njÉêÇ?j¢tlï?–wÁRÉÀ¿|R„…ÜÔ¿ð€MŽÂœÇ¿•J ªbò¿Vóâ©Fžþ¿lÙbú¿¨VÌü«Ø¿²ûðtì¿ÑWÖòп@BŸ)°Å?à³J÷ȼ?À7PÜþþ ¿li•%Žçò?¸)`Ô)Ò?ôßnv×?XPxÚÊÜ¿0é%¬Þ»¿¨Ø {0Û?¶Ü(—â¿Ö]º„ö?®°ÜÕª~è¿Z{£>@4Š4úÓ?ÆN †?$–5hC×?àÊàó;U¦?°Š8§Xâ?ꈿOÀ:qÉû9ßó¿Úß 4ÉÀ¾£æTûö¿PÞeö?ï¿;YÚ¡»ýù¿ µñŽÖæ¿L•È7}rÓ?ÄìæÇë?øg39ïŸ@ ÖaE’Jô?µ6^nê?àè{iUÙ»?Õ:ü Ù¿ðâ5æ†yí¿Êbïѽæ¿%-ןóªü?™øº¸¿ÍN±àFÿ¿m¤ž®3ó¿ª3n¼¼ù¿R! _Zðñ¿>f€=Îé¿yrð»d×?6ü瀰é¿lö#ש!å¿ÍGŒ`ª|ð?02js¥ö?8¤òŽšå?€XçÞ¬¿ØQï£Jпy‘äëÚܿۆÃ÷Ðð¿8f¥ ‹á?0xHG-7ô?,)O<*V翲û¢xBÏ?q¦Š ÷ÀXzÖô·á¿àÔÍfé?›!±müØ¿xa¼ùað׿Èì½®†“Ð?@m„Æ-hØ?ÀÖ9²kdÜ?î7A²)Fõ?!ÕDôÀ':8À_®54„" À-'}Lû¿«°DmÔ׿2@6G’ö¿0™øqÎ?è°:-ó?²£BÄäû?ijÄÛMü?ಘշ Ì¿ ¦­ãÔ"Ü¿À4ϳʯ¿ i=iDìÙ¿Hº‹ÃÑ?™¿ÃÎÁ?Àýö2£?È%PG/»ö?`ì»ê¯È?ŽÚ@ Ù?F`$hAnð?¯hösÃë?¶w¸qŽ«þ?æ|$Ö¿@eˆó§´?wœcƒÂá? ½ãšQÿpè;s¡`á?ÔJ`ˆ9Žë¿€±ºlΘ¿Èzå¼0å¿eh=¦¸rÀš&)ÉåRó¿ Ì~UäîÕ¿àœz­4kå¿Ð†…ÚÖß?@ÇÎÉÈP²?PˆË‹é?äyrL?kî?œ+4}™û¿ˆ´0™WÐ?ÐfcT†Û?j!ñŽKõ¿´t¢üžê¿Ê§?;7À¢³Çu€|ó¿Pv/¥¡ì¿Õ Úí¸ÀPvŠ£ëå¿4òVضë¿zUH¯#Áñ¿ðÜÚ©øο(àk_‘;Õ¿øô!'Õ¿¶@=‹;ñ¿TÅÕbâ?^ûéÀN÷¿ ‹Êe ü¡?°ào‡]¾?R°xC⿃-Î$“@Œ£[d:ƒÞ¿(] ø»wâ?ƼF¬¶ó?&-½1Iî?<+¼Éº<ß?\–EÎÖà?è)ï î®Å¿ÈÅøþR'Ü¿ŒF_ÎÁú¿0‚·9ä`å¿à¬¤pIÀ9gÔ©ù­ñ¿Ràåö.À`€ºžˆ¸?:ø#UÖ÷?\ÿÀ‘s ç¿¢3ÌxRí?øsCn‹ê?À£Â€Míå?v6!@Ü7ñ?\” ïB®ø¿`ê{vƒN¡¿îw…á]ã¿ðH#Á¤·Ë¿kŽýšø?À©;Pòâ¿¶ q×î?ÇÉïªÀ£ð?\™qÁF–ó?üü˜æ¥²þ?òÿŸ@ rsž@pŸ˜Çìƒâ?Ø”ÃM[Ý¿`_l!hùª¿`˜ˆ¥çÝá¿S|ÂÂ&^ï¿òÔÈ<˜¸å¿á(NZáÀè¿rK›ˆ fò¿ LšcÖ­¿7INj锿H»cY‡å¿üËÆkוô?‹‘L}ÛŠì?®«#+ëŽÚ?ïè“ýÄ„í?¼v`Zù¿þ‚ˆ¤ð¿²îøÍà!ð¿"(tê®@à¿c`ÿagìê?Ä'Xèl‹Ø¿'Xþ1æõ?t¨ÿ•­?pmúxÿ¸Á¿ŠtËríîì?»8¾âð?¨ÚæßSw¾¿¸r¥t-úñ?z¦ñ˜ì?@r¼ DÅ?Æ|ñ¯ÜÄ?çâ%ƈ©ð?<6ŸÂjâ?œÂ—˜OöÀ¿ìµÍ‚gÔ?dR*ÑÚEÕ¿ ¢Ó AJÅ¿4¢í]íÖ?Ê[ðC}à?Dž!Ïüã?¼°éîœóû?8EH³< Æ?ÔjÒ†Úò?¤ŸaQúOò?öôz\¡«â?ûi=ïòò?ä"èå8”Ñ?WpjÄÈFø¿Ò¬€¢%ÀÖµ@XŠã¿Ñú³R¹?ÊøîuFÉõ?øÔFbü?OÓ\·½ø?:¿?÷?î|êKÏì? V|Qô?ÙCubS/@5†µD¡Wë?~Áзßò¿ÿГº*°ñ¿¥Ñ%ë'ù¿N¶Ùc!Š À+™…`À(lö ‚À¶|€! è¿#V¶³T‹ã?ˆ G—pÑí¿•©!:=ö¿Ñ×–Ñð¿ïÑ—N&å¿ÙYLË;bè¿äÉéÿ_ó¿,Ñjò«ê?öH¹"}濚…}P*#ο¾ ÿdí+Ò¿ò9'Þ-¿û!WËbÍà? !½¯×?ïú¬\¤²µ¿þTžJß¿@E‰¦1ˆ?Ìñ}ý#Æî¿„–xI¹ë¿+û3\ÀwKÙ¦û¿.›©Rbè¿Ò£ï‘ ÷¿â °'5Ïí¿v\K1 ֿűp½Ÿ÷?ü¤VzÆ@¸Îÿ·Æø?Еϡ,¡ç?`¦F¤±À?fVELÀõ–|ï* ñ¿ÎÚ{pqtë¿gâO|×û¿¤ Zfú¿jiõ’¾ à?äˆS­ðõ?¬5VŒ,ù?¨eÄE @ ¡”é @ ¨yinÇ@ðLÉ`bÓ?x¨ç¥êòµ?99nd œà¿~U„ÿmâé¿SYs`ò¿fx;Z*Œú¿€àü€Xš¿ ®ÑMHp¿Üµ¾¬+„Ê?RÒ² ²Æú? ‰{‡¼,Ì¿GÍôžÈà?7”as_ž@šæÓËÂ@ò?ð æx†£?¨¼¿£¡Î?¾Qëᨕì¿NÙfn=¶ó¿\~½=Ù í?! ÿÓÀ¿l ¯ïÞ?h¡l–ù-ê¿è­ 4ê?°¨K#`ÃÌ¿ºWÓ™ ñ¿ÀS ¸?\™+]§Õ?StH¥—¿ô¿Š&5¦ôã?¦{x.á¿PFX¿5]à¿Òh¬¨ó?âæÛÐ^øÀ—Ü”vRæð¿ÈlËníÑî¿€,cÞÍŽ?®ÏŽHµä¿0c¬ Þ¿@6UÅdp±?ø’ቪÎü¿qÔäO?Ìñ¿… P|=î¿ø£H´Hê¿þ[(Rwpà?n™jÊzÏ@èN”°â€õ?pUrÈ’—÷?ÝËà1Ðõ?0W¢]±Kä?È¡3bäò¿ÎÌ&H$-ᅳþB0‚]í¿( +3Nò¿¼“ôÏõ? u÷p›¦?øBn,úNÎ?äï¨ãwâ?; ÇÙ‘ò? ìó9‹€©?ïXÇ£†½@éÞ¿Ä¥÷?$%ííØZÛ?)×ÓÇ,Pô?ðù`é‰'å?w¯ 7Xê¿-¡ËÀ{ºÛ¡‰?ÒJ†¥Fõ¿@K ’È?üàréÚ!î?V›æ _bñ¿X;üi‚³ä?q1­ë¿`a3êÛ´¿ Gß³Ëjÿh2kM6á?GýØGúè¿8sZî¢îÒ?À€ÙÿWý ?ð¶ëD“þ? #Û’à0ò?h!<øê? bEaŸ »¿Øƒõ`¥Cì¿À”ŽK›Ä?`%â5t¿l Ž… IÀ€æxâ'8¿l>M¥Ö?@:Ú>…Bº¿ø˜{ó„Ø? 3h›¼?(TÆ4|Ô¿´T áU@꧈ÝÅ @© „F‡@΀~ä&Â@€ŒÈw"$ۿ˺_¥XÀlµ6 àà¿kHëfÉÀ.ÕØÎTÀÞ‡0ˆ—)ø¿ŒÀÝH῎çÕȈ㿜æP(µôç? ’†:ÉqÔ?æ´'×óû?’!é§{ú?‰`ÇÖ÷¿l©a&½õã¿ê O¦»ÀŒý³Îfú¿LE­„ò¿¿²G4Ù?êcE‘ø?”viHSkô?ý€†ë?¬ÿ1yªëò?>>6}£ö?XË6V Fè?DÁ;X¹úà?bìkŒK4ò¿ü Á4ô¿, ª^ô¿èRB2!›å¿`#ý¦)AÑ?0Ê5‰RÆ¿¨o\Èlpß?"2›Îøcñ?aÂzÞ@é¿L&)öõFõ?ÏH~z=Þ?ϪÍð[Ào“$/ÀŒv•÷ÀŒäK<(pø¿<&Ùá¿ Í‘·¤À•ôGOã¿ ·¥ò?€ Ô¹í?¦¨3jã„÷?Þñ[¹ˆyþ? r±Á Ñ?P¡z%ê¿lOÚ­nQÿ¿îf"‡ ù¿\a+Ù{ê¿àRëðH6Ý¿¯tÔæ?€cóx'HŸ¿ž  ~É@§¾²ë˜%@ AWÝb–ë?xÉD94 ä?ðœ€Áë¿À-IxþöÓ¿=~òGÙè¿àÙ½€ØiË¿ð©måfê¿2Ç}ŒÜcø¿€­Õ¥ˆò꿯´—@ïåçê@ü ´šR@°µC0Ì@ ý®•ó»¿Ðªp7{o׿Úg­óDÀØH¾°r8ç¿€G<Ø+±Û¿PúmHå¿ »qèv¸î?ä@pE½Çì?¨ B{ü?Z…ï’Ë@„¬v¼Ý5ú?(ÙW_‚cã?üÎã…3lð¿æ˜Ìh£ú¿œdvhx|å¿ðPÙž´ì¿˜s…LÛ¿8¬Ü;]hï?l3=Z¦@ª‰\'Ó@0:‰ @— ÚŽ@&š£:îšõ? Yr3eö?¨ßÆu;ƒî¿^s/@ý¶ó¿~ÆØ…ô¿F¦Ê4 À¸!d_ñ¿ØV‹Ý®?Ñ?ÆÕ?˜ßÎó?Ù&Æ@@¦BeÖæ@C(m¬Á@ ìtž´Ê?"ð©Àì¿°èRPãë¿ ò ˆX À¾sz5þìÀþȼ¿áô¿ºÓ50Àÿà„ï¤^¸?²_J;@Ö¿ÈÝi¯·@ò|ÿ¯ý?†R Zïþ?–AÏ“ƒÀú¿–'ÀÁÒþ¿×„²ù±xÀx‘•S›ÀÀ³«f]Ë¿è^[²ÉÙò¿sËiÎò¿ÆïK\Cù?Hò§ ù?pb*;¶@xëém{ƒÑ¿ºþdŠ(¼ÿ¿Z'Ýù·îæ¿1±Ûÿ}áô¿:÷ÂÊÀ½½úGˆ¼ÀÚëÏæ»ØÀ$Æå¢vï¿rÀ`ó¶æè?Ç(¨iOHó?Ýúzü @¡‘¤õ?¨‹{¦}¿àÿ_0D£ä?É®Ÿû¿¥¨.¦óÎú¿ÄV /Å9Ô?•¸5Ä@€õ¿„x«î?û£OÙgûø¿ Îã'Ú~÷¿ﶺ£@`vùÃUÂ?G<É K@²¡]÷Â@½°UÆ—@¥S¯ÆMþ?Ä«JÍÃï?¤¡¥¡{ôð¿…æp™^Šü?^¾·Õ ã?5WÛËä¿è *²œÅ¿p]Dÿ­Ê¿eýUá,§@Lú÷CÐwó?ŽðÆQ‡ÿ?îÐ?Õ-pü?@¾¼¼`'Ü? ³¯ ð?W-]ÏÄ´?ÕT^Ùó¿S>—¢ý#ð?º.Q®µ«é¿ï Çñ³?¿[vÛJYù?X‘ØCÚÛ?; Fnô¿²4#æâbý¿Úz*Evë¿t à@üÙJ$Âuè?FÅHÙÁ?²Î¦í©oß¿¨ÛÆßµÞ?àHu¿ð?(÷mŽy±?»ÇÀÏù¿ðì)M2û¿]ËɺV1À>ýv æõ¿º…ÿ |Œò¿ÂŸ¾£Y2é?ƒÃeôwç¿ØíÙË â?xWwgÊ连¬©Ó ö?}ÅwÄ”ê?ºð`ž¼aÕ¿D)„ø ð?Ë2¢©#_ß¿GªÅ2É׿Èd‡EäÍ?žU¦zÖ¿üxÒš½¿ÐMfÚû?>ØKG¿Gû?¤H8® è?FjE“KÐä?úëF‰}õ¿bñ ˆx~í¿DÇI çÿ»?YŸ¸ùæì¿@ >*ß?Øïƒ›4®ˆ?Í:5âˆ0Ê?»”Wâ‰Öä¿”ÎIxäï?9„¤v@áx^HÀN;]—û¿¾ˆ<•ÃÛ À&¿9˜šÿ¿X„AdíÓ„?Á§µ‡ê¿üßÃü“æ?¨qýAïcï¿ö¾Ï6‘ÞØ?ˆ’Ùì?V ,—$0¿¿Þ]3—ù@—*\š8ì?Nˆ¹d%ô?M¨'PñeÀ&Jͪ²çÀTÄÝ£þ ü¿ÇSR<æ×ÿ¿È£ÏËoö¿Rü‘&5ÿ¿Üý´]Ý¿ú+œÕFíÀ øJxŸâÀ|6‘ß—äê¿jj±Jàù¿.Š}1±0Ù?À`ÍîÊê¿oSCaÀÃyrqˆÿ¿#¬§À²>ëHî]Ê¿–R- Ú?¾¿,éð?¦r.É¿ö;ÃÒ8L¹?6ãN`»Ù¿×Ó«t¨Ä?>Sl™æžï¿qòÅkä±î¿o øý Pö¿»!7mIÀkTHùÆA忤»æ+ÕEñ¿£lî0ã?Æò´I8ƒ @~ y€ä“þ?5Ž»ñ‡Â@þ‡BJAû?^¤S4ÌHË?HÔœŠcÔ¸?¯^±ò.XÀoÃWi¦qå¿ãÈ óÄâ¿™µ+«Ní¿¹©‹Èì®?޶;Ðô?G 'B @8:²®/@À=4H@;‡:/vž@Ì5Åö @6úÜhú¤¿Z1©èï¿ý»¡€UTç?=®gW¡BÕ?Ù ¸«ØÐä¿»®ú}+:ö¿¸f·‘Š°Ö¿åÅ×ý£û?@7¶í3@›}ßeo @RB:LA@¥n  tY@?lTb@Âà€hò?ÕtŒf§ë?D Y Ü*ó?ÒP-ñ?|Þ£sQý?{¸®àê?^a˜‘f@ ì­Ð%e@èNbön @Öó,¶@à|÷^TQõ?@6ƒ ‚§£?[Pž™€Õë?2¬P&d𿳎4iü¿_!ë߀ÿ¿¬úTÍ®àÛ?ý % This is part of the TSA-toolbox. See also % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [yr,yc] = size(Y); if yr < yc, fprintf(2,'Warning LCP: data vector Y must be a column not a row vector\n'); end; if nargin < 2, P = yr-1; end; % you can use any of the following routines. % the lattice methods are preferable for stochastic time series. % but can fail for deterministic signals see: % http://sourceforge.net/mailarchive/message.php?msg_name=20080516115110.GB20642%40localhost % [AR,RC,PE] = lattice(Y.',P); % Burg method % [AR,RC,PE] = lattice(Y.',P,'GEOL'); % geometric lattice [AR,RC,PE] = durlev(acovf(Y.',P)); % Yule-Walker A = ar2poly(AR); tsa/inst/ac2poly.m0000664002356700235670000000260312115040716014557 0ustar schloeglschloeglfunction [A,E] = ac2poly(acf); % converts the autocorrelation sequence into an AR polynomial % [A,Efinal] = ac2poly(r) % % see also ACOVF ACORF AR2RC RC2AR DURLEV AC2POLY, POLY2RC, RC2POLY, RC2AC, AC2RC, POLY2AC % % $Id: ac2poly.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if all(size(acf))>1, fprintf(2,'Error AC2POLY: "r" must be a vector\n'); return; end; mfilename='AC2POLY'; if ~exist('durlev','file') fprintf(2,'Error %s: DURLEV.M not found. \n Download TSA toolbox from http://pub.ist.ac.at/~schloegl/matlab/tsa/\n',mfilename); return; end; [AR,RC,PE] = durlev(acf); A = [ones(size(AR,1),1),-AR]; E = PE(:,size(PE,2)); tsa/inst/invest0.m0000664002356700235670000000520612115040716014600 0ustar schloeglschloeglfunction [AutoCov,AutoCorr,MX,E,NC]=invest0(Y,Pmax,Mode); % First Investigation of a signal (time series) - automated part % [AutoCov,AutoCorr,ARPMX,E,ACFsd,NC]=invest0(Y,Pmax); % % [AutoCov,AutoCorr,ARPMX,E,ACFsd,NC]=invest0(AutoCov,Pmax,Mode); % % % Y time series % Pmax maximal order (optional) % % AutoCov Autocorrelation % AutoCorr normalized Autocorrelation % PartACF Partial Autocorrelation % ARPMX Autoregressive Parameter for order Pmax-1 % E Error function E(p) % NC Number of values (length-missing values) % % REFERENCES: % P.J. Brockwell and R.A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % M.S. Grewal and A.P. Andrews "Kalman Filtering" Prentice Hall, 1993. % S. Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % $Id: invest0.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2003,2008,2010 by Alois Schloegl % This is part of the TSA-toolbox. See also % http://biosig-consulting.com/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if nargin<3 Mode=0; else Mode=1; end; [nr,nc]=size(Y); NC = sumskipnan(real(~isnan(Y)),2); % number of valid components (data points) if Mode==0 if nargin<2, Pmax = min([100 nc/3]); end; M = min(Pmax,nc-1); AutoCov = acovf(Y,M); else AutoCov=Y; M = min(Pmax,nc-1); nc = Pmax; %M=size(AutoCov,2)-1; end; AutoCorr = AutoCov(:,2:M+1)./AutoCov(:,ones(M,1)); if 1,Pmax<100; % this needs change of sinvest1 K=M-1; [MX,E]=lattice(Y,Pmax); %[MX,E]=ulsar(Y,Pmax); %[MX,E]=durlev(AutoCov); % ARP=MX(:,K*(K-1)/2+(1:K)); % PartACF =MX(:,(1:K).*(2:K+1)/2); else %if nargout > 2 [ARP,RC,E]=lattice(Y,Pmax); %[ARP,PartACF,E]=durlev(AutoCov); %MX=[ARP,RC]; end; tsa/inst/sinvest1.m0000664002356700235670000003451412115040716014770 0ustar schloeglschloegl%SINVEST1 shows the parameters of a time series calculated by INVEST1 % only called by INVEST1 % $Id: sinvest1.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . Fs=flag_implicit_samplerate; M=size(AutoCorr,2); oo=M-1; while 1, K=menu('Select','Autocovariance ACOVF','Autocorrelation ACF', ... 'Partial ACF PACF','Coeff. of Determination R²', ... 'Error curve',... 'Autoregressive Parameters',... 'Information Criteria', ... 'Matched Filter', ... 'Log PSD and Phase', ... 'Poles', ... 'Inverse Filtering', ... 'Spectra H(f,p)', ... 'Entropy H=ln(det(R))', ... 'Histogram of MOPS', ... 'end'); subplot(111); if K==1 plot(0:M,AutoCov); title('Autocovariance function ACOVF(k)'); xlabel('Lag k'); elseif K==2 if exist('OCTAVE_VERSION')==5 %%%%% Fuer OCTAVE plot(1:M,AutoCorr); elseif strcmp(version,'MIDEVA') %%%%% fuer MatCom plot(1:M,AutoCorr); else %%%%% fuer Matlab % size(AutoCorr),size(ACFsd)] % errorbar(ones(nr,1)*(1:M),AutoCorr,ACFsd,ACFsd); % errorbar(1:M,AutoCorr,AutoCorr-ACFsd,AutoCorr+ACFsd); plot(1:M,AutoCorr,'b',[1,M],[-1,1;-1,1]/sqrt(min(NC)),'b:'); if exist('OCTAVE_VERSION')<5 legend({'ACF','1/sqrt(N)'}) end; end; title('Autocorrelation function ACF(k)'); xlabel('Lag k'); elseif K==3 %rc=ARPMX(:,(1:M).*(2:M+1)/2); %plot(1:M,PartACF); if size(ARPMX,2)==2*Pmax, RC=ARPMX(:,Pmax+1:2*Pmax); else RC=ARPMX(:,(1:M).*(2:M+1)/2); end; % according to http://www.itl.nist.gov/div898/handbook/pmc/section4/pmc4463.htm % is the 95% confidence interval 2/sqrt(N) %plot(1:M,RC,'b',[1,M],[3,3;2,2;1,1;-1,-1;-2,-2;-3,-3]*(1/sqrt(min(NC))),'b:'); %legend({'Part. ACF','1/sqrt(N)','2/sqrt(N) = 95% confidence interval','3/sqrt(N)'}) plot(1:M,RC,'b',[1,M],[2,2;-2,-2]'*(1/sqrt(min(NC))),'b:'); if exist('OCTAVE_VERSION')<5 legend({'Part. ACF','2/sqrt(N) = 95% confidence interval'}) end; title('Partial Autocorrelation function PACF(k)'); xlabel('Lag k'); elseif K==4 plot(0:M,(E(1)-E)/E(1)); title('Determination of Regression R²=1-var{E}/var{Y}'); xlabel('Model order p'); elseif K==5 plot(0:M,E,'r'); if exist('OCTAVE_VERSION')<5 v=axis; v(3)=min([v(3); 0]); axis(v); end title('Mean Square (prediction) Error decrease with increasing model order'); xlabel('Model order p'); elseif K==6 plot(1:oo,ARPMX(:,oo/2*(oo-1)+(1:oo))','o-'); title( ['AutoRegressive Parameters with model order' int2str(oo)]) elseif K==7 while 1 K=menu('Select Criterion',... 'Final Predection Error FPE', ... 'Akaike Information Criterion AIC', ... 'Bayesian Akaike Information Criterion BIC', ... 'Schwartz''s Bayesian Crit. SBC (=MDL)', ... 'Parzen''s CAT Criterion ', ... 'PHI Criterion', ... 'end'); if K==1 tmp=min(2*optFPE+2,M); oo=optFPE; plot(0:tmp-1,FPE(1:tmp),'r',optFPE,FPE(optFPE+1),'ro'); if exist('OCTAVE_VERSION')<5 text(optFPE,FPE(optFPE+1),sprintf('%i',optFPE)); v=axis; v(3)=min([v(3); 0]); axis(v); end; title('Final Prediction Error FPE criterion'); elseif K==2 tmp=min(2*optAIC+2,M); oo=optAIC; plot(0:tmp-1,AIC(1:tmp),'r',optAIC,AIC(optAIC+1),'ro'); if exist('OCTAVE_VERSION')<5 text(optAIC,AIC(optAIC+1),sprintf('%i',optAIC)); v=axis; v(3)=min([v(3); 0]); axis(v); end; title('Akaike''s Information Criterion AIC'); elseif K==3 tmp=min(2*optBIC+2,M); oo=optBIC; plot(0:tmp-1,BIC(1:tmp),'r',optBIC,BIC(optBIC+1),'ro'); if exist('OCTAVE_VERSION')<5 text(optBIC,BIC(optBIC+1),sprintf('%i',optBIC)); v=axis; v(3)=min([v(3); 0]); axis(v); end; title('Bayesian Akaike Information Criterion BIC'); elseif K==4 tmp=min(2*optSBC+2,M); oo=optSBC; plot(0:tmp-1,SBC(1:tmp),'r',optSBC,SBC(optSBC+1),'ro'); if exist('OCTAVE_VERSION')<5 text(optSBC,SBC(optSBC+1),sprintf('%i',optSBC)); v=axis; v(3)=min([v(3); 0]); axis(v); end; title('Schwartz''s Bayesian Criterion SBC'); %elseif K==5 % tmp=min(2*optMDL,M); % oo=optMDL; % plot(0:tmp-1,MDL(1:tmp),'r',optMDL,MDL(optMDL+1),'ro'); % v=axis; v(3)=min([v(3); 0]); axis(v); % text(optMDL,MDL(optMDL+1),sprintf('%i',optMDL)) % title('Minimal Description length Criterion MDL'); elseif K==5 tmp=min(2*optCAT+2,M); oo=optCAT; plot(0:tmp-1,CATcrit(1:tmp),'r',optCAT,CATcrit(optCAT+1),'ro'); if exist('OCTAVE_VERSION')<5 text(optCAT,CATcrit(optCAT+1),sprintf('%i',optCAT)); v=axis; v(3)=min([v(3); 0]); axis(v); end; title('Parzen''s CAT Criterion '); elseif K==6 tmp=min(2*optPHI+2,M); oo=optPHI; plot(0:tmp-1,PHI(1:tmp),'r',optPHI,PHI(optPHI+1),'ro'); if exist('OCTAVE_VERSION')<5 text(optPHI,PHI(optPHI+1),sprintf('%i',optPHI)); v=axis; v(3)=min([v(3); 0]); axis(v); end; title('Phi criterion '); elseif K==7 %[ARP,rc,res] =durlev(sum(AutoCov(:,1:(oo+1)),1)); ARP=ARPMX(:,oo/2*(oo-1)+(1:oo)); break; end; %IF end; %WHILE elseif K==8 % if model order p is given then the filter parameters A are % A=[1; -earpyw(signal,p)] % inverse filtering is invfiltsignal=filter(A,1,signal); h=zeros(nr,512)'; w=zeros(nr,512)'; for k=1:nr, tmp=freqz(sqrt(E(k,oo+1)),[1 -ARPMX(k,oo/2*(oo-1)+(1:oo))],512); h(:,k)=tmp(:); end plot((0:511)/512/2*Fs,abs(h)); %plot((0:511)/512/2,abs(freqz(1,[1 -ARP]'))); title('Matched Filter'); xlabel('Frequency f'); ylabel('|H(f)|'); elseif K==9 h=zeros(nr,512)'; w=zeros(nr,512)'; for k=1:nr, tmp=freqz(sqrt(E(k,oo+1)),[1 -ARPMX(k,oo/2*(oo-1)+(1:oo))],512); h(:,k)=tmp(:); end; subplot(211); semilogy((0:511)/512/2*Fs,abs(h')) %semilogy((0:511)/512/2,abs(freqz(1,[1 -ARP]'))) title('Logarithmic Spectral Density Fct.'); subplot(212); plot((0:511)/512/2*Fs,angle(h)'); %plot((0:511)/512/2,angle(freqz(1,[1 -ARP]'))); ylabel('rad'); elseif K==10 % clf; %r = roots([1 -ARP]); r = roots([1 -ARPMX(:,oo/2*(oo-1)+(1:oo))]); t = 0:1/70:2*pi; plot(cos(t), sin(t), 'b:',real(r), imag(r), 'rx'); % zplane([],[1 -ARPmx(oo+1,1:oo)]); title('Pole Diagram'); xlabel('real(z)'); ylabel('imag(z)'); MATLAB_VERSION = version; if MATLAB_VERSION(1)=='4' ax = gca; tmp = get(ax,'Aspect'); set(ax,'Aspect',[tmp(1),1]); elseif MATLAB_VERSION(1)=='5' ax = gca; tmp = get(ax,'DataAspectRatio'); set(ax,'PlotBoxAspectRatio',tmp); end; elseif K==11 plot([Y(:) filter([1 -ARPMX(:,oo/2*(oo-1)+(1:oo))],1,Y(:))-max(Y)+min(Y)]); elseif K==12 %[tmp,ARPmx,PE]= acf2pacf(AutoCov(2:length(AutoCov))/AutoCov(1),AutoCov(1)); %[arp,rc,PE,ARPMX] = durlev(AutoCov); %[tmp,ARPmx]=arp2pacf(AR); N = Pmax-1; %2*oo-1; %2^(ceil(log(oo)/log(2))); sdf = zeros(512,N); %length(AR)); for k = 1:N; %[k,size(sdf),N],%length(AR); % [sdf(:,k),F] = freqz(1,[1 -ARPmx(k,1:k)],N); [tmp,F] = freqz(1,[1 -ARPMX(1,k/2*(k+1)+(1:k))]',512); sdf(:,k)=tmp(:); % sdf(:,k)=sqrt(E(k+1))*sdf(:,k); end; mesh(F/2/pi*Fs,1:N,log10(abs(sdf)')); zlabel('log10 |H(f,p)|');ylabel('model order p'); xlabel('frequency f [2*pi rad/s]'); if exist('OCTAVE_VERSION')<5 view(30,45); end; elseif K==13 for k=1:M, xxx=eig(toeplitz(AutoCov(1:k)/E(k))); H(k) = .5*sum(log(xxx)); H1(k)= H(k)/(k); %xxx1=eig(toeplitz(AutoCov(1:k)/E(k)*E(1))); %xxx1=eig(toeplitz(AutoCorr(1:k))); %H2(k) =.5*sum(log(xxx1)); %H3(k)=.5*sum(log(xxx1))/(k); %if any(xxx<=0) fprintf(1,'COV positive definite for p up to %i\n',k-1); break; end; end; if 0 subplot(211); plot(0:k-1,H2'); title('Entropy H(k) depending on number of coefficients') subplot(212); plot(0:k-1,H3'); title('Entropy rate H(k) depending on number of coefficients') else subplot(311); plot(0:k-1,H'); title('Entropy H(k) depending on number of coefficients') subplot(312); xxx=(-diff(log(E(:))))/2; %[size(xxx)size(H) M k] plot(0:k-1,H1','b',1:k,xxx(1:k),'g'); %plot(0:k-1,H1','b',0:k-1,log(E(1:k))-log(E(1)),'b',1:k,xxx(1:k),'g'); %plot(0:k-1,H1','b',1:k,xxx,'g',1:k-1,cumprod(1+xxx(1:M-1))./H(2:M)','r'); title('Entropy rate H(k) and Entropy difference H(k)-H(k+1)') subplot(313); plot(1:k,log(xxx(1:k)),'g'); title('LOG Entropy difference for p->p+1: H(k)-H(k+1)') end; elseif K==14, tmp = histo3(MOPS(1:max(1,size(MOPS,1)-1),:)); if exist('OCTAVE_VERSION')<5, bar(tmp.X,tmp.H,'stacked'); else bar(tmp.X,sum(tmp.H,2)); end; xlabel('model order p') if exist('OCTAVE_VERSION')<5 %legend({'FPE','AIC','BIC','SBC','MDL','CAT','PHI','JEW','HAR'}); legend('FPE','AIC','BIC','SBC','MDL','CAT','PHI','JEW','HAR'); end; elseif K==15 break; end; end; tsa/inst/mvaar.m0000664002356700235670000001207012115040716014313 0ustar schloeglschloeglfunction [x,e,Kalman,Q2] = mvaar(y,p,UC,mode,Kalman) % Multivariate (Vector) adaptive AR estimation base on a multidimensional % Kalman filer algorithm. A standard VAR model (A0=I) is implemented. The % state vector is defined as X=(A1|A2...|Ap) and x=vec(X') % % [x,e,Kalman,Q2] = mvaar(y,p,UC,mode,Kalman) % % The standard MVAR model is defined as: % % y(n)-A1(n)*y(n-1)-...-Ap(n)*y(n-p)=e(n) % % The dimension of y(n) equals s % % Input Parameters: % % y Observed data or signal % p prescribed maximum model order (default 1) % UC update coefficient (default 0.001) % mode update method of the process noise covariance matrix 0...4 ^ % correspond to S0...S4 (default 0) % % Output Parameters % % e prediction error of dimension s % x state vector of dimension s*s*p % Q2 measurement noise covariance matrix of dimension s x s % % $Id: mvaar.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 2001-2002 Christian Kasess % Copyright (C) 2003, 2008 Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if nargin<4, mode=0; end; if nargin<3, UC=0.001 end; if nargin<2, p=1; end if nargin<1, fprintf(2,'No arguments supplied\n'); return end; if ~any(mode==(0:4)) fprintf(2,'Invalid mode (0...4)\n'); return end; [M,LEN] = size(y'); %number of channels, total signal length L = M*M*p; if LEN<(p+1), fprintf(2,'Not enough observed data supplied for given model order\n'); return end ye = zeros(size(y)); %prediction of y if nargout>1, x=zeros(L,LEN); end; if nargout>3, Q2=zeros(M,M,LEN); end if nargin<5, %Kalman Filter initialsiation (Kp (K predicted or a-priori) equals K(n+1,n) ) Kalman=struct('F',eye(L),'H',zeros(M,L),'G',zeros(L,M),'x',zeros(L,1),'Kp',eye(L),'Q1',eye(L)*UC,'Q2',eye(M),'ye',zeros(M,1)); end; upd = eye(L)/L*UC; %diagonal matrix containing UC if(mode==3) Block=kron(eye(M),ones(M*p)); elseif(mode==4) index=[]; Block1=[]; Block0=[]; for i=1:M, index=[index ((i-1)*M*p+i:M:i*M*p)]; mone=eye(M); mone(i,i)=0; mzero=eye(M)-mone; Block1=Blkdiag(Block1,kron(eye(p),mone)); Block0=Blkdiag(Block0,kron(eye(p),mzero)); end; end; for n = 2:LEN, if(n<=p) Yr=[y(n-1:-1:1,:)' zeros(M,p-n+1)]; %vector of past observations Yr=Yr(:)'; else Yr=y(n-1:-1:n-p,:)'; %vector of past observations Yr=Yr(:)'; end %Update of measurement matrix Kalman.H=kron(eye(M),Yr); %calculate prediction error ye(n,:)=(Kalman.H*Kalman.x)'; err=y(n,:)-ye(n,:); if ~any(isnan(err(:))), %update of Q2 using the prediction error of the previous step Kalman.Q2=(1-UC)*Kalman.Q2+UC*err'*err; KpH=Kalman.Kp*Kalman.H'; HKp=Kalman.H*Kalman.Kp; %Kalman gain Kalman.G=KpH*inv(Kalman.H*KpH+Kalman.Q2); %calculation of the a-posteriori state error covariance matrix %K=Kalman.Kp-Kalman.G*KpH'; Althouh PK is supposed to be symmetric, this operation makes the filter unstable K=Kalman.Kp-Kalman.G*HKp; %mode==0 no update of Q1 %update of Q1 using the predicted state error cov matrix if(mode==1) Kalman.Q1=diag(diag(K)).*UC; elseif(mode==2) Kalman.Q1=upd*trace(K); elseif(mode==3) Kalman.Q1=diag(sum((Block*diag(diag(K)))'))/(p*M)*UC; elseif(mode==4) avg=trace(K(index,index))/(p*M)*UC; Kalman.Q1=Block1*UC+Block0*avg; end %a-priori state error covariance matrix for the next time step Kalman.Kp=K+Kalman.Q1; %current estimation of state x Kalman.x=Kalman.x+Kalman.G*(err)'; end; % isnan>(err) if nargout>1, x(:,n) = Kalman.x; end; if nargout>3, Q2(:,:,n)=Kalman.Q2; end; end; e = y - ye; x = x'; tsa/inst/bispec.m0000664002356700235670000000315512115040716014456 0ustar schloeglschloeglfunction [BISPEC,BIACF,ACF] = bispec(Z,N); % Calculates Bispectrum % [BISPEC] = bispec(Z,N); % % Input: Z Signal % N # of coefficients % Output: BiACF bi-autocorrelation function = 3rd order cumulant % BISPEC Bi-spectrum % % Reference(s): % C.L. Nikias and A.P. Petropulu "Higher-Order Spectra Analysis" Prentice Hall, 1993. % M.B. Priestley, "Non-linear and Non-stationary Time series Analysis", Academic Press, London, 1988. % $Id: bispec.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1997-2003,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . P=N+1; ACF=zeros(1,N+1); BIACF=zeros(2*N+1,2*N+1); Z=Z(:); M=size(Z,1); M1=sum(Z)/M; Z=Z-M1*ones(size(Z)); for K=0:N, jc2=Z(1:M-K).*Z(1+K:M); ACF(K+1)=sum(jc2)/M; for L = K:N, jc3 = sum(jc2(1:M-L).*Z(1+L:M))/M; BIACF(K+P, L+P) =jc3; BIACF(L+P, K+P) =jc3; BIACF(L-K+P, -K+P)=jc3; BIACF(-K+P, L-K+P)=jc3; BIACF(K-L+P, -L+P)=jc3; BIACF(-L+P, K-L+P)=jc3; end; end; BISPEC=fft2(BIACF,128,128); tsa/inst/mvfilter.m0000664002356700235670000000615212115040716015041 0ustar schloeglschloeglfunction [x,z]=mvfilter(B,A,x,z) % Multi-variate filter function % % Y = MVFILTER(B,A,X) % [Y,Z] = MVFILTER(B,A,X,Z) % % Y = MVFILTER(B,A,X) filters the data in matrix X with the % filter described by cell arrays A and B to create the filtered % data Y. The filter is a 'Direct Form II Transposed' % implementation of the standard difference equation: % % a0*Y(n) = b0*X(:,n) + b1*X(:,n-1) + ... + bq*X(:,n-q) % - a1*Y(:,n-1) - ... - ap*Y(:,n-p) % % A=[a0,a1,a2,...,ap] and B=[b0,b1,b2,...,bq] must be matrices of % size Mx((p+1)*M) and Mx((q+1)*M), respectively. % a0,a1,...,ap, b0,b1,...,bq are matrices of size MxM % a0 is usually the identity matrix I or must be invertible % X should be of size MxN, if X has size NxM a warning % is raised, and the output Y is also transposed. % % A simulated MV-AR process can be generiated with % Y = mvfilter(eye(M), [eye(M),-AR],randn(M,N)); % % A multivariate inverse filter can be realized with % [AR,RC,PE] = mvar(Y,P); % E = mvfilter([eye(M),-AR],eye(M),Y); % % see also: MVAR, FILTER % $Id: mvfilter.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2003,2010,2012 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . [ra, ca] = size(A); [rb, cb] = size(B); [M, N ] = size(x); if (ra~=rb), fprintf(2,'ERROR MVFILTER: number of rows of A and B do not fit\n'); return; end; if nargin<4, z = []; %zeros(M,oo); end; if (M~=ra), if (N==ra), fprintf(2,'Warning MVFILTER: dimensions fit only to transposed data. X has been transposed.\n'); x = x.'; %[x,z] = mvfilter(B,A,x,z); x = x.'; return; else fprintf(2,'ERROR MVFILTER: dimensions do not fit\n'); return; end; end; p = ca/M-1; q = cb/M-1; oo = max(p,q); if isempty(z) z = zeros(M,oo); else if any(size(z)~=[M,oo]) fprintf('Error MVFILTER: size of z does not fit\n'); [size(z),oo,M] return; end; end; %%%%% normalization to A{1}=I; for k=1:p, A(:,k*M+(1:M)) = A(:,1:M) \ A(:,k*M+(1:M)); end; for k=0:q, B(:,k*M+(1:M)) = A(:,1:M) \ B(:,k*M+(1:M)); end; A(:,1:M) = eye(M); for k = 1:N, acc = B(:,1:M) * x(:,k) + z(:,1); % / A{1}; z = [z(:,2:oo), zeros(M,1)]; for l = 1:q, z(:,l) = z(:,l) + B(:,l*M+(1:M)) * x(:,k); end; for l = 1:p, z(:,l) = z(:,l) - A(:,l*M+(1:M)) * acc; end; x(:,k) = acc; end; tsa/inst/contents.m0000664002356700235670000001355112115040716015047 0ustar schloeglschloegl% Time Series Analysis - A toolbox for the use with Matlab and Octave. % % $Id: contents.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1996-2004,2008 by Alois Schloegl % WWW: http://pub.ist.ac.at/~schloegl/matlab/tsa/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . % % % Time Series Analysis - a toolbox for the use with Matlab % aar adaptive autoregressive estimator % acovf (*) Autocovariance function % acorf (acf) (*) autocorrelation function % pacf (*) partial autocorrelation function, includes signifcance test and confidence interval % parcor (*) partial autocorrelation function % biacovf biautocovariance function (3rd order cumulant) % bispec Bi-spectrum % durlev (*) solves Yule-Walker equation - converts ACOVF into AR parameters % lattice (*) calcultes AR parameters with lattice method % lpc (*) calculates the prediction coefficients form a given time series % invest0 (*) a prior investigation (used by invest1) % invest1 (*) investigates signal (useful for 1st evaluation of the data) % rmle AR estimation using recursive maximum likelihood function % selmo (*) Select Order of Autoregressive model using different criteria % histo (*) histogram % hup (*) test Hurwitz polynomials % ucp (*) test Unit Circle Polynomials % y2res (*) computes mean, variance, skewness, kurtosis, entropy, etc. from data series % ar_spa (*) spectral analysis based on the autoregressive model % detrend (*) removes trend, can handle missing values, non-equidistant sampled data % flix floating index, interpolates data for non-interger indices % % % Multivariate analysis % adim adaptive information matrix (inverse correlation matrix) % mvar multivariate (vector) autoregressive estimation % mvaar multivariate adaptvie autoregressive estimation using Kalman filtering % mvfilter multivariate filter % mvfreqz multivariate spectra % arfit2 provides compatibility to ARFIT [Schneider and Neumaier, 2001] % % % Conversions between Autocorrelation (AC), Autoregressive parameters (AR), % prediction polynom (POLY) and Reflection coefficient (RC) % ac2poly (*) transforms autocorrelation into prediction polynom % ac2rc (*) transforms autocorrelation into reflexion coefficients % ar2rc (*) transforms autoregressive parameters into reflection coefficients % rc2ar (*) transforms reflection coefficients into autoregressive parameters % poly2ac (*) transforms polynom to autocorrelation % poly2ar (*) transforms polynom to AR % poly2rc (*) % rc2ac (*) % rc2poly (*) % ar2poly (*) % % Utility functions % sinvest1 shows the parameter calculated by INVEST1 % % Test suites % tsademo demonstrates INVEST1 on EEG data % invfdemo demonstration of matched, inverse filtering % bisdemo demonstrates bispectral estimation % % (*) indicates univariate analysis of multiple data series (each in a row) can be processed. % (-) indicates that these functions will be removed in future % % REFERENCES (sources): % http://www.itl.nist.gov/ % http://mathworld.wolfram.com/ % P.J. Brockwell and R.A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % O. Foellinger "Lineare Abtastsysteme", Oldenburg Verlag, Muenchen, 1986. % F. Gausch "Systemtechnik", Textbook, University of Technology Graz, 1993. % M.S. Grewal and A.P. Andrews "Kalman Filtering" Prentice Hall, 1993. % S. Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996. % E.I. Jury "Theory and Application of the z-Transform Method", Robert E. Krieger Publishing Co., 1973. % M.S. Kay "Modern Spectal Estimation" Prentice Hall, 1988. % Ch. Langraf and G. Schneider "Elemente der Regeltechnik", Springer Verlag, 1970. % S.L. Marple "Digital Spetral Analysis with Applications" Prentice Hall, 1987. % C.L. Nikias and A.P. Petropulu "Higher-Order Spectra Analysis" Prentice Hall, 1993. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % T. Schneider and A. Neumaier "Algorithm 808: ARFIT - a matlab package for the estimation of parameters and eigenmodes of multivariate autoregressive models" % ACM Transactions on Mathematical software, 27(Mar), 58-65. % C.E. Shannon and W. Weaver "The mathematical theory of communication" University of Illinois Press, Urbana 1949 (reprint 1963). % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % % % REFERENCES (applications): % [1] A. Schlögl, B. Kemp, T. Penzel, D. Kunz, S.-L. Himanen,A. Värri, G. Dorffner, G. Pfurtscheller. % Quality Control of polysomnographic Sleep Data by Histogram and Entropy Analysis. % Clin. Neurophysiol. 1999, Dec; 110(12): 2165 - 2170. % [2] Penzel T, Kemp B, Klösch G, Schlögl A, Hasan J, Varri A, Korhonen I. % Acquisition of biomedical signals databases % IEEE Engineering in Medicine and Biology Magazine 2001, 20(3): 25-32 % [3] Alois Schlögl (2000) % The electroencephalogram and the adaptive autoregressive model: theory and applications % Shaker Verlag, Aachen, Germany,(ISBN3-8265-7640-3). % % Features: % - Multiple Signal Processing % - Efficient algorithms % - Model order selection tools % - higher (3rd) order analysis % - Maximum entropy spectral estimation % - can deal with missing values (NaN's) tsa/inst/ar2poly.m0000664002356700235670000000275712115040716014610 0ustar schloeglschloeglfunction [A] = ar2poly(A); % converts autoregressive parameters into AR polymials % Multiple polynomials can be converted. % function [A] = ar2poly(AR); % % INPUT: % AR AR parameters, each row represents one set of AR parameters % % OUTPUT % A denominator polynom % % % see also ACOVF ACORF DURLEV RC2AR FILTER FREQZ ZPLANE % % REFERENCES: % P.J. Brockwell and R. A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991. % S. Haykin "Adaptive Filter Theory" 3rd ed. Prentice Hall, 1996. % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. % W.S. Wei "Time Series Analysis" Addison Wesley, 1990. % $Id: ar2poly.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . % Inititialization [lr,lc]=size(A); A = [ones(size(A,1),1),-A]; tsa/inst/poly2ar.m0000664002356700235670000000244712115040716014604 0ustar schloeglschloeglfunction [A] = poly2ar(A); % Converts AR polymials into autoregressive parameters. % Multiple polynomials can be converted. % % function [AR] = poly2ar(A); % % INPUT: % A AR polynomial, each row represents one polynomial % % OUTPUT % AR autoregressive model parameter % % see also ACOVF ACORF DURLEV RC2AR AR2POLY % $Id: poly2ar.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . % Inititialization [lr,lc]=size(A); if ~all(A(:,1)==1) fprintf(2,'Warning POLY2AR: input argument might not be an AR-polynom'); end; A = -A(:,2:size(A,2))./A(:,ones(1,size(A,2)-1)); tsa/inst/rc2poly.m0000664002356700235670000000324312115040716014601 0ustar schloeglschloeglfunction [a,efinal] = rc2poly(RC,E); % converts reflection coefficients into an AR-polynomial % [a,efinal] = rc2poly(K) % % see also ACOVF ACORF AR2RC RC2AR DURLEV AC2POLY, POLY2RC, RC2POLY, RC2AC, AC2RC, POLY2AC % % $Id: rc2poly.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1998-2002,2008,2012 by Alois Schloegl % This is part of the TSA-toolbox. See also % http://pub.ist.ac.at/~schloegl/matlab/tsa/ % http://octave.sourceforge.net/ % http://biosig.sourceforge.net/ % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . if nargout>1 && nargin<2 fprintf('Zero-lag autocorrelation, R0 not specified\n') return; end; mfilename='RC2POLY'; if all(size(RC))>1, fprintf(2,'Error %s: "K" must be a vector\n',mfilename); return; end; if ~exist('rc2ar','file') fprintf(2,'Error %s: RC2AR.M not found. \n Download TSA toolbox from http://pub.ist.ac.at/~schloegl/matlab/tsa/\n',mfilename); return; end; [AR,RC,PE] = rc2ar(RC(:).'); a=[1,-AR]; efinal=PE(length(PE))/PE(1); tsa/inst/sumskipnan.m0000664002356700235670000001271412264575422015416 0ustar schloeglschloeglfunction [o,count,SSQ] = sumskipnan(x, DIM, W) % SUMSKIPNAN adds all non-NaN values. % % All NaN's are skipped; NaN's are considered as missing values. % SUMSKIPNAN of NaN's only gives O; and the number of valid elements is return. % SUMSKIPNAN is also the elementary function for calculating % various statistics (e.g. MEAN, STD, VAR, RMS, MEANSQ, SKEWNESS, % KURTOSIS, MOMENT, STATISTIC etc.) from data with missing values. % SUMSKIPNAN implements the DIMENSION-argument for data with missing values. % Also the second output argument return the number of valid elements (not NaNs) % % Y = sumskipnan(x [,DIM]) % [Y,N,SSQ] = sumskipnan(x [,DIM]) % [...] = sumskipnan(x, DIM, W) % % x input data % DIM dimension (default: []) % empty DIM sets DIM to first non singleton dimension % W weight vector for weighted sum, numel(W) must fit size(x,DIM) % Y resulting sum % N number of valid (not missing) elements % SSQ sum of squares % % the function FLAG_NANS_OCCURED() returns whether any value in x % is a not-a-number (NaN) % % features: % - can deal with NaN's (missing values) % - implements dimension argument. % - computes weighted sum % - compatible with Matlab and Octave % % see also: FLAG_NANS_OCCURED, SUM, NANSUM, MEAN, STD, VAR, RMS, MEANSQ, % SSQ, MOMENT, SKEWNESS, KURTOSIS, SEM % This program is free software; you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation; either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program; If not, see . % $Id: sumskipnan.m 9033 2011-11-08 20:58:07Z schloegl $ % Copyright (C) 2000-2005,2009,2011 by Alois Schloegl % This function is part of the NaN-toolbox % http://pub.ist.ac.at/~schloegl/matlab/NaN/ global FLAG_NANS_OCCURED; if nargin<2, DIM = []; end; if nargin<3, W = []; end; % an efficient implementation in C of the following lines % could significantly increase performance % only one loop and only one check for isnan is needed % An MEX-Implementation is available in sumskipnan.cpp % % Outline of the algorithm: % for { k=1,o=0,count=0; k++; k1,1); if isempty(DIM), DIM = 1; end; end if (DIM<1), DIM = 1; end; %% Hack, because min([])=0 for FreeMat v3.5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % non-float data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if (isempty(W) && (~(isa(x,'float') || isa(x,'double')))) || ~flag_implicit_skip_nan(), %%% skip always NaN's if ~isempty(W) error('SUMSKIPNAN: weighted sum of integers not supported, yet'); end; x = double(x); o = sum(x,DIM); if nargout>1 sz = size(x); N = sz(DIM); sz(DIM) = 1; count = repmat(N,sz); if nargout>2 x = x.*x; SSQ = sum(x,DIM); end; end; return; end; if (length(size(x))=3), [o,count,SSQ] = sumskipnan_mex(real(x),DIM,FLAG_NANS_OCCURED,W); if (~isreal(x)) [io,icount,iSSQ] = sumskipnan_mex(imag(x),DIM,FLAG_NANS_OCCURED,W); if any(count(:)-icount(:)) error('Number of NaNs differ for REAL and IMAG part'); else o = o+i*io; SSQ = SSQ+iSSQ; end; end; return; end; end; if ~isempty(W) error('weighted sumskipnan requires sumskipnan_mex'); end; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % count non-NaN's %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if nargout>1, count = sum(x==x,DIM); FLAG_NANS_OCCURED = any(count(:)2, x = real(x).^2 + imag(x).^2; SSQ = sum(x,DIM); end; %!assert(sumskipnan([1,2],1),[1,2]) %!assert(sumskipnan([1,NaN],2),1) %!assert(sumskipnan([1,NaN],2),1) %!assert(sumskipnan([nan,1,4,5]),10) %!assert(sumskipnan([nan,1,4,5]',1,[3;2;1;0]),6) tsa/inst/biacovf.m0000664002356700235670000000250312115040716014616 0ustar schloeglschloeglfunction [BIACF,ACF,M1] = biacovf(Z,N); % BiAutoCovariance function % [BiACF] = biacovf(Z,N); % % Input: Z Signal % N # of coefficients % Output: BIACF bi-autocorrelation function (joint cumulant 3rd order % Output: ACF covariance function (joint cumulant 2nd order) % $Id: biacovf.m 11693 2013-03-04 06:40:14Z schloegl $ % Copyright (C) 1997, 1998, 2008 by Alois Schloegl % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . ACF=zeros(1,N+1); BIACF=zeros(N+1,N+1); Z=Z(:); M=size(Z,1); M1=sum(Z)/M; Z=Z-M1*ones(size(Z)); for K=0:N, tmp=Z(1:M-K).*Z(1+K:M); ACF(K+1)=sum(tmp)/M; for L = K:N, BIACF(K+1,L+1) = sum(tmp(1:M-L).*Z(1+L:M))/M; end; end; BIACF=BIACF+BIACF'-diag(diag(BIACF));