├── tsdata ├── tsdata.c └── tsdata.h ├── doc └── minunit_prez.pdf ├── src ├── waveletarima.h ├── autoutils.h ├── pred.h ├── neldermead.h ├── brent.h ├── boxcox.h ├── errors.h ├── waveletarima.c ├── polyroot.h ├── real.h ├── conjgrad.h ├── lls.h ├── unitroot.h ├── erfunc.h ├── filter.h ├── wavefilt.h ├── spectrum.h ├── initest.h ├── boxjenkins.h ├── stats.h ├── seastest.h ├── hsfft.h ├── pdist.h ├── conv.h ├── nls.h ├── secant.h ├── dist.h ├── stl.h ├── CMakeLists.txt ├── wtmath.h ├── talg.h ├── optimc.h ├── regression.h ├── real.c ├── newtonmin.h ├── errors.c ├── lnsrchmp.h ├── boxcox.c ├── pred.c ├── stats.c ├── brent.c ├── wavelib.h ├── matrix.h ├── conv.c ├── filter.c └── spectrum.c ├── data ├── usdaily.txt ├── itdaily.txt ├── seriesF.txt ├── dowjones.dat ├── ohlc_min.csv ├── lynx.txt ├── huron.dat ├── seriesG.txt ├── wineind.txt ├── seriesA.txt ├── e1m.dat ├── seriesC.txt ├── signal.txt ├── seriesD.txt ├── e6.dat ├── seriesB.txt ├── eusm.txt ├── e6m.dat ├── islm.dat └── us-daily.csv ├── .gitignore ├── test ├── csv_in.h ├── auxlib.h ├── artest.c ├── pacftest.c ├── acftest.c ├── arimatest.c ├── dwttests.c ├── autoarimatest1.c ├── autoarimatest2.c ├── sarimatest3.c ├── sarimatest.c ├── artest2.c ├── sarimatest2.c ├── CMakeLists.txt ├── autoarimatest3.c ├── sarimaxtest1.c ├── sarimaxtest2.c ├── sarimaxtest3.c ├── sarimatest_mu.c ├── csv_in.c └── auxlib.c ├── components_copyright ├── MIT_COPYRIGHT ├── BSD-3-CLAUSE └── CC0 ├── COPYRIGHT ├── CMakeLists.txt └── README.md /tsdata/tsdata.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | -------------------------------------------------------------------------------- /doc/minunit_prez.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafat/ctsa/HEAD/doc/minunit_prez.pdf -------------------------------------------------------------------------------- /src/waveletarima.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef WAVELETARIMA_H_ 3 | #define WAVELETARIMA_H_ 4 | #include "wavelib.h" 5 | #include "ctsa.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | 12 | 13 | #ifdef __cplusplus 14 | } 15 | #endif 16 | 17 | 18 | #endif /* WAVELETARIMA_H_ */ -------------------------------------------------------------------------------- /data/usdaily.txt: -------------------------------------------------------------------------------- 1 | 9 2 | 18 3 | 31 4 | 35 5 | 60 6 | 157 7 | 204 8 | 296 9 | 421 10 | 562 11 | 793 12 | 1006 13 | 1260 14 | 1519 15 | 2164 16 | 2672 17 | 3452 18 | 4298 19 | 6031 20 | 8098 21 | 12070 22 | 17422 23 | 23670 24 | 32319 25 | 42595 26 | 52561 27 | 69492 28 | 86793 29 | 105474 30 | 124897 31 | 144391 32 | 165597 33 | 190096 34 | 215381 35 | 243462 36 | 275542 37 | 309097 38 | 335069 39 | 363815 40 | 394230 41 | 424391 42 | 458474 43 | 492678 44 | -------------------------------------------------------------------------------- /src/autoutils.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef AUTOUTILS_H_ 3 | #define AUTOUTILS_H_ 4 | 5 | #include "seastest.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | int ndiffs(double *x, int N,double *alpha, const char *test,const char *type, int *max_d); 12 | 13 | int nsdiffs(double *x, int N,int f,double *alpha, const char *test, int *max_D); 14 | 15 | int is_constant(double *x, int N); 16 | 17 | #ifdef __cplusplus 18 | } 19 | #endif 20 | 21 | 22 | #endif /* AUTOUTILS_H_ */ -------------------------------------------------------------------------------- /src/pred.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * pred.h 4 | * 5 | * Created on: Jul 14, 2014 6 | * Author: Rafat Hussaint 7 | */ 8 | 9 | #ifndef PRED_H_ 10 | #define PRED_H_ 11 | 12 | #include "initest.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | void predictarima(double *zt,int lenzt,int p,int d, int q,double *phi, double *theta, 19 | double constant,int forlength,double *oup); 20 | 21 | #ifdef __cplusplus 22 | } 23 | #endif 24 | 25 | #endif /* PRED_H_ */ 26 | -------------------------------------------------------------------------------- /data/itdaily.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 20 3 | 62 4 | 155 5 | 229 6 | 322 7 | 453 8 | 655 9 | 888 10 | 1128 11 | 1694 12 | 2036 13 | 2502 14 | 3089 15 | 3858 16 | 4636 17 | 5883 18 | 7375 19 | 9172 20 | 10149 21 | 12462 22 | 12462 23 | 17660 24 | 21157 25 | 24747 26 | 27980 27 | 31506 28 | 35713 29 | 41035 30 | 47021 31 | 53578 32 | 59138 33 | 63927 34 | 69176 35 | 74386 36 | 80589 37 | 86498 38 | 92472 39 | 97689 40 | 101739 41 | 105792 42 | 110574 43 | 115242 44 | 119827 45 | 124632 46 | 128948 47 | 132547 48 | 135586 49 | 139422 50 | 143626 51 | 147577 -------------------------------------------------------------------------------- /src/neldermead.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * neldormead.h 4 | * 5 | * Created on: Jan 5, 2014 6 | * Author: HOME 7 | */ 8 | 9 | #ifndef NELDERMEAD_H_ 10 | #define NELDERMEAD_H_ 11 | 12 | #include "brent.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | int nel_min(custom_function *funcpt,double *xc,int N,double *dx,double fsval,int MAXITER,int *niter, 19 | double eps,double *xf); 20 | 21 | #ifdef __cplusplus 22 | } 23 | #endif 24 | 25 | 26 | #endif /* NELDORMEAD_H_ */ 27 | -------------------------------------------------------------------------------- /data/seriesF.txt: -------------------------------------------------------------------------------- 1 | 47 2 | 64 3 | 23 4 | 71 5 | 38 6 | 64 7 | 55 8 | 41 9 | 59 10 | 48 11 | 71 12 | 35 13 | 57 14 | 40 15 | 58 16 | 44 17 | 80 18 | 55 19 | 37 20 | 74 21 | 51 22 | 57 23 | 50 24 | 60 25 | 45 26 | 57 27 | 50 28 | 45 29 | 25 30 | 59 31 | 50 32 | 71 33 | 56 34 | 74 35 | 50 36 | 58 37 | 45 38 | 54 39 | 36 40 | 54 41 | 48 42 | 55 43 | 45 44 | 57 45 | 50 46 | 62 47 | 44 48 | 64 49 | 43 50 | 52 51 | 38 52 | 59 53 | 55 54 | 41 55 | 53 56 | 49 57 | 34 58 | 35 59 | 54 60 | 45 61 | 68 62 | 38 63 | 50 64 | 60 65 | 39 66 | 59 67 | 40 68 | 57 69 | 54 70 | 23 -------------------------------------------------------------------------------- /src/brent.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: LGPL-3.0-or-later 2 | // see https://people.math.sc.edu/Burkardt/f_src/brent/brent.html 3 | #ifndef BRENT_H_ 4 | #define BRENT_H_ 5 | 6 | #include "secant.h" 7 | 8 | 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | 13 | 14 | double brent_zero(custom_funcuni *funcuni,double a, double b, double tol, double eps); 15 | 16 | double brent_local_min(custom_funcuni *funcuni, double a, double b, double t, double eps, double *x); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | #endif /* SECANT_H_ */ 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-3-Clause 2 | #Folders Ignore 3 | .geany/ 4 | Bin/ 5 | .geany/ 6 | 7 | #cmake ignore 8 | CMakeCache.txt 9 | CMakeFiles/ 10 | CMakeScripts/ 11 | Makefile/ 12 | */test/* 13 | !*/test/*.c 14 | cmake_install.cmake 15 | install_manifest.txt 16 | CTestTestfile.cmake 17 | Makefile 18 | 19 | #yml 20 | *.yml 21 | 22 | #Compiled 23 | *.a 24 | *.o 25 | *.lib 26 | *.so 27 | *.exe 28 | *.dll 29 | 30 | #Eclipse 31 | .project 32 | .cproject 33 | language.settings.xml 34 | 35 | #tcl 36 | *.tcl 37 | 38 | #stackdump 39 | *.stackdump 40 | 41 | #vim 42 | *.swp 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/boxcox.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef BOXCOX_H_ 3 | #define BOXCOX_H_ 4 | 5 | #include "optimc.h" 6 | #include "stats.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | int checkConstant(double *x, int N); 13 | 14 | void boxcox_eval(double *x, int N, double lambda,double *bxcx); 15 | 16 | double inv_boxcox_eval(double *x,int N, double lambda,double *bxcx); 17 | 18 | double boxcox_loglik(double lambda, void *params); 19 | 20 | double boxcox(double *x, int N,double *lambda,double *y); 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | 26 | 27 | 28 | #endif /* BOXCOX_H_ */ -------------------------------------------------------------------------------- /tsdata/tsdata.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef TSDATA_H_ 3 | #define TSDATA_H_ 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define BUFFER 16384 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | typedef struct tsdata_set* tsdata_object; 18 | 19 | tsdata_object tsdata_read_file(const char *filepath, const char *delimiter, int index, int header); 20 | 21 | struct tsdata_set { 22 | int N; 23 | double *data; 24 | double *exog; 25 | float params[1]; 26 | }; 27 | 28 | 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | 33 | #endif /* TSDATA_H_ */ 34 | -------------------------------------------------------------------------------- /src/errors.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef ERRORS_H_ 3 | #define ERRORS_H_ 4 | 5 | #include "initest.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | double me(double *predicted, double *actual, int N); 12 | 13 | double mse(double *predicted, double *actual, int N); 14 | 15 | double rmse(double *predicted, double *actual, int N); 16 | 17 | double mae(double *predicted, double *actual, int N); 18 | 19 | double mape(double *predicted, double *actual, int N); 20 | 21 | double mpe(double *predicted, double *actual, int N); 22 | 23 | double mase(double *predicted, double *actual, int N, double *tseries, int length); 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | 29 | 30 | #endif /* ERRORS_H_ */ 31 | -------------------------------------------------------------------------------- /test/csv_in.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * Header for a tiny library that reads CSV files in C 4 | * Created on Mon Dec 30 08:29:10 2024 5 | * 6 | * @author: hilton 7 | * 8 | * OBS C99 at least is recommended 9 | */ 10 | 11 | #if !defined(__CSV_IN_H) 12 | #define __CSV_IN_H 13 | 14 | #include /* possible definition of NAN, if >= C99 */ 15 | 16 | /** 17 | * Will use Not a Number as the huge DBL_MAX if NAN is not defined 18 | **/ 19 | #if ! defined(NAN) 20 | #include /** DBL_MAX */ 21 | #define NAN DBL_MAX 22 | 23 | #endif /* ! defined(NAN) */ 24 | 25 | size_t 26 | read_csv_column(const char* csv_name, const char* col_name, double** column); 27 | 28 | #endif /* !defined(__CSV_IN_H) */ 29 | -------------------------------------------------------------------------------- /src/waveletarima.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include "waveletarima.h" 3 | 4 | void waveletarima(double *x, int N, char *wname, int levels,int p, int q, double *forecasts, int lengthfor) { 5 | wave_object obj; 6 | wt_object wt; 7 | int d, Nused, M, retval, J, i; 8 | double *phi, *theta; 9 | double *inp; 10 | 11 | d = 0; 12 | Nused = N; 13 | M = 1; 14 | retval = 0; 15 | 16 | J = wmaxiter(N); 17 | 18 | obj = wave_init(wname); 19 | wt = wt_init(obj, "modwt", N, J); 20 | 21 | modwt(wt,x); 22 | 23 | for(i = 0; i <= J; ++i) { 24 | inp = &wt->output[i*N]; 25 | 26 | } 27 | 28 | 29 | wave_free(obj); 30 | wt_free(wt); 31 | free(phi); 32 | free(theta); 33 | } -------------------------------------------------------------------------------- /test/auxlib.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /** 3 | * Header for a tiny lib of string functions that C misses 4 | * Created on Sun Dec 29 21:34:24 2024 5 | * 6 | * @author: hilton 7 | * 8 | **/ 9 | 10 | #if !defined(__AUXLIB_H) 11 | #define __AUXLIB_H 12 | 13 | #define NUMERIC_MASK "[+-]?[0-9]\\.?[0-9]*(e[+-]?[0-9]+)?" 14 | 15 | #define ARRAY_SIZE(x) ( sizeof(x) / sizeof(x[0]) ) 16 | 17 | 18 | char* deblank(const char* input); 19 | int is_numeric(const char* input); 20 | size_t split_fields(const char* text, const char* sep, char*** fields); 21 | int free_fields(size_t n_fields, char*** fields); 22 | int find_text(char* const* texts, size_t n_texts, const char* key); 23 | 24 | #endif /* !defined(__AUXLIB_H) */ 25 | -------------------------------------------------------------------------------- /src/polyroot.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * polyroot.h 4 | * 5 | * Created on: August 21, 2014 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef POLYROOT_H_ 10 | #define POLYROOT_H_ 11 | 12 | #include 13 | #include "stats.h" 14 | #include "matrix.h" 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | int cpoly(double *OPR, double *OPI, int DEGREE, double *ZEROR, double *ZEROI); 21 | 22 | int polyroot(double *coeff, int DEGREE, double *ZEROR, double *ZEROI); // Find roots of a real polynomial 23 | 24 | int cpolyroot(double *rcoeff,double *icoeff, int DEGREE, double *ZEROR, double *ZEROI);// Find roots of a complex polynomial 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | 30 | 31 | 32 | #endif /* POLYROOT_H_ */ -------------------------------------------------------------------------------- /src/real.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * real.h 4 | * 5 | * Created on: Apr 20, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef REAL_H_ 10 | #define REAL_H_ 11 | 12 | #include "hsfft.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | typedef struct fft_real_set* fft_real_object; 19 | 20 | fft_real_object fft_real_init(int N, int sgn); 21 | 22 | struct fft_real_set{ 23 | fft_object cobj; 24 | fft_data twiddle2[1]; 25 | }; 26 | 27 | void fft_r2c_exec(fft_real_object obj,fft_type *inp,fft_data *oup); 28 | 29 | void fft_c2r_exec(fft_real_object obj,fft_data *inp,fft_type *oup); 30 | 31 | void free_real_fft(fft_real_object object); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif /* REAL_H_ */ 38 | -------------------------------------------------------------------------------- /data/dowjones.dat: -------------------------------------------------------------------------------- 1 | 110.94 2 | 110.69 3 | 110.43 4 | 110.56 5 | 110.75 6 | 110.84 7 | 110.46 8 | 110.56 9 | 110.46 10 | 110.05 11 | 109.6 12 | 109.31 13 | 109.31 14 | 109.25 15 | 109.02 16 | 108.54 17 | 108.77 18 | 109.02 19 | 109.44 20 | 109.38 21 | 109.53 22 | 109.89 23 | 110.56 24 | 110.56 25 | 110.72 26 | 111.23 27 | 111.48 28 | 111.58 29 | 111.9 30 | 112.19 31 | 112.06 32 | 111.96 33 | 111.68 34 | 111.36 35 | 111.42 36 | 112 37 | 112.22 38 | 112.7 39 | 113.15 40 | 114.36 41 | 114.65 42 | 115.06 43 | 115.86 44 | 116.4 45 | 116.44 46 | 116.88 47 | 118.07 48 | 118.51 49 | 119.28 50 | 119.79 51 | 119.7 52 | 119.28 53 | 119.66 54 | 120.14 55 | 120.97 56 | 121.13 57 | 121.55 58 | 121.96 59 | 122.26 60 | 123.79 61 | 124.11 62 | 124.14 63 | 123.37 64 | 123.02 65 | 122.86 66 | 123.02 67 | 123.11 68 | 123.05 69 | 123.05 70 | 122.83 71 | 123.18 72 | 122.67 73 | 122.73 74 | 122.86 75 | 122.67 76 | 122.09 77 | 122 78 | 121.23 -------------------------------------------------------------------------------- /src/conjgrad.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef CONJGRAD_H_ 3 | #define CONJGRAD_H_ 4 | 5 | #include "newtonmin.h" 6 | 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | int ichol(double *A, int N); 13 | 14 | int stopcheck2(double fx,int N,double *xc,double *xf,double *jac,double *dx,double fsval,double gtol,double stol) ; 15 | 16 | int cgpr_mt(custom_function *funcpt, custom_gradient *funcgrad, double *xi, int N, double *dx,double maxstep, int MAXITER, int *niter, 17 | double eps,double gtol,double ftol,double xtol,double *xf); //Polak Ribiere + (More Thuentes Line Search) 18 | 19 | int conjgrad_min_lin(custom_function *funcpt, custom_gradient *funcgrad, double *xi, int N, double *dx, double maxstep, int MAXITER, int *niter, 20 | double eps,double gtol,double ftol,double xtol,double *xf); 21 | 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | 27 | #endif /* CONJGRAD_H_ */ 28 | -------------------------------------------------------------------------------- /data/ohlc_min.csv: -------------------------------------------------------------------------------- 1 | Timestamp,Open,High,Low,Close,Volume 2 | 1730220600000,72746.09000000,72779.25000000,72718.00000000,72756.30000000,29.59009000 3 | 1730220660000,72756.29000000,72830.00000000,72746.17000000,72797.55000000,40.17450000 4 | 1730220720000,72797.54000000,72850.00000000,72796.00000000,72836.00000000,50.33458000 5 | 1730220780000,72835.99000000,72923.74000000,72830.01000000,72907.87000000,146.46485000 6 | 1730220840000,72907.87000000,72907.87000000,72750.00000000,72778.00000000,110.22572000 7 | 1730220900000,72777.99000000,72794.00000000,72754.13000000,72789.32000000,43.99987000 8 | 1730220960000,72789.31000000,72800.00000000,72750.01000000,72760.44000000,29.90988000 9 | 1730221020000,72760.44000000,72797.96000000,72756.33000000,72792.07000000,46.53466000 10 | 1730221080000,72792.08000000,72792.08000000,72664.00000000,72680.01000000,48.78564000 11 | 1730221140000,72680.01000000,72739.44000000,72670.00000000,72739.44000000,14.03746000 12 | -------------------------------------------------------------------------------- /src/lls.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * lls.h 4 | * 5 | * Created on: Apr 14, 2014 6 | * Author: HOME 7 | */ 8 | 9 | #ifndef LLS_H_ 10 | #define LLS_H_ 11 | 12 | #include "neldermead.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | int lls_normal(double *A,double *b,int M,int N,double *x); 19 | 20 | int lls_qr(double *A,double *b,int M,int N,double *xo); 21 | 22 | void bidiag(double *A, int M, int N); 23 | 24 | void bidiag_orth(double *A, int M, int N,double *U,double *V); 25 | 26 | int svd_gr(double *A,int M,int N,double *U,double *V,double *q); 27 | 28 | int svd_gr2(double *A,int M,int N,double *U,double *V,double *q); 29 | 30 | int minfit(double *AB,int M,int N,int P,double *q); 31 | 32 | int lls_svd(double *Ai,double *bi,int M,int N,double *xo); 33 | 34 | int lls_svd2(double *Ai,double *bi,int M,int N,double *xo); 35 | 36 | #ifdef __cplusplus 37 | } 38 | #endif 39 | 40 | #endif /* LLS_H_ */ 41 | -------------------------------------------------------------------------------- /src/unitroot.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef UNITROOT_H_ 3 | #define UNITROOT_H_ 4 | 5 | #include "errors.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | void ur_df(double *y, int N,const char* alternative, int *klag, double *statistic,double *pval); 12 | 13 | void ur_kpss(double *y, int N,const char* type,int lshort, int *klag, double *statistic,double *pval); 14 | 15 | void ur_pp(double *y, int N,const char* alternative,const char* type,int lshort, int *klag, double *statistic,double *pval); 16 | 17 | void ur_pp2(double *x, int N,const char* type,const char* model,int lshort, int *klag,double *cval,double *cprobs, double *auxstat,int *laux,double *teststat); 18 | 19 | void ur_df2(double *y, int N,const char* type, int *lags,const char *selectlags,double *cval,int *cvrows, int *cvcols, double *cprobs, double *teststat,int *ltstat); 20 | 21 | #ifdef __cplusplus 22 | } 23 | #endif 24 | 25 | 26 | #endif /* UNITROOT_H_ */ -------------------------------------------------------------------------------- /src/erfunc.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * erfunc.h 4 | * 5 | * Created on: Jun 16, 2013 6 | * Author: USER 7 | */ 8 | 9 | #ifndef ERFUNC_H_ 10 | #define ERFUNC_H_ 11 | 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #define PIVAL 3.14159265358979323846264338327950288 22 | 23 | #define XINFVAL 1.79e+308 24 | 25 | #define XNINFVAL 2.2251e-308 26 | 27 | #ifndef DBL_MAX_EXP 28 | #define DBL_MAX_EXP +1024 29 | #endif 30 | 31 | #ifndef DBL_MIN_EXP 32 | #define DBL_MIN_EXP -1021 33 | #endif 34 | 35 | double erf__(double x); 36 | 37 | double erfc1__(int ind, double x); 38 | 39 | double erf(double x); 40 | 41 | double erfc(double x); 42 | 43 | double erfcx(double x); 44 | 45 | double erfinv(double x); 46 | 47 | double erfcinv(double x); 48 | 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | 55 | 56 | #endif /* ERFUNC_H_ */ 57 | -------------------------------------------------------------------------------- /src/filter.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * filter.h 4 | * 5 | * Created on: May 26, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef FILTER_H_ 10 | #define FILTER_H_ 11 | 12 | #include "conv.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | double norm2(double *array, int N); 19 | 20 | void conv(double *sig, int len_sig, double *filt, int len_filt, double *oup); 21 | 22 | void convx(double *sig, int len_sig, double *filt, int len_filt, double *oup); 23 | 24 | void filter(double *sig, int len_sig, double *filt, int len_filt, double *oup); 25 | 26 | void mafilter(double *sig,int N, int window,double *oup); 27 | 28 | void mafilter2(double *sig,int N, int window,double *oup); 29 | 30 | void expfilter(double *sig,int N, double alpha,double *oup); 31 | 32 | void mafilter_wt(double *sig,int N, double *weights, int window,double *oup); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif /* FILTER_H_ */ 39 | -------------------------------------------------------------------------------- /src/wavefilt.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | Copyright (c) 2014, Rafat Hussain 4 | Copyright (c) 2016, Holger Nahrstaedt 5 | */ 6 | #ifndef WAVEFILT_H_ 7 | #define WAVEFILT_H_ 8 | 9 | #include 10 | #include "conv.h" 11 | #define _USE_MATH_DEFINES 12 | #include "math.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | /** To deal with non-standard GCC extensions */ 19 | #if !defined(M_SQRT2) 20 | #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ 21 | 22 | #endif /* !defined(M_SQRT2) */ 23 | 24 | int filtlength(const char* name); 25 | 26 | int filtcoef(const char* name, double *lp1, double *hp1, double *lp2, double *hp2); 27 | 28 | void copy_reverse(const double *in, int N, double *out); 29 | void qmf_even(const double *in, int N, double *out); 30 | void qmf_wrev(const double *in, int N, double *out); 31 | void copy(const double *in, int N, double *out); 32 | #ifdef __cplusplus 33 | } 34 | #endif 35 | 36 | 37 | #endif /* WAVEFILT_H_ */ 38 | -------------------------------------------------------------------------------- /src/spectrum.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * spectrum.h 4 | * 5 | * Created on: May 18, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef SPECTRUM_H_ 10 | #define SPECTRUM_H_ 11 | 12 | #include "stats.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | typedef struct spectrum_set* spectrum_fft_object; 19 | 20 | spectrum_fft_object spectrum_fft_init(int N,int Nfft); 21 | 22 | struct spectrum_set{ 23 | fft_object fobj; 24 | fft_object fobj2; 25 | int len; 26 | int lenfft; 27 | }; 28 | 29 | int isOdd(int N); 30 | 31 | void spectrum_shift(double* spec,int N); 32 | 33 | void periodogram(spectrum_fft_object obj,double* vec,double* spec, double *freq, int side); 34 | 35 | void psd(spectrum_fft_object obj,double* vec,double* spec, double *freq, int side); 36 | 37 | void psd_autocovar(auto_fft_object obj,double *vec,double* spec, double *freq,int Nfft, int side); 38 | 39 | void free_spectrum(spectrum_fft_object object); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* SPECTRUM_H_ */ 46 | -------------------------------------------------------------------------------- /components_copyright/MIT_COPYRIGHT: -------------------------------------------------------------------------------- 1 | 2 | 3 | Copyright 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /src/initest.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * initest.h 4 | * 5 | * Created on: Jul 13, 2014 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef INITEST_H_ 10 | #define INITEST_H_ 11 | 12 | #include "boxjenkins.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | void innalg(double *vec, int l, int N,double *v, double *theta); 19 | 20 | void ma_inn(double *inp, int N,int q, double* theta, int lag); 21 | 22 | void burgalg(double *x, int N, int p, double *phi,double *var); 23 | 24 | void ywalg(double *x, int N, int p, double *phi); 25 | 26 | void ywalg2(double *x, int N, int p, double *phi, double *var); 27 | 28 | void hralg(double *x, int N, int p,int q, double *phi,double *theta, double *var); 29 | 30 | void hrstep2(double *inp, int N, int m, int p, int q, int pq, double *a, double *sos, double *var); 31 | 32 | void pacf_burg(double* vec, int N, double* par, int M); 33 | 34 | void pacf_yw(double* vec, int N, double* par, int M); 35 | 36 | void pacf_mle(double* vec, int N, double* par, int M); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | 43 | #endif /* INITEST_H_ */ 44 | -------------------------------------------------------------------------------- /data/lynx.txt: -------------------------------------------------------------------------------- 1 | 269 2 | 321 3 | 585 4 | 871 5 | 1475 6 | 2821 7 | 3928 8 | 5943 9 | 4950 10 | 2577 11 | 523 12 | 98 13 | 184 14 | 279 15 | 409 16 | 2285 17 | 2685 18 | 3409 19 | 1824 20 | 409 21 | 151 22 | 45 23 | 68 24 | 213 25 | 546 26 | 1033 27 | 2129 28 | 2536 29 | 957 30 | 361 31 | 377 32 | 225 33 | 360 34 | 731 35 | 1638 36 | 2725 37 | 2871 38 | 2119 39 | 684 40 | 299 41 | 236 42 | 245 43 | 552 44 | 1623 45 | 3311 46 | 6721 47 | 4254 48 | 687 49 | 255 50 | 473 51 | 358 52 | 784 53 | 1594 54 | 1676 55 | 2251 56 | 1426 57 | 756 58 | 299 59 | 201 60 | 229 61 | 469 62 | 736 63 | 2042 64 | 2811 65 | 4431 66 | 2511 67 | 389 68 | 73 69 | 39 70 | 49 71 | 59 72 | 188 73 | 377 74 | 1292 75 | 4031 76 | 3495 77 | 587 78 | 105 79 | 153 80 | 387 81 | 758 82 | 1307 83 | 3465 84 | 6991 85 | 6313 86 | 3794 87 | 1836 88 | 345 89 | 382 90 | 808 91 | 1388 92 | 2713 93 | 3800 94 | 3091 95 | 2985 96 | 3790 97 | 674 98 | 81 99 | 80 100 | 108 101 | 229 102 | 399 103 | 1132 104 | 2432 105 | 3574 106 | 2935 107 | 1537 108 | 529 109 | 485 110 | 662 111 | 1000 112 | 1590 113 | 2657 114 | 3396 -------------------------------------------------------------------------------- /src/boxjenkins.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * boxjenkins.h 4 | * 5 | * Created on: Jun 29, 2014 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef BOXJENKINS_H_ 10 | #define BOXJENKINS_H_ 11 | 12 | #include "talg.h" 13 | #include "optimc.h" 14 | 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | void USPE(double *inp,int N,int p, int q, double *phi,double *theta,double *thetac,double *var); 21 | 22 | void USPE_seasonal(double *inp,int N,int s,int ps, int qs, double *phis,double *thetas); 23 | 24 | void avaluem(double *w,int N,int p, int q, double *phi,double *theta,int tval,double *a) ; 25 | 26 | int nlalsm(double *vec, int N, int p, int delta, int q, double *phi, double *theta, 27 | int M, double *thetac, double *var, double eps, double *varcovar, double *residuals); 28 | 29 | int nlalsms(double *vec,int N,int p,int delta, int q, double *phi,double *theta, 30 | int s,int lps,int deltas,int lqs,double *phis,double *thetas, 31 | int M,double *thetac,double *var,double eps,double *varcovar,double *residuals); 32 | 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | 39 | #endif /* BOXJENKINS_H_ */ 40 | -------------------------------------------------------------------------------- /data/huron.dat: -------------------------------------------------------------------------------- 1 | 10.38 2 | 11.86 3 | 10.97 4 | 10.80 5 | 9.79 6 | 10.39 7 | 10.42 8 | 10.82 9 | 11.40 10 | 11.32 11 | 11.44 12 | 11.68 13 | 11.17 14 | 10.53 15 | 10.01 16 | 9.91 17 | 9.14 18 | 9.16 19 | 9.55 20 | 9.67 21 | 8.44 22 | 8.24 23 | 9.10 24 | 9.09 25 | 9.35 26 | 8.82 27 | 9.32 28 | 9.01 29 | 9.00 30 | 9.90 31 | 9.83 32 | 9.72 33 | 9.89 34 | 10.01 35 | 9.37 36 | 8.69 37 | 8.19 38 | 8.67 39 | 9.55 40 | 8.92 41 | 8.09 42 | 9.37 43 | 10.13 44 | 10.14 45 | 9.51 46 | 9.24 47 | 8.66 48 | 8.86 49 | 8.05 50 | 7.79 51 | 6.75 52 | 6.75 53 | 7.82 54 | 8.64 55 | 10.58 56 | 9.48 57 | 7.38 58 | 6.90 59 | 6.94 60 | 6.24 61 | 6.84 62 | 6.85 63 | 6.90 64 | 7.79 65 | 8.18 66 | 7.51 67 | 7.23 68 | 8.42 69 | 9.61 70 | 9.05 71 | 9.26 72 | 9.22 73 | 9.38 74 | 9.10 75 | 7.95 76 | 8.12 77 | 9.75 78 | 10.85 79 | 10.41 80 | 9.96 81 | 9.61 82 | 8.76 83 | 8.18 84 | 7.21 85 | 7.13 86 | 9.10 87 | 8.25 88 | 7.91 89 | 6.89 90 | 5.96 91 | 6.80 92 | 7.68 93 | 8.38 94 | 8.52 95 | 9.74 96 | 9.31 97 | 9.89 98 | 9.96 99 | -------------------------------------------------------------------------------- /src/stats.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * stats.h 4 | * 5 | * Created on: May 13, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef STATS_H_ 10 | #define STATS_H_ 11 | 12 | #include "conv.h" 13 | #include "pdist.h" 14 | #include "lls.h" 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | typedef struct auto_set* auto_fft_object; 21 | 22 | auto_fft_object auto_fft_init(int N); 23 | 24 | struct auto_set{ 25 | fft_real_object fobj; 26 | fft_real_object iobj; 27 | int ilen;// Length if Input 28 | int clen;// Length of FFT 29 | }; 30 | 31 | double mean(double* vec, int N); 32 | 33 | double var(double* vec, int N); 34 | 35 | // N length of the time series 36 | // M Length of the covariance/correlation vector. The maximum lag is M-1 or N-1 , whichever is smaller. 37 | 38 | void autocovar(double* vec,int N,double* acov, int M); 39 | 40 | void autocorr(double* vec,int N,double* acorr, int M); 41 | 42 | void autocovar_fft(auto_fft_object obj,double* vec,double* acov, int M); 43 | 44 | void autocorr_fft(auto_fft_object obj,double* vec,double* acorr, int M); 45 | 46 | void free_auto(auto_fft_object object); 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | 52 | 53 | 54 | #endif /* STATS_H_ */ 55 | -------------------------------------------------------------------------------- /src/seastest.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef SEASTEST_H_ 3 | #define SEASTEST_H_ 4 | 5 | #include "unitroot.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | void decompose(double *x,int N, int f,double *filter, const char *type, double *trend, int *ltrend, double *seas, int *lseas, double *random, int *lrandom); 12 | 13 | double* genLags(double *y, int N, int maxLags, int *rows, int *cols); 14 | 15 | reg_object fitOCSB(double *x, int N, int f, int lag, int mlags); 16 | 17 | void OCSBtest(double *x, int N, int f, int mlags, const char *method,double *statistics,double *critical); 18 | 19 | void stl(double *x,int N,int f, const char *s_window_type,int *s_window, int *s_degree, int *t_window, int *t_degree,int *l_window,int *l_degree, 20 | int *s_jump, int *t_jump, int *l_jump, int *robust,int *inner, int *outer,double *seasonal,double *trend, double *remainder); 21 | 22 | void modstl(double *x, int N, int f, int *s_window,double *lambda, double *seasonal, double *trend,double *remainder); 23 | 24 | void mstl(double *x, int N, int *f, int *Nseas, int *s_window,double *lambda,int *iterate, double **seasonal, double *trend,double *remainder); 25 | 26 | void SHtest(double *x, int N, int *f, int Nseas, double *season); 27 | 28 | double* seasdummy(double *x, int N,int f,int *rows, int *cols); 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | 34 | 35 | #endif /* SEASTEST_H_ */ -------------------------------------------------------------------------------- /src/hsfft.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * hsfft.h 4 | * 5 | * Created on: Apr 14, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef HSFFT_H_ 10 | #define HSFFT_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #define PI2 6.28318530717958647692528676655900577 22 | 23 | #ifndef fft_type 24 | #define fft_type double 25 | #endif 26 | 27 | 28 | typedef struct fft_t { 29 | fft_type re; 30 | fft_type im; 31 | } fft_data; 32 | /* 33 | #define SADD(a,b) ((a)+(b)) 34 | 35 | #define SSUB(a,b) ((a)+(b)) 36 | 37 | #define SMUL(a,b) ((a)*(b)) 38 | */ 39 | 40 | typedef struct fft_set* fft_object; 41 | 42 | fft_object fft_init(int N, int sgn); 43 | 44 | struct fft_set{ 45 | int N; 46 | int sgn; 47 | int factors[64]; 48 | int lf; 49 | int lt; 50 | fft_data twiddle[1]; 51 | }; 52 | 53 | void fft_exec(fft_object obj,fft_data *inp,fft_data *oup); 54 | 55 | int divideby(int M,int d); 56 | 57 | int dividebyN(int N); 58 | 59 | //void arrrev(int M, int* arr); 60 | 61 | int factors(int M, int* arr); 62 | 63 | void twiddle(fft_data *sig,int N, int radix); 64 | 65 | void longvectorN(fft_data *sig,int N, int *array, int M); 66 | 67 | void free_fft(fft_object object); 68 | 69 | #ifdef __cplusplus 70 | } 71 | #endif 72 | 73 | 74 | 75 | 76 | #endif /* HSFFT_H_ */ 77 | -------------------------------------------------------------------------------- /src/pdist.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * pdist.h 4 | * 5 | * Created on: Jun 19, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef PDIST_H_ 10 | #define PDIST_H_ 11 | 12 | #include "dist.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | // Probability Distributions (pdf,cdf and inverse cdf) 19 | 20 | // 1. Normal Distribution 21 | 22 | double normalpdf(double x, double mu, double sigma); 23 | 24 | double normalcdf(double x, double mu, double sigma); 25 | 26 | double normalinv(double p, double mu, double sigma); 27 | 28 | // 2. Student's T Distribution 29 | 30 | double tpdf(double t, int df); 31 | 32 | double tcdf(double t, int df); 33 | 34 | double tinv_appx(double p, int df); 35 | 36 | double tinv(double p, int df); 37 | 38 | // 3. F Distribution 39 | 40 | double fpdf(double x, int k1,int k2); 41 | 42 | double fcdf(double x, int k1,int k2); 43 | 44 | double finv(double p, int k1,int k2); 45 | 46 | // 4. Gamma Distribution 47 | 48 | double gammapdf(double x, double k, double th); 49 | 50 | double gammacdf(double x, double k, double th); 51 | 52 | double gammainv(double p, double k, double th); 53 | 54 | // 5. Chi-squared Distribution 55 | 56 | double chipdf(double x, int df); 57 | 58 | double chicdf(double x, int df); 59 | 60 | double chiinv(double p, int df); 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | 67 | #endif /* PDIST_H_ */ 68 | -------------------------------------------------------------------------------- /src/conv.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * conv.h 4 | * 5 | * Created on: May 1, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef CONV_H_ 10 | #define CONV_H_ 11 | 12 | #include "real.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | #define MIN(a,b) (((a)<(b))?(a):(b)) 19 | #define MAX(a,b) (((a)>(b))?(a):(b)) 20 | 21 | typedef struct conv_set* conv_object; 22 | 23 | conv_object conv_init(int N, int L); 24 | 25 | struct conv_set{ 26 | fft_real_object fobj; 27 | fft_real_object iobj; 28 | int ilen1; 29 | int ilen2; 30 | int clen; 31 | }; 32 | 33 | int factorf(int M); 34 | 35 | int findnext(int M); 36 | 37 | int findnexte(int M); 38 | 39 | void conv_direct(fft_type *inp1,int N, fft_type *inp2, int L,fft_type *oup); 40 | 41 | void conv_directx(fft_type *inp1,int N, fft_type *inp2, int L,fft_type *oup); 42 | 43 | //void conv_fft(const conv_object obj,fft_type *inp1,fft_type *inp2,fft_type *oup); 44 | 45 | //void conv_fft(const conv_object obj,fft_type *inp1,fft_type *inp2,fft_type *oup); 46 | 47 | int convolve(const char *type, const char *method, fft_type *inp1,int N, fft_type *inp2, int L,fft_type *oup); 48 | 49 | void conv_fft(const conv_object obj,fft_type *inp1,fft_type *inp2,fft_type *oup); 50 | 51 | //void free_conv(conv_object object); 52 | 53 | void free_conv(conv_object object); 54 | 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif /* CONV_H_ */ 61 | -------------------------------------------------------------------------------- /src/nls.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * nls.h 4 | * 5 | * Created on: May 21, 2014 6 | * Author: HOME 7 | */ 8 | 9 | #ifndef NLS_H_ 10 | #define NLS_H_ 11 | 12 | #include "lls.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | double enorm(double *x, int N); 19 | 20 | void qrfac(double *A, int M, int N, int lda, int pivot, int *ipvt, int lipvt,double *rdiag, double *acnorm,double eps); 21 | 22 | void qrsolv(double *r,int ldr,int N,int *ipvt,double *diag,double *qtb,double *x,double *sdiag); 23 | 24 | void fdjac2(custom_funcmult *funcmult, double *x, int M, int N, double *fvec, double *fjac, int ldfjac, 25 | double epsfcn,double eps); 26 | 27 | void lmpar(double *r,int ldr,int N,int *ipvt,double *diag,double *qtb,double delta,double *par,double *x,double *sdiag); 28 | 29 | int lmder(custom_funcmult *funcmult,custom_jacobian *jacobian,double *xi,int M, int N, 30 | double *fvec,double *fjac,int ldfjac,int maxfev,double *diag,int mode,double factor,int nprint, 31 | double eps,double ftol,double gtol,double xtol,int *nfev,int *njev,int *ipvt, double *qtf); 32 | 33 | int lmdif(custom_funcmult *funcmult, double *x, int M, int N, double *fvec, double *fjac, int ldfjac, 34 | int maxfev,double *diag,int mode,double factor,int nprint,double eps,double epsfcn,double ftol,double gtol, 35 | double xtol,int *nfev,int *njev,int *ipvt, double *qtf); 36 | 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | 43 | #endif /* NLS_H_ */ 44 | -------------------------------------------------------------------------------- /COPYRIGHT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Rafat Hussain 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 9 | in the documentation and/or other materials provided with the distribution. 10 | 11 | 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 14 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 15 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 16 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 17 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 18 | OF SUCH DAMAGE. -------------------------------------------------------------------------------- /data/seriesG.txt: -------------------------------------------------------------------------------- 1 | 112 2 | 118 3 | 132 4 | 129 5 | 121 6 | 135 7 | 148 8 | 148 9 | 136 10 | 119 11 | 104 12 | 118 13 | 115 14 | 126 15 | 141 16 | 135 17 | 125.027 18 | 149.064 19 | 169.898 20 | 169.654 21 | 158.608 22 | 132.749 23 | 114 24 | 140 25 | 145 26 | 150 27 | 178 28 | 163 29 | 172 30 | 178 31 | 199 32 | 199 33 | 184 34 | 162 35 | 146 36 | 166 37 | 171 38 | 180 39 | 193 40 | 181 41 | 183 42 | 218 43 | 230 44 | 242 45 | 209 46 | 191 47 | 172 48 | 194 49 | 196 50 | 196 51 | 236 52 | 235 53 | 229 54 | 243 55 | 263.993 56 | 271.984 57 | 236.975 58 | 210.964 59 | 180.005 60 | 201.067 61 | 204.125 62 | 188.196 63 | 234.962 64 | 226.602 65 | 233.832 66 | 264.221 67 | 302.125 68 | 292.959 69 | 258.984 70 | 229.007 71 | 203 72 | 229 73 | 242 74 | 233 75 | 267 76 | 269 77 | 270 78 | 315 79 | 364 80 | 347 81 | 312 82 | 274 83 | 237 84 | 278 85 | 284 86 | 277 87 | 317 88 | 313 89 | 318 90 | 374 91 | 413 92 | 405 93 | 355 94 | 306 95 | 271 96 | 306 97 | 315 98 | 301 99 | 356 100 | 348 101 | 355 102 | 422 103 | 465 104 | 467 105 | 404 106 | 347 107 | 305 108 | 336 109 | 340 110 | 318 111 | 362 112 | 348 113 | 363 114 | 435 115 | 491 116 | 505 117 | 404 118 | 359 119 | 310 120 | 337 121 | 360 122 | 342 123 | 406 124 | 396 125 | 420 126 | 472 127 | 548 128 | 559 129 | 463 130 | 407 131 | 362 132 | 405 133 | 417 134 | 391 135 | 419 136 | 461 137 | 472 138 | 535 139 | 622 140 | 606 141 | 508 142 | 461 143 | 390 144 | 432 145 | -------------------------------------------------------------------------------- /components_copyright/BSD-3-CLAUSE: -------------------------------------------------------------------------------- 1 | Copyright 2 | 3 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 | 5 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 | 7 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | 9 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | -------------------------------------------------------------------------------- /src/secant.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef SECANT_H_ 3 | #define SECANT_H_ 4 | 5 | #include "conjgrad.h" 6 | 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | void bfgs_naive(double *H,int N,double eps,double *xi,double *xf,double *jac,double *jacf); 13 | 14 | int bfgs_min_naive(custom_function *funcpt, custom_gradient *funcgrad, double *xi, int N, double *dx, double fsval,double maxstep, int MAXITER, 15 | double eps, double *xf); 16 | 17 | void bfgs_factored(double *H,int N,double eps,double *xi,double *xf,double *jac,double *jacf); 18 | 19 | int bfgs_min(custom_function *funcpt, custom_gradient *funcgrad, double *xi, int N, double *dx, double fsval, double maxstep, int MAXITER, int *niter, 20 | double eps,double gtol,double stol,double *xf); 21 | 22 | int bfgs_min2(custom_function *funcpt, custom_gradient *funcgrad, double *xi, int N, int m, double *dx, double fsval, double maxstep, int MAXITER, int *niter, 23 | double eps, double gtol, double ftol, double xtol, double *xf); 24 | 25 | void inithess_l(double *H, int N, int k, double *tsk, double *tyk, double *dx); 26 | 27 | void bfgs_rec(double *H, int N, int iter, int m, double *jac, double *sk, double *yk, double *r); 28 | 29 | int bfgs_l_min(custom_function *funcpt, custom_gradient *funcgrad, double *xi, int N, int m, double *dx, double fsval,double maxstep, int MAXITER, int *niter, 30 | double eps,double gtol,double ftol,double xtol,double *xf); 31 | 32 | #ifdef __cplusplus 33 | } 34 | #endif 35 | 36 | #endif /* SECANT_H_ */ 37 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-3-Clause 2 | cmake_minimum_required(VERSION 2.8.0 FATAL_ERROR) 3 | 4 | set(PROJECT_NAME ctsa) 5 | project(${PROJECT_NAME} CXX C) 6 | 7 | SET(CMAKE_C_FLAGS "-W -Wall -Wextra -O0") 8 | 9 | # src root path 10 | set(CTSA_SRC_ROOT ${PROJECT_SOURCE_DIR} CACHE PATH "ctsa source root") 11 | # binary output by default 12 | set(COMMON_BIN_PATH ${CMAKE_BINARY_DIR}/Bin) 13 | set(LIBRARY_OUTPUT_PATH ${COMMON_BIN_PATH}/${CMAKE_BUILD_TYPE}) 14 | set(EXECUTABLE_OUTPUT_PATH ${COMMON_BIN_PATH}/${CMAKE_BUILD_TYPE}) 15 | # set where to find additional cmake modules if any 16 | set(CMAKE_MODULE_PATH ${CTSA_SRC_ROOT}/cmake ${CMAKE_MODULE_PATH}) 17 | 18 | 19 | set(CTSA_VERSION "0.1.0" CACHE STRING "ctsa version" FORCE) 20 | message(">>> Building ctsa version: ${CTSA_VERSION}") 21 | message(">>> EXECUTABLE_OUTPUT_PATH = ${EXECUTABLE_OUTPUT_PATH}") 22 | 23 | 24 | # cleanup prefix lib for Unix-like OSes 25 | set(CMAKE_SHARED_MODULE_PREFIX) 26 | 27 | # install target to this folder by default 28 | set(CTSALIB_BINARY_DIR ${CTSA_SRC_ROOT}/bin) 29 | if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) 30 | set(CMAKE_INSTALL_PREFIX "${CTSA_BINARY_DIR}" CACHE PATH "default install path" FORCE) 31 | endif() 32 | 33 | # make include globaly visible 34 | set(PROJECT_WIDE_INCLUDE ${CTSA_SRC_ROOT}/include) 35 | include_directories(${PROJECT_WIDE_INCLUDE}) 36 | 37 | 38 | 39 | 40 | include_directories(${COMMON_BIN_PATH}) 41 | 42 | 43 | add_subdirectory(src) 44 | add_subdirectory(test) 45 | 46 | install(DIRECTORY ${CTSA_SRC_ROOT}/include/ DESTINATION include FILES_MATCHING PATTERN "*.h") 47 | -------------------------------------------------------------------------------- /src/dist.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * dist.h 4 | * 5 | * Created on: Jun 11, 2013 6 | * Author: USER 7 | */ 8 | 9 | #ifndef DIST_H_ 10 | #define DIST_H_ 11 | 12 | #include 13 | #include 14 | #include "erfunc.h" 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | 21 | double fix(double x); 22 | 23 | double normf(double x); 24 | 25 | double eps(double x); 26 | 27 | double log1p(double x); 28 | 29 | double recfact(double N); 30 | 31 | double factorial(int N); 32 | 33 | double r8_max ( double x, double y ); 34 | 35 | double gamma(double x); 36 | 37 | double gamma_log(double x); 38 | 39 | double beta(double a, double b); 40 | 41 | double beta_log(double a, double b); 42 | 43 | double pgamma(double x, double a); //Incomplete Gamma (Lower Tail) 44 | 45 | double qgamma(double x, double a); ////Incomplete Gamma (Upper Tail) 46 | 47 | double bfrac(double x,double a, double b, int* ctr); // Continued Fractions Algorithm (ibeta) 48 | 49 | double ibeta_appx(double x, double a , double b); // Incomplete Beta Approximation 50 | 51 | double ibeta(double x, double a , double b); //Incomplete Beta 52 | 53 | double ibetac(double x, double a , double b); //Incomplete Beta Complement 54 | 55 | double ibetad(double x, double a , double b); //Incomplete Beta Derivative 56 | 57 | double betapdf(double x, double a , double b); 58 | 59 | double betacdf(double x, double a , double b); 60 | 61 | double betainv(double alpha, double p, double q); // Beta Inverse 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | 67 | 68 | 69 | #endif /* DIST_H_ */ 70 | -------------------------------------------------------------------------------- /src/stl.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef STL_H_ 3 | #define STL_H_ 4 | 5 | #include "polyroot.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | void stl_(double *y, int *n, int *np, int *ns, int *nt, int *nl, int *isdeg, int *itdeg, int * 12 | ildeg, int *nsjump, int *ntjump, int *nljump, int *ni, int *no, double *rw, double *season, double *trend, 13 | double *work); 14 | 15 | int stless_(double *y, int *n, int *len, int *ideg, int *njump, int *userw, double *rw, double *ys, 16 | double *res); 17 | 18 | int stlest_(double *y, int *n, int *len, int *ideg, double *xs, double *ys, int *nleft, int * 19 | nright, double *w, int *userw, double *rw, int *ok); 20 | 21 | int stlfts_(double *x, int *n, int *np, double *trend, double *work); 22 | 23 | int stlma_(double *x, int *n, int *len, double *ave); 24 | 25 | int stlstp_(double *y, int *n, int *np, int *ns, int *nt, int *nl, int *isdeg, int *itdeg, int 26 | *ildeg, int *nsjump, int *ntjump, int *nljump, int *ni, int *userw, double *rw, double *season, 27 | double *trend, double *work); 28 | 29 | int stlrwt_(double *y, int *n, double *fit, double *rw); 30 | 31 | int stlss_(double *y, int *n, int *np, int *ns, int *isdeg, int *nsjump, int *userw, double *rw, 32 | double *season, double *work1, double *work2, double *work3, double *work4); 33 | 34 | void stlez_(double *y, int *n, int *np, int *ns, int *isdeg, int *itdeg,int *robust, int *no, 35 | double *rw, double *season, double *trend, double *work); 36 | 37 | int psort_(double *a, int n, int *ind, int ni); 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | 43 | 44 | 45 | #endif /* STL_H_ */ -------------------------------------------------------------------------------- /test/artest.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | 7 | int main(void) { 8 | int i, N, L,method; 9 | double *inp; 10 | int p; 11 | double *phi; 12 | double *xpred, *amse; 13 | 14 | ar_object obj; 15 | p = 0; 16 | 17 | 18 | L = 5; 19 | 20 | phi = (double*)malloc(sizeof(double)* p); 21 | 22 | xpred = (double*)malloc(sizeof(double)* L); 23 | amse = (double*)malloc(sizeof(double)* L); 24 | 25 | FILE *ifp; 26 | double temp[2000]; 27 | 28 | ifp = fopen("../data/seriesA.txt", "r"); 29 | i = 0; 30 | if (!ifp) { 31 | printf("Cannot Open File"); 32 | exit(100); 33 | } 34 | while (!feof(ifp)) { 35 | fscanf(ifp, "%lf \n", &temp[i]); 36 | i++; 37 | } 38 | N = i; 39 | 40 | inp = (double*)malloc(sizeof(double)* N); 41 | //wmean = mean(temp, N); 42 | 43 | for (i = 0; i < N; ++i) { 44 | inp[i] = temp[i]; 45 | //printf("%g \n",inp[i]); 46 | } 47 | 48 | method = 0; // method 0 - Yule Walker, Method 1 - Burg, Method 2, MLE (Box-Jenkins) 49 | obj = ar_init(method, N); 50 | ar_exec(obj, inp); 51 | ar_summary(obj); 52 | // Predict the next 5 values using the obtained ARIMA model 53 | ar_predict(obj, inp, L, xpred, amse); 54 | printf("\n"); 55 | printf("Predicted Values : "); 56 | for (i = 0; i < L; ++i) { 57 | printf("%g ", xpred[i]); 58 | } 59 | printf("\n"); 60 | printf("Standard Errors : "); 61 | for (i = 0; i < L; ++i) { 62 | printf("%g ", sqrt(amse[i])); 63 | } 64 | printf("\n"); 65 | 66 | ar_free(obj); 67 | //ar_estimate(inp, N, 2); 68 | free(inp); 69 | free(phi); 70 | free(xpred); 71 | free(amse); 72 | return 0; 73 | 74 | } 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CTSA 2 | 3 | ## A Univariate Time Series Analysis and ARIMA Modeling Package in ANSI C 4 | 5 | CTSA is a C software package for univariate time series analysis. 6 | ARIMA and Seasonal ARIMA models have been added as of 10/30/2014. Other functionality will be added soon 7 | 8 | *07/24/2020 Update : SARIMAX and Auto ARIMA added. Documentation will be added in the coming days. Software is still in beta stage and older ARIMA 9 | and SARIMA functions are now superseded by SARIMAX.* 10 | 11 | ## Dependencies 12 | 13 | Git and CMake 14 | 15 | ## Getting Started 16 | ``` 17 | git clone https://github.com/rafat/ctsa.git 18 | cd ctsa 19 | cmake . 20 | make 21 | ``` 22 | 23 | 24 | |**[Auto ARIMA](https://github.com/rafat/ctsa/wiki/AUTO-ARIMA)**| Auto ARIMA Class + Examples | 25 | |:-----------------------------------------------------|:----------------------------------| 26 | |**[SARIMAX](https://github.com/rafat/ctsa/wiki/SARIMAX/)**| SARIMAX Class + Examples | 27 | |**[ARIMA](https://github.com/rafat/ctsa/wiki/ARIMA)**| ARIMA Class + Example | 28 | |**[Seasonal ARIMA](https://github.com/rafat/ctsa/wiki/SARIMA)**| Seasonal ARIMA Class + Example | 29 | |**[AR](https://github.com/rafat/ctsa/wiki/AR)** | AR Class + Example | 30 | |**[ACF](https://github.com/rafat/ctsa/wiki/ACF)** | Autocovariance, Autocorrelation and Partial Autocorrelation + Examples| 31 | |**[References](https://github.com/rafat/ctsa/wiki/References)**| References (List Being Updated) | 32 | 33 | Wiki is available at 34 | 35 | https://github.com/rafat/ctsa/wiki 36 | 37 | License : BSD 3- Clause Check COPYRIGHT file 38 | 39 | Contact rafat.hsn@gmail.com 40 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-3-Clause 2 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}) 3 | 4 | set(SOURCE_FILES autoutils.c 5 | boxcox.c 6 | boxjenkins.c 7 | brent.c 8 | conjgrad.c 9 | conv.c 10 | ctsa.c 11 | dist.c 12 | emle.c 13 | erfunc.c 14 | filter.c 15 | hsfft.c 16 | initest.c 17 | lls.c 18 | lnsrchmp.c 19 | matrix.c 20 | neldermead.c 21 | newtonmin.c 22 | nls.c 23 | optimc.c 24 | pdist.c 25 | polyroot.c 26 | pred.c 27 | real.c 28 | regression.c 29 | secant.c 30 | spectrum.c 31 | stats.c 32 | stl.c 33 | talg.c 34 | errors.c 35 | unitroot.c 36 | seastest.c 37 | wtmath.c 38 | wavefilt.c 39 | wavelib.c 40 | ) 41 | 42 | set(HEADER_FILES autoutils.h 43 | boxcox.h 44 | boxjenkins.h 45 | brent.h 46 | conjgrad.h 47 | conv.h 48 | ctsa.h 49 | dist.h 50 | emle.h 51 | erfunc.h 52 | filter.h 53 | hsfft.h 54 | initest.h 55 | lls.h 56 | lnsrchmp.h 57 | matrix.h 58 | neldermead.h 59 | newtonmin.h 60 | nls.h 61 | optimc.h 62 | pdist.h 63 | polyroot.h 64 | pred.h 65 | real.h 66 | regression.h 67 | secant.h 68 | spectrum.h 69 | stats.h 70 | stl.h 71 | talg.h 72 | errors.h 73 | unitroot.h 74 | seastest.h 75 | wtmath.h 76 | wavefilt.h 77 | wavelib.h 78 | waveletarima.h 79 | ) 80 | 81 | add_library(ctsalib STATIC ${SOURCE_FILES} ${HEADER_FILES}) 82 | 83 | set_property(TARGET ctsalib PROPERTY FOLDER "lib") 84 | 85 | target_include_directories(ctsalib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) 86 | -------------------------------------------------------------------------------- /test/pacftest.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | 7 | int main(void) { 8 | int i, N, M; 9 | double *inp, *par; 10 | int method; 11 | 12 | FILE *ifp; 13 | double temp[1200]; 14 | 15 | ifp = fopen("../data/seriesC.txt", "r"); 16 | i = 0; 17 | if (!ifp) { 18 | printf("Cannot Open File"); 19 | exit(100); 20 | } 21 | while (!feof(ifp)) { 22 | fscanf(ifp, "%lf \n", &temp[i]); 23 | i++; 24 | } 25 | N = i; 26 | 27 | inp = (double*)malloc(sizeof(double)* N); 28 | //wmean = mean(temp, N); 29 | 30 | for (i = 0; i < N; ++i) { 31 | inp[i] = temp[i]; 32 | //printf("%g \n",inp[i]); 33 | } 34 | 35 | M = 10; 36 | par = (double*)malloc(sizeof(double)* M); 37 | 38 | // Default Method is Yule-Walker 39 | 40 | printf("\n Default Method : pacf \n"); 41 | 42 | pacf(inp, N, par, M); 43 | 44 | for (i = 0; i < M; ++i) { 45 | printf("%g ", par[i]); 46 | } 47 | printf("\n"); 48 | 49 | // pacf_opt : Method 0 Yule Walker 50 | method = 0; 51 | printf("\n pacf_opt Method 0 Yule Walker \n"); 52 | pacf_opt(inp, N, method, par, M); 53 | 54 | for (i = 0; i < M; ++i) { 55 | printf("%g ", par[i]); 56 | } 57 | 58 | printf("\n"); 59 | // pacf_opt : Method 1 Burg 60 | method = 1; 61 | printf("\n pacf_opt Method 1 Burg \n"); 62 | pacf_opt(inp, N, method, par, M); 63 | 64 | for (i = 0; i < M; ++i) { 65 | printf("%g ", par[i]); 66 | } 67 | printf("\n"); 68 | 69 | // pacf_opt : Method 2 MLE (Box-Jenkins) 70 | method = 2; 71 | printf("\n pacf_opt Method 2 MLE (Box-Jenkins) \n"); 72 | pacf_opt(inp, N, method, par, M); 73 | 74 | for (i = 0; i < M; ++i) { 75 | printf("%g ", par[i]); 76 | } 77 | printf("\n"); 78 | 79 | free(inp); 80 | free(par); 81 | return 0; 82 | 83 | } 84 | -------------------------------------------------------------------------------- /test/acftest.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | 7 | int main(void) { 8 | int i, N, M; 9 | double *inp, *acf; 10 | int method; 11 | 12 | /* 13 | acvf and acvf_opt functions will calculate autocovariance from lag 0 to lag M-1 14 | */ 15 | 16 | FILE *ifp; 17 | double temp[1200]; 18 | 19 | ifp = fopen("../data/seriesC.txt", "r"); 20 | i = 0; 21 | if (!ifp) { 22 | printf("Cannot Open File"); 23 | exit(100); 24 | } 25 | while (!feof(ifp)) { 26 | fscanf(ifp, "%lf \n", &temp[i]); 27 | i++; 28 | } 29 | N = i; 30 | 31 | inp = (double*)malloc(sizeof(double)* N); 32 | //wmean = mean(temp, N); 33 | 34 | for (i = 0; i < N; ++i) { 35 | inp[i] = temp[i]; 36 | //printf("%g \n",inp[i]); 37 | } 38 | 39 | M = 10; 40 | acf = (double*)malloc(sizeof(double)* M); 41 | 42 | // Default Method 43 | 44 | printf("\n Default Method : acvf \n"); 45 | 46 | acvf(inp, N, acf, M); 47 | 48 | for (i = 0; i < M; ++i) { 49 | printf("%g ", acf[i]); 50 | } 51 | printf("\n"); 52 | 53 | // acvf_opt : Method 0 General Method 54 | method = 0; 55 | printf("\n acvf_opt Method 0 General Method \n"); 56 | acvf_opt(inp, N, method, acf, M); 57 | 58 | for (i = 0; i < M; ++i) { 59 | printf("%g ", acf[i]); 60 | } 61 | 62 | printf("\n"); 63 | // acvf_opt : Method 1 FFT 64 | method = 1; 65 | printf("\n acvf_opt Method 1 FFT \n"); 66 | acvf_opt(inp, N, method, acf, M); 67 | 68 | for (i = 0; i < M; ++i) { 69 | printf("%g ", acf[i]); 70 | } 71 | printf("\n \n"); 72 | 73 | // Calculate Autocorrelation from already caluclated Covariance function 74 | printf("Autocorrelation \n"); 75 | acvf2acf(acf, M); 76 | 77 | for (i = 0; i < M; ++i) { 78 | printf("%g ", acf[i]); 79 | } 80 | printf("\n"); 81 | 82 | free(inp); 83 | free(acf); 84 | return 0; 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/wtmath.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | Copyright (c) 2014, Rafat Hussain 4 | */ 5 | #ifndef WTMATH_H_ 6 | #define WTMATH_H_ 7 | 8 | #include "wavefilt.h" 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | void dwt_per_stride(double *inp, int N, double *lpd,double*hpd,int lpd_len, 15 | double *cA, int len_cA, double *cD, int istride, int ostride); 16 | 17 | void dwt_sym_stride(double *inp, int N, double *lpd, double*hpd, int lpd_len, 18 | double *cA, int len_cA, double *cD, int istride, int ostride); 19 | 20 | void modwt_per_stride(int M, double *inp, int N, double *filt, int lpd_len, 21 | double *cA, int len_cA, double *cD, int istride, int ostride); 22 | 23 | void swt_per_stride(int M, double *inp, int N, double *lpd, double*hpd, int lpd_len, 24 | double *cA, int len_cA, double *cD, int istride, int ostride); 25 | 26 | void idwt_per_stride(double *cA, int len_cA, double *cD, double *lpr, double *hpr, 27 | int lpr_len, double *X, int istride, int ostride); 28 | 29 | void idwt_sym_stride(double *cA, int len_cA, double *cD, double *lpr, double *hpr, 30 | int lpr_len, double *X, int istride, int ostride); 31 | 32 | void imodwt_per_stride(int M, double *cA, int len_cA, double *cD, double *filt, 33 | int lf,double *X,int istride, int ostride); 34 | 35 | void idwt2_shift(int shift, int rows, int cols, double *lpr, double *hpr, int lf, 36 | double *A,double *H, double *V,double *D, double *oup); 37 | 38 | int upsamp(double *x, int lenx, int M, double *y); 39 | 40 | int upsamp2(double *x, int lenx, int M, double *y); 41 | 42 | int downsamp(double *x, int lenx, int M, double *y); 43 | 44 | int per_ext(double *sig, int len, int a,double *oup); 45 | 46 | int symm_ext(double *sig, int len, int a,double *oup); 47 | 48 | void circshift(double *array, int N, int L); 49 | 50 | int testSWTlength(int N, int J); 51 | 52 | int wmaxiter(int sig_len); 53 | 54 | double costfunc(double *x, int N, char *entropy, double p); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | 61 | #endif /* WAVELIB_H_ */ 62 | -------------------------------------------------------------------------------- /test/arimatest.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | 7 | int main(void) { 8 | int i, N, d, L; 9 | double *inp; 10 | int p, q; 11 | double *phi, *theta; 12 | double *xpred, *amse; 13 | arima_object obj; 14 | p = 0; 15 | d = 1; 16 | q = 0; 17 | 18 | 19 | L = 0; 20 | 21 | phi = (double*)malloc(sizeof(double)* p); 22 | theta = (double*)malloc(sizeof(double)* q); 23 | 24 | xpred = (double*)malloc(sizeof(double)* L); 25 | amse = (double*)malloc(sizeof(double)* L); 26 | 27 | FILE *ifp; 28 | double temp[1200]; 29 | 30 | ifp = fopen("../data/seriesA.txt", "r"); 31 | i = 0; 32 | if (!ifp) { 33 | printf("Cannot Open File"); 34 | exit(100); 35 | } 36 | while (!feof(ifp)) { 37 | fscanf(ifp, "%lf \n", &temp[i]); 38 | i++; 39 | } 40 | N = i; 41 | 42 | inp = (double*)malloc(sizeof(double)* N); 43 | //wmean = mean(temp, N); 44 | 45 | for (i = 0; i < N; ++i) { 46 | inp[i] = temp[i]; 47 | //printf("%g \n",inp[i]); 48 | } 49 | 50 | 51 | obj = arima_init(p, d, q, N); 52 | arima_setMethod(obj, 0); // Method 0 ("MLE") is default so this step is unnecessary. The method also accepts values 1 ("CSS") and 2 ("Box-Jenkins") 53 | //arima_setCSSML(obj,0); // 0 - Only MLE , 1 - CSS + MLE (Default) 54 | arima_setOptMethod(obj,5);// Method 5 ("BFGS") is default. The method also accepts values 0,1,2,3,4,5,6,7. Check the documentation for details. 55 | arima_exec(obj, inp); 56 | arima_summary(obj); 57 | // Predict the next 5 values using the obtained ARIMA model 58 | arima_predict(obj, inp, L, xpred, amse); 59 | printf("\n"); 60 | printf("Predicted Values : "); 61 | for (i = 0; i < L; ++i) { 62 | printf("%g ", xpred[i]); 63 | } 64 | printf("\n"); 65 | printf("Standard Errors : "); 66 | for (i = 0; i < L; ++i) { 67 | printf("%g ", sqrt(amse[i])); 68 | } 69 | printf("\n"); 70 | arima_free(obj); 71 | free(inp); 72 | free(phi); 73 | free(theta); 74 | free(xpred); 75 | free(amse); 76 | return 0; 77 | 78 | } 79 | -------------------------------------------------------------------------------- /data/wineind.txt: -------------------------------------------------------------------------------- 1 | 15136 2 | 16733 3 | 20016 4 | 17708 5 | 18019 6 | 19227 7 | 22893 8 | 23739 9 | 21133 10 | 22591 11 | 26786 12 | 29740 13 | 15028 14 | 17977 15 | 20008 16 | 21354 17 | 19498 18 | 22125 19 | 25817 20 | 28779 21 | 20960 22 | 22254 23 | 27392 24 | 29945 25 | 16933 26 | 17892 27 | 20533 28 | 23569 29 | 22417 30 | 22084 31 | 26580 32 | 27454 33 | 24081 34 | 23451 35 | 28991 36 | 31386 37 | 16896 38 | 20045 39 | 23471 40 | 21747 41 | 25621 42 | 23859 43 | 25500 44 | 30998 45 | 24475 46 | 23145 47 | 29701 48 | 34365 49 | 17556 50 | 22077 51 | 25702 52 | 22214 53 | 26886 54 | 23191 55 | 27831 56 | 35406 57 | 23195 58 | 25110 59 | 30009 60 | 36242 61 | 18450 62 | 21845 63 | 26488 64 | 22394 65 | 28057 66 | 25451 67 | 24872 68 | 33424 69 | 24052 70 | 28449 71 | 33533 72 | 37351 73 | 19969 74 | 21701 75 | 26249 76 | 24493 77 | 24603 78 | 26485 79 | 30723 80 | 34569 81 | 26689 82 | 26157 83 | 32064 84 | 38870 85 | 21337 86 | 19419 87 | 23166 88 | 28286 89 | 24570 90 | 24001 91 | 33151 92 | 24878 93 | 26804 94 | 28967 95 | 33311 96 | 40226 97 | 20504 98 | 23060 99 | 23562 100 | 27562 101 | 23940 102 | 24584 103 | 34303 104 | 25517 105 | 23494 106 | 29095 107 | 32903 108 | 34379 109 | 16991 110 | 21109 111 | 23740 112 | 25552 113 | 21752 114 | 20294 115 | 29009 116 | 25500 117 | 24166 118 | 26960 119 | 31222 120 | 38641 121 | 14672 122 | 17543 123 | 25453 124 | 32683 125 | 22449 126 | 22316 127 | 27595 128 | 25451 129 | 25421 130 | 25288 131 | 32568 132 | 35110 133 | 16052 134 | 22146 135 | 21198 136 | 19543 137 | 22084 138 | 23816 139 | 29961 140 | 26773 141 | 26635 142 | 26972 143 | 30207 144 | 38687 145 | 16974 146 | 21697 147 | 24179 148 | 23757 149 | 25013 150 | 24019 151 | 30345 152 | 24488 153 | 25156 154 | 25650 155 | 30923 156 | 37240 157 | 17466 158 | 19463 159 | 24352 160 | 26805 161 | 25236 162 | 24735 163 | 29356 164 | 31234 165 | 22724 166 | 28496 167 | 32857 168 | 37198 169 | 13652 170 | 22784 171 | 23565 172 | 26323 173 | 23779 174 | 27549 175 | 29660 176 | 23356 -------------------------------------------------------------------------------- /test/dwttests.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "../src/wavelib.h" 7 | 8 | double absmax(double *array, int N) { 9 | double max; 10 | int i; 11 | 12 | max = 0.0; 13 | for (i = 0; i < N; ++i) { 14 | if (fabs(array[i]) >= max) { 15 | max = fabs(array[i]); 16 | } 17 | } 18 | 19 | return max; 20 | } 21 | 22 | int main() { 23 | wave_object obj; 24 | wt_object wt; 25 | double *inp, *out, *diff; 26 | int N, i, J; 27 | 28 | FILE *ifp; 29 | double temp[1200]; 30 | 31 | char *name = "db4"; 32 | obj = wave_init(name); 33 | wave_summary(obj); 34 | 35 | ifp = fopen("../data/signal.txt", "r"); 36 | i = 0; 37 | if (!ifp) { 38 | printf("Cannot Open File"); 39 | exit(100); 40 | } 41 | while (!feof(ifp)) { 42 | fscanf(ifp, "%lf \n", &temp[i]); 43 | i++; 44 | } 45 | N = 177; 46 | 47 | fclose(ifp); 48 | 49 | inp = (double*)malloc(sizeof(double)* N); 50 | out = (double*)malloc(sizeof(double)* N); 51 | diff = (double*)malloc(sizeof(double)* N); 52 | //wmean = mean(temp, N); 53 | 54 | for (i = 0; i < N; ++i) { 55 | inp[i] = temp[i]; 56 | //printf("%g \n",inp[i]); 57 | } 58 | J = 2; 59 | 60 | wt = wt_init(obj, "modwt", N, J);// Initialize the wavelet transform object 61 | 62 | modwt(wt, inp);// Perform MODWT 63 | //MODWT output can be accessed using wt->output vector. Use wt_summary to find out how to extract appx and detail coefficients 64 | 65 | for (i = 0; i < wt->outlength; ++i) { 66 | printf("%g ",wt->output[i]); 67 | } 68 | 69 | imodwt(wt, out);// Perform ISWT (if needed) 70 | // Test Reconstruction 71 | 72 | 73 | for (i = 0; i < wt->siglength; ++i) { 74 | diff[i] = out[i] - inp[i]; 75 | } 76 | 77 | printf("\n MAX %g \n", absmax(diff, wt->siglength));// If Reconstruction succeeded then the output should be a small value. 78 | 79 | wt_summary(wt);// Prints the full summary. 80 | 81 | wave_free(obj); 82 | wt_free(wt); 83 | 84 | free(inp); 85 | free(out); 86 | free(diff); 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /test/autoarimatest1.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | /* 7 | 8 | Auto ARIMA example 1 9 | 10 | */ 11 | 12 | int main(void) { 13 | int i, N, d, D, L; 14 | double *inp; 15 | int p, q, P, Q, s, r; 16 | double *xpred, *amse; 17 | auto_arima_object obj; 18 | /* 19 | Make sure all the parameter values are correct and consistent with other values. eg., if xreg is NULL r should be 0 20 | or if P = D = Q = 0 then make sure that s is also 0. 21 | Recheck the values if the program fails to execute. 22 | */ 23 | p = 5; 24 | d = 2; 25 | q = 5; 26 | P = 2; 27 | D = 1; 28 | Q = 2; 29 | 30 | int order[3] = {p,d,q}; 31 | int seasonal[3] = {P,D,Q}; 32 | 33 | s = 0; 34 | r = 0; 35 | L = 5; 36 | 37 | xpred = (double*)malloc(sizeof(double)* L); 38 | amse = (double*)malloc(sizeof(double)* L); 39 | 40 | FILE *ifp; 41 | double temp[1200]; 42 | double temp1[1200]; 43 | double temp2[1200]; 44 | 45 | ifp = fopen("../data/seriesA.txt", "r"); 46 | i = 0; 47 | if (!ifp) { 48 | printf("Cannot Open File"); 49 | exit(100); 50 | } 51 | while (!feof(ifp)) { 52 | fscanf(ifp, "%lf\n", &temp[i]); 53 | i++; 54 | } 55 | N = i; 56 | 57 | inp = (double*)malloc(sizeof(double)* N); 58 | 59 | 60 | for (i = 0; i < N; ++i) { 61 | inp[i] = temp[i]; 62 | 63 | } 64 | 65 | 66 | obj = auto_arima_init(order,seasonal,s,r,N); 67 | 68 | auto_arima_setApproximation(obj,0); 69 | auto_arima_setStepwise(obj,0); 70 | 71 | auto_arima_exec(obj,inp,NULL); 72 | 73 | auto_arima_summary(obj); 74 | 75 | auto_arima_predict(obj, inp, NULL, L,NULL, xpred, amse); 76 | 77 | printf("\n"); 78 | printf("Predicted Values : "); 79 | for (i = 0; i < L; ++i) { 80 | printf("%g ", xpred[i]); 81 | } 82 | printf("\n"); 83 | printf("Standard Errors : "); 84 | for (i = 0; i < L; ++i) { 85 | printf("%g ", sqrt(amse[i])); 86 | } 87 | printf("\n"); 88 | 89 | auto_arima_free(obj); 90 | 91 | 92 | free(inp); 93 | free(xpred); 94 | free(amse); 95 | fclose(ifp); 96 | 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /test/autoarimatest2.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | /* 7 | 8 | Auto ARIMA example 2 9 | 10 | */ 11 | 12 | int main(void) { 13 | int i, N, d, D, L; 14 | double *inp; 15 | int p, q, P, Q, s, r; 16 | double *xpred, *amse; 17 | auto_arima_object obj; 18 | /* 19 | Make sure all the parameter values are correct and consistent with other values. eg., if xreg is NULL r should be 0 20 | or if P = D = Q = 0 then make sure that s is also 0. 21 | Recheck the values if the program fails to execute. 22 | */ 23 | p = 5; 24 | d = 2; 25 | q = 5; 26 | s = 12; 27 | P = 2; 28 | D = 1; 29 | Q = 2; 30 | r = 0; 31 | int order[3] = {p,d,q}; 32 | int seasonal[3] = {P,D,Q}; 33 | 34 | 35 | L = 0; 36 | 37 | xpred = (double*)malloc(sizeof(double)* L); 38 | amse = (double*)malloc(sizeof(double)* L); 39 | 40 | FILE *ifp; 41 | double temp[1200]; 42 | 43 | ifp = fopen("../data/seriesG.txt", "r"); 44 | i = 0; 45 | if (!ifp) { 46 | printf("Cannot Open File"); 47 | exit(100); 48 | } 49 | while (!feof(ifp)) { 50 | fscanf(ifp, "%lf\n", &temp[i]); 51 | i++; 52 | } 53 | N = i; 54 | 55 | inp = (double*)malloc(sizeof(double)* N); 56 | 57 | for (i = 0; i < N; ++i) { 58 | inp[i] = log(temp[i]); 59 | } 60 | 61 | 62 | obj = auto_arima_init(order,seasonal,s,r,N); 63 | 64 | auto_arima_setApproximation(obj,1); 65 | auto_arima_setStepwise(obj,1); 66 | auto_arima_setVerbose(obj,1); 67 | 68 | auto_arima_exec(obj,inp,NULL); 69 | 70 | auto_arima_summary(obj); 71 | 72 | auto_arima_predict(obj, inp, NULL, L,NULL, xpred, amse); 73 | 74 | printf("\n\n\n"); 75 | printf("Forecast : 5 Point Look Ahead"); 76 | printf("\n"); 77 | printf("Predicted Values : "); 78 | for (i = 0; i < L; ++i) { 79 | printf("%g ", xpred[i]); 80 | } 81 | printf("\n"); 82 | printf("Standard Errors : "); 83 | for (i = 0; i < L; ++i) { 84 | printf("%g ", exp(sqrt(amse[i]))); 85 | } 86 | printf("\n"); 87 | 88 | auto_arima_free(obj); 89 | 90 | 91 | free(inp); 92 | free(xpred); 93 | free(amse); 94 | fclose(ifp); 95 | 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /data/seriesA.txt: -------------------------------------------------------------------------------- 1 | 16.9969 2 | 16.5969 3 | 16.3012 4 | 16.1018 5 | 17.0998 6 | 16.8997 7 | 16.8002 8 | 17.4001 9 | 17.0998 10 | 16.9996 11 | 16.6991 12 | 17.3993 13 | 17.2002 14 | 17.4013 15 | 17.4029 16 | 17.0015 17 | 17.2965 18 | 17.1966 19 | 17.4013 20 | 16.802 21 | 17.1 22 | 17.3995 23 | 17.3995 24 | 17.5008 25 | 17.3998 26 | 17.6 27 | 17.4 28 | 17.3 29 | 17 30 | 17.8 31 | 17.5 32 | 18.1 33 | 17.5 34 | 17.4 35 | 17.4 36 | 17.1 37 | 17.6 38 | 17.7 39 | 17.4 40 | 17.8 41 | 17.6 42 | 17.5 43 | 16.5 44 | 17.8 45 | 17.3 46 | 17.3 47 | 17.1 48 | 17.4 49 | 16.9 50 | 17.3 51 | 17.6 52 | 16.9 53 | 16.7 54 | 16.8 55 | 16.8 56 | 17.2 57 | 16.8 58 | 17.6 59 | 17.2 60 | 16.6 61 | 17.1 62 | 16.9 63 | 16.6 64 | 18 65 | 17.2 66 | 17.3 67 | 17 68 | 16.9 69 | 17.3 70 | 16.8 71 | 17.3 72 | 17.4 73 | 17.7 74 | 16.8 75 | 16.9 76 | 17 77 | 16.9 78 | 17 79 | 16.6 80 | 16.7 81 | 16.8 82 | 16.7 83 | 16.4 84 | 16.5 85 | 16.4 86 | 16.6 87 | 16.5 88 | 16.7 89 | 16.4 90 | 16.4 91 | 16.2 92 | 16.4 93 | 16.3 94 | 16.4 95 | 17 96 | 16.9 97 | 17.1 98 | 17.1 99 | 16.7 100 | 16.9 101 | 16.5 102 | 17.2 103 | 16.4 104 | 17 105 | 17 106 | 16.7 107 | 16.2 108 | 16.6 109 | 16.9 110 | 16.5 111 | 16.6 112 | 16.6 113 | 17 114 | 17.1 115 | 17.1 116 | 16.7 117 | 16.8 118 | 16.3 119 | 16.6 120 | 16.8 121 | 16.9 122 | 17.1 123 | 16.8 124 | 17 125 | 17.2 126 | 17.3 127 | 17.2 128 | 17.3 129 | 17.2 130 | 17.2 131 | 17.5 132 | 16.9 133 | 16.9 134 | 16.9 135 | 17.0001 136 | 16.4999 137 | 16.6996 138 | 16.8001 139 | 16.7013 140 | 16.6985 141 | 16.6005 142 | 16.5 143 | 17 144 | 16.7 145 | 16.7 146 | 16.9 147 | 17.4 148 | 17.1 149 | 17 150 | 16.8 151 | 17.2 152 | 17.2 153 | 17.4 154 | 17.2 155 | 16.9 156 | 16.8 157 | 17 158 | 17.4 159 | 17.2 160 | 17.2 161 | 17.1 162 | 17.1 163 | 17.1 164 | 17.4 165 | 17.2 166 | 16.9 167 | 16.9 168 | 17 169 | 16.7 170 | 16.9 171 | 17.3 172 | 17.8 173 | 17.8 174 | 17.6 175 | 17.5 176 | 17 177 | 16.8999 178 | 17.1 179 | 17.2002 180 | 17.3998 181 | 17.5001 182 | 17.9 183 | 17 184 | 17 185 | 17 186 | 17.2 187 | 17.3 188 | 17.4 189 | 17.4 190 | 17.0001 191 | 18.0001 192 | 18.2001 193 | 17.5999 194 | 17.7996 195 | 17.6992 196 | 17.2002 197 | 17.4027 198 | -------------------------------------------------------------------------------- /test/sarimatest3.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include /* calloc(), malloc(), free()*/ 4 | #include 5 | #include "../header/ctsa.h" 6 | 7 | #include "csv_in.h" /* csv_in() */ 8 | 9 | #define INPUT_FILE "../data/1m-20241125_001625-20241125_165625.csv" 10 | 11 | int main(void) { 12 | int i, N, d, L; 13 | double *inp; 14 | int p, q; 15 | int s, P, D, Q; 16 | double *xpred, *amse; 17 | double *column = (double*)NULL; 18 | size_t n_rows = 0; 19 | sarima_object obj; 20 | p = 0; 21 | d = 1; 22 | q = 1; 23 | s = 12; 24 | P = 0; 25 | D = 1; 26 | Q = 1; 27 | 28 | L = 5; 29 | 30 | xpred = (double*)malloc(sizeof(double)* L); 31 | amse = (double*)malloc(sizeof(double)* L); 32 | 33 | n_rows = read_csv_column(INPUT_FILE, "Close", &column); 34 | if (0 == n_rows) { 35 | printf("Could not load data from file '%s'", INPUT_FILE); 36 | exit(100); 37 | } 38 | 39 | printf("Read %lu lines\n", n_rows); 40 | 41 | inp = (double*)calloc(n_rows, sizeof(double)); 42 | //wmean = mean(temp, N); 43 | 44 | for (i = 0; i < n_rows; ++i) { 45 | inp[i] = log(column[i]); 46 | //printf("%g \n",inp[i]); 47 | } 48 | 49 | obj = sarima_init(p, d, q, s, P, D, Q, n_rows); 50 | sarima_setMethod(obj, 0); 51 | /** \brief Method 0 ("MLE") is default so this step is unnecessary. 52 | * The method also accepts values 1 ("CSS") and 2 ("Box-Jenkins") 53 | **/ 54 | 55 | // sarima_setOptMethod(obj, 7); 56 | /** \brief Method 7 ("BFGS with More Thuente Line Search") is 57 | * default so this step is unnecessary. The method also accepts 58 | * values 0,1,2,3,4,5,6. Check the documentation for details. 59 | **/ 60 | 61 | sarima_exec(obj, inp); 62 | sarima_summary(obj); 63 | 64 | // Predict the next 5 values using the obtained ARIMA model 65 | sarima_predict(obj, inp, L, xpred, amse); 66 | printf("\n"); 67 | 68 | printf("Predicted Values : "); 69 | for (i = 0; i < L; ++i) { 70 | printf("%g ", xpred[i]); 71 | } 72 | printf("\n"); 73 | printf("Standard Errors : "); 74 | for (i = 0; i < L; ++i) { 75 | printf("%g ", sqrt(amse[i])); 76 | } 77 | printf("\n"); 78 | 79 | sarima_free(obj); 80 | free(column); 81 | free(inp); 82 | free(xpred); 83 | free(amse); 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /data/e1m.dat: -------------------------------------------------------------------------------- 1 | 180 451 415 2 | 179 465 421 3 | 185 485 434 4 | 192 493 448 5 | 211 509 459 6 | 202 520 458 7 | 207 521 479 8 | 214 540 487 9 | 231 548 497 10 | 229 558 510 11 | 234 574 516 12 | 237 583 525 13 | 206 591 529 14 | 250 599 538 15 | 259 610 546 16 | 263 627 555 17 | 264 642 574 18 | 280 653 574 19 | 282 660 586 20 | 292 694 602 21 | 286 709 617 22 | 302 734 639 23 | 304 751 653 24 | 307 763 668 25 | 317 766 679 26 | 314 779 686 27 | 306 808 697 28 | 304 785 688 29 | 292 794 704 30 | 275 799 699 31 | 273 799 709 32 | 301 812 715 33 | 280 837 724 34 | 289 853 746 35 | 303 876 758 36 | 322 897 779 37 | 315 922 798 38 | 339 949 816 39 | 364 979 837 40 | 371 988 858 41 | 375 1025 881 42 | 432 1063 905 43 | 453 1104 934 44 | 460 1131 968 45 | 475 1137 983 46 | 496 1178 1013 47 | 494 1211 1034 48 | 498 1256 1064 49 | 526 1290 1101 50 | 519 1314 1102 51 | 516 1346 1145 52 | 531 1385 1173 53 | 573 1416 1216 54 | 551 1436 1229 55 | 538 1462 1242 56 | 532 1493 1267 57 | 558 1516 1295 58 | 524 1557 1317 59 | 525 1613 1355 60 | 519 1642 1371 61 | 526 1690 1402 62 | 510 1759 1452 63 | 519 1756 1485 64 | 538 1780 1516 65 | 549 1807 1549 66 | 570 1831 1567 67 | 559 1873 1588 68 | 584 1897 1631 69 | 611 1910 1650 70 | 597 1943 1685 71 | 603 1976 1722 72 | 619 2018 1752 73 | 635 2040 1774 74 | 658 2070 1807 75 | 675 2121 1831 76 | 700 2132 1842 77 | -------------------------------------------------------------------------------- /test/sarimatest.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | 7 | int main(void) { 8 | int i, N, d, L; 9 | double *inp; 10 | int p, q; 11 | int s, P, D, Q; 12 | // double *phi, *theta; 13 | // double *PHI, *THETA; 14 | double *xpred, *amse; 15 | sarima_object obj; 16 | p = 0; 17 | d = 1; 18 | q = 1; 19 | s = 12; 20 | P = 0; 21 | D = 1; 22 | Q = 1; 23 | 24 | 25 | L = 5; 26 | 27 | // phi = (double*)malloc(sizeof(double)* p); 28 | // theta = (double*)malloc(sizeof(double)* q); 29 | // PHI = (double*)malloc(sizeof(double)* P); 30 | // THETA = (double*)malloc(sizeof(double)* Q); 31 | 32 | xpred = (double*)malloc(sizeof(double)* L); 33 | amse = (double*)malloc(sizeof(double)* L); 34 | 35 | FILE *ifp; 36 | double temp[1200]; 37 | 38 | ifp = fopen("../data/seriesG.txt", "r"); 39 | i = 0; 40 | if (!ifp) { 41 | printf("Cannot Open File"); 42 | exit(100); 43 | } 44 | while (!feof(ifp)) { 45 | fscanf(ifp, "%lf \n", &temp[i]); 46 | i++; 47 | } 48 | N = i; 49 | 50 | inp = (double*)malloc(sizeof(double)* N); 51 | //wmean = mean(temp, N); 52 | 53 | for (i = 0; i < N; ++i) { 54 | inp[i] = log(temp[i]); 55 | //printf("%g \n",inp[i]); 56 | } 57 | 58 | 59 | obj = sarima_init(p, d, q,s,P,D,Q, N); 60 | sarima_setMethod(obj, 0); // Method 0 ("MLE") is default so this step is unnecessary. The method also accepts values 1 ("CSS") and 2 ("Box-Jenkins") 61 | //sarima_setOptMethod(obj, 7);// Method 7 ("BFGS with More Thuente Line Search") is default so this step is unnecessary. The method also accepts values 0,1,2,3,4,5,6. Check the documentation for details. 62 | sarima_exec(obj, inp); 63 | sarima_summary(obj); 64 | // Predict the next 5 values using the obtained ARIMA model 65 | sarima_predict(obj, inp, L, xpred, amse); 66 | printf("\n"); 67 | printf("Predicted Values : "); 68 | for (i = 0; i < L; ++i) { 69 | printf("%g ", xpred[i]); 70 | } 71 | printf("\n"); 72 | printf("Standard Errors : "); 73 | for (i = 0; i < L; ++i) { 74 | printf("%g ", sqrt(amse[i])); 75 | } 76 | printf("\n"); 77 | sarima_free(obj); 78 | free(inp); 79 | // free(phi); 80 | // free(theta); 81 | // free(PHI); 82 | // free(THETA); 83 | free(xpred); 84 | free(amse); 85 | return 0; 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/talg.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * talg.h 4 | * 5 | * Created on: Jul 31, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #ifndef TALG_H_ 10 | #define TALG_H_ 11 | 12 | #include "filter.h" 13 | #include "regression.h" 14 | #include "conv.h" 15 | #include "stl.h" 16 | #include "boxcox.h" 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | void detrend_ma(double *sig,int N, int window,double *oup); 23 | 24 | int poly(double *A, double *B, double *C, int lA, int lB); 25 | 26 | int upsample(double *x,int lenx, int M, double *y); 27 | 28 | int downsample(double *x,int lenx, int M, double *y); 29 | 30 | void deld(int d, double* oup); 31 | 32 | void delds(int D, int s, double *C); 33 | 34 | int diff(double *sig, int N, int del, double *oup); 35 | 36 | int diffs(double *sig, int N, int D,int s, double *oup); 37 | 38 | void deseason_ma(double *sig,int N,int s,double *oup); 39 | 40 | void psiweight(double *phi,double *theta,double *psi,int p,int q,int j); 41 | 42 | void piweight(double *phi,double *theta,double *piw,int p,int q,int j); 43 | 44 | void arma_autocovar(double *phi,double *theta,int p,int q,double var,double* acov, int lag); 45 | 46 | int twacf(double *P, int MP, double *Q, int MQ, double *ACF, int MA, double *CVLI, int MXPQ1, double *ALPHA, int MXPQ); 47 | 48 | void artrans(int p, double *old, double *new1); 49 | 50 | void arinvtrans(int p, double *old, double *new1); 51 | 52 | void gradtrans(double *raw, int p, int q, int P, int Q, int M, double *A); 53 | 54 | void transall(int p, int q, int P, int Q, double *old, double *new1); 55 | 56 | void invtransall(int p, int q, int P, int Q, double *old, double *new1); 57 | 58 | int archeck(int p, double *ar); 59 | 60 | int invertroot(int q, double *ma); 61 | 62 | double interpolate_linear(double *x,double *y, int N, double z); 63 | 64 | void approx(double *x,double *y, int N,double *xout, double *yout,int Nout); 65 | 66 | void linspace(double *x, int N,double xlo,double xhi); 67 | 68 | void arrayminmax(double *x, int N, double *amin,double *amax); 69 | 70 | void cumsum(double *x, int N, double *csum); 71 | 72 | void ppsum(double *x, int N, int l, double *sum); 73 | 74 | void supsmu(double *x, int N, double *y,double *w, int periodic,double span, double alpha,double *oup); 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | 81 | 82 | #endif /* TALG_H_ */ 83 | -------------------------------------------------------------------------------- /test/artest2.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | 7 | double ar_mean(double *x, int N) { 8 | double mn; 9 | int i; 10 | 11 | mn = 0.0; 12 | 13 | for(i = 0; i < N;++i) { 14 | mn += x[i]; 15 | } 16 | 17 | mn /= (double) N; 18 | 19 | return mn; 20 | } 21 | 22 | int main(void) { 23 | int i, N; 24 | double *inp; 25 | int p,q; 26 | double var, wmean; 27 | double *phi,*theta; 28 | 29 | 30 | FILE *ifp; 31 | double temp[1200]; 32 | printf("OK"); 33 | 34 | ifp = fopen("../data/seriesA.txt", "r"); 35 | i = 0; 36 | if (!ifp) { 37 | printf("Cannot Open File"); 38 | exit(100); 39 | } 40 | while (!feof(ifp)) { 41 | fscanf(ifp, "%lf \n", &temp[i]); 42 | i++; 43 | } 44 | N = i; 45 | 46 | inp = (double*)malloc(sizeof(double)* N); 47 | wmean = ar_mean(temp, N); 48 | 49 | for (i = 0; i < N; ++i) { 50 | inp[i] = temp[i]; 51 | //printf("%g \n",inp[i]); 52 | } 53 | 54 | fclose(ifp); 55 | p = 7; 56 | 57 | phi = (double*)malloc(sizeof(double)* p); 58 | 59 | printf("AR Coefficients Using Yule Walker Algorithm \n"); 60 | yw(inp, N, p, phi, &var); 61 | printf("\n"); 62 | printf("PHI : "); 63 | for (i = 0; i < p; ++i) { 64 | printf("%g ", phi[i]); 65 | } 66 | printf("\n"); 67 | 68 | printf("VAR %g \n",var); 69 | printf("\n"); 70 | 71 | printf("AR Coefficients Using Burg Algorithm \n"); 72 | burg(inp, N, p, phi, &var); 73 | printf("\n"); 74 | printf("PHI : "); 75 | for (i = 0; i < p; ++i) { 76 | printf("%g ", phi[i]); 77 | } 78 | printf("\n"); 79 | 80 | printf("VAR %g \n", var); 81 | free(phi); 82 | 83 | p = 1; 84 | q = 1; 85 | 86 | phi = (double*)malloc(sizeof(double)* p); 87 | theta = (double*)malloc(sizeof(double)* q); 88 | printf("\n"); 89 | printf("ARMA Coefficients Using Hannan Rissanen Algorithm \n"); 90 | hr(inp, N, p, q, phi,theta, &var); 91 | printf("\n"); 92 | printf("PHI : "); 93 | for (i = 0; i < p; ++i) { 94 | printf("%g ", phi[i]); 95 | } 96 | printf("\n"); 97 | 98 | printf("THETA : "); 99 | for (i = 0; i < q; ++i) { 100 | printf("%g ", theta[i]); 101 | } 102 | printf("\n"); 103 | 104 | printf("VAR %g \n", var); 105 | 106 | 107 | 108 | 109 | free(inp); 110 | free(phi); 111 | free(theta); 112 | 113 | return 0; 114 | 115 | } 116 | -------------------------------------------------------------------------------- /data/seriesC.txt: -------------------------------------------------------------------------------- 1 | 26.6 2 | 27 3 | 27.1 4 | 27.1 5 | 27.1 6 | 27.1 7 | 26.9 8 | 26.8 9 | 26.7 10 | 26.4 11 | 26 12 | 25.8 13 | 25.6 14 | 25.2 15 | 25 16 | 24.6 17 | 24.2 18 | 24 19 | 23.7 20 | 23.4 21 | 23.1 22 | 22.9 23 | 22.8 24 | 22.7 25 | 22.6 26 | 22.4 27 | 22.2 28 | 22 29 | 21.8 30 | 21.4 31 | 20.9 32 | 20.3 33 | 19.7 34 | 19.4 35 | 19.3 36 | 19.2 37 | 19.1 38 | 19 39 | 18.9 40 | 18.9 41 | 19.2 42 | 19.3 43 | 19.3 44 | 19.4 45 | 19.5 46 | 19.6 47 | 19.6 48 | 19.6 49 | 19.6 50 | 19.6 51 | 19.7 52 | 19.9 53 | 20 54 | 20.1 55 | 20.2 56 | 20.3 57 | 20.6 58 | 21.6 59 | 21.9 60 | 21.7 61 | 21.3 62 | 21.2 63 | 21.4 64 | 21.7 65 | 22.2 66 | 23 67 | 23.8 68 | 24.6 69 | 25.1 70 | 25.6 71 | 25.8 72 | 26.1 73 | 26.3 74 | 26.3 75 | 26.2 76 | 26 77 | 25.8 78 | 25.6 79 | 25.4 80 | 25.2 81 | 24.8998 82 | 24.7 83 | 24.5007 84 | 24.3992 85 | 24.4003 86 | 24.4 87 | 24.4 88 | 24.4 89 | 24.3 90 | 24.4 91 | 24.4 92 | 24.4 93 | 24.4 94 | 24.4 95 | 24.5 96 | 24.5 97 | 24.4 98 | 24.3 99 | 24.2 100 | 24.2 101 | 24 102 | 23.9 103 | 23.7 104 | 23.6 105 | 23.5 106 | 23.5 107 | 23.5 108 | 23.5 109 | 23.5 110 | 23.7 111 | 23.8 112 | 23.8 113 | 23.9 114 | 23.9 115 | 23.8 116 | 23.7 117 | 23.6 118 | 23.4 119 | 23.1998 120 | 23 121 | 22.8007 122 | 22.5992 123 | 22.4002 124 | 22 125 | 21.6 126 | 21.3 127 | 21.2 128 | 21.2 129 | 21.1 130 | 21 131 | 20.9 132 | 21 133 | 21 134 | 21.1 135 | 21.2 136 | 21.1 137 | 20.9 138 | 20.8 139 | 20.8 140 | 20.8 141 | 20.8 142 | 20.9 143 | 20.8 144 | 20.8 145 | 20.7 146 | 20.7 147 | 20.8 148 | 20.9 149 | 21.2 150 | 21.4 151 | 21.7 152 | 21.8 153 | 21.9 154 | 22.2 155 | 22.5 156 | 22.8 157 | 23.1 158 | 23.4 159 | 23.8 160 | 24.1 161 | 24.6 162 | 24.9 163 | 24.9 164 | 25.1 165 | 25 166 | 25 167 | 25 168 | 25 169 | 24.9 170 | 24.8 171 | 24.7 172 | 24.6 173 | 24.5 174 | 24.5 175 | 24.5002 176 | 24.5 177 | 24.4993 178 | 24.5008 179 | 24.4998 180 | 24.4 181 | 24.4 182 | 24.2 183 | 24.2 184 | 24.1 185 | 24.1 186 | 24 187 | 24 188 | 24 189 | 23.9 190 | 23.8 191 | 23.8 192 | 23.7 193 | 23.7 194 | 23.6 195 | 23.7 196 | 23.6 197 | 23.6 198 | 23.6 199 | 23.5 200 | 23.5 201 | 23.4 202 | 23.3 203 | 23.3 204 | 23.3 205 | 23.4 206 | 23.4 207 | 23.3 208 | 23.2 209 | 23.3 210 | 23.3 211 | 23.2 212 | 23.1 213 | 22.9 214 | 22.8 215 | 22.6 216 | 22.4 217 | 22.2 218 | 21.8 219 | 21.3002 220 | 20.8 221 | 20.1995 222 | 19.7006 223 | 19.2998 224 | 19.1 225 | 19 226 | 18.8 227 | -------------------------------------------------------------------------------- /test/sarimatest2.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include /* calloc(), malloc(), free()*/ 4 | #include 5 | #include "../header/ctsa.h" 6 | 7 | #include "csv_in.h" /* read_csv_column() */ 8 | 9 | #define INPUT_FILE "../data/ohlc_min.csv" 10 | 11 | int main(void) { 12 | int i, N, d, L; 13 | double *inp; 14 | int p, q; 15 | int s, P, D, Q; 16 | double *xpred, *amse; 17 | double *column = (double*)NULL; 18 | size_t n_rows = 0; 19 | sarima_object obj; 20 | p = 0; 21 | d = 1; 22 | q = 1; 23 | s = 12; 24 | P = 0; 25 | D = 0; // OBS.: not enough data for seasonal differencing 26 | Q = 1; 27 | 28 | L = 5; 29 | 30 | xpred = (double*)malloc(sizeof(double)* L); 31 | amse = (double*)malloc(sizeof(double)* L); 32 | 33 | n_rows = read_csv_column(INPUT_FILE, "Close", &column); 34 | if (0 == n_rows) { 35 | printf("Could not load data from file '%s'", INPUT_FILE); 36 | exit(100); 37 | } 38 | 39 | printf("Read %lu lines of '%s'\n", n_rows, INPUT_FILE); 40 | fflush(stdout); 41 | 42 | inp = (double*)calloc(n_rows, sizeof(double)); 43 | //wmean = mean(temp, N); 44 | 45 | puts("Will take the logarithm"); fflush(stdout); 46 | for (i = 0; i < n_rows; ++i) { 47 | inp[i] = log(column[i]); 48 | //printf("%g \n",inp[i]); 49 | } 50 | 51 | puts("Initializing SARIMA"); fflush(stdout); 52 | obj = sarima_init(p, d, q, s, P, D, Q, n_rows); 53 | sarima_setMethod(obj, 0); 54 | /** \brief Method 0 ("MLE") is default so this step is unnecessary. 55 | * The method also accepts values 1 ("CSS") and 2 ("Box-Jenkins") 56 | **/ 57 | 58 | // sarima_setOptMethod(obj, 7); 59 | /** \brief Method 7 ("BFGS with More Thuente Line Search") is 60 | * default so this step is unnecessary. The method also accepts 61 | * values 0,1,2,3,4,5,6. Check the documentation for details. 62 | **/ 63 | 64 | puts("Will exec SARIMA"); fflush(stdout); 65 | sarima_exec(obj, inp); 66 | sarima_summary(obj); 67 | 68 | // Predict the next 5 values using the obtained ARIMA model 69 | printf("Will predict %d values", L); fflush(stdout); 70 | sarima_predict(obj, inp, L, xpred, amse); 71 | printf("\n"); 72 | 73 | printf("Predicted Values : "); 74 | for (i = 0; i < L; ++i) { 75 | printf("%g ", xpred[i]); 76 | } 77 | printf("\n"); 78 | printf("Standard Errors : "); 79 | for (i = 0; i < L; ++i) { 80 | printf("%g ", sqrt(amse[i])); 81 | } 82 | printf("\n"); 83 | 84 | sarima_free(obj); 85 | free(column); 86 | free(inp); 87 | free(xpred); 88 | free(amse); 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-3-Clause 2 | add_executable(acftest acftest.c) 3 | 4 | target_link_libraries(acftest ctsalib m) 5 | 6 | add_executable(arimatest arimatest.c) 7 | 8 | target_link_libraries(arimatest ctsalib m) 9 | 10 | add_executable(artest artest.c) 11 | 12 | target_link_libraries(artest ctsalib m) 13 | 14 | add_executable(artest2 artest2.c) 15 | 16 | target_link_libraries(artest2 ctsalib m) 17 | 18 | add_executable(pacftest pacftest.c) 19 | 20 | target_link_libraries(pacftest ctsalib m) 21 | 22 | add_executable(sarimatest sarimatest.c) 23 | 24 | target_link_libraries(sarimatest ctsalib m) 25 | 26 | # TODO add the header files via a `target_include_directories` 27 | add_executable(sarimatest2 28 | sarimatest2.c 29 | auxlib.c 30 | auxlib.h 31 | csv_in.c 32 | csv_in.h 33 | ) 34 | 35 | target_link_libraries(sarimatest2 ctsalib m) 36 | 37 | # TODO add the header files via a `target_include_directories` 38 | add_executable(sarimatest3 39 | sarimatest3.c 40 | auxlib.c 41 | auxlib.h 42 | csv_in.c 43 | csv_in.h 44 | ) 45 | 46 | target_link_libraries(sarimatest3 ctsalib m) 47 | 48 | # TODO add the header files via a `target_include_directories` 49 | add_executable(sarimatest_mu 50 | sarimatest_mu.c 51 | minunit.h 52 | ) 53 | 54 | target_link_libraries(sarimatest_mu ctsalib m) 55 | 56 | add_executable(misctests misctests.c) 57 | 58 | target_link_libraries(misctests ctsalib m) 59 | 60 | add_executable(dwttests dwttests.c) 61 | 62 | target_link_libraries(dwttests ctsalib m) 63 | 64 | add_executable(sarimaxtest1 sarimaxtest1.c) 65 | 66 | target_link_libraries(sarimaxtest1 ctsalib m) 67 | 68 | add_executable(sarimaxtest2 sarimaxtest2.c) 69 | 70 | target_link_libraries(sarimaxtest2 ctsalib m) 71 | 72 | add_executable(sarimaxtest3 sarimaxtest3.c) 73 | 74 | target_link_libraries(sarimaxtest3 ctsalib m) 75 | 76 | add_executable(autoarimatest1 autoarimatest1.c) 77 | 78 | target_link_libraries(autoarimatest1 ctsalib m) 79 | 80 | add_executable(autoarimatest2 autoarimatest2.c) 81 | 82 | target_link_libraries(autoarimatest2 ctsalib m) 83 | 84 | add_executable(autoarimatest3 autoarimatest3.c) 85 | 86 | target_link_libraries(autoarimatest3 ctsalib m) 87 | 88 | 89 | set_target_properties(acftest arimatest artest artest2 pacftest sarimatest misctests dwttests sarimaxtest1 sarimaxtest2 sarimaxtest3 autoarimatest1 autoarimatest2 autoarimatest3 90 | PROPERTIES 91 | RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}" 92 | ) 93 | -------------------------------------------------------------------------------- /test/autoarimatest3.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | /* 7 | 8 | Auto ARIMA example 1 9 | 10 | */ 11 | 12 | int main(void) { 13 | int i, N, d, D, L; 14 | double *inp; 15 | int p, q, P, Q, s, r; 16 | double *xpred, *amse,*xreg,*newxreg; 17 | auto_arima_object obj; 18 | /* 19 | Make sure all the parameter values are correct and consistent with other values. eg., if xreg is NULL r should be 0 20 | or if P = D = Q = 0 then make sure that s is also 0. 21 | Recheck the values if the program fails to execute. 22 | */ 23 | p = 5; 24 | d = 2; 25 | q = 5; 26 | P = 2; 27 | D = 1; 28 | Q = 2; 29 | 30 | int order[3] = {p,d,q}; 31 | int seasonal[3] = {P,D,Q}; 32 | 33 | s = 0; 34 | r = 2; 35 | L = 5; 36 | 37 | xpred = (double*)malloc(sizeof(double)* L); 38 | amse = (double*)malloc(sizeof(double)* L); 39 | 40 | FILE *ifp; 41 | double temp[1200]; 42 | double temp1[1200]; 43 | double temp2[1200]; 44 | 45 | ifp = fopen("../data/e1m.dat", "r"); 46 | i = 0; 47 | if (!ifp) { 48 | printf("Cannot Open File"); 49 | exit(100); 50 | } 51 | while (!feof(ifp)) { 52 | fscanf(ifp, "%lf %lf %lf \n", &temp[i],&temp1[i],&temp2[i]); 53 | i++; 54 | } 55 | N = i - L; 56 | 57 | inp = (double*)malloc(sizeof(double)* N); 58 | xreg = (double*)malloc(sizeof(double)* N * 2); 59 | newxreg = (double*)malloc(sizeof(double)* L * 2); 60 | 61 | 62 | for (i = 0; i < N; ++i) { 63 | inp[i] = temp[i]; 64 | xreg[i] = temp1[i]; 65 | xreg[N+i] = temp2[i]; 66 | } 67 | 68 | for(i = 0; i < L;++i) { 69 | newxreg[i] = temp1[N + i]; 70 | newxreg[i+L] = temp2[N + i]; 71 | } 72 | 73 | obj = auto_arima_init(order,seasonal,s,r,N); 74 | 75 | auto_arima_setApproximation(obj,1); 76 | auto_arima_setStepwise(obj,1); 77 | auto_arima_setVerbose(obj,1); 78 | 79 | auto_arima_exec(obj,inp,xreg); 80 | 81 | auto_arima_summary(obj); 82 | 83 | auto_arima_predict(obj, inp, xreg, L,newxreg, xpred, amse); 84 | 85 | printf("\n"); 86 | printf("Predicted Values : "); 87 | for (i = 0; i < L; ++i) { 88 | printf("%g ", xpred[i]); 89 | } 90 | printf("\n"); 91 | printf("Standard Errors : "); 92 | for (i = 0; i < L; ++i) { 93 | printf("%g ", sqrt(amse[i])); 94 | } 95 | printf("\n"); 96 | 97 | auto_arima_free(obj); 98 | 99 | 100 | free(inp); 101 | free(xpred); 102 | free(amse); 103 | free(xreg); 104 | free(newxreg); 105 | 106 | return 0; 107 | } -------------------------------------------------------------------------------- /data/signal.txt: -------------------------------------------------------------------------------- 1 | -18.3237 -18.2232 -18.0974 -17.9410 -17.7480 -17.5113 -17.2230 -16.8744 -16.4558 -15.9565 -15.3653 -14.6701 -13.8586 -12.9182 -11.8363 -10.6008 -9.2006 -7.6257 -5.8680 -3.9217 -1.7839 0.5452 3.0614 5.7562 8.6167 11.6252 14.7591 17.9909 21.2884 24.6155 27.9319 31.1947 34.3587 37.3775 40.2049 42.7957 13.2164 14.2125 15.0317 15.6595 16.0845 16.2990 16.2990 16.0845 15.6595 15.0317 14.2125 13.2164 12.0608 10.7654 9.3517 34.3587 31.1947 27.9319 24.6155 21.2884 17.9909 14.7591 11.6252 8.6167 5.7562 3.0614 0.5452 -1.7839 -3.9217 -5.8680 -7.6257 -9.2006 -10.6008 -11.8363 -12.9182 -13.8586 -14.6701 -15.3653 -15.9565 -16.4558 -16.8744 -17.2230 -17.5113 -17.7480 -17.9410 -18.0974 -18.2232 -18.3237 -18.4035 -18.0080 -17.8889 -17.7403 -17.5533 -17.3156 -17.0102 -16.6129 -16.0884 -15.3848 -14.4239 -13.0840 -11.1708 -8.3634 -4.1098 2.5833 13.6048 32.7934 28.0187 10.9660 1.0776 -4.9459 -8.7354 -11.1225 -12.4865 -12.8019 -11.2050 -3.3124 1.8995 -11.3573 -15.0684 -16.5028 -17.1937 -17.5831 -17.8288 -17.9968 -18.1185 -18.2103 -18.2818 -18.3388 -18.3849 -18.4229 -18.4545 -18.4810 -17.4642 -17.2104 -16.9033 -16.5317 -16.0822 -15.5384 -14.8804 -14.0844 -13.1214 -11.9563 -10.5467 -8.8414 -6.7782 -4.2822 -1.2624 2.3911 6.8111 12.1585 18.6280 26.4549 35.9241 35.9241 26.4549 18.6280 12.1585 6.8111 2.3911 -1.2624 -4.2822 -6.7782 -8.8414 -10.5467 -11.9563 -13.1214 -14.0844 -14.8804 -15.5384 -16.0822 -16.5317 -16.9033 -17.2104 -17.4642 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 6.3259 34.8066 34.6752 34.5285 34.3645 34.1812 33.9763 33.7474 33.4917 33.2058 32.8863 32.5294 32.1304 31.6846 31.1864 30.6296 30.0074 29.3121 28.5350 27.6667 26.6963 25.6118 24.3999 23.0456 21.5322 19.8408 17.9507 15.8385 13.4781 10.8403 7.8925 4.5982 0.9168 -3.1972 -7.7947 -12.9325 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.6741 -18.3237 -------------------------------------------------------------------------------- /src/optimc.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * optimc.h 4 | * 5 | * Created on: Mar 16, 2014 6 | * Author: HOME 7 | */ 8 | 9 | #ifndef OPTIMC_H_ 10 | #define OPTIMC_H_ 11 | 12 | #include "nls.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | typedef struct opt_set* opt_object; 19 | 20 | opt_object opt_init(int N); 21 | 22 | struct opt_set{ 23 | int N; 24 | double objfunc; 25 | double eps; 26 | double gtol; 27 | double stol; 28 | double ftol; 29 | double xtol; 30 | double maxstep; 31 | int MaxIter; 32 | int Iter; 33 | int Method; 34 | int retval; 35 | char MethodName[50]; 36 | double xopt[1]; 37 | }; 38 | 39 | typedef struct nls_set* nls_object; 40 | 41 | nls_object nls_init(int M,int N); 42 | 43 | struct nls_set{ 44 | int M; 45 | int N; 46 | double eps; 47 | double epsfcn; 48 | double factor; 49 | double gtol; 50 | double ftol; 51 | double xtol; 52 | int MaxIter; 53 | int Maxfev; 54 | int Iter; 55 | int nfev; 56 | int njev; 57 | int ldfjac; 58 | int mode; 59 | int retval; 60 | double xopt[1]; 61 | }; 62 | 63 | void setnlsTOL(nls_object obj,double gtol,double ftol,double xtol); 64 | 65 | void optsummary(opt_object obj); 66 | 67 | void setMaxIter(opt_object obj,int MaxIter); 68 | 69 | void setMaxStep(opt_object obj, double maxstep); 70 | 71 | void setTOL(opt_object obj,double gtol,double stol,double ftol,double xtol); 72 | 73 | int fminsearch(custom_function *funcpt,int N,double *xi,double *xf); 74 | 75 | double fminbnd(custom_funcuni *funcuni,double a, double b); 76 | 77 | int fminunc(custom_function *funcpt,custom_gradient *funcgrad,int N,double *xi,double maxstep, int method,double *xf); 78 | 79 | int fminnewt(custom_function *funcpt, custom_gradient *funcgrad, int N, double *xi, 80 | double delta,double *dx,double fsval,double maxstep,int method,double *xf); 81 | 82 | double brent_local_min(custom_funcuni *funcuni,double a, double b, double t, double eps, double *x); 83 | 84 | void optimize(opt_object obj, custom_function *funcpt, custom_gradient *funcgrad, int N, double *xi, 85 | int method); 86 | 87 | void free_opt(opt_object object); 88 | 89 | int levmar(custom_funcmult *funcmult, custom_jacobian *jacobian, 90 | double *xi,int M, int N,double *xf); 91 | 92 | void nls(nls_object obj, custom_funcmult *funcmult, custom_jacobian *jacobian, 93 | double *xi); 94 | 95 | void nls_scale(nls_object obj, custom_funcmult *funcmult, custom_jacobian *jacobian, 96 | double *diag,double *xi); 97 | 98 | void nlssummary(nls_object obj); 99 | 100 | void free_nls(nls_object object); 101 | 102 | #ifdef __cplusplus 103 | } 104 | #endif 105 | 106 | #endif /* OPTIMC_H_ */ 107 | -------------------------------------------------------------------------------- /src/regression.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * regression.h 4 | * 5 | * Created on: Jun 5, 2013 6 | * Author: USER 7 | */ 8 | 9 | #ifndef REGRESSION_H_ 10 | #define REGRESSION_H_ 11 | 12 | #include "stats.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | typedef struct bparam_t { 19 | double value; 20 | double lower; 21 | double upper; 22 | double stdErr; 23 | } bparam; 24 | 25 | typedef struct reg_set* reg_object; 26 | 27 | reg_object reg_init(int N, int p); 28 | 29 | struct reg_set{ 30 | int N; 31 | int p; 32 | double alpha; 33 | double sigma; 34 | double sigma_lower; 35 | double sigma_upper; 36 | double r2; 37 | double r2adj; 38 | double R2[2]; 39 | char lls[10]; 40 | int df; 41 | int intercept; 42 | int rank; // Use method "qr" to calculate the rank 43 | double TSS; 44 | double ESS; 45 | double RSS; 46 | int df_ESS; 47 | int df_RSS; 48 | double FStat; 49 | double PVal; 50 | double loglik; 51 | double aic; 52 | double bic; 53 | double aicc; 54 | bparam beta[1]; 55 | }; 56 | 57 | void linreg_clrm(double *x,double *y, int N, double* b, 58 | double *var,double *res,double alpha,double *anv, 59 | double* ci_lower, double* ci_upper); 60 | 61 | void zerohyp_clrm(int N,double *b, double *val, double *tval, double *pval); 62 | 63 | //void linreg_multi2(int p, double *x,double *y, int N, double* b); // p number of variables. 64 | // p = 2 for one dependent variable and one independent variable 65 | // p = 3 for one dependent variable and two independent variables etc. 66 | 67 | void linreg_multi(int p, double *xi,double *y, int N, double* b,double *sigma2, 68 | double *xxti,double *R2,double *res,double alpha,double *anv, 69 | double* ci_lower, double* ci_upper,int *rank,char *llsmethod, int intercept); 70 | 71 | void zerohyp_multi(int N,double *b,int p, double *varcovar, double *tval, double *pval); 72 | 73 | void regress(reg_object obj,double *x,double *y,double *res,double *varcovar,double alpha); 74 | 75 | void regress_poly(reg_object obj,double *x,double *y,double *res,double *varcovar,double alpha); 76 | 77 | void setIntercept(reg_object obj,int intercept); 78 | 79 | void setLLSMethod(reg_object obj,char *llsmethod); 80 | 81 | void summary(reg_object obj); 82 | 83 | void anova(reg_object obj); 84 | 85 | void anova_list(reg_object *list, int N); 86 | 87 | void confint(reg_object obj); 88 | 89 | void zerohyp_val(reg_object obj, double *tval, double *pval); 90 | 91 | double fitted(reg_object obj,double *inp,double *varcovar,double *var); 92 | 93 | void free_reg(reg_object object); 94 | 95 | 96 | #ifdef __cplusplus 97 | } 98 | #endif 99 | 100 | #endif /* REGRESSION_H_ */ 101 | -------------------------------------------------------------------------------- /src/real.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * real.c 4 | * 5 | * Created on: Apr 20, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | #include 9 | #include "real.h" 10 | 11 | fft_real_object fft_real_init(int N, int sgn) { 12 | fft_real_object obj = NULL; 13 | fft_type PI, theta; 14 | int k; 15 | 16 | PI = 3.1415926535897932384626433832795; 17 | 18 | obj = (fft_real_object) malloc (sizeof(struct fft_real_set) + sizeof(fft_data)* (N/2)); 19 | 20 | obj->cobj = fft_init(N/2,sgn); 21 | 22 | for (k = 0; k < N/2;++k) { 23 | theta = PI2*k/N; 24 | obj->twiddle2[k].re = cos(theta); 25 | obj->twiddle2[k].im = sin(theta); 26 | 27 | } 28 | 29 | 30 | return obj; 31 | 32 | 33 | } 34 | 35 | void fft_r2c_exec(fft_real_object obj,fft_type *inp,fft_data *oup) { 36 | fft_data* cinp; 37 | fft_data* coup; 38 | int i,N2,N; 39 | fft_type temp1,temp2; 40 | N2 = obj->cobj->N; 41 | N = N2*2; 42 | 43 | cinp = (fft_data*) malloc (sizeof(fft_data) * N2); 44 | coup = (fft_data*) malloc (sizeof(fft_data) * N2); 45 | 46 | for (i = 0; i < N2; ++i) { 47 | cinp[i].re = inp[2*i]; 48 | cinp[i].im = inp[2*i+1]; 49 | } 50 | 51 | fft_exec(obj->cobj,cinp,coup); 52 | 53 | oup[0].re = coup[0].re + coup[0].im; 54 | oup[0].im = 0.0; 55 | 56 | for (i = 1; i < N2; ++i) { 57 | temp1 = coup[i].im + coup[N2-i].im ; 58 | temp2 = coup[N2-i].re - coup[i].re ; 59 | oup[i].re = (coup[i].re + coup[N2-i].re + (temp1 * obj->twiddle2[i].re) + (temp2 * obj->twiddle2[i].im)) / 2.0; 60 | oup[i].im = (coup[i].im - coup[N2-i].im + (temp2 * obj->twiddle2[i].re) - (temp1 * obj->twiddle2[i].im)) / 2.0; 61 | } 62 | 63 | 64 | 65 | oup[N2].re = coup[0].re - coup[0].im; 66 | oup[N2].im = 0.0; 67 | 68 | for (i = 1; i < N2;++i) { 69 | oup[N-i].re = oup[i].re; 70 | oup[N-i].im = -oup[i].im; 71 | } 72 | 73 | 74 | free(cinp); 75 | free(coup); 76 | 77 | } 78 | 79 | void fft_c2r_exec(fft_real_object obj,fft_data *inp,fft_type *oup) { 80 | 81 | fft_data* cinp; 82 | fft_data* coup; 83 | int i,N2,N; 84 | fft_type temp1,temp2; 85 | N2 = obj->cobj->N; 86 | N = N2*2; 87 | 88 | cinp = (fft_data*) malloc (sizeof(fft_data) * N2); 89 | coup = (fft_data*) malloc (sizeof(fft_data) * N2); 90 | 91 | for (i = 0; i < N2; ++i) { 92 | temp1 = -inp[i].im - inp[N2-i].im ; 93 | temp2 = -inp[N2-i].re + inp[i].re ; 94 | cinp[i].re = inp[i].re + inp[N2-i].re + (temp1 * obj->twiddle2[i].re) - (temp2 * obj->twiddle2[i].im); 95 | cinp[i].im = inp[i].im - inp[N2-i].im + (temp2 * obj->twiddle2[i].re) + (temp1 * obj->twiddle2[i].im); 96 | } 97 | 98 | fft_exec(obj->cobj,cinp,coup); 99 | for (i = 0; i < N2; ++i) { 100 | oup[2*i] = coup[i].re; 101 | oup[2*i+1] = coup[i].im; 102 | } 103 | free(cinp); 104 | free(coup); 105 | 106 | 107 | } 108 | 109 | void free_real_fft(fft_real_object object) { 110 | free_fft(object->cobj); 111 | free(object); 112 | } 113 | 114 | -------------------------------------------------------------------------------- /test/sarimaxtest1.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | /* 7 | 8 | ARMA example for a stationary time series with no seasonal components and no exogenous variable. 9 | 10 | */ 11 | int main(void) { 12 | int i, N, d, D, L; 13 | double *inp; 14 | int p, q, P, Q, s, r,imean; 15 | double *phi, *theta; 16 | double *xpred, *amse; 17 | sarimax_object obj; 18 | /* 19 | Make sure all the parameter values are correct and consistent with other values. eg., if xreg is NULL r should be 0 20 | or if P = D = Q = 0 then make sure that s is also 0. 21 | Recheck the values if the program fails to execute. 22 | */ 23 | p = 0; 24 | d = 1; 25 | q = 0; 26 | s = 0; 27 | P = 0; 28 | D = 0; 29 | Q = 0; 30 | r = 0; 31 | 32 | imean = 1; 33 | 34 | L = 5; 35 | 36 | phi = (double*)malloc(sizeof(double)* p); 37 | theta = (double*)malloc(sizeof(double)* q); 38 | 39 | xpred = (double*)malloc(sizeof(double)* L); 40 | amse = (double*)malloc(sizeof(double)* L); 41 | 42 | FILE *ifp; 43 | double temp[1200]; 44 | 45 | ifp = fopen("../data/seriesA.txt", "r"); 46 | i = 0; 47 | if (!ifp) { 48 | printf("Cannot Open File"); 49 | exit(100); 50 | } 51 | while (!feof(ifp)) { 52 | fscanf(ifp, "%lf \n", &temp[i]); 53 | i++; 54 | } 55 | N = i; 56 | 57 | inp = (double*)malloc(sizeof(double)* N); 58 | 59 | for (i = 0; i < N; ++i) { 60 | inp[i] = temp[i]; 61 | } 62 | 63 | 64 | obj = sarimax_init(p, d, q, P, D, Q, s, r ,imean, N); 65 | 66 | /* setMethod() 67 | Method 0 ("CSS-MLE") is default. The method also accepts values 1 ("MLE") and 2 ("CSS") 68 | */ 69 | 70 | sarimax_setMethod(obj, 0); 71 | 72 | /*sarimax_exec(object, input time series, exogenous time series) 73 | set exogenous to NULL if deadling only with a univariate time series. 74 | */ 75 | sarimax_exec(obj, inp,NULL); 76 | sarimax_summary(obj); 77 | /* sarimax_predict(sarimax_object obj, double *inp, double *xreg, int L,double *newxreg, double *xpred, double *amse) 78 | inp - Input Time Series 79 | xreg - Exogenous Time Series 80 | L - L point prediction 81 | newxreg - Exogenous Time Series of dimension r * L where r is the number of exogenus time series and L is the length of each 82 | xpred - L future values 83 | amse - MSE for L future values 84 | */ 85 | sarimax_predict(obj, inp, NULL, L, NULL, xpred, amse); 86 | printf("\n"); 87 | printf("Predicted Values : "); 88 | for (i = 0; i < L; ++i) { 89 | printf("%g ", xpred[i]); 90 | } 91 | printf("\n"); 92 | printf("Standard Errors : "); 93 | for (i = 0; i < L; ++i) { 94 | printf("%g ", sqrt(amse[i])); 95 | } 96 | printf("\n"); 97 | sarimax_free(obj); 98 | free(inp); 99 | free(phi); 100 | free(theta); 101 | free(xpred); 102 | free(amse); 103 | return 0; 104 | 105 | } 106 | -------------------------------------------------------------------------------- /data/seriesD.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 8 3 | 7.4 4 | 8 5 | 8 6 | 8 7 | 8 8 | 8.8 9 | 8.4 10 | 8.4 11 | 8 12 | 8.2 13 | 8.2 14 | 8.2 15 | 8.4 16 | 8.4 17 | 8.4 18 | 8.6 19 | 8.8 20 | 8.6 21 | 8.6 22 | 8.6 23 | 8.6 24 | 8.6 25 | 8.8 26 | 8.9 27 | 9.1 28 | 9.5 29 | 8.5 30 | 8.4 31 | 8.3 32 | 8.2 33 | 8.1 34 | 8.3 35 | 8.4 36 | 8.7 37 | 8.8 38 | 8.8 39 | 9.2 40 | 9.59999 41 | 8.99997 42 | 8.80002 43 | 8.60015 44 | 8.59998 45 | 8.7995 46 | 8.80056 47 | 8.59982 48 | 8.6 49 | 8.4 50 | 8.3 51 | 8.4 52 | 8.3 53 | 8.3 54 | 8.1 55 | 8.2 56 | 8.3 57 | 8.5 58 | 8.1 59 | 8.1 60 | 7.9 61 | 8.3 62 | 8.1 63 | 8.1 64 | 8.1 65 | 8.4 66 | 8.7 67 | 9 68 | 9.3 69 | 9.3 70 | 9.5 71 | 9.3 72 | 9.5 73 | 9.5 74 | 9.5 75 | 9.5 76 | 9.5 77 | 9.5 78 | 9.9 79 | 9.5 80 | 9.7 81 | 9.1 82 | 9.1 83 | 8.9 84 | 9.3 85 | 9.1 86 | 9.1 87 | 9.3 88 | 9.5 89 | 9.3 90 | 9.3 91 | 9.3 92 | 9.9 93 | 9.7 94 | 9.1 95 | 9.3 96 | 9.5 97 | 9.4 98 | 9 99 | 9 100 | 8.8 101 | 9 102 | 8.8 103 | 8.6 104 | 8.6 105 | 8 106 | 8 107 | 8 108 | 8 109 | 8.6 110 | 8 111 | 8 112 | 8 113 | 7.6 114 | 8.6 115 | 9.6 116 | 9.6 117 | 10 118 | 9.4 119 | 9.3 120 | 9.2 121 | 9.5 122 | 9.5 123 | 9.5 124 | 9.9 125 | 9.9 126 | 9.5 127 | 9.3 128 | 9.5 129 | 9.5 130 | 9.1 131 | 9.3 132 | 9.5 133 | 9.3 134 | 9.1 135 | 9.3 136 | 9.1 137 | 9.5 138 | 9.4 139 | 9.5 140 | 9.6 141 | 10.2 142 | 9.8 143 | 9.6 144 | 9.6 145 | 9.4 146 | 9.4 147 | 9.4 148 | 9.4 149 | 9.6 150 | 9.6 151 | 9.4 152 | 9.4 153 | 9 154 | 9.4 155 | 9.4 156 | 9.6 157 | 9.4 158 | 9.2 159 | 8.8 160 | 8.8 161 | 9.2 162 | 9.2 163 | 9.6 164 | 9.6 165 | 9.8 166 | 9.8 167 | 10 168 | 10 169 | 9.4 170 | 9.8 171 | 8.8 172 | 8.8 173 | 8.8 174 | 8.8 175 | 9.6 176 | 9.6 177 | 9.6 178 | 9.2 179 | 9.2 180 | 9 181 | 9 182 | 9 183 | 9.4 184 | 9 185 | 9 186 | 9.4 187 | 9.4 188 | 9.6 189 | 9.4 190 | 9.6 191 | 9.6 192 | 9.6 193 | 10 194 | 10 195 | 9.6 196 | 9.2 197 | 9.2 198 | 9.2 199 | 9 200 | 9 201 | 9.6 202 | 9.8 203 | 10.2 204 | 10 205 | 10 206 | 10 207 | 9.4 208 | 9.2 209 | 9.6 210 | 9.7 211 | 9.7 212 | 9.8 213 | 9.8 214 | 9.8 215 | 10 216 | 10 217 | 8.6 218 | 9 219 | 9.4 220 | 9.4 221 | 9.4 222 | 9.4 223 | 9.4 224 | 9.6 225 | 10 226 | 10 227 | 9.8 228 | 9.8 229 | 9.7 230 | 9.6 231 | 9.4 232 | 9.2 233 | 9 234 | 9.4 235 | 9.6 236 | 9.6 237 | 9.6 238 | 9.6 239 | 9.6 240 | 9.6 241 | 9 242 | 9.4 243 | 9.4 244 | 9.4 245 | 9.6 246 | 9.4 247 | 9.6 248 | 9.6 249 | 9.80002 250 | 9.8 251 | 9.79994 252 | 9.60007 253 | 9.19998 254 | 9.60001 255 | 9.20003 256 | 9.19997 257 | 9.59985 258 | 9.60002 259 | 9.60052 260 | 9.59942 261 | 9.60019 262 | 9.6 263 | 10 264 | 10 265 | 10.4 266 | 10.4 267 | 9.8 268 | 9 269 | 9.6 270 | 9.8 271 | 9.6 272 | 8.6 273 | 8 274 | 8 275 | 8 276 | 8 277 | 8.4 278 | 8.8 279 | 8.4 280 | 8.4 281 | 9 282 | 9 283 | 9.4 284 | 10 285 | 10 286 | 10 287 | 10.2 288 | 10 289 | 10 290 | 9.6 291 | 9 292 | 9 293 | 8.6 294 | 9 295 | 9.6 296 | 9.6 297 | 9 298 | 9 299 | 8.9 300 | 8.8 301 | 8.7 302 | 8.6 303 | 8.3 304 | 7.9 305 | 8.5 306 | 8.7 307 | 8.9 308 | 9.1 309 | 9.1 310 | 9.1 311 | -------------------------------------------------------------------------------- /test/sarimaxtest2.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | /* 7 | 8 | SARIMA example 9 | 10 | */ 11 | int main(void) { 12 | int i, N, d, D, L; 13 | double *inp; 14 | int p, q, P, Q, s, r; 15 | double *phi, *theta; 16 | double *PHI, *THETA; 17 | double *xpred, *amse; 18 | sarimax_object obj; 19 | int imean = 1; 20 | /* 21 | Make sure all the parameter values are correct and consistent with other values. eg., if xreg is NULL r should be 0 22 | or if P = D = Q = 0 then make sure that s is also 0. 23 | Recheck the values if the program fails to execute. 24 | */ 25 | p = 2; 26 | d = 1; 27 | q = 2; 28 | s = 12; 29 | P = 1; 30 | D = 1; 31 | Q = 1; 32 | r = 0; 33 | 34 | 35 | L = 5; 36 | 37 | phi = (double*)malloc(sizeof(double)* p); 38 | theta = (double*)malloc(sizeof(double)* q); 39 | PHI = (double*)malloc(sizeof(double)* P); 40 | THETA = (double*)malloc(sizeof(double)* Q); 41 | 42 | xpred = (double*)malloc(sizeof(double)* L); 43 | amse = (double*)malloc(sizeof(double)* L); 44 | 45 | FILE *ifp; 46 | double temp[1200]; 47 | 48 | ifp = fopen("../data/seriesG.txt", "r"); 49 | i = 0; 50 | if (!ifp) { 51 | printf("Cannot Open File"); 52 | exit(100); 53 | } 54 | while (!feof(ifp)) { 55 | fscanf(ifp, "%lf \n", &temp[i]); 56 | i++; 57 | } 58 | N = i; 59 | 60 | inp = (double*)malloc(sizeof(double)* N); 61 | 62 | /* 63 | 64 | */ 65 | 66 | for (i = 0; i < N; ++i) { 67 | inp[i] = log(temp[i]); 68 | } 69 | 70 | 71 | obj = sarimax_init(p, d, q, P, D, Q, s, r ,imean, N); 72 | 73 | /* setMethod() 74 | Method 0 ("CSS-MLE") is default. The method also accepts values 1 ("MLE") and 2 ("CSS") 75 | */ 76 | 77 | sarimax_setMethod(obj, 2); 78 | 79 | /*sarimax_exec(object, input time series, exogenous time series) 80 | set exogenous to NULL if deadling only with a univariate time series. 81 | */ 82 | sarimax_exec(obj, inp,NULL); 83 | sarimax_summary(obj); 84 | /* sarimax_predict(sarimax_object obj, double *inp, double *xreg, int L,double *newxreg, double *xpred, double *amse) 85 | inp - Input Time Series 86 | xreg - Exogenous Time Series 87 | L - L point prediction 88 | newxreg - Exogenous Time Series of dimension r * L where r is the number of exogenus time series and L is the length of each 89 | xpred - L future values 90 | amse - MSE for L future values 91 | */ 92 | sarimax_predict(obj, inp, NULL, L, NULL, xpred, amse); 93 | printf("\n"); 94 | printf("Predicted Values : "); 95 | for (i = 0; i < L; ++i) { 96 | printf("%g ", exp(xpred[i])); 97 | } 98 | printf("\n"); 99 | printf("Standard Errors : "); 100 | for (i = 0; i < L; ++i) { 101 | printf("%g ", sqrt(amse[i])); 102 | } 103 | printf("\n"); 104 | sarimax_free(obj); 105 | free(inp); 106 | free(phi); 107 | free(theta); 108 | free(PHI); 109 | free(THETA); 110 | free(xpred); 111 | free(amse); 112 | return 0; 113 | 114 | } 115 | -------------------------------------------------------------------------------- /src/newtonmin.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef NEWTONMIN_H_ 3 | #define NEWTONMIN_H_ 4 | 5 | #include "lnsrchmp.h" 6 | 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | double signx(double x); 13 | 14 | double cholmod(double *A, int N, double *L, double eps,double maxinp); 15 | 16 | double modelhess(double *A,int N,double *dx,double eps,double *L); 17 | 18 | void linsolve_lower(double *L,int N,double *b,double *x); 19 | 20 | int hessian_fd(custom_function *funcpt, double *x, int N, double *dx, double eps, double *f); 21 | 22 | int hessian_fd2(custom_function *funcpt,double *x,int N,double *dx,double eps,double *f); 23 | 24 | void fdjac(custom_gradient *funcgrad, double *x, int N, double *jac, double *dx, double eps2, double *J); 25 | 26 | void hessian_fdg(custom_gradient *funcgrad, double *x, int N, double *jac, double *dx, double eps2, double *H); 27 | 28 | int hessian_opt(custom_function *funcpt, custom_gradient *funcgrad, double *x, int N, double *jac, 29 | double *dx,double eps,double eps2,double *H); 30 | 31 | int lnsrch(custom_function *funcpt, double *xi, double *jac, double *p, int N, double * dx, double maxstep, double stol, double *x); 32 | 33 | int lnsrchmod(custom_function *funcpt, custom_gradient *funcgrad, double *xi, double *jac, double *p, int N, double * dx, double maxstep, 34 | double eps2,double stol,double *x,double *jacf); 35 | 36 | int lnsrchcg(custom_function *funcpt, custom_gradient *funcgrad, double *xi, double *jac, double *p, int N, double * dx, double maxstep, 37 | double eps2,double stol,double *x,double *jacf); 38 | 39 | int stopcheck(double fx,int N,double *xc,double *xf,double *jac,double *dx,double fsval,double gtol,double stol,int retval); 40 | 41 | int newton_min_func(custom_function *funcpt, custom_gradient *funcgrad, double *xi, int N, double *dx, double fsval, double maxstep, int MAXITER, 42 | int *niter, double eps, double gtol, double stol, double *xf); 43 | 44 | int trsrch(custom_function *funcpt, double *xi, double *jac, double *sN, int N, double * dx, double maxstep, 45 | int iter,double *L,double *hess,double stol,double *ioval,double eps,double *x); 46 | 47 | void trstep(double *jac,double *sN,int N,double * dx,double *L,double *hess,double nlen,double *ioval,double eps, 48 | double *step); 49 | 50 | int trupdate(custom_function *funcpt, double *xi, double *jac, double *step, int N, double * dx, double maxstep, 51 | int retcode,double *L,double *hess,double stol,int method,double *ioval,double *xprev,double *funcprev,double *x); 52 | 53 | void trstep_ddl(double *jac,double *sN,int N,double * dx,double maxstep,double *L,double *hess,double nlen,double *ioval, 54 | double *ssd,double *v,double *step); 55 | 56 | int trsrch_ddl(custom_function *funcpt, double *xi, double *jac, double *sN, int N, double * dx, double maxstep, 57 | int iter,double *L,double *hess,double stol,double *ioval,double *x); 58 | 59 | int newton_min_trust(custom_function *funcpt, custom_gradient *funcgrad, double *xi, int N, double *dx, double fsval, double delta, 60 | int method,int MAXITER,int *niter,double eps,double gtol,double stol,double *xf); 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | #endif /* NEWTONMIN_H_ */ 67 | -------------------------------------------------------------------------------- /test/sarimaxtest3.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include 3 | #include 4 | #include 5 | #include "../header/ctsa.h" 6 | /* 7 | 8 | ARIMA example with exogenous variables 9 | 10 | 11 | */ 12 | int main(void) { 13 | int i, N, d, D, L; 14 | double *inp; 15 | int p, q, P, Q, s, r; 16 | double *xpred, *amse,*xreg,*newxreg; 17 | sarimax_object obj; 18 | int imean = 1; 19 | /* 20 | Make sure all the parameter values are correct and consistent with other values. eg., if xreg is NULL r should be 0 21 | or if P = D = Q = 0 then make sure that s is also 0. 22 | Recheck the values if the program fails to execute. 23 | */ 24 | p = 2; 25 | d = 0; 26 | q = 2; 27 | s = 0; 28 | P = 0; 29 | D = 0; 30 | Q = 0; 31 | r = 2; 32 | 33 | 34 | L = 5; 35 | 36 | xpred = (double*)malloc(sizeof(double)* L); 37 | amse = (double*)malloc(sizeof(double)* L); 38 | 39 | FILE *ifp; 40 | double temp[1200]; 41 | double temp1[1200]; 42 | double temp2[1200]; 43 | 44 | ifp = fopen("../data/e1m.dat", "r"); 45 | i = 0; 46 | if (!ifp) { 47 | printf("Cannot Open File"); 48 | exit(100); 49 | } 50 | while (!feof(ifp)) { 51 | fscanf(ifp, "%lf %lf %lf \n", &temp[i],&temp1[i],&temp2[i]); 52 | i++; 53 | } 54 | N = i - L; 55 | 56 | inp = (double*)malloc(sizeof(double)* N); 57 | xreg = (double*)malloc(sizeof(double)* N * 2); 58 | newxreg = (double*)malloc(sizeof(double)* L * 2); 59 | 60 | /* 61 | 62 | */ 63 | 64 | for (i = 0; i < N; ++i) { 65 | inp[i] = temp[i]; 66 | xreg[i] = temp1[i]; 67 | xreg[N+i] = temp2[i]; 68 | } 69 | 70 | for(i = 0; i < L;++i) { 71 | newxreg[i] = temp1[N + i]; 72 | newxreg[i+L] = temp2[N + i]; 73 | } 74 | 75 | obj = sarimax_init(p, d, q, P, D, Q, s, r ,imean, N); 76 | 77 | /* setMethod() 78 | Method 0 ("CSS-MLE") is default. The method also accepts values 1 ("MLE") and 2 ("CSS") 79 | */ 80 | 81 | sarimax_setMethod(obj, 2); 82 | 83 | /*sarimax_exec(object, input time series, exogenous time series) 84 | set exogenous to NULL if deadling only with a univariate time series. 85 | */ 86 | sarimax_exec(obj, inp,xreg); 87 | sarimax_summary(obj); 88 | /* sarimax_predict(sarimax_object obj, double *inp, double *xreg, int L,double *newxreg, double *xpred, double *amse) 89 | inp - Input Time Series 90 | xreg - Exogenous Time Series 91 | L - L point prediction 92 | newxreg - Exogenous Time Series of dimension r * L where r is the number of exogenus time series and L is the length of each 93 | xpred - L future values 94 | amse - MSE for L future values 95 | */ 96 | sarimax_predict(obj, inp, xreg, L, newxreg, xpred, amse); 97 | printf("\n"); 98 | printf("Predicted Values : "); 99 | for (i = 0; i < L; ++i) { 100 | printf("%g ", xpred[i]); 101 | } 102 | printf("\n"); 103 | printf("Standard Errors : "); 104 | for (i = 0; i < L; ++i) { 105 | printf("%g ", sqrt(amse[i])); 106 | } 107 | printf("\n"); 108 | 109 | sarimax_free(obj); 110 | free(inp); 111 | free(xpred); 112 | free(amse); 113 | free(xreg); 114 | free(newxreg); 115 | return 0; 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/errors.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include "errors.h" 3 | 4 | static void pe(double *predicted, double *actual, int N,double *err) { 5 | double temp; 6 | int i; 7 | 8 | 9 | for(i = 0; i < N;++i) { 10 | temp = (actual[i] - predicted[i]); 11 | 12 | err[i] = temp * 100 / actual[i]; 13 | } 14 | 15 | } 16 | 17 | double me(double *predicted, double *actual, int N) { 18 | double err,temp; 19 | int i; 20 | 21 | temp = 0.0; 22 | 23 | for(i = 0; i < N;++i) { 24 | temp += (actual[i] - predicted[i]); 25 | } 26 | 27 | err = temp / N; 28 | 29 | return err; 30 | } 31 | 32 | double mse(double *predicted, double *actual, int N) { 33 | double err,temp; 34 | int i; 35 | 36 | temp = 0.0; 37 | 38 | for(i = 0; i < N;++i) { 39 | temp += ((actual[i] - predicted[i])*(actual[i] - predicted[i])); 40 | } 41 | 42 | err = temp / N; 43 | 44 | return err; 45 | } 46 | 47 | double rmse(double *predicted, double *actual, int N) { 48 | double err; 49 | 50 | err = mse(predicted,actual,N); 51 | 52 | err = sqrt(err); 53 | 54 | return err; 55 | } 56 | 57 | double mae(double *predicted, double *actual, int N) { 58 | double err,temp,t; 59 | int i; 60 | 61 | temp = 0.0; 62 | 63 | for(i = 0; i < N;++i) { 64 | t = fabs(actual[i] - predicted[i]); 65 | temp += t; 66 | } 67 | 68 | err = temp / N; 69 | 70 | return err; 71 | } 72 | 73 | double mape(double *predicted, double *actual, int N) { 74 | double err,temp; 75 | double *errvec; 76 | int i,j; 77 | 78 | errvec = (double*) malloc(sizeof(double)*N); 79 | 80 | pe(predicted,actual,N,errvec); 81 | j = 0; 82 | temp = 0.0; 83 | for(i = 0; i < N;++i) { 84 | if (errvec[i] == errvec[i]) { 85 | j++; 86 | temp += fabs(errvec[i]); 87 | } 88 | } 89 | 90 | err = temp/j; 91 | 92 | free(errvec); 93 | return err; 94 | } 95 | 96 | double mpe(double *predicted, double *actual, int N) { 97 | double err,temp; 98 | double *errvec; 99 | int i,j; 100 | 101 | errvec = (double*) malloc(sizeof(double)*N); 102 | 103 | pe(predicted,actual,N,errvec); 104 | j = 0; 105 | temp = 0.0; 106 | for(i = 0; i < N;++i) { 107 | if (errvec[i] == errvec[i]) { 108 | j++; 109 | temp += errvec[i]; 110 | } 111 | } 112 | 113 | err = temp/j; 114 | 115 | free(errvec); 116 | return err; 117 | } 118 | 119 | double mase(double *predicted, double *actual, int N, double *tseries, int length) { 120 | double err,temp,den; 121 | double *dif; 122 | int i; 123 | 124 | // tseries : Only training values are included 125 | 126 | dif = (double*)malloc(sizeof(double)* (N-1)); 127 | 128 | temp = 0.0; 129 | 130 | for(i = 1; i < length;++i) { 131 | temp += fabs(tseries[i] - tseries[i-1]); 132 | } 133 | 134 | den = temp / (length-1); 135 | 136 | temp = 0.0; 137 | 138 | for(i = 0; i < N;++i) { 139 | temp += fabs(actual[i] - predicted[i]); 140 | } 141 | 142 | err = temp /(N * den); 143 | 144 | 145 | return err; 146 | } -------------------------------------------------------------------------------- /src/lnsrchmp.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef LNSRCHMP_H_ 3 | #define LNSRCHMP_H_ 4 | 5 | #include "matrix.h" 6 | 7 | #define EPSILON 2.7182818284590452353602874713526624977572 8 | 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | 13 | typedef struct custom_function_set custom_function; 14 | 15 | struct custom_function_set{ 16 | double(*funcpt) (double *x,int N,void *params);// Function in N variables 17 | void *params; 18 | }; 19 | 20 | typedef struct custom_gradient_set custom_gradient; 21 | 22 | struct custom_gradient_set{ 23 | void(*funcgrad) (double *x, int N,double *g, void *params); 24 | void *params; 25 | }; 26 | 27 | typedef struct custom_funcuni_set custom_funcuni; 28 | 29 | struct custom_funcuni_set{ 30 | double(*funcuni) (double x, void *params);// Function in one variable 31 | void *params; 32 | }; 33 | 34 | typedef struct custom_funcmult_set custom_funcmult; 35 | 36 | struct custom_funcmult_set{ 37 | void(*funcmult) (double *x,int M, int N, double *f, void *params);// M Functions in N variables 38 | void *params; 39 | }; 40 | 41 | typedef struct custom_jacobian_set custom_jacobian; 42 | 43 | struct custom_jacobian_set{ 44 | void(*jacobian) (double *x, int M, int N, double *jac, void *params); 45 | void *params; 46 | }; 47 | 48 | #define FUNCPT_EVAL(F,x,N) (*((F)->funcpt))(x,N,(F)->params) 49 | 50 | #define FUNCGRAD_EVAL(F,x,N,g) (*((F)->funcgrad))(x,N,(g),(F)->params) 51 | 52 | #define FUNCUNI_EVAL(F,x) (*((F)->funcuni))(x,(F)->params) 53 | 54 | #define FUNCMULT_EVAL(F,x,M,N,f) (*((F)->funcmult))(x,M,N,(f),(F)->params) 55 | 56 | #define JACOBIAN_EVAL(F,x,M,N,jac) (*((F)->jacobian))(x,M,N,(jac),(F)->params) 57 | 58 | int stopcheck_mt(double fx, int N, double *xc, double *xf, double *jac, double *dx, double fsval, double gtol, double stol, int retval); 59 | 60 | int stopcheck2_mt(double fx, int N, double fo, double *jac, double *dx, double eps,double stoptol, double functol, int retval); 61 | 62 | int stopcheck3_mt(double *xi,double *xf,double fx, int N, double fo, double *jac, double *dx, double eps, 63 | double stoptol, double functol, int retval); 64 | 65 | int grad_fd(custom_function *funcpt,custom_gradient *funcgrad, double *x, int N, double *dx, double eps2, double *f); 66 | 67 | int grad_cd(custom_function *funcpt, custom_gradient *funcgrad, double *x, int N, double *dx, 68 | double eps3, double *f); 69 | 70 | int grad_calc2(custom_function *funcpt, double *x, int N, double *dx, double eps3, double *f); 71 | 72 | int grad_calc(custom_function *funcpt, double *x, int N, double *dx, double eps2, double *f); 73 | 74 | int cstep(double *stx, double *fx, double *dx, double *sty, double *fy, double *dy, double *stp, double *fp, double *dp, int *brackt, 75 | double stpmin, double stpmax); 76 | 77 | int cvsrch(custom_function *funcpt, custom_gradient *funcgrad, double *x, double *f, double *g, double *stp, double *s, int N, double *dx, double maxstep, 78 | int MAXITER,double eps2,double ftol, double gtol, double xtol); 79 | 80 | int lnsrchmt(custom_function *funcpt, custom_gradient *funcgrad, double *xi, double *f, double *jac, double *alpha, double *p, int N, double *dx, double maxstep, int MAXITER, 81 | double eps2,double ftol, double gtol, double xtol, double *x); 82 | 83 | #ifdef __cplusplus 84 | } 85 | #endif 86 | 87 | #endif /* LNSRCHMP_H_ */ 88 | -------------------------------------------------------------------------------- /data/e6.dat: -------------------------------------------------------------------------------- 1 | -0.00313258 0.083 2 | 0.0188713 0.083 3 | 0.0248036 0.087 4 | 0.0162776 0.087 5 | 2.89679E-4 0.102 6 | 0.016829 0.098 7 | 0.0385835 0.097 8 | -0.00144386 0.107 9 | 0.0127239 0.109 10 | 0.0238972 0.108 11 | 0.0427728 0.099 12 | -0.0103779 0.089 13 | 0.00558424 0.084 14 | 0.00870609 0.087 15 | 0.0389199 0.086 16 | -0.0196176 0.078 17 | 0.00731993 0.083 18 | 0.0180764 0.081 19 | 0.0237775 0.074 20 | -0.0144377 0.07 21 | 0.0110059 0.064 22 | 0.0104017 0.06 23 | 0.0354648 0.06 24 | -0.0157599 0.056 25 | 0.0100193 0.06 26 | 0.0180621 0.064 27 | 0.0256915 0.066 28 | -0.0170689 0.071 29 | 0.00350475 0.08 30 | 0.0256438 0.078 31 | 0.0315075 0.08 32 | -0.0132775 0.095 33 | 0.0130968 0.083 34 | 0.0151458 0.083 35 | 0.0312119 0.091 36 | -0.0207896 0.104 37 | 0.0104489 0.111 38 | 0.018106 0.113 39 | 0.0405002 0.099 40 | -0.0201483 0.096 41 | 0.00380802 0.092 42 | 0.0215569 0.088 43 | 0.0327106 0.08 44 | -0.0201726 0.074 45 | -0.00333166 0.081 46 | 0.0192871 0.084 47 | 0.0345473 0.083 48 | -0.0262156 0.079 49 | -0.00519753 0.081 50 | 0.0132318 0.077 51 | 0.0374279 0.07 52 | -0.0291948 0.077 53 | -0.00261354 0.07 54 | 0.0172353 0.064 55 | 0.0376387 0.066 56 | -0.0204763 0.06 57 | 9.00269E-4 0.06 58 | 0.0125732 0.058 59 | 0.0344677 0.06 60 | -0.0220394 0.056 61 | -0.00202703 0.055 62 | 0.00173426 0.062 63 | 0.0365901 0.058 64 | -0.0252557 0.056 65 | 9.2268E-4 0.06 66 | 0.00451374 0.063 67 | 0.0383272 0.062 68 | -0.0211368 0.07 69 | -5.33104E-4 0.071 70 | 0.00981092 0.071 71 | 0.0373549 0.078 72 | -0.016232 0.09 73 | 0.00197124 0.09 74 | 0.00687361 0.091 75 | 0.0274582 0.09 76 | -0.0140786 0.086 77 | 0.0166497 0.086 78 | 0.0144682 0.088 79 | 0.043004 0.087 80 | -0.0127769 0.082 81 | 0.00927353 0.084 82 | 0.0179157 0.082 83 | 0.030508 0.074 84 | -0.0105906 0.065 85 | 0.00747108 0.067 86 | 0.00467587 0.061 87 | 0.0310678 0.056 88 | -0.0153685 0.062 89 | 8.10146E-4 0.069 90 | 0.00806475 0.074 91 | 0.0287657 0.074 92 | -0.0183783 0.071 93 | 0.00449467 0.064 94 | 0.00988674 0.061 95 | 0.0245948 0.055 96 | -0.0185189 0.058 97 | -0.00590181 0.059 98 | 0.00581503 0.055 99 | 0.0243654 0.051 100 | -0.0157485 0.051 101 | -0.00749254 0.05 102 | 0.00388288 0.051 103 | 0.0242448 0.051 104 | -0.014647 0.047 105 | -0.00204897 0.047 106 | 0.00247526 0.041 107 | 0.0239234 0.038 108 | -------------------------------------------------------------------------------- /src/boxcox.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #include "boxcox.h" 3 | 4 | int checkConstant(double *x, int N) { 5 | int i, cc; 6 | double diff; 7 | cc = 0; 8 | 9 | /// heap buffer overflow fix, since accessing x[i+1] need to 10 | /// stop loop at -1 index from end of array as dictated by length defined by N 11 | for(i = 1; i < N - 1;++i) { 12 | diff = x[i+1] - x[i]; 13 | if (fabs(diff) > 0) { 14 | return cc; 15 | } 16 | } 17 | 18 | cc = 1; 19 | 20 | return cc; 21 | } 22 | 23 | static int checkNegative(double *x, int N) { 24 | int i,cn; 25 | cn = 1; 26 | 27 | for(i = 0; i < N;++i) { 28 | if (x[i] < 0) { 29 | return cn; 30 | } 31 | } 32 | 33 | cn = 0; 34 | 35 | return cn; 36 | } 37 | 38 | void boxcox_eval(double *x, int N, double lambda,double *bxcx) { 39 | double eps; 40 | int i; 41 | 42 | eps = macheps() / 744.44; 43 | 44 | if (fabs(lambda) < eps) { 45 | for(i = 0; i < N;++i) { 46 | bxcx[i] = log(x[i]); 47 | } 48 | } else { 49 | for(i = 0; i < N;++i) { 50 | bxcx[i] = expm1(lambda * log(x[i])) / lambda; 51 | } 52 | } 53 | 54 | } 55 | 56 | double inv_boxcox_eval(double *x,int N, double lambda,double *bxcx) { 57 | int i; 58 | 59 | if (lambda == 0) { 60 | for(i = 0; i < N;++i) { 61 | bxcx[i] = exp(x[i]); 62 | } 63 | } else { 64 | for(i = 0; i < N;++i) { 65 | bxcx[i] = exp(log1p(lambda * x[i])/lambda); 66 | } 67 | } 68 | } 69 | 70 | double boxcox_loglik(double lambda, void *params) { 71 | double loglik,variance,sum; 72 | int N,i; 73 | double *tdata; 74 | double *xpar = (double*) params; 75 | 76 | N = (int) xpar[0]; 77 | tdata = (double*) malloc(sizeof(double) *N); 78 | 79 | sum = 0.0; 80 | for(i = 0; i < N;++i) { 81 | tdata[i] = log(xpar[i+1]); 82 | sum += tdata[i]; 83 | } 84 | 85 | if (lambda == 0) { 86 | variance = var(tdata,N); 87 | } else { 88 | for(i = 0; i < N;++i) { 89 | tdata[i] = pow(xpar[i+1],lambda) / lambda; 90 | } 91 | variance = var(tdata,N); 92 | } 93 | 94 | loglik = (lambda-1)*sum - (xpar[0]/2.0)*log(variance); 95 | 96 | free(tdata); 97 | return -loglik; 98 | } 99 | 100 | double boxcox(double *x, int N,double *lambda,double *y) { 101 | double *params; 102 | double lmbd,a,b; 103 | int cc,cn; 104 | 105 | // Check if x has all constant values 106 | cc = checkConstant(x,N); 107 | 108 | if (cc == 1) { 109 | printf("Input is a constant vector. Exiting. \n"); 110 | exit(-1); 111 | } 112 | 113 | // Check for negative values 114 | 115 | if (cn == 1) { 116 | printf("Input vector cannot have negative values. Exiting. \n"); 117 | exit(-1); 118 | } 119 | 120 | params = (double*) malloc(sizeof(double)*(N+1)); 121 | 122 | params[0] = (double) N; 123 | memcpy(params+1,x,sizeof(double)*N); 124 | 125 | custom_funcuni boxcox_mle = {boxcox_loglik,params}; 126 | 127 | if (lambda == NULL) { 128 | a = -2; 129 | b = 2; 130 | lmbd = fminbnd(&boxcox_mle,a,b); 131 | } else { 132 | lmbd = *lambda; 133 | } 134 | 135 | boxcox_eval(x,N,lmbd,y); 136 | 137 | free(params); 138 | return lmbd; 139 | } -------------------------------------------------------------------------------- /test/sarimatest_mu.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /** 3 | * Adapting to minunit test library 4 | * Started on Fri Jan 17 11:57:23 2025 5 | * From: `sarimatest.c` 6 | **/ 7 | #include 8 | #include 9 | #include 10 | #include "../header/ctsa.h" 11 | 12 | #include "minunit.h" /* Test support macros */ 13 | 14 | struct expected_results { 15 | double aic; 16 | double log_likelihood; 17 | double sigma_square; 18 | }; 19 | 20 | struct singleton_like { 21 | double tolerance; 22 | double *inp; 23 | double *xpred; 24 | double *amse; 25 | sarima_object obj; 26 | struct expected_results expected; 27 | }; 28 | 29 | struct singleton_like globals = {0}; 30 | 31 | void test_setup(void) { 32 | int i; 33 | int N; 34 | int p; 35 | int q; 36 | int d; 37 | int L; 38 | int P; 39 | int D; 40 | int Q; 41 | int s; 42 | 43 | FILE *ifp; 44 | double temp[1200]; 45 | 46 | p = 0; 47 | d = 1; 48 | q = 1; 49 | P = 0; 50 | D = 1; 51 | Q = 1; 52 | s = 12; 53 | L = 5; 54 | 55 | globals.xpred = (double*)(double*)calloc(sizeof(double), L); 56 | globals.amse = (double*)(double*)calloc(sizeof(double), L); 57 | 58 | ifp = fopen("../data/seriesG.txt", "r"); 59 | i = 0; 60 | if (!ifp) { 61 | printf("Cannot Open File"); 62 | exit(100); 63 | } 64 | while (!feof(ifp)) { 65 | fscanf(ifp, "%lf \n", &temp[i]); 66 | i++; 67 | } 68 | N = i; 69 | 70 | fclose(ifp); 71 | 72 | globals.inp = (double*)calloc(sizeof(double), N); 73 | 74 | for (i = 0; i < N; ++i) { 75 | globals.inp[i] = log(temp[i]); 76 | //printf("%g \n",inp[i]); 77 | } 78 | 79 | globals.obj = sarima_init(p, d, q, s, P, D, Q, N); 80 | 81 | sarima_setMethod(globals.obj, 0); 82 | /** 83 | * Method 0 ("MLE") is default so this step is unnecessary. The 84 | * method also accepts values 1 ("CSS") and 2 ("Box-Jenkins") 85 | **/ 86 | 87 | //sarima_setOptMethod(globals.obj, 7); 88 | /** 89 | * Method 7 ("BFGS with More Thuente Line Search") is default so 90 | * this step is unnecessary. The method also accepts values 91 | * 0,1,2,3,4,5,6. Check the documentation for details. 92 | **/ 93 | 94 | /** Execute the fitting of the SARIMA model */ 95 | sarima_exec(globals.obj, globals.inp); 96 | 97 | /** Set tolerance */ 98 | globals.tolerance = 0.01; 99 | 100 | /** Expected results assignment */ 101 | globals.expected.aic = -483.145; 102 | globals.expected.log_likelihood = 244.572; 103 | globals.expected.sigma_square = 0.00135071; 104 | } 105 | 106 | void test_teardown(void) { 107 | sarima_free(globals.obj); 108 | 109 | free(globals.inp); 110 | free(globals.xpred); 111 | free(globals.amse); 112 | } 113 | 114 | MU_TEST(test_assert_var) { 115 | sarima_summary(globals.obj); 116 | 117 | mu_assert_double_tol(globals.expected.sigma_square, 118 | globals.obj->var, 119 | globals.tolerance); 120 | } 121 | 122 | MU_TEST(test_assert_loglik) { 123 | mu_assert_double_tol(globals.expected.log_likelihood, 124 | globals.obj->loglik, 125 | globals.tolerance); 126 | } 127 | 128 | MU_TEST(test_assert_aic) { 129 | mu_assert_double_eq(globals.expected.aic, globals.obj->aic); 130 | } 131 | 132 | MU_TEST_SUITE(test_suite) { 133 | MU_SUITE_CONFIGURE(&test_setup, &test_teardown); 134 | 135 | MU_RUN_TEST(test_assert_var); 136 | MU_RUN_TEST(test_assert_loglik); 137 | MU_RUN_TEST(test_assert_aic); 138 | } 139 | 140 | int main(void) { 141 | MU_RUN_SUITE(test_suite); 142 | MU_REPORT(); 143 | return MU_EXIT_CODE; 144 | } 145 | -------------------------------------------------------------------------------- /data/seriesB.txt: -------------------------------------------------------------------------------- 1 | 460 2 | 457 3 | 452 4 | 459 5 | 462 6 | 459 7 | 463 8 | 479 9 | 493 10 | 490 11 | 492 12 | 498 13 | 499 14 | 497 15 | 496 16 | 490 17 | 489 18 | 478 19 | 487 20 | 491 21 | 487 22 | 482 23 | 479 24 | 478 25 | 479 26 | 477 27 | 479 28 | 475 29 | 479 30 | 476 31 | 476 32 | 478 33 | 479 34 | 477 35 | 476 36 | 475 37 | 475 38 | 473 39 | 474 40 | 474 41 | 474 42 | 465 43 | 466 44 | 467 45 | 471 46 | 471 47 | 467 48 | 473 49 | 481 50 | 488 51 | 490 52 | 489 53 | 489 54 | 485 55 | 491 56 | 492 57 | 494 58 | 499 59 | 498 60 | 500 61 | 497 62 | 494 63 | 495 64 | 500 65 | 504 66 | 513 67 | 511 68 | 514 69 | 510 70 | 509 71 | 515 72 | 519 73 | 523 74 | 519 75 | 523 76 | 531 77 | 547 78 | 551 79 | 547 80 | 541 81 | 545 82 | 549 83 | 545 84 | 549 85 | 547 86 | 543 87 | 540 88 | 539 89 | 532 90 | 517 91 | 527 92 | 540 93 | 542 94 | 538 95 | 541 96 | 541 97 | 547 98 | 553 99 | 559 100 | 557 101 | 557 102 | 560 103 | 571.001 104 | 570.999 105 | 568.996 106 | 575.001 107 | 580.013 108 | 583.985 109 | 585.005 110 | 590 111 | 599 112 | 603 113 | 599 114 | 596 115 | 585 116 | 587 117 | 585 118 | 581 119 | 583 120 | 592 121 | 592 122 | 596 123 | 596 124 | 595 125 | 598 126 | 598 127 | 595 128 | 595 129 | 592 130 | 588 131 | 582 132 | 576 133 | 578 134 | 589 135 | 585 136 | 580 137 | 579 138 | 584 139 | 581 140 | 581 141 | 577 142 | 577 143 | 578 144 | 580 145 | 586 146 | 583 147 | 581 148 | 576 149 | 571 150 | 575 151 | 575 152 | 573 153 | 577 154 | 582 155 | 584 156 | 579 157 | 572 158 | 577 159 | 571 160 | 560 161 | 549 162 | 556 163 | 557 164 | 563 165 | 564 166 | 567 167 | 561 168 | 559 169 | 553 170 | 553 171 | 553 172 | 547 173 | 550 174 | 544 175 | 541 176 | 532 177 | 525 178 | 542.001 179 | 555.001 180 | 558.001 181 | 550.999 182 | 550.996 183 | 551.993 184 | 552.994 185 | 557.001 186 | 557.01 187 | 548.024 188 | 547.012 189 | 544.972 190 | 544.973 191 | 539.011 192 | 539.016 193 | 534.998 194 | 536.996 195 | 535.001 196 | 536 197 | 537 198 | 543 199 | 548 200 | 546 201 | 547 202 | 548 203 | 549 204 | 553 205 | 553 206 | 552 207 | 551 208 | 550 209 | 553 210 | 554 211 | 551 212 | 551 213 | 545 214 | 547 215 | 547 216 | 537 217 | 539 218 | 538 219 | 533 220 | 525 221 | 513 222 | 510 223 | 521 224 | 521 225 | 521 226 | 523 227 | 516 228 | 511 229 | 518 230 | 517 231 | 520 232 | 519 233 | 519 234 | 519 235 | 518 236 | 513 237 | 499 238 | 485 239 | 454 240 | 462 241 | 473 242 | 482 243 | 486 244 | 475 245 | 459 246 | 451 247 | 453 248 | 446 249 | 455 250 | 452 251 | 457 252 | 449 253 | 450 254 | 435 255 | 415 256 | 398 257 | 399 258 | 361 259 | 383 260 | 393 261 | 385 262 | 360 263 | 364 264 | 365 265 | 370 266 | 374 267 | 359 268 | 335 269 | 323 270 | 306 271 | 333 272 | 330 273 | 336 274 | 328 275 | 316 276 | 320 277 | 332 278 | 320 279 | 333 280 | 344 281 | 339 282 | 350 283 | 351 284 | 350 285 | 345 286 | 350 287 | 359 288 | 375 289 | 379 290 | 376 291 | 382 292 | 370 293 | 365 294 | 367 295 | 372 296 | 372.999 297 | 362.997 298 | 371.002 299 | 369.015 300 | 375.998 301 | 386.949 302 | 387.058 303 | 375.981 304 | 385 305 | 385 306 | 380 307 | 373 308 | 382 309 | 377 310 | 376 311 | 379 312 | 386 313 | 387 314 | 386 315 | 389 316 | 394 317 | 393 318 | 409 319 | 411 320 | 409 321 | 408 322 | 393 323 | 391 324 | 388 325 | 396 326 | 387 327 | 383 328 | 388 329 | 382 330 | 384 331 | 382 332 | 383 333 | 383 334 | 388 335 | 395 336 | 392 337 | 386 338 | 383 339 | 377 340 | 364 341 | 369 342 | 355 343 | 350 344 | 353 345 | 340 346 | 350 347 | 349 348 | 358 349 | 360 350 | 360 351 | 366 352 | 358.999 353 | 355.998 354 | 355.002 355 | 367.011 356 | 356.998 357 | 360.962 358 | 355.043 359 | 347.986 360 | 343 361 | 330 362 | 340 363 | 339 364 | 331 365 | 345 366 | 352 367 | 346 368 | 352 369 | 357 370 | -------------------------------------------------------------------------------- /data/eusm.txt: -------------------------------------------------------------------------------- 1 | 1628.75 2 | 1613.63 3 | 1606.51 4 | 1621.04 5 | 1618.16 6 | 1610.61 7 | 1630.75 8 | 1640.17 9 | 1635.47 10 | 1645.89 11 | 1647.84 12 | 1638.35 13 | 1629.93 14 | 1621.49 15 | 1624.74 16 | 1627.63 17 | 1631.99 18 | 1621.18 19 | 1613.42 20 | 1604.95 21 | 1605.75 22 | 1616.67 23 | 1619.29 24 | 1620.49 25 | 1619.67 26 | 1623.07 27 | 1613.98 28 | 1631.87 29 | 1630.37 30 | 1633.47 31 | 1626.55 32 | 1650.43 33 | 1650.06 34 | 1654.11 35 | 1653.6 36 | 1501.82 37 | 1524.28 38 | 1603.65 39 | 1622.49 40 | 1636.68 41 | 1652.1 42 | 1645.81 43 | 1650.36 44 | 1651.55 45 | 1649.88 46 | 1653.52 47 | 1657.51 48 | 1649.55 49 | 1649.09 50 | 1646.41 51 | 1638.65 52 | 1625.8 53 | 1628.64 54 | 1632.22 55 | 1633.65 56 | 1631.17 57 | 1635.8 58 | 1621.27 59 | 1624.7 60 | 1616.13 61 | 1618.12 62 | 1627.8 63 | 1625.79 64 | 1614.8 65 | 1612.8 66 | 1605.47 67 | 1609.32 68 | 1607.48 69 | 1607.48 70 | 1604.89 71 | 1589.12 72 | 1582.27 73 | 1567.99 74 | 1568.16 75 | 1569.71 76 | 1571.74 77 | 1585.41 78 | 1570.01 79 | 1561.89 80 | 1565.18 81 | 1570.34 82 | 1577 83 | 1590.29 84 | 1572.72 85 | 1572.07 86 | 1579.19 87 | 1588.73 88 | 1586.01 89 | 1579.77 90 | 1572.58 91 | 1568.09 92 | 1578.21 93 | 1573.94 94 | 1582.06 95 | 1610.18 96 | 1605.16 97 | 1623.84 98 | 1615.26 99 | 1627.08 100 | 1626.97 101 | 1605.7 102 | 1589.7 103 | 1589.7 104 | 1603.26 105 | 1599.75 106 | 1590.86 107 | 1603.5 108 | 1589.86 109 | 1587.92 110 | 1571.06 111 | 1549.81 112 | 1549.36 113 | 1554.65 114 | 1557.52 115 | 1555.31 116 | 1559.76 117 | 1548.44 118 | 1543.99 119 | 1550.21 120 | 1557.03 121 | 1551.78 122 | 1562.89 123 | 1570.28 124 | 1559.26 125 | 1545.87 126 | 1542.77 127 | 1542.77 128 | 1542.77 129 | 1542.77 130 | 1564.27 131 | 1577.26 132 | 1577.26 133 | 1577.26 134 | 1598.19 135 | 1604.05 136 | 1604.69 137 | 1593.65 138 | 1581.68 139 | 1599.14 140 | 1613.82 141 | 1620.45 142 | 1629.51 143 | 1663.7 144 | 1664.09 145 | 1669.29 146 | 1685.14 147 | 1687.07 148 | 1680.13 149 | 1671.84 150 | 1669.52 151 | 1686.71 152 | 1685.51 153 | 1671.01 154 | 1683.06 155 | 1685.7 156 | 1685.66 157 | 1678.77 158 | 1685.85 159 | 1683.71 160 | 1686.59 161 | 1683.73 162 | 1679.14 163 | 1685.03 164 | 1680.81 165 | 1676.17 166 | 1688.46 167 | 1696.55 168 | 1690.24 169 | 1711.35 170 | 1711.29 171 | 1729.86 172 | 1716.63 173 | 1743.36 174 | 1745.17 175 | 1746.76 176 | 1749.29 177 | 1763.86 178 | 1762.27 179 | 1762.29 180 | 1746.77 181 | 1753.5 182 | 1753.21 183 | 1739.88 184 | 1723.92 185 | 1734.42 186 | 1723.13 187 | 1732.92 188 | 1729.89 189 | 1725.74 190 | 1730.9 191 | 1714.17 192 | 1716.2 193 | 1719.06 194 | 1718.21 195 | 1698.84 196 | 1714.76 197 | 1718.35 198 | 1706.69 199 | 1723.37 200 | 1716.18 201 | 1738.78 202 | 1737.41 203 | 1714.77 204 | 1724.24 205 | 1733.77 206 | 1729.96 207 | 1734.46 208 | 1744.35 209 | 1746.88 210 | 1746.88 211 | 1746.88 212 | 1747.47 213 | 1753.1 214 | 1745.17 215 | 1745.72 216 | 1742.92 217 | 1731.68 218 | 1731.18 219 | 1728.09 220 | 1728.09 221 | 1731.29 222 | 1733.82 223 | 1745.78 224 | 1752.57 225 | 1748.13 226 | 1750.7 227 | 1747.91 228 | 1745.79 229 | 1735.34 230 | 1719.92 231 | 1763.59 232 | 1766.76 233 | 1785.4 234 | 1783.56 235 | 1804.42 236 | 1812.33 237 | 1799.51 238 | 1792.8 239 | 1792.8 240 | 1806.36 241 | 1798.23 242 | 1800.62 243 | 1786.19 244 | 1791.35 245 | 1789.05 246 | 1789.05 247 | 1784.71 248 | 1789.45 249 | 1779.74 250 | 1786.97 251 | 1773.25 252 | 1781.62 253 | 1773.75 254 | 1773.75 255 | 1776.34 256 | 1770.72 257 | 1772.39 258 | 1762.55 259 | 1764.35 260 | 1752.83 261 | 1755.98 262 | 1754.95 263 | 1759.9 264 | 1759.84 265 | 1776.5 266 | 1769.98 267 | 1766.98 268 | 1752.29 269 | 1760.17 270 | 1750.32 271 | 1731.44 272 | 1735.51 273 | 1733.84 274 | 1730.78 275 | 1699.46 276 | 1652.71 277 | 1654.09 278 | 1636.81 279 | 1622.81 280 | 1613.36 281 | 1617.78 282 | 1617.18 283 | 1637.62 284 | 1622.2 285 | 1608.49 286 | 1605.11 287 | 1609.61 288 | 1624.94 289 | 1618.07 290 | 1611.96 291 | 1578.95 292 | 1561.39 293 | 1547.87 294 | 1548.63 295 | 1560.16 296 | 1554.76 297 | 1531.87 298 | 1526.14 299 | 1509.03 300 | 1530.03 301 | -------------------------------------------------------------------------------- /src/pred.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * pred.c 4 | * 5 | * Created on: Jul 14, 2014 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | 10 | #include "pred.h" 11 | 12 | void predictarima(double *inp,int leninp,int p,int d, int q,double *phi, double *theta, 13 | double constant,int forlength,double *oup) { 14 | int lenzt,i,count,c2,K,L,lentheta,M,inilen,counter2,atlen2,ztc,atc,it; 15 | double *zt,*phi1,*theta1,*coeff,*coeff2,*ztl,*at,*delta,*sum1,*sum2; 16 | double temp,temp2; 17 | 18 | lenzt = leninp - d; 19 | 20 | zt = (double*) malloc(sizeof(double) * lenzt); 21 | phi1 = (double*) malloc(sizeof(double) * (p+1)); 22 | theta1 = (double*) malloc(sizeof(double) * (q+1)); 23 | delta = (double*) malloc(sizeof(double) * (d+1)); 24 | at = (double*) malloc(sizeof(double) * (q+1+lenzt)); 25 | ztl = (double*) malloc(sizeof(double) * (lenzt+forlength)); 26 | 27 | 28 | if (d > 0) { 29 | lenzt = diff(inp,leninp,d,zt); 30 | } else { 31 | lenzt = leninp; 32 | for(i = 0; i < lenzt;++i) { 33 | zt[i] = inp[i]; 34 | } 35 | } 36 | 37 | deld(d,delta); 38 | K = p+1; 39 | L = p + d + 1; 40 | lentheta = q + 1; 41 | 42 | sum1 = (double*) malloc(sizeof(double) * (d+1)); 43 | sum2 = (double*) malloc(sizeof(double) * L); 44 | 45 | phi1[0] = theta1[0] = 1.0; 46 | 47 | for(i = 0; i < p;++i) { 48 | phi1[i+1] = - phi[i]; 49 | } 50 | 51 | for(i = 0; i < q;++i) { 52 | theta1[i+1] = - theta[i]; 53 | } 54 | 55 | for(i = 0; i < q+1+lenzt;++i) { 56 | at[i] = 0.0; 57 | } 58 | 59 | coeff = (double*) malloc(sizeof(double) * L); 60 | coeff2 = (double*) malloc(sizeof(double) * (L-1)); 61 | 62 | //mdisplay(coeff2,1,L-1); 63 | 64 | //poly(phi1,delta,coeff,p+1,d+1); 65 | 66 | for(i = 0; i < L;++i) { 67 | coeff[i] = 0.0; 68 | } 69 | 70 | for(it = 0; it < K;++it) { 71 | for(i = 0; i < d+1;++i) { 72 | sum1[i] = delta[i] * phi1[it]; 73 | } 74 | for(i = 0; i < L;++i) { 75 | sum2[i] = 0.0; 76 | } 77 | for(i = it; i < it+d+1;++i) { 78 | sum2[i] = sum1[i-it]; 79 | } 80 | for(i = 0; i < L;++i) { 81 | coeff[i] += sum2[i]; 82 | } 83 | 84 | } 85 | 86 | for(i = 0; i < L-1;++i) { 87 | coeff2[i] = -coeff[i+1]; 88 | } 89 | 90 | 91 | inilen = 2; 92 | M = lenzt-inilen; 93 | 94 | counter2 = 0; 95 | atlen2 = lentheta; 96 | 97 | for(c2 = lenzt-inilen; c2 > 0; c2--) { 98 | 99 | for(i = 0; i < lenzt-c2+1;++i) { 100 | ztl[i] = zt[i]; 101 | } 102 | 103 | for(count = 0; count < forlength;++count) { 104 | temp = constant; 105 | temp2 = 0.0; 106 | 107 | for(ztc = 0; ztc < L-1;++ztc) { 108 | temp=temp+coeff2[ztc]*ztl[lenzt-c2+count-ztc]; 109 | } 110 | 111 | if (counter2 < M - 1) { 112 | for(atc = 0; atc < lentheta;++atc) { 113 | if ((atc > 0) && (count < lentheta-1) && (atlen2+count-atc <= atlen2-1)) { 114 | temp2=temp2+theta1[atc]*at[atlen2+count-atc]; 115 | } 116 | } 117 | } else { 118 | for(atc = 0; atc < lentheta;++atc) { 119 | if ((atc > 0) && (count < lentheta-1) && (atlen2+count-atc <= atlen2-1)) { 120 | temp2=temp2+theta1[atc]*at[atlen2+count-atc]; 121 | } 122 | } 123 | } 124 | ztl[lenzt-c2+1+count]=temp+temp2; 125 | } 126 | //mdisplay(ztl,1,lenzt+forlength); 127 | 128 | if (c2 > 1) { 129 | at[atlen2] = zt[counter2+inilen+1]-ztl[lenzt-c2+1]; 130 | atlen2++; 131 | } 132 | counter2++; 133 | } 134 | 135 | if (d == 0) { 136 | for(i = 0; i < forlength;++i) { 137 | oup[i] = ztl[lenzt+i]; 138 | } 139 | } else { 140 | for (i = 0; i < forlength;++i) { 141 | oup[i] = ztl[lenzt+i]; 142 | for(it = 1; it < d+1;++it) { 143 | if (leninp+i-it < leninp) { 144 | oup[i] -= (delta[it] * inp[leninp+i-it]); 145 | } else { 146 | oup[i] -= (delta[it] * oup[i-it]); 147 | } 148 | } 149 | } 150 | } 151 | 152 | free(zt); 153 | free(phi1); 154 | free(theta1); 155 | free(delta); 156 | free(coeff); 157 | free(coeff2); 158 | free(at); 159 | free(ztl); 160 | free(sum1); 161 | free(sum2); 162 | } 163 | 164 | -------------------------------------------------------------------------------- /src/stats.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * stats.c 4 | * 5 | * Created on: May 13, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #include "stats.h" 10 | 11 | double mean(double* vec, int N) { 12 | int i; 13 | double m; 14 | m = 0.0; 15 | 16 | for (i = 0; i < N; ++i) { 17 | m+= vec[i]; 18 | } 19 | m = m / N; 20 | return m; 21 | } 22 | 23 | double var(double* vec, int N) { 24 | double v,temp,m; 25 | int i; 26 | v = 0.0; 27 | m = mean(vec,N); 28 | 29 | for (i = 0; i < N; ++i) { 30 | temp = vec[i] - m; 31 | v+= temp*temp; 32 | } 33 | 34 | v = v / N; 35 | 36 | return v; 37 | 38 | } 39 | 40 | auto_fft_object auto_fft_init(int N) { 41 | auto_fft_object obj = NULL; 42 | 43 | 44 | obj = (auto_fft_object) malloc (sizeof(struct auto_set)); 45 | 46 | //obj->clen = npow2(conv_len); 47 | //obj->clen = conv_len; 48 | obj->clen = findnexte(2*N); 49 | //obj->clen = 125; 50 | obj->ilen = N; 51 | 52 | obj->fobj = fft_real_init(obj->clen,1); 53 | obj->iobj = fft_real_init(obj->clen,-1); 54 | 55 | return obj; 56 | } 57 | 58 | void autocovar(double* vec,int N, double* acov,int M) { 59 | double m,temp1,temp2; 60 | int i,t; 61 | m = mean(vec,N); 62 | 63 | if ( M > N) { 64 | M = N-1; 65 | printf("\n Lag is greater than the length N of the input vector. It is automatically set to length N - 1.\n"); 66 | printf("\n The Output Vector only contains N calculated values."); 67 | } else if ( M < 0) { 68 | M = 0; 69 | } 70 | 71 | for(i = 0; i < M;i++) { 72 | acov[i] = 0.0; 73 | for (t = 0; t < N-i;t++) { 74 | temp1 = vec[t] - m; 75 | temp2 = vec[t+i] - m; 76 | acov[i]+= temp1*temp2; 77 | } 78 | acov[i] = acov[i] / N; 79 | 80 | } 81 | 82 | 83 | } 84 | 85 | void autocorr(double* vec,int N,double* acorr, int M) { 86 | double var; 87 | int i; 88 | if (M > N) { 89 | M = N - 1; 90 | printf("\n Lag is greater than the length N of the input vector. It is automatically set to length N - 1.\n"); 91 | printf("\n The Output Vector only contains N calculated values."); 92 | } 93 | else if (M < 0) { 94 | M = 0; 95 | } 96 | autocovar(vec,N,acorr,M); 97 | var = acorr[0]; 98 | acorr[0] = 1.0; 99 | 100 | for(i = 1; i < M; i++) { 101 | acorr[i] = acorr[i]/var; 102 | } 103 | 104 | } 105 | 106 | void autocovar_fft(auto_fft_object obj,double* vec,double* acov, int M) { 107 | int i,N,L,N2; 108 | double m; 109 | fft_type* a; 110 | fft_type* b; 111 | fft_data* ao; 112 | fft_data* bo; 113 | 114 | N = obj->clen; 115 | L = obj->ilen; 116 | N2 = N * L; 117 | if (M > N) { 118 | M = N - 1; 119 | printf("\n Lag is greater than the length N of the input vector. It is automatically set to length N - 1.\n"); 120 | printf("\n The Output Vector only contains N calculated values."); 121 | } 122 | else if (M < 0) { 123 | M = 0; 124 | } 125 | 126 | a = (fft_type*) malloc (sizeof(fft_data) * N); 127 | b = (fft_type*) malloc (sizeof(fft_data) * N); 128 | ao = (fft_data*) malloc (sizeof(fft_data) * N); 129 | bo = (fft_data*) malloc (sizeof(fft_data) * N); 130 | 131 | m = mean(vec,L); 132 | 133 | for (i = 0; i < N;i++) { 134 | if (i < L) { 135 | a[i] = vec[i] - m; 136 | } else { 137 | a[i] = 0.0; 138 | } 139 | 140 | } 141 | 142 | fft_r2c_exec(obj->fobj,a,ao); 143 | 144 | for (i = 0; i < N;i++) { 145 | bo[i].re = ao[i].re * ao[i].re + ao[i].im * ao[i].im; 146 | bo[i].im = 0.0; 147 | } 148 | 149 | fft_c2r_exec(obj->iobj,bo,b); 150 | 151 | for (i = 0; i < M;i++) { 152 | acov[i] = b[i]/N2; 153 | } 154 | 155 | free(a); 156 | free(b); 157 | free(ao); 158 | free(bo); 159 | 160 | } 161 | 162 | void autocorr_fft(auto_fft_object obj,double* vec,double* acorr, int M) { 163 | double var; 164 | int i,N; 165 | N = obj->clen; 166 | if (M > N) { 167 | M = N - 1; 168 | printf("\n Lag is greater than the length N of the input vector. It is automatically set to length N - 1.\n"); 169 | printf("\n The Output Vector only contains N calculated values."); 170 | } 171 | else if (M < 0) { 172 | M = 0; 173 | } 174 | autocovar_fft(obj,vec,acorr,M); 175 | var = acorr[0]; 176 | acorr[0] = 1.0; 177 | 178 | for(i = 1; i < M; i++) { 179 | acorr[i] = acorr[i]/var; 180 | } 181 | 182 | } 183 | 184 | void free_auto(auto_fft_object object) { 185 | free(object); 186 | } 187 | -------------------------------------------------------------------------------- /test/csv_in.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * Tiny library to read CSV files in C 4 | * Created on Fri Dec 27 19:37:13 2024 5 | * 6 | * @author: hilton 7 | * 8 | * OBS C99 at least is recommended 9 | */ 10 | 11 | #include /* fopen(), fclose() */ 12 | #include /* calloc(), free() */ 13 | #include /* strtok(), strlen(), strcnmp() */ 14 | #include /* bool, true, false */ 15 | 16 | #include "csv_in.h" /* NAN, read_csv_column */ 17 | #include "auxlib.h" /* deblank(), find_text(), is_numeric(), 18 | split_fields(), free_fields() */ 19 | 20 | #define QUANTUM 2048 21 | 22 | #if FUTURE_VERSION 23 | struct _CSV { 24 | 25 | }; 26 | 27 | /* TODO struct object-like CSV with open that opens the file and reads the 1st line */ 28 | /* TODO holds the field names, returns some columns, closes the file */ 29 | 30 | #endif /* 0 */ 31 | 32 | /** 33 | * Create a double vector from a CSV file name, given its column. 34 | * 35 | * @param[in] csv_name Name of input file. 36 | * @param[in] col_name Name of column to extract. 37 | * @param[out] column `double` vector that contains the column data. 38 | * 39 | * \retval The number of rows in the output vector. 40 | * In case of success. 41 | * 42 | * \retval 0 43 | * In case of failure. 44 | **/ 45 | 46 | size_t 47 | read_csv_column(const char* csv_name, const char* col_name, double** column) 48 | { 49 | int rv; 50 | char buf[1024] = ""; 51 | char* pbuf = (char*)NULL; 52 | char** row = (char**)NULL; 53 | char** columns = (char**)NULL; 54 | double* work = NULL; 55 | size_t row_no = 0; 56 | size_t n_fields = 0; 57 | size_t n_columns = 0; 58 | size_t col_no = (size_t)-1; 59 | size_t total_rows = 0; 60 | size_t result = 0; 61 | FILE* csv_f = NULL; 62 | 63 | /* HINT opening the input file*/ 64 | csv_f = fopen(csv_name, "r"); 65 | if (NULL == csv_f) { 66 | /* TODO report opening problem */ 67 | printf("ERROR Could not open file '%s' for input\n", csv_name); 68 | 69 | return (size_t)0; 70 | } 71 | 72 | /* HINT getting the header */ 73 | pbuf = fgets(buf, sizeof(buf) - 1, csv_f); 74 | if (NULL == pbuf) { 75 | /* TODO report reading problem */ 76 | printf("ERROR Could not read 1st line\n"); 77 | 78 | return (size_t)0; 79 | } 80 | 81 | n_columns = split_fields(buf, ",", &columns); 82 | 83 | rv = find_text(columns, n_columns, col_name); 84 | 85 | /* TODO if the suggested column name is not in the header, leave */ 86 | if (-1 == rv) { 87 | /* TODO report reading problem */ 88 | printf("ERROR Column '%s' was not found in header\n", col_name); 89 | 90 | puts("Columns found\n"); 91 | for (col_no = 0; col_no < n_columns; col_no++) { 92 | printf("'%s' ", columns[col_no]); 93 | } 94 | 95 | return (size_t)0; 96 | } 97 | 98 | free_fields(n_columns, &columns); 99 | 100 | 101 | col_no = (size_t)rv; 102 | 103 | /* TODO allocate a number of elements in the workorary area */ 104 | work = (double*)calloc(QUANTUM, sizeof(double)); 105 | total_rows = QUANTUM; 106 | 107 | /* TODO for the rest of the file do */ 108 | for (pbuf = fgets(buf, sizeof(buf) - 1, csv_f); 109 | NULL != pbuf; 110 | pbuf = fgets(buf, sizeof(buf) - 1, csv_f)) { 111 | 112 | /* TODO break the CSV row in comma-separated values */ 113 | n_fields = split_fields(buf, ",", &row); 114 | 115 | /* TODO if work size is too small, realloc the work array with QUANTUM more */ 116 | if (total_rows <= (row_no + 1)) { 117 | work = realloc(work, total_rows + QUANTUM); 118 | total_rows += QUANTUM; 119 | } 120 | 121 | /* TODO if the column number > number of fields, set NAN to it */ 122 | if (col_no > (n_fields - 1)) { 123 | work[row_no] = NAN; 124 | } 125 | 126 | /* TODO if there's no element in the suggested position, set NAN to it */ 127 | if ( ! strlen(row[col_no]) ) { 128 | work[row_no] = NAN; 129 | } 130 | 131 | /* TODO if there's no valid number in the suggested position, set NAN to it */ 132 | if ( ! is_numeric(row[col_no]) ) { 133 | work[row_no] = NAN; 134 | } 135 | 136 | /* TODO add the number to the work area*/ 137 | work[row_no] = strtod(row[col_no], NULL); 138 | 139 | row_no++; 140 | 141 | free_fields(n_fields, &row); 142 | } 143 | 144 | /* Reallocate the result area, using the number of elements */ 145 | work = (double*)realloc(work, row_no*sizeof(double)); 146 | 147 | /* Copy the work area to the result area */ 148 | *column = work; 149 | 150 | result = row_no; 151 | 152 | /* Close the input file */ 153 | fclose(csv_f); 154 | 155 | /* Normal function termination */ 156 | return result; 157 | } 158 | -------------------------------------------------------------------------------- /data/e6m.dat: -------------------------------------------------------------------------------- 1 | -0.00313258 0.083 2 | 0.0188713 0.083 3 | 0.0248036 0.087 4 | 0.0162776 0.087 5 | 2.89679E-4 0.102 6 | 0.016829 0.098 7 | 0.0385835 0.097 8 | -0.00144386 0.107 9 | 0.0127239 0.109 10 | 0.0238972 0.108 11 | 0.0427728 0.099 12 | -0.0103779 0.089 13 | 0.00558424 0.084 14 | 0.00870609 0.087 15 | 0.0389199 0.086 16 | -0.0196176 0.078 17 | 0.00731993 0.083 18 | 0.0180764 0.081 19 | 0.0237775 0.074 20 | -0.0144377 0.07 21 | 0.0110059 0.064 22 | 0.0104017 0.06 23 | 0.0354648 0.06 24 | -0.0157599 0.056 25 | 0.0100193 0.06 26 | 0.0180621 0.064 27 | 0.0256915 0.066 28 | -0.0170689 0.071 29 | 0.00350475 0.08 30 | 0.0256438 0.078 31 | 0.0315075 0.08 32 | -0.0132775 0.095 33 | 0.0130968 0.083 34 | 0.0151458 0.083 35 | 0.0312119 0.091 36 | -0.0207896 0.104 37 | 0.0104489 0.111 38 | 0.018106 0.113 39 | 0.0405002 0.099 40 | -0.0201483 0.096 41 | 0.00380802 0.092 42 | 0.0215569 0.088 43 | 0.0327106 0.08 44 | -0.0201726 0.074 45 | -0.00333166 0.081 46 | 0.0192871 0.084 47 | 0.0345473 0.083 48 | -0.0262156 0.079 49 | -0.00519753 0.081 50 | 0.0132318 0.077 51 | 0.0374279 0.07 52 | -0.0291948 0.077 53 | -0.00261354 0.07 54 | 0.0172353 0.064 55 | 0.0376387 0.066 56 | -0.0204763 0.06 57 | 9.00269E-4 0.06 58 | 0.0125732 0.058 59 | 0.0344677 0.06 60 | -0.0220394 0.056 61 | -0.00202703 0.055 62 | 0.00173426 0.062 63 | 0.0365901 0.058 64 | -0.0252557 0.056 65 | 9.2268E-4 0.06 66 | 0.00451374 0.063 67 | 0.0383272 0.062 68 | -0.0211368 0.07 69 | -5.33104E-4 0.071 70 | 0.00981092 0.071 71 | 0.0373549 0.078 72 | -0.016232 0.09 73 | 0.00197124 0.09 74 | 0.00687361 0.091 75 | 0.0274582 0.09 76 | -0.0140786 0.086 77 | 0.0166497 0.086 78 | 0.0144682 0.088 79 | 0.043004 0.087 80 | -0.0127769 0.082 81 | 0.00927353 0.084 82 | 0.0179157 0.082 83 | 0.030508 0.074 84 | -0.0105906 0.065 85 | 0.00747108 0.067 86 | 0.00467587 0.061 87 | 0.0310678 0.056 88 | -0.0153685 0.062 89 | 8.10146E-4 0.069 90 | 0.00806475 0.074 91 | 0.0287657 0.074 92 | -0.0183783 0.071 93 | 0.00449467 0.064 94 | 0.00988674 0.061 95 | 0.0245948 0.055 96 | -0.0185189 0.058 97 | -0.00590181 0.059 98 | 0.00581503 0.055 99 | 0.0243654 0.051 100 | -0.0157485 0.051 101 | -0.00749254 0.05 102 | 0.00388288 0.051 103 | 0.0242448 0.051 104 | -0.014647 0.047 105 | -0.00204897 0.047 106 | 0.00247526 0.041 107 | 0.0239234 0.038 108 | -------------------------------------------------------------------------------- /src/brent.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: LGPL-3.0-or-later 2 | // see https://people.math.sc.edu/Burkardt/f_src/brent/brent.html 3 | #include "brent.h" 4 | 5 | /* 6 | * brent.c 7 | * 8 | * Copyright (c) 2014, Rafat Hussain 9 | * License : BSD 3-Clause 10 | * See COPYRIGHT for more details 11 | */ 12 | 13 | /* 14 | * Algorithms are C translation of Richard Brent's Algol 60 procedure as published in 15 | * Algoritms For Minimization Without Derivatives, Richard P. Brent 16 | * Chapters 3-5 17 | */ 18 | double brent_zero(custom_funcuni *funcuni, double ai, double bi, double tol, double eps) { 19 | double bz; 20 | double a,b,c,d,e,fa,fb,fc; 21 | double m,s,etol,fd,p,q,r; 22 | int iter; 23 | 24 | fd = eps; 25 | 26 | a = ai; 27 | b = bi; 28 | c = a; 29 | fa = FUNCUNI_EVAL(funcuni,a); 30 | fb = FUNCUNI_EVAL(funcuni,b); 31 | fc = fa; 32 | e = b - a; 33 | d = e; 34 | 35 | if (fabs(fc) < fabs(fb)) { 36 | a = b; 37 | b = c; 38 | c = a; 39 | fa = fb; 40 | fb = fc; 41 | fc = fa; 42 | } 43 | 44 | etol = 2 * fd * fabs(b) + tol; 45 | m = 0.5 * (c - b); 46 | 47 | bz = b; 48 | iter = 0; 49 | while (fabs ( m ) >= etol && fb != 0.0) { 50 | 51 | iter++; 52 | 53 | if (fabs(e) < etol || fabs(fa) <= fabs(fb)) { 54 | e = m; 55 | d = e; 56 | } else { 57 | 58 | s = fb/fa; 59 | if (a == c) { 60 | // linear interpolation 61 | p = 2.0 * m * s; 62 | q = 1.0 - s; 63 | } else { 64 | // inverse quadratic interpolation 65 | q = fa/fc; 66 | r = fb/fc; 67 | p = s * (2 * m * q * (q - r) - (b - a) * (r - 1.0)); 68 | q = (q - 1.0) * (r - 1.0) * (s - 1.0); 69 | } 70 | 71 | if (p > 0.0) { 72 | q = - q; 73 | } else { 74 | p = - p; 75 | } 76 | 77 | s = e; 78 | e = d; 79 | 80 | if ( 2.0 * p < 3.0 * m * q - fabs( etol * q ) && p < fabs( 0.5 * s * q ) ) { 81 | d = p /q; 82 | } else { 83 | e = m; 84 | d = e; 85 | } 86 | 87 | } 88 | a = b; 89 | fa = fb; 90 | 91 | if ( fabs(d) > etol ) { 92 | b += d; 93 | } else if ( 0.0 < m ) { 94 | b += etol; 95 | } else { 96 | b -= etol; 97 | } 98 | 99 | fb = FUNCUNI_EVAL(funcuni,b); 100 | 101 | bz = b; 102 | if ( ( 0.0 < fb && 0.0 < fc ) || ( fb <= 0.0 && fc <= 0.0 ) ) { 103 | c = a; 104 | fc = fa; 105 | e = b - a; 106 | d = e; 107 | } 108 | 109 | // To the end of the loop 110 | 111 | if (fabs(fc) < fabs(fb)) { 112 | a = b; 113 | b = c; 114 | c = a; 115 | fa = fb; 116 | fb = fc; 117 | fc = fa; 118 | } 119 | 120 | etol = 2 * fd * fabs(b) + tol; 121 | m = 0.5 * (c - b); 122 | 123 | } 124 | 125 | return bz; 126 | } 127 | 128 | double brent_local_min(custom_funcuni *funcuni, double a, double b, double t, double eps, double *x) { 129 | double c,d,e,m,p,q,r,tol,t2; 130 | double u,v,w,fu,fv,fw,fx; 131 | double fd; 132 | 133 | fd = eps; 134 | 135 | c = (3.0 - sqrt(5.0)) / 2.0; 136 | *x = a + c * (b - a); 137 | w = *x; v = w; 138 | e = 0; 139 | fx = FUNCUNI_EVAL(funcuni,*x); 140 | fw = fx; fv = fw; 141 | 142 | m = 0.5 * (a + b); 143 | tol = fd * fabs(*x) + t; 144 | t2 = 2.0 * tol; 145 | 146 | while (fabs(*x - m) > t2 - 0.5 * (b - a)) { 147 | p = 0; q = 0; r = 0; 148 | 149 | if (fabs(e) > tol) { 150 | r = (*x - w) * (fx - fv); 151 | q = (*x - v) * (fx - fw); 152 | p = (*x - v) * q - (*x - w) * r; 153 | q = 2.0 * (q - r); 154 | if (q > 0.0) { 155 | p = -p; 156 | } else { 157 | q = -q; 158 | } 159 | r = e; 160 | e = d; 161 | } 162 | 163 | if (fabs(p) < fabs(0.5 * q * r) && p < q * (a - *x) && p < q * (b - *x)) { 164 | d = p / q; 165 | u = *x + d; 166 | 167 | if ( (u - a) < t2 || (b - u) < t2) { 168 | if (*x < m) { 169 | d = tol; 170 | } else { 171 | d = -tol; 172 | } 173 | } 174 | } else { 175 | if (*x < m) { 176 | e = b - *x; 177 | } else { 178 | e = a - *x; 179 | } 180 | d = c * e; 181 | } 182 | 183 | if (fabs(d) >= tol) { 184 | u = *x + d; 185 | } else if (d > 0) { 186 | u = *x + tol; 187 | } else { 188 | u = *x - tol; 189 | } 190 | 191 | fu = FUNCUNI_EVAL(funcuni,u); 192 | 193 | if (fu <= fx) { 194 | if (u < *x) { 195 | b = *x; 196 | } else { 197 | a = *x; 198 | } 199 | v = w; 200 | fv = fw; 201 | w = *x; 202 | fw = fx; 203 | *x = u; 204 | fx = fu; 205 | } else { 206 | if (u < *x) { 207 | a = u; 208 | } else { 209 | b = u; 210 | } 211 | if (fu <= fw || w == *x) { 212 | v = w; 213 | fv = fw; 214 | w = u; 215 | fw = fu; 216 | } else if (fu <= fv || v == *x || v == w) { 217 | v = u; 218 | fv = fu; 219 | } 220 | } 221 | 222 | //End of the loop 223 | m = 0.5 * (a + b); 224 | tol = fd * fabs(*x) + t; 225 | t2 = 2.0 * tol; 226 | 227 | } 228 | 229 | return fx; 230 | } 231 | -------------------------------------------------------------------------------- /src/wavelib.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | #ifndef WAVELIB_H_ 3 | #define WAVELIB_H_ 4 | 5 | #include "wtmath.h" 6 | 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #if defined(_MSC_VER) 13 | #pragma warning(disable : 4200) 14 | #pragma warning(disable : 4996) 15 | #endif 16 | 17 | #ifndef fft_type 18 | #define fft_type double 19 | #endif 20 | 21 | #ifndef cplx_type 22 | #define cplx_type double 23 | #endif 24 | 25 | 26 | typedef struct cplx_t { 27 | cplx_type re; 28 | cplx_type im; 29 | } cplx_data; 30 | 31 | typedef struct wave_set* wave_object; 32 | 33 | wave_object wave_init(const char* wname); 34 | 35 | struct wave_set{ 36 | char wname[50]; 37 | int filtlength;// When all filters are of the same length. [Matlab uses zero-padding to make all filters of the same length] 38 | int lpd_len;// Default filtlength = lpd_len = lpr_len = hpd_len = hpr_len 39 | int hpd_len; 40 | int lpr_len; 41 | int hpr_len; 42 | double *lpd; 43 | double *hpd; 44 | double *lpr; 45 | double *hpr; 46 | double params[0]; 47 | }; 48 | 49 | typedef struct wt_set* wt_object; 50 | 51 | wt_object wt_init(wave_object wave,const char* method, int siglength, int J); 52 | 53 | struct wt_set{ 54 | wave_object wave; 55 | conv_object cobj; 56 | char method[10]; 57 | int siglength;// Length of the original signal. 58 | int modwtsiglength; // Modified signal length for MODWT 59 | int outlength;// Length of the output DWT vector 60 | int lenlength;// Length of the Output Dimension Vector "length" 61 | int J; // Number of decomposition Levels 62 | int MaxIter;// Maximum Iterations J <= MaxIter 63 | int even;// even = 1 if signal is of even length. even = 0 otherwise 64 | char ext[10];// Type of Extension used - "per" or "sym" 65 | char cmethod[10]; // Convolution Method - "direct" or "FFT" 66 | 67 | int N; // 68 | int cfftset; 69 | int zpad; 70 | int length[102]; 71 | double *output; 72 | double params[0]; 73 | }; 74 | 75 | typedef struct wtree_set* wtree_object; 76 | 77 | wtree_object wtree_init(wave_object wave, int siglength, int J); 78 | 79 | struct wtree_set{ 80 | wave_object wave; 81 | conv_object cobj; 82 | char method[10]; 83 | int siglength;// Length of the original signal. 84 | int outlength;// Length of the output DWT vector 85 | int lenlength;// Length of the Output Dimension Vector "length" 86 | int J; // Number of decomposition Levels 87 | int MaxIter;// Maximum Iterations J <= MaxIter 88 | int even;// even = 1 if signal is of even length. even = 0 otherwise 89 | char ext[10];// Type of Extension used - "per" or "sym" 90 | 91 | int N; // 92 | int nodes; 93 | int cfftset; 94 | int zpad; 95 | int length[102]; 96 | double *output; 97 | int *nodelength; 98 | int *coeflength; 99 | double params[0]; 100 | }; 101 | 102 | typedef struct wpt_set* wpt_object; 103 | 104 | wpt_object wpt_init(wave_object wave, int siglength, int J); 105 | 106 | struct wpt_set{ 107 | wave_object wave; 108 | conv_object cobj; 109 | int siglength;// Length of the original signal. 110 | int outlength;// Length of the output DWT vector 111 | int lenlength;// Length of the Output Dimension Vector "length" 112 | int J; // Number of decomposition Levels 113 | int MaxIter;// Maximum Iterations J <= MaxIter 114 | int even;// even = 1 if signal is of even length. even = 0 otherwise 115 | char ext[10];// Type of Extension used - "per" or "sym" 116 | char entropy[20]; 117 | double eparam; 118 | 119 | int N; // 120 | int nodes; 121 | int length[102]; 122 | double *output; 123 | double *costvalues; 124 | double *basisvector; 125 | int *nodeindex; 126 | int *numnodeslevel; 127 | int *coeflength; 128 | double params[0]; 129 | }; 130 | 131 | void dwt(wt_object wt, const double *inp); 132 | 133 | void idwt(wt_object wt, double *dwtop); 134 | 135 | double *getDWTmra(wt_object wt, double *wavecoeffs); 136 | 137 | void wtree(wtree_object wt, const double *inp); 138 | 139 | void dwpt(wpt_object wt, const double *inp); 140 | 141 | void idwpt(wpt_object wt, double *dwtop); 142 | 143 | void swt(wt_object wt, const double *inp); 144 | 145 | void iswt(wt_object wt, double *swtop); 146 | 147 | double *getSWTmra(wt_object wt, double *wavecoeffs); 148 | 149 | void modwt(wt_object wt, const double *inp); 150 | 151 | void imodwt(wt_object wt, double *dwtop); 152 | 153 | double* getMODWTmra(wt_object wt, double *wavecoeffs); 154 | 155 | void setDWTExtension(wt_object wt, const char *extension); 156 | 157 | void setWTREEExtension(wtree_object wt, const char *extension); 158 | 159 | void setDWPTExtension(wpt_object wt, const char *extension); 160 | 161 | void setDWPTEntropy(wpt_object wt, const char *entropy, double eparam); 162 | 163 | void setWTConv(wt_object wt, const char *cmethod); 164 | 165 | int getWTREENodelength(wtree_object wt, int X); 166 | 167 | void getWTREECoeffs(wtree_object wt, int X, int Y, double *coeffs, int N); 168 | 169 | int getDWPTNodelength(wpt_object wt, int X); 170 | 171 | void getDWPTCoeffs(wpt_object wt, int X, int Y, double *coeffs, int N); 172 | 173 | void wave_summary(wave_object obj); 174 | 175 | void wt_summary(wt_object wt); 176 | 177 | void wtree_summary(wtree_object wt); 178 | 179 | void wpt_summary(wpt_object wt); 180 | 181 | void wave_free(wave_object object); 182 | 183 | void wt_free(wt_object object); 184 | 185 | void wtree_free(wtree_object object); 186 | 187 | void wpt_free(wpt_object object); 188 | 189 | 190 | #ifdef __cplusplus 191 | } 192 | #endif 193 | 194 | 195 | #endif /* WAVELIB_H_ */ 196 | -------------------------------------------------------------------------------- /src/matrix.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * matrix.h 4 | * 5 | * Created on: Jul 1, 2013 6 | * Author: USER 7 | */ 8 | 9 | #ifndef MATRIX_H_ 10 | #define MATRIX_H_ 11 | 12 | #ifdef _OPENMP 13 | #include 14 | #endif 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define CUTOFF 192 24 | #define TOL 1e-12 25 | #define BLOCKSIZE 64 26 | #define TBLOCK 64 27 | #define SVDMAXITER 50 28 | #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a)) 29 | #define RSVD_POWER_ITERATIONS 5 30 | 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | double macheps(); 37 | 38 | double pmax(double a, double b); 39 | 40 | double pmin(double a, double b); 41 | 42 | int imax(int a, int b); 43 | 44 | int imin(int a, int b); 45 | 46 | double signx(double x); 47 | 48 | int iabs(int a); 49 | 50 | double l2norm(double *vec, int N); 51 | 52 | int compare (const void* ind1, const void* ind2); 53 | 54 | int compare_ascending (const void* ind1, const void* ind2); 55 | 56 | void sort1d(double* v,int N, int* pos); 57 | 58 | void sort1d_ascending(double* v,int N, int* pos); 59 | 60 | //Array Parallel Implementation may have a lot of overhead 61 | 62 | double array_max_abs(double *array,int N); 63 | 64 | double array_max(double *array,int N); 65 | 66 | double array_min(double *array,int N); 67 | 68 | //void mmult(double* A, double *B, double *C,int ra,int ca, int rb, int cb); 69 | 70 | void dtranspose(double *sig, int rows, int cols,double *col); 71 | 72 | void stranspose(double *sig, int rows, int cols,double *col); 73 | 74 | void rtranspose(double *m, int rows, int cols,double *n, int r, int c); 75 | 76 | void ctranspose(double *sig, int rows, int cols,double *col); 77 | 78 | void mtranspose(double *sig, int rows, int cols,double *col); 79 | 80 | void itranspose(double *A, int M, int N); 81 | 82 | //int minverse(double *xxt, int p); 83 | 84 | void mdisplay(double *A, int row, int col); 85 | 86 | void madd(double* A, double* B, double* C,int rows,int cols); 87 | 88 | void msub(double* A, double* B, double* C,int rows,int cols); 89 | 90 | void scale(double *A, int rows, int cols, double alpha); 91 | 92 | void nmult(double* A, double* B, double* C,int m,int n, int p); 93 | 94 | void tmult(double* A, double* B, double* C,int m,int n, int p); 95 | 96 | void recmult(double* A, double* B, double* C,int m,int n, int p,int sA,int sB, int sC); 97 | 98 | void rmult(double* A, double* B, double* C,int m,int n, int p); 99 | 100 | int findrec(int *a, int *b, int *c); 101 | 102 | double house_2(double*x,int N,double *v); 103 | 104 | void add_zero_pad(double *X, int rows, int cols, int zrow, int zcol,double *Y); 105 | 106 | void remove_zero_pad(double *X, int rows, int cols, int zrow, int zcol,double *Y); 107 | 108 | void madd_stride(double* A, double* B, double* C,int rows,int cols,int sA,int sB,int sC); 109 | 110 | void msub_stride(double* A, double* B, double* C,int rows,int cols,int sA,int sB,int sC); 111 | 112 | void rmadd_stride(double* A, double* B, double* C,int rows,int cols,int p,int sA,int sB,int sC); 113 | 114 | void rmsub_stride(double* A, double* B, double* C,int rows,int cols,int p,int sA,int sB,int sC); 115 | 116 | void srecmult(double* A, double* B, double* C,int m,int n, int p,int sA,int sB,int sC); 117 | 118 | void smult(double* A, double* B, double* C,int m,int n, int p); 119 | 120 | void mmult(double* A, double* B, double* C,int m,int n, int p); 121 | 122 | void ludecomp(double *A,int N,int *ipiv); 123 | 124 | int rludecomp(double *A, int M, int N, int *ipiv); 125 | 126 | void getPLU(double *A, int M , int N, int *ipiv,double *P, double *L, double *U); 127 | 128 | void getPU(double *A, int M, int N, int *ipiv, double *P,double *U); 129 | 130 | double* marsaglia_generate(double *values, int N, double average, double deviation); 131 | 132 | void random_matrix(double *A, int M, int N); 133 | 134 | void linsolve(double *A,int N,double *b,int *ipiv,double *x); 135 | 136 | void minverse(double *A,int M,int *ipiv,double *inv); 137 | 138 | void eye(double *mat,int N); 139 | 140 | void eye_scale(double *mat, int N, double lambda); 141 | 142 | double house(double*x,int N,double *v); 143 | 144 | void housemat(double *v, int N,double beta,double *mat); 145 | 146 | void qrdecomp(double *A, int M, int N,double *bvec); 147 | 148 | void getQR(double *A,int M,int N,double *bvec,double *Q, double *R); 149 | 150 | void hessenberg(double *A,int N); 151 | 152 | void francisQR(double *A,int N); 153 | 154 | void eig22(double *A, int stride,double *eigre,double *eigim); 155 | 156 | int francis_iter(double *A, int N, double *H); 157 | 158 | void eig(double *A,int N,double *eigre,double *eigim); 159 | 160 | void eigensystem(double *mat, int N, double *eval, double *evec);; 161 | 162 | int cholu(double *A, int N); 163 | 164 | int bcholu(double *A, int N); 165 | 166 | int chol(double *A, int N); 167 | 168 | void chold(double *A, int N); 169 | 170 | void svd_sort(double *U,int M,int N,double *V,double *q); 171 | 172 | int svd(double *A,int M,int N,double *U,double *V,double *q); 173 | 174 | int svd_transpose(double *A, int M, int N, double *U, double *V, double *q); 175 | 176 | int rank(double *A, int M,int N); 177 | 178 | void rsvd(double *A, int M, int N,int K, int oversample, int n_iter,double *U, double *V, double *S); 179 | 180 | #ifdef __cplusplus 181 | } 182 | #endif 183 | 184 | #endif /* MATRIX_H_ */ 185 | -------------------------------------------------------------------------------- /data/islm.dat: -------------------------------------------------------------------------------- 1 | 8.1923491000 0.0818000000 0.7317114200 2 | 8.1910752000 0.0778000000 0.7319381500 3 | 8.2017699000 0.0728000000 0.7444181500 4 | 8.1947540000 0.0598333330 0.7513086400 5 | 8.2182479000 0.0422000000 0.7550880500 6 | 8.2215058000 0.0486666670 0.7584732900 7 | 8.2258243000 0.0555333330 0.7720879900 8 | 8.2291377000 0.0476000000 0.7757256000 9 | 8.2477439000 0.0379666670 0.7769706600 10 | 8.2676800000 0.0438333330 0.7895475200 11 | 8.2774883000 0.0474666670 0.8037314100 12 | 8.2948743000 0.0507333330 0.8159771500 13 | 8.3171556000 0.0395666670 0.8291989000 14 | 8.3225156000 0.0697333330 0.8317180000 15 | 8.3244576000 0.0914000000 0.8307049000 16 | 8.3301887000 0.0924666670 0.8261410800 17 | 8.3258390000 0.0844000000 0.8295907500 18 | 8.3296098000 0.0972333330 0.8331966800 19 | 8.3194737000 0.1165666700 0.8204784200 20 | 8.3151749000 0.0987333330 0.8105554200 21 | 8.2983653000 0.0776000000 0.8032297600 22 | 8.3081251000 0.0605666670 0.8024234600 23 | 8.3238269000 0.0625000000 0.8002981200 24 | 8.3376837000 0.0631666670 0.7961177100 25 | 8.3565079000 0.0513000000 0.8000457600 26 | 8.3623856000 0.0511000000 0.8114585700 27 | 8.3668353000 0.0545333330 0.8095908300 28 | 8.3771952000 0.0502000000 0.8093570900 29 | 8.3911310000 0.0469000000 0.8116975700 30 | 8.4062169000 0.0492000000 0.8177978400 31 | 8.4199772000 0.0550666670 0.8230846300 32 | 8.4183448000 0.0636666670 0.8222820800 33 | 8.4259109000 0.0671000000 0.8327918000 34 | 8.4542108000 0.0696333330 0.8378218300 35 | 8.4625680000 0.0785000000 0.8378043100 36 | 8.4748492000 0.0941000000 0.8354914600 37 | 8.4771626000 0.1023000000 0.8319173500 38 | 8.4790343000 0.0990333330 0.8259875900 39 | 8.4857644000 0.1008333300 0.8257984700 40 | 8.4889377000 0.1283000000 0.8288474300 41 | 8.4931669000 0.1343333300 0.8306247500 42 | 8.4675196000 0.1404666700 0.8103491000 43 | 8.4669940000 0.0885000000 0.8178884100 44 | 8.4827467000 0.1298666700 0.8144000800 45 | 8.4956018000 0.1668000000 0.7965377100 46 | 8.4898014000 0.1536333300 0.7947258600 47 | 8.4969905000 0.1686333300 0.7918184500 48 | 8.4854135000 0.1429666700 0.7870923900 49 | 8.4686330000 0.1322000000 0.7901172400 50 | 8.4751204000 0.1365666700 0.7929717800 51 | 8.4709608000 0.1241333300 0.8023095600 52 | 8.4738681000 0.0946666670 0.8186684600 53 | 8.4792421000 0.0836333330 0.8345624900 54 | 8.5009215000 0.0846333330 0.8532373400 55 | 8.5168131000 0.0932000000 0.8667607400 56 | 8.5333027000 0.0913333330 0.8836572300 57 | 8.5499508000 0.0937666670 0.8947832600 58 | 8.5611527000 0.1031333300 0.9071942000 59 | 8.5671636000 0.1119000000 0.9093596800 60 | 8.5735168000 0.1005333300 0.9149549600 61 | 8.5804872000 0.0833666670 0.9204060500 62 | 8.5879312000 0.0832666670 0.9333423700 63 | 8.6010195000 0.0751000000 0.9534501500 64 | 8.6068328000 0.0775666670 0.9674121100 65 | 8.6179069000 0.0760333330 0.9736521900 66 | 8.6170928000 0.0670000000 0.9959659400 67 | 8.6225257000 0.0621000000 1.0173809000 68 | 8.6265856000 0.0561666670 1.0363711000 69 | 8.6316998000 0.0589666670 1.0496749000 70 | 8.6417091000 0.0647000000 1.0636857000 71 | 8.6493086000 0.0668666670 1.0694336000 72 | 8.6634211000 0.0741000000 1.0803522000 73 | 8.6704120000 0.0691333330 1.0870966000 74 | 8.6790909000 0.0680666670 1.1003056000 75 | 8.6845703000 0.0778000000 1.1068075000 76 | 8.6965771000 0.0825333330 1.1102841000 77 | 8.7053974000 0.0905333330 1.1035995000 78 | 8.7114598000 0.0962000000 1.1044979000 79 | 8.7174693000 0.0866000000 1.1064638000 80 | 8.7196278000 0.0840666670 1.1097757000 81 | 8.7294133000 0.0805000000 1.1100622000 82 | 8.7320787000 0.0816000000 1.1243880000 83 | 8.7272596000 0.0787000000 1.1397464000 84 | 8.7196768000 0.0783333330 1.1533666000 85 | 8.7123816000 0.0697333330 1.1668467000 86 | 8.7143705000 0.0597000000 1.1792511000 87 | 8.7163063000 0.0579000000 1.1899434000 88 | 8.7193664000 0.0514666670 1.2027768000 89 | 8.7310296000 0.0413000000 1.2149204000 90 | 8.7362163000 0.0395666670 1.2330178000 91 | 8.7435635000 0.0346666670 1.2562993000 92 | 8.7539240000 0.0326666670 1.2800881000 93 | 8.7565250000 0.0321333330 1.2930066000 94 | 8.7604687000 0.0306000000 1.3143022000 95 | 8.7670799000 0.0312666670 1.3364869000 96 | 8.7789265000 0.0318333330 1.3547644000 97 | 8.7870371000 0.0324333330 1.3719875000 98 | 8.7974276000 0.0404666670 1.3851222000 99 | 8.8021315000 0.0461333330 1.3962957000 100 | 8.8116373000 0.0535666670 1.4030894000 101 | 8.8161119000 0.0611666670 1.4083836000 102 | 8.8185710000 0.0598333330 1.4177737000 103 | 8.8250896000 0.0571333330 1.4163120000 104 | 8.8337251000 0.0567000000 1.4167983000 105 | 8.8402038000 0.0530000000 1.4132052000 106 | 8.8558630000 0.0526000000 1.4150437000 107 | 8.8593777000 0.0538333330 1.4209676000 108 | 8.8714211000 0.0533333330 1.4275593000 109 | 8.8837650000 0.0532666670 1.4368882000 110 | 8.8947800000 0.0556000000 1.4405309000 111 | 8.9059054000 0.0555000000 1.4517468000 112 | 8.9100000000 0.0559000000 1.4672324000 -------------------------------------------------------------------------------- /src/conv.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * conv.c 4 | * 5 | * Created on: May 1, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #include "conv.h" 10 | 11 | int factorf(int M) { 12 | int N; 13 | N = M; 14 | while (N%7 == 0){ 15 | N = N/7; 16 | } 17 | while (N%3 == 0){ 18 | N = N/3; 19 | } 20 | while (N%5 == 0){ 21 | N = N/5; 22 | } 23 | while (N%2 == 0){ 24 | N = N/2; 25 | } 26 | 27 | return N; 28 | } 29 | 30 | int factor2(int M) { 31 | int N; 32 | N = M; 33 | 34 | while (N % 2 == 0){ 35 | N = N / 2; 36 | } 37 | 38 | return N; 39 | } 40 | 41 | int findnext2(int M) { 42 | int N; 43 | N = M; 44 | 45 | while (factor2(N) != 1) { 46 | ++N; 47 | } 48 | 49 | return N; 50 | 51 | } 52 | 53 | 54 | int findnext(int M) { 55 | int N; 56 | N = M; 57 | 58 | while (factorf(N) != 1) { 59 | ++N; 60 | } 61 | 62 | return N; 63 | 64 | } 65 | 66 | int findnexte(int M) { 67 | int N; 68 | N = M; 69 | 70 | while (factorf(N) != 1 || N%2 != 0) { 71 | ++N; 72 | } 73 | 74 | return N; 75 | 76 | } 77 | 78 | 79 | conv_object conv_init(int N, int L) { 80 | 81 | conv_object obj = NULL; 82 | int conv_len; 83 | conv_len = N + L - 1; 84 | 85 | obj = (conv_object) malloc (sizeof(struct conv_set)); 86 | 87 | //obj->clen = npow2(conv_len); 88 | //obj->clen = conv_len; 89 | obj->clen = findnexte(conv_len); 90 | obj->ilen1 = N; 91 | obj->ilen2 = L; 92 | 93 | obj->fobj = fft_real_init(obj->clen,1); 94 | obj->iobj = fft_real_init(obj->clen,-1); 95 | 96 | return obj; 97 | 98 | } 99 | 100 | void conv_directx(fft_type *inp1,int N, fft_type *inp2, int L,fft_type *oup){ 101 | int M,k,n; 102 | 103 | M = N + L - 1; 104 | 105 | for (k = 0; k < M;++k) { 106 | oup[k] = 0.0; 107 | for ( n = 0; n < N; ++n) { 108 | if ( (k-n) >= 0 && (k-n) < L ) { 109 | oup[k]+= inp1[n] * inp2[k-n]; 110 | } 111 | } 112 | 113 | } 114 | 115 | } 116 | 117 | void conv_direct(fft_type *inp1,int N, fft_type *inp2, int L,fft_type *oup) { 118 | 119 | int M,k,m,i; 120 | fft_type t1,tmin; 121 | 122 | M = N + L -1; 123 | i = 0; 124 | 125 | if (N >= L) { 126 | 127 | for (k = 0; k < L; k++) { 128 | oup[k] = 0.0; 129 | for (m = 0; m <= k;m++) { 130 | oup[k]+= inp1[m] * inp2[k-m]; 131 | } 132 | } 133 | 134 | for (k = L; k < M; k++) { 135 | oup[k] = 0.0; 136 | i++; 137 | t1 = (fft_type) L + i; 138 | tmin = MIN(t1,N); 139 | for (m = i; m < tmin;m++) { 140 | oup[k]+= inp1[m] * inp2[k-m]; 141 | } 142 | } 143 | 144 | 145 | } else { 146 | for (k = 0; k < N; k++) { 147 | oup[k] = 0.0; 148 | for (m = 0; m <= k;m++) { 149 | oup[k]+= inp2[m] * inp1[k-m]; 150 | } 151 | } 152 | 153 | for (k = N; k < M; k++) { 154 | oup[k] = 0.0; 155 | i++; 156 | t1 = (fft_type) N + i; 157 | tmin = MIN(t1,L); 158 | for (m = i; m < tmin;m++) { 159 | oup[k]+= inp2[m] * inp1[k-m]; 160 | } 161 | } 162 | 163 | } 164 | 165 | 166 | } 167 | 168 | 169 | void conv_fft(const conv_object obj,fft_type *inp1,fft_type *inp2,fft_type *oup) { 170 | int i,N,L1,L2,ls; 171 | fft_type* a; 172 | fft_type* b; 173 | fft_data* c; 174 | fft_data* ao; 175 | fft_data* bo; 176 | fft_type* co; 177 | 178 | N = obj->clen; 179 | L1 = obj->ilen1; 180 | L2 = obj->ilen2; 181 | ls = L1 + L2 - 1; 182 | 183 | a = (fft_type*) malloc (sizeof(fft_data) * N); 184 | b = (fft_type*) malloc (sizeof(fft_data) * N); 185 | c = (fft_data*) malloc (sizeof(fft_data) * N); 186 | ao = (fft_data*) malloc (sizeof(fft_data) * N); 187 | bo = (fft_data*) malloc (sizeof(fft_data) * N); 188 | co = (fft_type*) malloc (sizeof(fft_data) * N); 189 | 190 | for (i = 0; i < N;i++) { 191 | if (i < L1) { 192 | a[i] = inp1[i]; 193 | } else { 194 | a[i] = 0.0; 195 | } 196 | 197 | if (i < L2) { 198 | b[i] = inp2[i]; 199 | } else { 200 | b[i] = 0.0; 201 | } 202 | 203 | } 204 | 205 | fft_r2c_exec(obj->fobj,a,ao); 206 | fft_r2c_exec(obj->fobj,b,bo); 207 | 208 | for (i = 0; i < N;i++) { 209 | c[i].re = ao[i].re * bo[i].re - ao[i].im * bo[i].im; 210 | c[i].im = ao[i].im * bo[i].re + ao[i].re * bo[i].im; 211 | } 212 | 213 | fft_c2r_exec(obj->iobj,c,co); 214 | 215 | for (i = 0; i < ls;i++) { 216 | oup[i] = co[i]/N; 217 | } 218 | 219 | free(a); 220 | free(b); 221 | free(c); 222 | free(ao); 223 | free(bo); 224 | free(co); 225 | 226 | 227 | } 228 | 229 | int convolve(const char *type, const char *method, fft_type *inp1, int N, fft_type *inp2, int L, fft_type *oup) { 230 | /* 231 | 232 | method - 'direct' or 'fft' 233 | type - One of 'full', 'same' or 'valid' 234 | inp1 - First vector of length N 235 | inp2 - Second vector of length L 236 | oup2 - Output vector of : 237 | 238 | length N+L-1, if type is 'full' 239 | length max(N,L), if type is 'same' 240 | length max(N,L), - min(N,L) + 1 if type is valid 241 | 242 | */ 243 | int M, LB, LS, start, pad, i; 244 | fft_type *oup2; 245 | conv_object conv; 246 | 247 | M = N + L - 1; 248 | 249 | LB = N >= L ? N : L; 250 | LS = LB == N ? L : N; 251 | 252 | oup2 = (fft_type*)malloc(sizeof(fft_type)*M); 253 | 254 | pad = 0; 255 | 256 | if (!strcmp(method, "direct") || method == NULL) { 257 | 258 | conv_direct(inp1, N, inp2, L, oup2); 259 | 260 | } 261 | else if (!strcmp(method, "fft")) { 262 | 263 | conv = conv_init(N, L); 264 | 265 | conv_fft(conv, inp1, inp2, oup2); 266 | 267 | free_conv(conv); 268 | } 269 | else { 270 | printf("method - 'direct' or 'fft' \n"); 271 | exit(-1); 272 | } 273 | 274 | 275 | if (!strcmp(type, "full") || type == NULL) { 276 | 277 | start = 0; 278 | 279 | pad = M; 280 | 281 | } 282 | else if (!strcmp(type, "same")) { 283 | 284 | start = (M - LB) / 2; 285 | 286 | pad = LB; 287 | 288 | } 289 | else if (!strcmp(type, "valid")) { 290 | 291 | start = LS - 1; 292 | 293 | if (LS == 0) { 294 | pad = LB; 295 | } 296 | else { 297 | pad = LB - LS + 1; 298 | } 299 | 300 | } 301 | else { 302 | printf("type - One of 'full', 'same' or 'valid' \n"); 303 | exit(-1); 304 | } 305 | 306 | memcpy(oup, oup2 + start, sizeof(fft_type) * pad); 307 | 308 | 309 | free(oup2); 310 | 311 | return pad; 312 | } 313 | 314 | 315 | void free_conv(conv_object object) { 316 | free_real_fft(object->fobj); 317 | free_real_fft(object->iobj); 318 | free(object); 319 | } 320 | -------------------------------------------------------------------------------- /src/filter.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * filter.c 4 | * 5 | * Created on: May 26, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #include "filter.h" 10 | 11 | double norm2(double *array, int N) { 12 | int i; 13 | double nrm; 14 | 15 | nrm = 0.0; 16 | for (i = 0; i < N; ++i) { 17 | nrm += array[i]*array[i]; 18 | } 19 | nrm = sqrt(nrm); 20 | return nrm; 21 | } 22 | 23 | void conv(double *sig, int len_sig, double *filt, int len_filt, double *oup) { 24 | if (len_sig >= len_filt) { 25 | conv_direct(sig,len_sig, filt, len_filt,oup); 26 | } else { 27 | conv_direct(filt,len_filt, sig, len_sig,oup); 28 | } 29 | } 30 | 31 | void convx(double *sig, int len_sig, double *filt, int len_filt, double *oup) { 32 | if (len_sig >= len_filt) { 33 | conv_directx(sig,len_sig, filt, len_filt,oup); 34 | } else { 35 | conv_directx(filt,len_filt, sig, len_sig,oup); 36 | } 37 | } 38 | 39 | void filter(double *sig, int len_sig, double *filt, int len_filt, double *oup) { 40 | int N,i; 41 | double *temp; 42 | /* 43 | * 44 | * filter returns the output which is of same length as the input 45 | * unlike conv function 46 | * 47 | */ 48 | 49 | N = len_sig + len_filt - 1; 50 | temp = (double*) malloc (sizeof(double) * N); 51 | 52 | conv_direct(sig,len_sig, filt, len_filt,temp); 53 | 54 | for (i = 0; i < len_sig; ++i) { 55 | oup[i] = temp[i]; 56 | } 57 | 58 | free(temp); 59 | 60 | 61 | } 62 | 63 | void mafilter(double *sig,int N, int window,double *oup) { 64 | int odd,q,N2,i,j; 65 | double sum; 66 | double *temp; 67 | /* 68 | * mafilter is a moving average filter that smoothes the signal using 69 | * a smoothing window. In this implementation, the signal is extended 70 | * by the length of the window. Length floor(window/2) extension is carried 71 | * out at the beginning of the signal by setting values of the signal 72 | * to sig[0]. The rest of the extension is done at the tail of the signal 73 | * by setting it equal to sig[N-1]. 74 | * 75 | */ 76 | 77 | odd = window - ((window/2) * 2); 78 | 79 | if (odd) { 80 | q = (window - 1 ) / 2; 81 | } else { 82 | q = window / 2; 83 | } 84 | 85 | N2 = N + (2 * q); 86 | 87 | temp = (double*) malloc (sizeof(double) * N2); 88 | 89 | for (i = 0; i < q; ++i) { 90 | temp[i] = sig[0]; 91 | } 92 | 93 | for (i = q; i < N + q; ++i) { 94 | temp[i] = sig[i - q]; 95 | } 96 | 97 | for (i = N + q; i < N2; ++i) { 98 | temp[i] = sig[N-1]; 99 | } 100 | 101 | if (odd) { 102 | for (i = q; i < N+q; ++i) { 103 | sum = 0.0; 104 | for (j = i - q; j < i + q + 1;j++) { 105 | sum += temp[j]; 106 | } 107 | oup[i-q] = sum /window; 108 | } 109 | } else { 110 | for (i = q; i < N+q; ++i) { 111 | sum = 0.0; 112 | for (j = i - q; j < i + q + 1;j++) { 113 | if ( j == i - q || j == i + q) { 114 | sum += 0.5 * temp[j]; 115 | } else { 116 | sum += temp[j]; 117 | } 118 | } 119 | oup[i-q] = sum /window; 120 | } 121 | } 122 | 123 | 124 | free(temp); 125 | } 126 | 127 | void mafilter2(double *sig,int N, int window,double *oup) { 128 | int odd,q,N2,i,j; 129 | double sum; 130 | /* 131 | * mafilter2 is a smoothing filter that smoothes a signal by using 132 | * a window. No signal extension is done in this case. Instead, 133 | * first floor(window/2) signal terms and last [window - floor(window/2)] 134 | * terms are left untouched. The smoothing is performed over the rest 135 | * of the terms. 136 | * 137 | */ 138 | 139 | odd = window - ((window/2) * 2); 140 | 141 | if (odd) { 142 | q = (window - 1 ) / 2; 143 | } else { 144 | q = window / 2; 145 | } 146 | 147 | if (odd) { 148 | for (i = 0; i < q; i++) { 149 | oup[i] = sig[i]; 150 | } 151 | for (i = q; i < N-q; ++i) { 152 | sum = 0.0; 153 | for (j = i - q; j < i + q + 1;j++) { 154 | sum += sig[j]; 155 | } 156 | oup[i] = sum /window; 157 | } 158 | for (i = N-q; i < N; i++) { 159 | oup[i] = sig[i]; 160 | } 161 | } else { 162 | for (i = 0; i < q; i++) { 163 | oup[i] = sig[i]; 164 | } 165 | for (i = q; i < N-q; ++i) { 166 | sum = 0.0; 167 | for (j = i - q; j < i + q + 1;j++) { 168 | if ( j == i - q || j == i + q) { 169 | sum += 0.5 * sig[j]; 170 | } else { 171 | sum += sig[j]; 172 | } 173 | } 174 | oup[i] = sum /window; 175 | } 176 | for (i = N-q; i < N; i++) { 177 | oup[i] = sig[i]; 178 | } 179 | } 180 | } 181 | 182 | void expfilter(double *sig,int N, double alpha,double *oup) { 183 | int i; 184 | double beta; 185 | 186 | /* alpha accepts values between 0 and 1. 187 | * No error check is implemented presently 188 | * so make sure that the correct value of 189 | * alpha is entered. 190 | */ 191 | beta = 1.0 - alpha; 192 | 193 | oup[0] = sig[0]; 194 | 195 | for (i = 1; i < N; ++i) { 196 | oup[i] = alpha * sig[i] + beta * oup[i-1]; 197 | } 198 | } 199 | 200 | void mafilter_wt(double *sig,int N, double *weights, int window,double *oup) { 201 | int odd,q,N2,i,j,ct; 202 | double sum; 203 | double *temp; 204 | 205 | /* Accepts Normalized weights. 206 | * If the weight is not normalized then normalize it by 207 | * dividing all the weights by the sum of all the weights 208 | * [1,1,1,1,1] normalizes to [1,1,1,1,1] / 5 = [0.2,0.2,0.2,0.2,0.2] 209 | */ 210 | 211 | odd = window - ((window/2) * 2); 212 | 213 | if (odd) { 214 | q = (window - 1 ) / 2; 215 | N2 = N + (2 * q); 216 | } else { 217 | q = window / 2; 218 | N2 = N + (2 * q) - 1; 219 | } 220 | 221 | 222 | temp = (double*) malloc (sizeof(double) * N2); 223 | 224 | for (i = 0; i < q; ++i) { 225 | temp[i] = sig[0]; 226 | } 227 | 228 | for (i = q; i < N + q; ++i) { 229 | temp[i] = sig[i - q]; 230 | } 231 | 232 | for (i = N + q; i < N2; ++i) { 233 | temp[i] = sig[N-1]; 234 | } 235 | 236 | if (odd) { 237 | for (i = q; i < N+q; ++i) { 238 | sum = 0.0; 239 | ct = 0; 240 | for (j = i - q; j < i + q + 1;j++) { 241 | sum += weights[ct] * temp[j]; 242 | ct++; 243 | } 244 | oup[i-q] = sum; 245 | } 246 | } else { 247 | for (i = q; i < N+q; ++i) { 248 | sum = 0.0; 249 | ct = 0; 250 | for (j = i - q; j < i + q;j++) { 251 | sum += weights[ct] * temp[j]; 252 | ct++; 253 | } 254 | oup[i-q] = sum; 255 | } 256 | } 257 | 258 | 259 | free(temp); 260 | } 261 | 262 | -------------------------------------------------------------------------------- /src/spectrum.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /* 3 | * spectrum.c 4 | * 5 | * Created on: May 18, 2013 6 | * Author: Rafat Hussain 7 | */ 8 | 9 | #include 10 | #include "spectrum.h" 11 | 12 | spectrum_fft_object spectrum_fft_init(int N,int Nfft) { 13 | spectrum_fft_object obj = NULL; 14 | 15 | obj = (spectrum_fft_object) malloc (sizeof(struct spectrum_set)); 16 | obj->len = N; 17 | obj->lenfft = Nfft; 18 | obj->fobj = fft_init(N,1); 19 | if ( N == Nfft) { 20 | obj->fobj2 = obj->fobj; 21 | } else { 22 | obj->fobj2 = fft_init(Nfft,1); 23 | } 24 | 25 | return obj; 26 | } 27 | 28 | int isOdd(int N) { 29 | return N - ((N/2) * 2); 30 | } 31 | 32 | void spectrum_shift(double* spec,int N) { 33 | int N2,i; 34 | double temp,temp1; 35 | 36 | if (isOdd(N)) { 37 | N2 = N/2; 38 | temp1 = spec[0]; 39 | for (i = 0; i < N2; i++) { 40 | temp = spec[i+1]; 41 | spec[i] = spec[i + N2+1]; 42 | spec[i + N2+1] = temp; 43 | } 44 | spec[N2] = temp1; 45 | 46 | } else { 47 | N2 = N/2 - 1; 48 | temp = spec[N2 + 1]; 49 | temp1 = spec[0]; 50 | 51 | for (i = 0; i < N2; i++) { 52 | spec[i] = spec[i + N2+2]; 53 | spec[i + N2+1] = spec[i+1]; 54 | } 55 | spec[N2] = temp1; 56 | spec[N-1] = temp; 57 | } 58 | } 59 | 60 | void periodogram(spectrum_fft_object obj,double* vec,double* spec, double *freq, int side) { 61 | int i,N,t,N2; 62 | double m; 63 | fft_data *inp; 64 | fft_data *oup; 65 | 66 | N = obj->len; 67 | 68 | inp = (fft_data*) malloc (sizeof(fft_data) * N); 69 | oup = (fft_data*) malloc (sizeof(fft_data) * N); 70 | 71 | m = mean(vec,N); 72 | for (i = 0; i < N;++i) { 73 | inp[i].re = vec[i]; 74 | inp[i].im = 0.0; 75 | } 76 | 77 | fft_exec(obj->fobj,inp,oup); 78 | 79 | if (isOdd(N)) { 80 | spec[0] = oup[0].re * oup[0].re / N; 81 | freq[0] = 0.0; 82 | N2 = N/2; 83 | for (i = 1; i < N2+1 ;++i) { 84 | spec[i] = (oup[i].re * oup[i].re + oup[i].im * oup[i].im) / N; 85 | freq[i] = PI2 * i / N; 86 | } 87 | if (side == 2) { 88 | for (i = 0; i < N2; i++) { 89 | spec[N2 + i + 1] = spec[N2 - i]; 90 | freq[N2 + i + 1] = - freq[N2 - i]; 91 | } 92 | } else { 93 | for (i = 1; i < N2+1 ;++i) { 94 | spec[i]*= 2.0; 95 | } 96 | } 97 | 98 | 99 | } else { 100 | spec[0] = oup[0].re * oup[0].re / N; 101 | freq[0] = 0.0; 102 | N2 = N/2; 103 | for (i = 1; i < N2;++i) { 104 | spec[i] = (oup[i].re * oup[i].re + oup[i].im * oup[i].im) / N; 105 | freq[i] = PI2 * i / N; 106 | } 107 | 108 | spec[N2] = 0.0; 109 | freq[N2] = PI2 * N2 / N; 110 | 111 | for (t = 0; t < N;t+=2) { 112 | spec[N2] -= vec[t]; 113 | } 114 | 115 | for (t = 1; t < N;t+=2) { 116 | spec[N2] += vec[t]; 117 | } 118 | 119 | spec[N2] = spec[N2] * spec[N2] / N; 120 | 121 | if (side == 2) { 122 | for (i = 1; i < N2; i++) { 123 | spec[N2 + i] = spec[N2 - i]; 124 | freq[N2 + i] = - freq[N2 - i]; 125 | } 126 | } else { 127 | for (i = 1; i < N2;++i) { 128 | spec[i] *= 2.0; 129 | } 130 | } 131 | } 132 | 133 | } 134 | 135 | void psd(spectrum_fft_object obj,double* vec,double* spec, double *freq, int side) { 136 | int i,N,N2,NO; 137 | double m,NP; 138 | fft_data *inp; 139 | fft_data *oup; 140 | 141 | N = obj->lenfft; 142 | NO = obj->len; 143 | NP = NO * PI2; 144 | 145 | inp = (fft_data*) malloc (sizeof(fft_data) * N); 146 | oup = (fft_data*) malloc (sizeof(fft_data) * N); 147 | 148 | m = mean(vec,N); 149 | for (i = 0; i < NO;++i) { 150 | inp[i].re = vec[i]; 151 | inp[i].im = 0.0; 152 | } 153 | 154 | for (i = NO; i < N;++i) { 155 | inp[i].re = 0.0; 156 | inp[i].im = 0.0; 157 | } 158 | 159 | fft_exec(obj->fobj2,inp,oup); 160 | 161 | if (isOdd(N)) { 162 | spec[0] = oup[0].re * oup[0].re / NP; 163 | freq[0] = 0.0; 164 | N2 = N/2; 165 | for (i = 1; i < N2+1 ;++i) { 166 | spec[i] = (oup[i].re * oup[i].re + oup[i].im * oup[i].im) / NP; 167 | freq[i] = PI2 * i / N; 168 | } 169 | if (side == 2) { 170 | for (i = 0; i < N2; i++) { 171 | spec[N2 + i + 1] = spec[N2 - i]; 172 | freq[N2 + i + 1] = - freq[N2 - i]; 173 | } 174 | } else { 175 | for (i = 1; i < N2+1 ;++i) { 176 | spec[i]*= 2.0; 177 | } 178 | } 179 | 180 | 181 | } else { 182 | spec[0] = oup[0].re * oup[0].re / NP; 183 | freq[0] = 0.0; 184 | N2 = N/2; 185 | for (i = 1; i < N2+1;++i) { 186 | spec[i] = (oup[i].re * oup[i].re + oup[i].im * oup[i].im) / NP; 187 | freq[i] = PI2 * i / N; 188 | } 189 | 190 | /* 191 | spec[N2] = 0.0; 192 | freq[N2] = PI2 * N2 / N; 193 | 194 | for (t = 0; t < N;t+=2) { 195 | spec[N2] -= vec[t]; 196 | } 197 | 198 | for (t = 1; t < N;t+=2) { 199 | spec[N2] += vec[t]; 200 | } 201 | 202 | spec[N2] = spec[N2] * spec[N2] / N; 203 | */ 204 | if (side == 2) { 205 | for (i = 1; i < N2; i++) { 206 | spec[N2 + i] = spec[N2 - i]; 207 | freq[N2 + i] = - freq[N2 - i]; 208 | } 209 | } else { 210 | for (i = 1; i < N2;++i) { 211 | spec[i] *= 2.0; 212 | } 213 | } 214 | } 215 | 216 | } 217 | 218 | void psd_autocovar(auto_fft_object obj,double *vec,double* spec, double *freq,int Nfft, int side) { 219 | int N,f,N2; 220 | double NP; 221 | double *acov; 222 | fft_data *inp; 223 | fft_data *oup; 224 | 225 | N = obj->ilen; 226 | NP = PI2 * N; 227 | acov = (double*) malloc (sizeof(double) * N); 228 | inp = (fft_data*) malloc (sizeof(fft_data) * Nfft); 229 | oup = (fft_data*) malloc (sizeof(fft_data) * Nfft); 230 | autocovar_fft(obj,vec,acov,N); 231 | 232 | N2 = Nfft/2 + 1; 233 | fft_object fobj = fft_init(Nfft,1); 234 | 235 | for (f = 0; f < N;f++) { 236 | inp[f].re = acov[f]; 237 | inp[f].im = 0.0; 238 | } 239 | 240 | for (f = N; f < Nfft;f++) { 241 | inp[f].re = 0.0; 242 | inp[f].im = 0.0; 243 | } 244 | 245 | fft_exec(fobj,inp,oup); 246 | spec[0] = oup[0].re * oup[0].re / NP; 247 | freq[0] = 0.0; 248 | 249 | for (f = 1; f < N2;++f) { 250 | spec[f] = (oup[f].re * oup[f].re + oup[f].im * oup[f].im) / NP; 251 | freq[f] = PI2 * f / Nfft; 252 | } 253 | /* 254 | for (f = 0; f < N2;f++) { 255 | spec[f] = 0.0; 256 | for (k = 1; k < N;k++) { 257 | spec[f]+= acov[k] * cos(PI2 * f * k / Nfft); 258 | } 259 | spec[f] *= 2.0; 260 | spec[f] += acov[0]; 261 | //spec[f] /= PI2; 262 | freq[f] = PI2 * f / Nfft; 263 | } 264 | */ 265 | 266 | free_fft(fobj); 267 | free(acov); 268 | } 269 | 270 | void free_spectrum(spectrum_fft_object object) { 271 | free(object); 272 | } 273 | 274 | 275 | -------------------------------------------------------------------------------- /test/auxlib.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-3-Clause 2 | /** 3 | * A tiny lib of string functions that C misses 4 | * Created on Sun Dec 29 21:34:24 2024 5 | * 6 | * @author: hilton 7 | * 8 | **/ 9 | 10 | #include /* calloc(), realloc(), free() */ 11 | #include /* strcpy(), strlen() */ 12 | #include /* regex_t, regmatch_t, regcomp(), regexec() , 13 | regfree() */ 14 | 15 | #include "auxlib.h" 16 | 17 | /** 18 | * Strips the space characters of a string to the left and right 19 | * 20 | * @param[in] input The string to be deblanked 21 | * 22 | * \retval A new string with left and right spaces eliminated 23 | * In case of sucess. 24 | * 25 | * \retval The null char pointer 26 | * In case of failure. 27 | **/ 28 | 29 | char* 30 | deblank(const char* input) 31 | { 32 | int start = 0; 33 | int stop = 0; 34 | size_t length = 0; 35 | size_t full_length = 0; 36 | char* result = (char*)NULL; 37 | 38 | if (!input) return NULL; 39 | 40 | full_length = strlen(input); 41 | if (0 == full_length) { 42 | return (char*)calloc(1, sizeof(char*)); 43 | } 44 | 45 | for (start = 0; *(input + start) == ' '; start++) 46 | ; 47 | 48 | for (stop = full_length - 1; stop > 0; stop--) { 49 | if (*(input + stop) != ' ') break; 50 | } 51 | 52 | length = full_length - start + 1; 53 | length -= full_length - stop; 54 | 55 | result = (char*)calloc(length + 1, sizeof(char)); 56 | strncpy(result, input + start, length); 57 | 58 | /* Normal function termination */ 59 | return result; 60 | } 61 | 62 | 63 | /** 64 | * Checks if a string contains only a number. 65 | * Spaces to the left and right are ignored. 66 | * 67 | * @param[in] input The string to be verified. 68 | * 69 | * \retval 1 70 | * In case of sucess. 71 | * 72 | * \retval 0 73 | * If the string doesn't contain only a number 74 | **/ 75 | 76 | int 77 | is_numeric(const char* input) 78 | { 79 | int rv; 80 | int result; 81 | char* deb = (char*)NULL; 82 | regex_t regex; 83 | regmatch_t pmatch[3]; // Up to 3 sub-expressions 84 | 85 | rv = regcomp(®ex, NUMERIC_MASK, REG_EXTENDED | REG_ICASE | REG_NEWLINE); 86 | if (rv) { 87 | return 0; 88 | } 89 | 90 | /* printf("input '%s'\n", input); */ 91 | deb = deblank(input); 92 | 93 | /* printf("deb '%s'\n", deb); */ 94 | rv = regexec(®ex, deb, ARRAY_SIZE (pmatch), pmatch, 0); 95 | 96 | if (rv) { 97 | return 0; 98 | } 99 | 100 | result = (int) 101 | (strlen(deb) == (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so)); 102 | 103 | regfree(®ex); 104 | free(deb); 105 | 106 | /* Normal function termination */ 107 | return result; 108 | } 109 | 110 | /** 111 | * Splits a string in several others delimited by a separator. 112 | * 113 | * @param[in] text The string containing the text to be splitted. 114 | * @param[in] sep A delimiter in the input string. 115 | * @param[out] fields A char array pointing tho the strings splitted. 116 | * 117 | * \retval The number of substrings found. 118 | * In case of sucess. If the string doesn't contain a delimiter, it is 119 | * copied to the output, and 1 is returned. 120 | * 121 | * \retval 0 122 | * In case of some failure. 123 | * 124 | * \note Uses `strtoken()` and is therefore not reentrant. 125 | **/ 126 | 127 | size_t 128 | split_fields(const char* text, const char* sep, char*** fields) 129 | { 130 | char* buf = (char*)NULL; 131 | char* token = (char*)NULL; 132 | char** work = (char**)NULL; 133 | size_t result = 0; 134 | size_t n_field = 0; 135 | 136 | buf = (char*)calloc(strlen(text) + 1, sizeof(char)); 137 | strcpy(buf, text); 138 | 139 | token = strtok(buf, sep); 140 | /* TODO if separator not found, leave */ 141 | if (!token) { 142 | /* TODO report reading problem */ 143 | return 0; 144 | } 145 | // printf("1st token '%s'\n", token); 146 | 147 | work = (char**)calloc(1024, sizeof(char*)); 148 | 149 | /* TODO get all elements */ 150 | while (token) { 151 | work[n_field] = 152 | (char*)calloc(strlen(token) + 1, sizeof(char*)); 153 | strcpy(work[n_field], token); 154 | 155 | n_field++; 156 | token = strtok(NULL, sep); 157 | // printf("token %ld '%s'\n", n_field, token); 158 | } 159 | 160 | result = n_field; 161 | 162 | free(buf); 163 | work = (char**)realloc(work, result * sizeof(char*)); 164 | 165 | *fields = work; 166 | 167 | /* Normal function termination */ 168 | return result; 169 | } 170 | 171 | /** 172 | * Free an array of strings, just as the ones returned by 173 | * `split_fields()` 174 | * 175 | * @param[in] n_fields The number of elements in the string array. 176 | * @param[out] fields A string array to be freed. 177 | * 178 | * \retval 0. 179 | * In case of sucess. 180 | * 181 | * \retval 1 182 | * In case of some failure, like zero number of elements, or 183 | * if the char array is the null pointer 184 | **/ 185 | 186 | int 187 | free_fields(size_t n_fields, char*** fields) 188 | { 189 | size_t ind; 190 | char** work; 191 | 192 | if ((n_fields == 0) || (! fields) || (! *fields)) { 193 | return 1; 194 | } 195 | 196 | work = *fields; 197 | 198 | for (ind = 0; ind < n_fields; ind++) { 199 | if (! work[ind]) { 200 | continue; 201 | } 202 | free(work[ind]); 203 | } 204 | 205 | free(work); 206 | 207 | return 0; 208 | } 209 | 210 | /** 211 | * Search a text in an array of strings, just as the ones returned by 212 | * `split_fields()` 213 | * 214 | * @param[in] texts The array of strings. 215 | * @param[in] n_texts The number of texts in the string array. 216 | * @param[int] key The text to be freed. Should not be the empty string. 217 | * 218 | * \retval The zero-based position where the key is. 219 | * In case of sucess. 220 | * 221 | * \retval -1 222 | * In case text is not found or some failure 223 | * of some failure, like zero number of elements, or 224 | * if the char array is the null pointer 225 | **/ 226 | 227 | int 228 | find_text(char* const* texts, size_t n_texts, const char* key) 229 | { 230 | size_t ind; 231 | 232 | /* if there's not where to find, return nothing was found */ 233 | if ((NULL == texts) || (0 == n_texts)) { 234 | return -1; 235 | } 236 | 237 | /* if there's not what to find, return nothing was found */ 238 | if ( (NULL == key) || (0 == strlen(key)) ) { 239 | return -1; 240 | } 241 | 242 | for (ind = 0; ind < n_texts; ind++) { 243 | if (!strcmp(texts[ind], key)) { 244 | return ind; 245 | } 246 | } 247 | 248 | return -1; 249 | } 250 | -------------------------------------------------------------------------------- /components_copyright/CC0: -------------------------------------------------------------------------------- 1 | 2 | CC0 1.0 Universal 3 | 4 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER. 5 | Statement of Purpose 6 | 7 | The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). 8 | 9 | Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. 10 | 11 | For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. 12 | 1. Copyright and Related Rights. 13 | 14 | A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: 15 | 16 | i. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; 17 | ii. moral rights retained by the original author(s) and/or performer(s); 18 | iii. publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; 19 | iv. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; 20 | v. rights protecting the extraction, dissemination, use and reuse of data in a Work; 21 | vi. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and 22 | vii. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. 23 | 24 | 2. Waiver. 25 | 26 | To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. 27 | 3. Public License Fallback. 28 | 29 | Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. 30 | 4. Limitations and Disclaimers. 31 | 32 | a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. 33 | b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. 34 | c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. 35 | d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. 36 | 37 | -------------------------------------------------------------------------------- /data/us-daily.csv: -------------------------------------------------------------------------------- 1 | date,states,positive,negative,pending,hospitalizedCurrently,hospitalizedCumulative,inIcuCurrently,inIcuCumulative,onVentilatorCurrently,onVentilatorCumulative,recovered,hash,dateChecked,death,hospitalized,total,totalTestResults,posNeg,deathIncrease,hospitalizedIncrease,negativeIncrease,positiveIncrease,totalTestResultsIncrease 2 | 20200410,56,492678,2036047,17444,48465,50683,12688,1185,5937,41,23395,a239216f735d012730b1307b4c06b3c68f737a38,2020-04-10T20:00:00Z,18461,50683,2546169,2528725,2528725,2037,2339,119310,34204,153514 3 | 20200409,56,458474,1916737,17631,46665,48344,12244,924,5794,39,21162,f6953f452ca9a18354b32f9b69fa6469e1226a75,2020-04-09T20:00:00Z,16424,48344,2392842,2375211,2375211,1877,1190,128443,34083,162526 4 | 20200408,56,424391,1788294,17228,41095,47154,9927,1013,4131,216,19378,fa1d3a52eca9246c015bf7adf7d665128fe678fc,2020-04-08T20:00:00Z,14547,47154,2229913,2212685,2212685,1901,1654,109403,30161,139564 5 | 20200407,56,394230,1678891,16557,39677,45500,9875,889,4076,151,18477,a5e7884a0ca207621727dc0d50f1ca8875d3134c,2020-04-07T20:00:00Z,12646,45500,2089678,2073121,2073121,1926,2302,117662,30415,148077 6 | 20200406,56,363815,1561229,17292,32210,43198,6943,814,2961,147,16584,d06b016555bbea9176088a939586dd5efa8f8aae,2020-04-06T20:00:00Z,10720,43198,1942336,1925044,1925044,1166,2975,120496,28746,149242 7 | 20200405,56,335069,1440733,17307,27909,40223,5499,760,652,147,14542,cedd281495192a9f1431764ea6fd7826c5b69272,2020-04-05T20:00:00Z,9554,40223,1793109,1775802,1775802,1175,2556,96629,25972,122601 8 | 20200404,56,309097,1344104,15573,26374,37667,5209,554,656,147,12840,6db964853a15a1b164c0b08bdfd22d7a66030955,2020-04-04T20:00:00Z,8379,37667,1668774,1653201,1653201,1353,4166,195732,33555,229287 9 | 20200403,56,275542,1148372,61980,23825,33501,4811,486,605,147,10861,7735c50d9411c9642fcf864b5f9d8082216a97ce,2020-04-03T20:00:00Z,7026,33501,1485894,1423914,1423914,1191,3303,100121,32080,132201 10 | 20200402,56,243462,1048251,62101,21135,30198,4410,456,574,140,8586,f70a916e390f097841daf97802a209e491400ade,2020-04-02T20:00:00Z,5835,30198,1353814,1291713,1291713,1089,4141,93717,28081,121798 11 | 20200401,56,215381,954534,59669,19408,26057,3937,407,561,140,7084,b8f54a14394812f51258e10e715ff73cc7915836,2020-04-01T20:00:00Z,4746,26057,1229584,1169915,1169915,943,3890,78247,25285,103532 12 | 20200331,56,190096,876287,59518,17353,22167,3487,236,507,,5666,3ffacaf02c8e7c62a7b24bfec954abe33e50e9dd,2020-03-31T20:00:00Z,3803,22167,1125901,1066383,1066383,820,3656,82673,24499,107172 13 | 20200330,56,165597,793614,65369,15216,18511,3087,187,451,,4560,17a0a8ee5768e0627408b1412d8edaf75340ca6b,2020-03-30T20:00:00Z,2983,18511,1024580,959211,959211,516,2248,97242,21206,118448 14 | 20200329,56,144391,696372,65545,13501,16263,2456,156,439,,4061,15989d4587d2d15b79a2eba66c7c41a5806e9955,2020-03-29T20:00:00Z,2467,16263,906308,840763,840763,466,2514,73593,19494,93087 15 | 20200328,56,124897,622779,65709,11872,13749,2174,140,390,,3148,7317ab547081d5530e3b56472c746ad14e6955ca,2020-03-28T20:00:00Z,2001,13749,813385,747676,747676,427,2207,88530,19423,107953 16 | 20200327,56,105474,534249,60091,10511,11542,1792,124,324,,2422,19f20a6df502e3a72fa5d0fbb8d34867510dd85e,2020-03-27T20:00:00Z,1574,11542,699814,639723,639723,366,2395,91968,18681,110649 17 | 20200326,56,86793,442281,60251,7387,9147,1299,91,258,,97,8ba147a5216f07eb4b3d3597e18707e25bdd76c0,2020-03-26T20:00:00Z,1208,9147,589325,529074,529074,277,3711,79090,17301,96391 18 | 20200325,56,69492,363191,51235,740,5436,,74,167,,147,05d35a917a6c008c07eb5fa7861a2ab3d7c1bece,2020-03-25T20:00:00Z,931,5436,483918,432683,432683,225,1345,66606,16931,83537 19 | 20200324,56,52561,296585,14433,369,4091,,,,,,598b28bafc63887621832f6c0d7ee20c7aee1289,2020-03-24T20:00:00Z,706,4091,363579,349146,349146,197,833,56425,9965,66390 20 | 20200323,56,42596,240160,14571,67,3258,,,,,,7b2c45da163b66c54889df90d2d4f74fc377dab6,2020-03-23T20:00:00Z,509,3258,297327,282756,282756,83,760,45054,10277,55331 21 | 20200322,56,32319,195106,2842,56,2498,,,,,,ff8010db1ae2a0b794b14ee47e12e685f27378a0,2020-03-22T20:00:00Z,426,2498,230267,227425,227425,129,534,37570,8649,46219 22 | 20200321,56,23670,157536,3468,,1964,,,,,,4ee29c9ca3535c7c434e73cd0b8f76902319a679,2020-03-21T20:00:00Z,297,1964,184674,181206,181206,50,1964,38566,6248,44814 23 | 20200320,56,17422,118970,3330,,,,,,,,4d22dcfafd9874a387adf1bc95fc00e5832bb96f,2020-03-20T20:00:00Z,247,,139722,136392,136392,62,0,30031,5352,35383 24 | 20200319,56,12070,88939,3016,,,,,,,,5c347a71d44cd1b8ad1d1b94eedd72a8bb86f13b,2020-03-19T20:00:00Z,185,,104025,101009,101009,43,0,21771,3972,25743 25 | 20200318,56,8098,67168,2526,,,,,,,,0ddf1b0e944f3c2a48fb95513a8a7f75443662ed,2020-03-18T20:00:00Z,142,,77792,75266,75266,23,0,19115,2067,21182 26 | 20200317,56,6031,48053,1687,,,,,,,,927351735bcfd512ed12afc653263e7b24de45de,2020-03-17T20:00:00Z,119,,55771,54084,54084,22,0,11949,1733,13682 27 | 20200316,56,4298,36104,1691,,,,,,,,bc0b27ce3f429eb47e147fd755110a2c0dbe8c4a,2020-03-16T20:00:00Z,97,,42093,40402,40402,21,0,13480,846,14326 28 | 20200315,51,3452,22624,2242,,,,,,,,abcee792a3f9097f844c627f58cf9f9afbda04b7,2020-03-15T20:00:00Z,76,,28318,26076,26076,13,0,5522,780,6302 29 | 20200314,51,2672,17102,1236,,,,,,,,dc82e812d3750bab133c60316ea1257c2a656e4d,2020-03-14T20:00:00Z,63,,21010,19774,19774,8,0,3489,508,3997 30 | 20200313,51,2164,13613,1130,,,,,,,,5b69572d0bad1b62fc2433ea230a95dbc61c7d67,2020-03-13T20:00:00Z,55,,16907,15777,15777,4,0,5572,645,6217 31 | 20200312,51,1519,8041,673,,,,,,,,37d3e70d420867c3fcf9d136da3479da46adf680,2020-03-12T20:00:00Z,51,,10233,9560,9560,8,0,1935,259,2194 32 | 20200311,51,1260,6106,563,,,,,,,,ca45b454c3a719adec75ad7cc322eb125cb9e401,2020-03-11T20:00:00Z,43,,7929,7366,7366,6,0,2294,255,2549 33 | 20200310,51,1005,3812,469,,,,,,,,c2d8538be9f6fde497c4e9c24e99801be3854ce1,2020-03-10T20:00:00Z,37,,5286,4817,4817,2,0,468,212,680 34 | 20200309,51,793,3344,313,,,,,,,,f482c54dcbedc0ef22df020b101fcf648170791a,2020-03-09T20:00:00Z,35,,4450,4137,4137,4,0,1009,231,1240 35 | 20200308,51,562,2335,347,,,,,,,,97fe14e2c4043bfb2b3d1e44f5e1ee54b2a7c91b,2020-03-08T20:00:00Z,31,,3244,2897,2897,4,0,496,141,637 36 | 20200307,51,421,1839,602,,,,,,,,579e832a2337579596a847a3a8e400687bc3845b,2020-03-07T21:00:00Z,27,,2862,2260,2260,1,0,251,125,376 37 | 20200306,37,296,1588,458,,,,,,,,0ae66f25d8aa3c8d6618ab9479f34b0f6583f4d5,2020-03-06T21:00:00Z,26,,2342,1884,1884,6,0,618,92,710 38 | 20200305,25,204,970,197,,,,,,,,8a15dc10e69abcecfd9352c4e4830e758f8887e2,2020-03-05T21:00:00Z,20,,1371,1174,1174,4,0,211,47,258 39 | 20200304,15,157,759,103,,,,,,,,38dc0bcedacec7715adb8d1346348bd18a80cd96,2020-03-04T21:00:00Z,16,,1019,916,916,2,0,753,97,850 40 | 20200303,2,60,6,,,,,,,,,c164d965c7a1510b16f6334b1e1ba5103d0481ea,2020-03-03T21:00:00Z,14,,66,66,66,3,0,6,25,31 41 | 20200302,2,35,,,,,,,,,,3aef0d42c1db7236e50f664a77c2c3cddc1487a9,2020-03-02T21:00:00Z,11,,35,35,35,3,0,0,4,4 42 | 20200301,2,31,,,,,,,,,,73d01787c42bb880e75eae6c34518fc4c1d5ce35,2020-03-01T21:00:00Z,8,,31,31,31,3,0,0,13,13 43 | 20200229,1,18,,,,,,,,,,243260e18d47e670dade08ee5a5195fcc8b5a9db,2020-02-29T21:00:00Z,5,,18,18,18,1,0,0,9,9 44 | 20200228,1,9,,,,,,,,,,7de1cbe8b7b0a50a3afdd4b9c4d69ff78318681e,2020-02-28T21:00:00Z,4,,9,9,9,,,,, --------------------------------------------------------------------------------