From 5bfdf5575e2a186241f942f1cfe83e7e5cde96d9 Mon Sep 17 00:00:00 2001 From: Gabriele Gilardi Date: Sat, 27 Jun 2020 20:04:14 +0900 Subject: [PATCH] test and format --- Code_Python/filters.py | 64 ++-- Code_Python/sine_fast.csv | 500 -------------------------------- Code_Python/sine_fast_noise.csv | 500 -------------------------------- Code_Python/sine_slow.csv | 500 -------------------------------- Code_Python/sine_slow_noise.csv | 500 -------------------------------- Code_Python/synthetic.py | 70 ++--- Code_Python/test.py | 130 ++++----- README.md | 55 +++- 8 files changed, 181 insertions(+), 2138 deletions(-) delete mode 100644 Code_Python/sine_fast.csv delete mode 100644 Code_Python/sine_fast_noise.csv delete mode 100644 Code_Python/sine_slow.csv delete mode 100644 Code_Python/sine_slow_noise.csv diff --git a/Code_Python/filters.py b/Code_Python/filters.py index d98e912..f17f996 100644 --- a/Code_Python/filters.py +++ b/Code_Python/filters.py @@ -1,5 +1,5 @@ """ -Signal Filtering/Smoothing and Generation of Synthetic Time-Series. +Signal Filtering and Generation of Synthetic Time-Series. Copyright (c) 2020 Gabriele Gilardi @@ -17,12 +17,12 @@ na Number of coefficients in array Notes: - the filter is applied starting from index idx = MAX(0, nb-1, na-1). - non filtered data are set equal to the input, i.e. Y[0:idx-1] = X[0:idx-1] -- X needs to be a 1D array. +- X must be a 1D array. Filter list: ----------- -Generic b, Generic +Generic b, a Generic filter SMA N Simple moving average EMA N/alpha Exponential moving average WMA N Weighted moving average @@ -66,10 +66,11 @@ from scipy import signal import matplotlib.pyplot as plt -def plot_signals(signals, names=None, start=0): +def plot_signals(signals, names=None, start=0, end=None): """ Plot the signals specified in list with their names specified in - list . Each signal is plotted in its full length. + list . Each signal is plotted in its full length unless differently + specified. """ # Identify the signals by index if their name is not specified if (names is None): @@ -79,12 +80,12 @@ def plot_signals(signals, names=None, start=0): legend = names # Loop over the signals + t_max = 0 for signal in signals: signal = signal.flatten() - end = len(signal) - t = np.arange(start, end) - plt.plot(t, signal[start:end]) + t_max = np.amax([t_max, len(signal)]) + plt.plot(signal) # If no name is given use the list index to identify the signals if (names is None): @@ -96,6 +97,10 @@ def plot_signals(signals, names=None, start=0): plt.ylabel('Value') plt.grid(b=True) plt.legend(legend) + if (end is None): + plt.xlim(start, t_max) + else: + plt.xlim(start, end) plt.show() @@ -315,7 +320,7 @@ class Filter: elif (N == 3): beta = np.exp(-np.pi / P) - alpha = 2.0 * beta * np.cos(1.738 * np.pi / P) + alpha = 2.0 * beta * np.cos(np.sqrt(3.0) * np.pi / P) wb = np.array([1.0, 1.0]) \ * (1.0 - alpha * (1.0 - beta ** 2.0) - beta ** 4.0) / 2.0 wa = np.array([1.0, - (alpha + beta ** 2.0), @@ -376,7 +381,7 @@ class Filter: self.b = np.array([1.0 - alpha / 2.0, -(1.0 - alpha / 2.0)]) self.a = np.array([1.0, - (1.0 - alpha)]) - Y = self.data - self.data[0, :] + Y = self.data - self.data[0] # Shift to zero for i in range(N): Y, self.idx = filter_data(Y, self.b, self.a) @@ -481,7 +486,7 @@ class Filter: K = np.arange(1, nel) w = np.zeros(nel) - w[0] = 1.0 / N + w[0] = 1.0 / P w[1:] = np.sin(np.pi * K / P) / (np.pi * K) self.b = w @@ -534,7 +539,7 @@ class Filter: or an array with one value for each sample. For numerical stability it should be 0 < alpha, beta < 1. """ - n_samples = len(data) + n_samples = len(self.data) Y = np.zeros(n_samples) # Change scalar arguments to arrays if necessary @@ -615,20 +620,27 @@ class Filter: gamma = (beta ** 2.0) / (2.0 * alpha) # Apply the alpha-beta-gamma filter - Y = self.abg(alpha=alpha, beta=beta, gamma=gamma, dt=dt) + Y = self.ABG(alpha=alpha, beta=beta, gamma=gamma, dt=dt) return Y - def plot_frequency(self): + def plot_response(self): """ - Plots the frequency response (in decibels) of the filter with transfer - response coefficients and . + Plots the frequency response (in decibels) and the lag (group delay) + of the filter with coefficients and . """ + # Frequency response w, h = signal.freqz(self.b, self.a) h_db = 20.0 * np.log10(np.abs(h)) # Convert to decibels - wf = w / (2.0 * np.pi) # Scale to [0, 0.5] - # Plot and format + # Lag / Group delay + w, gd = signal.group_delay((self.b, self.a)) + + # Scale frequency to [0, 0.5] + wf = w / (2.0 * np.pi) + + # Plot and format frequency response + plt.subplot(1, 2, 1) plt.plot(wf, h_db) plt.axhline(-3.0, lw=1.5, ls='--', C='r') # -3 dB (50% power loss) plt.grid(b=True) @@ -637,20 +649,16 @@ class Filter: plt.ylabel('$h$ [db]') legend = ['Filter', '-3dB'] plt.legend(legend) - plt.show() + plt.title('Frequency Response', fontweight="bold") - def plot_lag(self): - """ - Plots the lag (group delay) of the filter with transfer response - coefficients and . - """ - w, gd = signal.group_delay((self.b, self.a)) - wf = w / (2.0 * np.pi) # Scale to [0, 0.5] - - # Plot and format + # Plot and format lag/group delay + plt.subplot(1, 2, 2) plt.plot(wf, gd) plt.grid(b=True) plt.xlim(np.amin(wf), np.amax(wf)) plt.xlabel(r'$\omega$ [rad/sample]') plt.ylabel('$gd$ [samples]') + plt.title('Lag / Group Delay', fontweight="bold") + + # Show plots plt.show() diff --git a/Code_Python/sine_fast.csv b/Code_Python/sine_fast.csv deleted file mode 100644 index 4744f24..0000000 --- a/Code_Python/sine_fast.csv +++ /dev/null @@ -1,500 +0,0 @@ -107.0614811 -107.7330878 -108.327428 -108.8385632 -109.2613865 -109.5916731 -109.8261228 -109.9623932 -109.9991226 -109.9359441 -109.7734889 -109.5133803 -109.1582171 -108.711548 -108.177836 -107.5624138 -106.8714305 -106.1117901 -105.2910827 -104.4175085 -103.4997961 -102.5471148 -101.5689837 -100.5751757 -99.57562082 -98.58030618 -97.59917665 -96.64203535 -95.71844572 -94.83763597 -94.00840685 -93.23904375 -92.53723389 -91.90998952 -91.36357787 -90.90345849 -90.53422874 -90.25957785 -90.08225004 -90.00401711 -90.02566074 -90.14696467 -90.36671687 -90.68272165 -91.09182159 -91.58992911 -92.17206728 -92.83241956 -93.56438794 -94.36065883 -95.21327615 -96.11372083 -97.05299593 -98.02171652 -99.01020346 -100.0085801 -101.0068711 -101.9951017 -102.9633979 -103.9020848 -104.8017834 -105.6535041 -106.4487369 -107.1795361 -107.8385997 -108.4193426 -108.9159622 -109.3234965 -109.6378735 -109.855952 -109.9755531 -109.9954818 -109.915539 -109.7365233 -109.4602235 -109.0894003 -108.6277588 -108.0799116 -107.4513326 -106.7483024 -105.9778453 -105.1476596 -104.2660402 -103.3417959 -102.3841615 -101.4027053 -100.4072338 -99.40769329 -98.41407094 -97.43629467 -96.4841341 -95.5671029 -94.69436374 -93.87463674 -93.11611235 -92.42636949 -91.81229984 -91.28003899 -90.8349051 -90.48134581 -90.22289377 -90.06213134 -90.00066481 -90.03910833 -90.17707779 -90.41319464 -90.74509968 -91.16947663 -91.68208524 -92.27780371 -92.95067982 -93.6939904 -94.50030854 -95.36157778 -96.26919261 -97.21408443 -98.18681219 -99.17765673 -100.1767179 -101.1740133 -102.1595783 -103.1235656 -104.0563432 -104.9485912 -105.7913945 -106.5763321 -107.2955612 -107.9418954 -108.5088769 -108.9908404 -109.3829705 -109.681349 -109.8829947 -109.9858927 -109.989015 -109.8923304 -109.6968049 -109.4043921 -109.0180138 -108.5415304 -107.9797029 -107.3381448 -106.6232664 -105.8422105 -105.0027812 -104.1133657 -103.1828509 -102.220534 -101.2360304 -100.2391767 -99.23993321 -98.24828408 -97.27413753 -96.32722688 -95.41701338 -94.55259156 -93.74259844 -92.99512721 -92.31764636 -91.71692504 -91.19896547 -90.76894293 -90.43115406 -90.18897394 -90.04482234 -90.00013959 -90.05537214 -90.20996812 -90.46238287 -90.81009433 -91.24962829 -91.77659307 -92.38572342 -93.07093311 -93.82537573 -94.64151317 -95.51119083 -96.42571919 -97.37596058 -98.35242051 -99.3453425 -100.3448056 -101.3408236 -102.3234444 -103.2828502 -104.2094548 -105.094 -105.9276476 -106.702068 -107.4095237 -108.0429458 -108.5960055 -109.0631767 -109.4397917 -109.7220873 -109.9072431 -109.993409 -109.9797241 -109.866325 -109.6543449 -109.3459018 -108.9440775 -108.452887 -107.877238 -107.2228822 -106.4963578 -105.7049239 -104.8564883 -103.9595283 -103.023006 -102.0562788 -101.069006 -100.071052 -99.07238803 -98.08299249 -97.11275106 -96.17135806 -95.26821959 -94.4123595 -93.61232928 -92.87612254 -92.21109524 -91.62389209 -91.12038025 -90.70559063 -90.38366768 -90.15782795 -90.03032794 -90.0024416 -90.07444756 -90.24562636 -90.51426763 -90.87768721 -91.33225392 -91.87342589 -92.4957959 -93.19314543 -93.95850681 -94.78423278 -95.66207298 -96.58325632 -97.53857862 -98.51849464 -99.51321336 -100.5127959 -101.5072548 -102.4866536 -103.4412067 -104.3613763 -105.2379685 -106.0622247 -106.8259091 -107.5213913 -108.1417222 -108.6807037 -109.1329505 -109.4939439 -109.760077 -109.9286906 -109.9980999 -109.9676116 -109.8375302 -109.6091554 -109.2847692 -108.8676126 -108.3618537 -107.772546 -107.1055775 -106.3676125 -105.5660244 -104.7088224 -103.8045714 -102.8623064 -101.8914422 -100.9016793 -99.90290715 -98.9051051 -97.91824288 -96.95218089 -96.0165717 -95.12076361 -94.27370723 -93.48386608 -92.75913199 -92.10674625 -91.5332273 -91.04430554 -90.64486612 -90.33890011 -90.12946461 -90.01865224 -90.0075702 -90.09632921 -90.28404242 -90.56883428 -90.94785922 -91.41733016 -91.97255631 -92.60799001 -93.31728224 -94.09334597 -94.92842704 -95.81418159 -96.74175945 -97.70189258 -98.68498763 -99.68122185 -100.6806412 -101.6732598 -102.6491598 -103.5985902 -104.5120647 -105.3804561 -106.1950878 -106.9478202 -107.6311323 -108.2381967 -108.7629477 -109.2001423 -109.545412 -109.7953072 -109.9473309 -109.9999641 -109.952681 -109.805954 -109.5612492 -109.2210115 -108.7886405 -108.2684563 -107.6656565 -106.9862639 -106.2370669 -105.4255512 -104.5598252 -103.6485389 -102.7007976 -101.7260708 -100.7340978 -99.7347898 -98.73813174 -97.75408184 -96.79247242 -95.86291156 -94.97468712 -94.13667394 -93.35724517 -92.64418862 -92.0046289 -91.44495629 -90.97076285 -90.58678656 -90.29686399 -90.10389194 -90.00979854 -90.01552392 -90.12101089 -90.32520546 -90.62606737 -91.02059052 -91.50483296 -92.0739563 -92.72227405 -93.44330843 -94.22985511 -95.07405517 -95.96747364 -96.90118378 -97.86585627 -98.85185241 -99.84932047 -100.8482941 -101.8387918 -102.8109169 -103.7549563 -104.6614774 -105.5214225 -106.3261994 -107.0677671 -107.7387159 -108.332342 -108.8427142 -109.2647328 -109.5941813 -109.827768 -109.9631588 -109.999001 -109.9349365 -109.7716054 -109.5106397 -109.1546468 -108.7071837 -108.1727212 -107.5565997 -106.8649751 -106.104758 -105.2835441 -104.4095388 -103.4914748 -102.5385252 -101.5602115 -100.5663086 -99.56674743 -98.57151513 -97.59055579 -96.63367081 -95.71042109 -94.83003142 -94.00129836 -93.23250235 -92.53132494 -91.90477207 -91.35910403 -90.89977297 -90.53136837 -90.25757121 -90.08111717 -90.00376934 -90.02630053 -90.14848564 -90.36910382 -90.68595073 -91.09586054 -91.59473757 -92.1775972 -92.8386157 -93.57118838 -94.36799563 -95.22107599 -96.12190579 -97.06148422 -98.03042334 -99.01904181 -100.0174617 -101.0157071 -102.0038039 -102.9718793 -103.9102608 -104.8095721 -105.6608279 -106.4555225 -107.1857156 -107.8441115 -108.4241316 -108.9199806 -109.326704 -109.6402381 -109.8574502 -109.9761699 -109.9952109 -109.9143831 -109.7344941 -109.4573413 -109.0856938 -108.623265 -108.0746755 -107.4454065 -106.7417454 -105.970723 -105.1400431 -104.2580057 -103.3334236 -102.3755351 -101.393911 -100.3983594 -99.39882755 -98.4053024 -97.42771095 -96.47582096 -95.5591434 -94.68683741 -93.86761879 -93.10967289 -92.42057287 -91.80720397 -91.27569478 -90.83135597 -90.47862722 -90.22103288 -90.06114674 -90.00056634 -90.03989698 -90.17874567 -90.41572509 -90.74846742 -91.173648 -91.68701858 -92.28344971 -92.95698207 -93.70088593 -94.50772846 -95.36944795 -96.27743439 -97.22261547 -98.19554726 -99.18650854 -100.185598 -101.182833 -102.1682495 -103.1320015 -104.0644597 diff --git a/Code_Python/sine_fast_noise.csv b/Code_Python/sine_fast_noise.csv deleted file mode 100644 index d050cea..0000000 --- a/Code_Python/sine_fast_noise.csv +++ /dev/null @@ -1,500 +0,0 @@ -99.74607254 -100.9985047 -105.6589958 -110.3758689 -111.4562084 -120.2469638 -92.47859905 -99.7988658 -111.3608434 -97.95849713 -116.4332767 -120.4499707 -109.0983808 -109.6064832 -109.6401406 -109.901406 -99.27412717 -85.79481934 -98.4200087 -103.7514383 -106.6847133 -82.19057649 -98.78947827 -98.48496831 -113.8385423 -90.1639471 -78.93094346 -99.02728187 -86.61426422 -91.37872779 -90.99097198 -86.27464383 -90.68010168 -97.21054672 -106.2564401 -83.25943317 -93.71136548 -92.17605257 -75.2199172 -91.33571727 -92.71473597 -88.49934963 -95.88603552 -100.5548966 -83.65217931 -64.61541321 -95.02446505 -104.0182222 -96.86731681 -96.7533356 -102.581885 -101.1446821 -119.619968 -99.10202402 -80.48133979 -102.0149845 -88.5733381 -113.6041862 -89.36855047 -96.11116099 -117.5312372 -117.8596423 -99.58612933 -101.364107 -100.9620158 -119.1091278 -100.8018557 -113.2488968 -101.8717012 -125.2536113 -108.9875467 -104.0605359 -99.13376678 -115.6371565 -117.0847191 -109.0894949 -116.900752 -109.8769949 -110.1912392 -105.9834098 -113.290719 -103.6682498 -114.0142086 -106.8546324 -100.9786188 -112.6259146 -105.0708015 -86.57901721 -86.16099103 -95.71501275 -95.32165336 -72.33052217 -79.04885583 -97.64257546 -98.83452607 -105.4753737 -82.66939704 -94.38495045 -95.62961653 -101.0090845 -102.2562397 -89.37424999 -87.00897709 -98.34732987 -87.17473187 -88.29022085 -98.71513962 -94.13589388 -80.98525401 -90.0555734 -105.663407 -91.85344181 -104.7828486 -94.72801184 -98.85182601 -85.84903801 -97.12691481 -103.1898604 -97.53009653 -100.77248 -96.8109706 -88.20273689 -91.28975471 -103.3826265 -100.9843703 -125.2089148 -117.6572149 -102.3665879 -98.09826068 -115.6305485 -98.2446131 -113.5826621 -98.52151793 -96.54353022 -110.0970167 -119.0894679 -82.70969544 -105.5948041 -108.1286679 -114.7791846 -96.55073367 -108.9916162 -103.6793435 -104.9947092 -111.5026331 -120.0264838 -86.72900841 -107.0418354 -110.8793625 -93.15449934 -92.0470496 -93.10674393 -115.028515 -94.87926251 -80.65842088 -90.26230642 -94.37404315 -90.41820803 -79.3694285 -104.7989829 -89.06354298 -74.92484744 -88.55520858 -88.17327988 -92.44427227 -106.2241627 -89.69936655 -110.1258867 -81.40795851 -79.50154712 -102.8429063 -91.98680097 -102.6747939 -87.29859659 -105.5483568 -98.71704962 -89.0729352 -107.3147853 -99.52825529 -90.68454466 -95.79189223 -106.4811685 -106.5963564 -91.72663379 -85.5984658 -107.1724503 -97.48127236 -100.7599682 -112.8430896 -95.08863157 -94.24392767 -122.5515637 -93.0184012 -126.8264613 -106.5008815 -107.1326088 -98.20586297 -119.7951298 -120.5518721 -104.4271292 -98.12433617 -119.4486154 -117.2844245 -98.65840409 -104.769913 -101.1362625 -101.6987705 -115.5489754 -103.7431279 -126.2359309 -101.432232 -115.0751813 -89.36542689 -95.37135596 -101.3991848 -112.1568858 -113.3831319 -92.53006936 -106.1232446 -94.13544376 -91.43306306 -104.9240959 -84.89509638 -99.068661 -104.8793253 -84.08844816 -90.63766684 -79.86846014 -89.03795827 -106.3052081 -104.302822 -89.86796622 -100.0547195 -87.27556042 -86.13651022 -84.52982443 -89.04938259 -88.45288282 -93.9221361 -77.90453416 -89.11116191 -103.8236699 -100.5704991 -105.0038536 -82.48819192 -90.70077556 -93.20727118 -96.469969 -124.0621945 -126.208824 -113.8096177 -116.2336238 -98.2431384 -104.3742794 -110.8824523 -82.66158983 -93.36370081 -94.50577091 -109.2124186 -108.6860623 -102.2125708 -92.7364688 -117.8992898 -111.8389321 -116.1499254 -103.7510718 -106.1711274 -115.8905823 -94.9394883 -111.3292051 -119.6381222 -92.55331545 -94.6690993 -98.14521121 -103.3060014 -83.8533364 -110.1232204 -86.89602288 -84.7500101 -81.00671318 -111.9346161 -72.63786168 -108.923088 -67.93841767 -81.28708323 -108.8012014 -86.33287375 -90.99581497 -115.0077996 -75.6884257 -92.27903243 -96.95099649 -73.17740219 -81.23584289 -108.1412357 -85.2937767 -73.67059556 -99.09771683 -86.41655575 -65.50046252 -77.50463602 -107.5223067 -97.43228837 -97.24231792 -92.36343746 -102.3239382 -107.6653067 -84.70401863 -102.1418889 -110.6478756 -110.8038522 -121.8958125 -116.6996419 -126.1771719 -102.6189077 -106.3284051 -104.7333304 -103.7911867 -110.1573359 -117.8848836 -110.7704487 -119.4976273 -99.29985521 -111.778141 -123.775525 -94.96070742 -113.0174536 -101.3223299 -112.8532801 -111.4698438 -98.0466213 -100.9512001 -112.8041168 -109.1341396 -110.2363954 -107.752259 -90.56497789 -95.08630261 -100.6715567 -102.8404892 -85.11606497 -87.83600724 -111.9823882 -90.93281134 -100.7576002 -104.9842738 -85.7248994 -72.57166009 -106.7911372 -83.80974127 -83.42667246 -103.4631772 -91.94917091 -89.44543328 -97.38905178 -85.58235769 -99.69552244 -92.69614929 -89.05675883 -79.6668893 -103.2663666 -89.02871412 -92.60746955 -78.08256782 -87.87930204 -106.0975357 -101.2546742 -95.97578469 -106.3580882 -105.5082865 -105.8051223 -109.3086288 -88.2841571 -107.1446562 -105.0249265 -91.34560811 -101.6362066 -97.97629225 -107.398384 -113.9392197 -106.0515676 -107.7642295 -107.9836656 -108.8387993 -130.8390171 -107.8067205 -104.2591576 -101.600015 -113.5900125 -134.1875781 -101.7261452 -92.00590968 -106.4209836 -102.6942079 -105.7008816 -103.9306204 -116.200302 -89.28654287 -98.26692557 -109.0136698 -110.6672568 -108.8077604 -90.38695194 -110.8817082 -103.4241109 -107.4745031 -90.91627193 -107.7650809 -94.89064041 -93.08468827 -111.8944482 -83.89803856 -92.31150457 -87.05840489 -85.72434748 -92.87781938 -92.677094 -92.16102828 -88.39332676 -78.57679378 -80.14248863 -99.90498722 -80.40409998 -94.60128577 -100.937819 -69.68454717 -79.35756318 -109.0345368 -97.2767808 -94.59856421 -99.47823966 -91.72744188 -89.55714145 -101.822452 -98.00835693 -100.9320037 -109.2875592 -109.663176 -91.50602111 -91.41620387 -108.7803863 -112.0525174 -94.71657797 -118.9798346 -112.2837288 -113.3716799 -121.3109877 -108.8753364 -115.4845303 -99.15231977 -102.6315514 -88.44010676 -103.2309702 -116.0166296 -100.8031655 -97.7434958 -107.4554646 -116.3636227 -97.52347706 -107.7511089 -90.12846298 -92.07561775 -96.17594686 -110.4881757 -99.48994336 -93.41407809 -77.49294169 -98.7835899 -88.90963562 -93.10638765 -95.05029833 -95.40861665 -75.66158751 -101.0185954 -78.31471081 -107.98879 -92.65178182 -97.7014015 -103.3937285 -97.54252609 -87.2119731 -90.3813286 -69.14035241 -92.06857878 -75.30933914 -95.78452738 -79.05232302 -77.2929126 -78.5735926 -86.2639689 -103.8629931 -114.2854047 -109.9225833 -108.1768798 -83.15688423 -121.5107716 -84.33275404 -103.371499 -104.8325784 -93.06763 diff --git a/Code_Python/sine_slow.csv b/Code_Python/sine_slow.csv deleted file mode 100644 index 0917159..0000000 --- a/Code_Python/sine_slow.csv +++ /dev/null @@ -1,500 +0,0 @@ -107.0742605 -106.8347785 -106.5877031 -106.3333087 -106.0718779 -105.8037012 -105.5290765 -105.248309 -104.9617106 -104.6695997 -104.3723008 -104.0701443 -103.7634657 -103.452606 -103.1379104 -102.8197285 -102.4984139 -102.1743235 -101.8478174 -101.5192584 -101.1890115 -100.8574436 -100.5249231 -100.1918193 -99.8585025 -99.52534286 -99.19271057 -98.86097518 -98.53050526 -98.20166796 -97.87482862 -97.55035036 -97.22859368 -96.90991606 -96.59467155 -96.28321039 -95.97587861 -95.67301766 -95.37496402 -95.08204884 -94.79459755 -94.51292949 -94.23735762 -93.96818809 -93.70571995 -93.45024482 -93.20204651 -92.96140078 -92.728575 -92.50382783 -92.28740897 -92.07955886 -91.88050842 -91.69047881 -91.50968115 -91.3383163 -91.17657465 -91.02463589 -90.88266885 -90.75083123 -90.62926951 -90.51811876 -90.41750245 -90.32753238 -90.24830849 -90.17991882 -90.12243934 -90.0759339 -90.04045419 -90.01603962 -90.0027173 -90.00050205 -90.00939633 -90.02939025 -90.0604616 -90.10257585 -90.15568623 -90.21973372 -90.29464716 -90.38034334 -90.47672703 -90.58369115 -90.70111688 -90.82887374 -90.96681979 -91.11480178 -91.2726553 -91.44020498 -91.61726465 -91.80363762 -91.99911681 -92.20348504 -92.41651527 -92.63797081 -92.86760563 -93.1051646 -93.35038379 -93.60299075 -93.86270484 -94.12923752 -94.40229267 -94.68156691 -94.96674998 -95.25752503 -95.55356901 -95.85455301 -96.16014263 -96.46999837 -96.78377597 -97.10112682 -97.42169834 -97.74513438 -98.07107559 -98.39915985 -98.72902266 -99.06029754 -99.39261643 -99.72561013 -100.0589087 -100.3921418 -100.7249392 -101.0569312 -101.387749 -101.7170249 -102.0443933 -102.3694903 -102.6919547 -103.0114284 -103.3275564 -103.6399874 -103.9483743 -104.2523746 -104.5516505 -104.8458694 -105.1347046 -105.417835 -105.6949462 -105.9657302 -106.2298863 -106.4871209 -106.7371483 -106.9796907 -107.2144785 -107.441251 -107.6597562 -107.8697514 -108.0710032 -108.2632881 -108.4463923 -108.6201126 -108.7842558 -108.9386397 -109.0830927 -109.2174542 -109.3415752 -109.4553175 -109.5585549 -109.6511727 -109.7330679 -109.8041496 -109.8643389 -109.9135687 -109.9517845 -109.9789438 -109.9950164 -109.9999845 -109.9938425 -109.9765972 -109.9482679 -109.908886 -109.8584952 -109.7971515 -109.7249232 -109.6418903 -109.5481453 -109.4437921 -109.3289469 -109.2037371 -109.0683018 -108.9227916 -108.7673682 -108.6022041 -108.4274829 -108.2433986 -108.0501559 -107.8479694 -107.6370638 -107.4176733 -107.1900417 -106.9544219 -106.7110757 -106.4602735 -106.2022938 -105.9374233 -105.6659563 -105.3881943 -105.1044461 -104.8150267 -104.5202579 -104.220467 -103.9159871 -103.6071565 -103.2943183 -102.9778201 -102.6580136 -102.3352539 -102.0098998 -101.6823127 -101.3528564 -101.0218972 -100.6898026 -100.3569417 -100.0236842 -99.69040033 -99.35746047 -99.02523447 -98.69409145 -98.3643993 -98.03652432 -97.71083078 -97.38768052 -97.06743257 -96.75044272 -96.43706316 -96.12764206 -95.82252317 -95.5220455 -95.22654287 -94.93634359 -94.65177007 -94.37313849 -94.10075839 -93.83493239 -93.57595584 -93.32411646 -93.07969403 -92.84296013 -92.61417775 -92.39360108 -92.18147519 -91.97803573 -91.78350875 -91.59811036 -91.42204654 -91.25551289 -91.09869444 -90.95176541 -90.81488905 -90.68821742 -90.57189126 -90.4660398 -90.37078066 -90.28621965 -90.21245074 -90.14955587 -90.09760493 -90.05665563 -90.02675346 -90.00793166 -90.00021112 -90.00360044 -90.01809583 -90.04368121 -90.08032813 -90.1279959 -90.18663154 -90.25616991 -90.33653376 -90.4276338 -90.52936882 -90.64162579 -90.76427999 -90.89719515 -91.0402236 -91.19320643 -91.35597369 -91.52834453 -91.71012745 -91.90112048 -92.10111144 -92.30987813 -92.5271886 -92.75280144 -92.98646596 -93.22792259 -93.47690304 -93.73313071 -93.99632093 -94.26618128 -94.54241195 -94.82470605 -95.11274994 -95.4062236 -95.70480099 -96.00815038 -96.31593475 -96.62781215 -96.94343607 -97.26245586 -97.58451709 -97.90926193 -98.23632961 -98.56535673 -98.89597776 -99.22782537 -99.56053086 -99.89372461 -100.2270364 -100.560096 -100.8925333 -101.223979 -101.5540649 -101.8824242 -102.208692 -102.532506 -102.8535064 -103.1713365 -103.4856432 -103.7960774 -104.1022941 -104.4039531 -104.7007192 -104.9922628 -105.27826 -105.558393 -105.8323506 -106.0998283 -106.3605292 -106.6141634 -106.8604492 -107.099113 -107.3298897 -107.5525227 -107.7667649 -107.9723781 -108.1691339 -108.3568138 -108.5352092 -108.7041219 -108.8633642 -109.0127593 -109.1521411 -109.2813548 -109.4002568 -109.5087151 -109.6066092 -109.6938302 -109.7702812 -109.8358775 -109.8905459 -109.9342259 -109.9668689 -109.9884386 -109.9989111 -109.9982747 -109.9865302 -109.9636905 -109.9297811 -109.8848396 -109.828916 -109.7620723 -109.684383 -109.5959342 -109.4968242 -109.3871632 -109.2670729 -109.1366869 -108.99615 -108.8456182 -108.6852589 -108.5152502 -108.335781 -108.1470506 -107.9492689 -107.7426554 -107.5274397 -107.3038611 -107.0721677 -106.8326171 -106.5854755 -106.3310173 -106.0695253 -105.80129 -105.5266095 -105.2457888 -104.95914 -104.6669816 -104.3696381 -104.0674399 -103.7607227 -103.4498274 -103.1350992 -102.816888 -102.4955471 -102.1714337 -101.8449078 -101.5163322 -101.186072 -100.854494 -100.5219666 -100.1888594 -99.85554228 -99.5223857 -99.18975974 -98.85803397 -98.52757694 -98.19875578 -97.87193581 -97.54748015 -97.22574925 -96.90710057 -96.59188812 -96.28046212 -95.97316855 -95.67034882 -95.37233938 -95.0794713 -94.79206997 -94.51045469 -94.23493835 -93.96582703 -93.70341973 -93.44800798 -93.19987555 -92.95929812 -92.72654296 -92.50186867 -92.28552487 -92.07775191 -91.87878064 -91.68883211 -91.50811735 -91.33683715 -91.17518179 -91.02333087 -90.88145311 -90.74970613 -90.62823631 -90.51717859 -90.41665636 -90.32678131 -90.24765328 -90.17936019 -90.12197791 -90.07557019 -90.0401886 -90.01587244 -90.00264873 -90.00053216 -90.00952508 -90.0296175 -90.06078709 -90.10299924 -90.15620703 -90.22035136 -90.29536095 -90.38115249 -90.47763064 -90.58468822 -90.70220629 -90.83005429 -90.96809017 -91.11616057 -91.274101 -91.44173597 -91.61887924 -91.80533401 -92.00089312 -92.2053393 -92.41844542 -92.6399747 -92.86968104 -93.10730921 -93.35259522 -93.60526656 -93.86504249 -94.13163442 -94.40474614 -94.68407424 -94.96930838 -95.26013166 -95.55622096 -95.85724735 -96.16287637 -96.47276846 -96.78657933 -97.10396035 -97.42455888 -97.74801876 -98.0739806 -98.40208226 -98.73195923 -99.063245 -99.39557151 -99.72856955 -100.0618691 -100.3951 -100.7278919 -101.0598751 -101.3906808 diff --git a/Code_Python/sine_slow_noise.csv b/Code_Python/sine_slow_noise.csv deleted file mode 100644 index 04bee1f..0000000 --- a/Code_Python/sine_slow_noise.csv +++ /dev/null @@ -1,500 +0,0 @@ -115.5672342 -101.3357809 -101.6724076 -113.7876408 -95.68910046 -108.8854032 -104.4579998 -83.7184095 -105.1376931 -103.3840573 -97.108047 -102.8863194 -91.80224238 -107.8733355 -100.0570623 -84.77812113 -104.2708433 -83.58915888 -119.2028286 -91.36226567 -103.5615001 -107.6779544 -94.76216627 -100.0407076 -98.59099393 -98.97322064 -89.03294327 -96.61044071 -101.9896298 -109.0401123 -110.352971 -104.1915472 -117.1589045 -97.90848846 -85.20625064 -92.69288295 -100.2260665 -100.1566312 -80.02766644 -91.42814089 -89.3864446 -88.9677123 -108.4958817 -94.18647487 -94.96847674 -77.22443059 -97.89079577 -83.87671394 -88.35074546 -98.77394575 -81.99414957 -83.64615794 -71.43748108 -89.16806585 -78.70996228 -99.22607491 -96.09085418 -105.6424002 -88.22616544 -86.34174213 -95.34869802 -93.71763483 -106.1807007 -83.92146704 -108.3799273 -88.06765169 -77.6132481 -83.73888411 -94.14809038 -87.01159366 -99.624985 -105.3576787 -85.83087919 -90.18409924 -72.30046881 -100.1933841 -98.31605299 -90.49155266 -96.62514348 -95.15200808 -94.15575222 -95.58012899 -101.6875235 -101.115882 -70.37226926 -72.90624072 -86.60314889 -97.59436959 -94.97845285 -98.98201763 -80.16347967 -83.20359805 -102.0810666 -85.19007105 -91.29049014 -95.0957678 -89.50163019 -91.17021425 -97.8800246 -75.52607134 -92.57844899 -106.1497789 -96.41411921 -92.36485672 -89.51422721 -104.7838475 -96.17725716 -90.01678633 -82.28560782 -96.74762492 -89.87980463 -102.9524253 -91.49356528 -88.47786854 -91.89953738 -92.05679454 -103.5521627 -104.9122446 -114.7613801 -87.09679678 -108.9969937 -97.64873656 -92.90714047 -99.90629576 -90.47980483 -95.68336412 -92.59225994 -109.7754145 -99.40222421 -117.9788645 -102.697881 -124.4631449 -92.88725376 -105.6340206 -98.15966424 -95.83107285 -105.6690485 -102.9762599 -104.4605389 -102.4323003 -108.6613406 -112.4860409 -101.3532559 -105.8401355 -124.8188451 -117.4719577 -114.0244769 -114.093192 -108.6186442 -117.267081 -109.8336009 -98.10280839 -108.2027746 -107.8761995 -108.6823563 -106.5287627 -117.1288934 -112.7673049 -107.2468872 -102.2892507 -106.3075369 -108.8614441 -96.3907438 -107.4083301 -122.8648027 -113.1388697 -116.0475937 -91.53295838 -114.0964309 -125.5864009 -123.4359527 -95.15809582 -114.3160099 -97.68391886 -114.2807207 -102.5933287 -123.757588 -114.7106846 -98.47571736 -114.3772897 -109.9982995 -88.58266139 -120.7402025 -101.9404413 -95.56595759 -114.6557442 -114.9659106 -107.4394786 -116.9104855 -101.2319698 -105.739113 -102.558704 -106.3714351 -93.32603438 -107.9719309 -113.4009992 -100.8891301 -121.2213373 -118.3015657 -116.9197236 -98.27747503 -109.7579138 -101.702454 -111.8144713 -94.45877862 -90.85046529 -111.7009087 -85.08914066 -104.9564443 -119.6430234 -79.82081659 -85.02040582 -100.6112569 -99.66734661 -94.28496216 -91.14047473 -102.4678132 -100.7167911 -108.9990478 -97.52459899 -88.60344384 -87.75681553 -82.11521886 -105.6632777 -86.63615451 -99.87026798 -90.38661165 -101.485997 -104.0821776 -96.0130618 -91.51229294 -102.1454517 -102.6591517 -83.90991997 -77.36318964 -87.21548837 -80.84724051 -101.5107836 -92.56915106 -82.69476282 -80.99644386 -96.46216773 -98.11848977 -96.07287469 -91.64962756 -66.33764057 -79.197356 -100.2617574 -78.81026006 -83.41548111 -99.93882935 -108.0824634 -91.217859 -90.37984683 -79.6301169 -90.36841859 -87.76541977 -92.01372667 -86.45720893 -88.59646597 -84.81470254 -107.7788264 -91.67119408 -92.77506032 -87.55260617 -89.11347241 -81.43556855 -86.38092042 -85.66987323 -109.7118734 -91.1040111 -83.88190076 -85.55381268 -91.4193938 -101.3131808 -81.43745522 -87.31455329 -90.33755891 -99.81151408 -106.684402 -97.70792837 -93.76621469 -91.69048182 -97.18574254 -92.30079581 -89.26458924 -97.42861582 -102.4719797 -108.3840771 -97.10767146 -103.4646264 -99.28298634 -88.67955167 -102.6768356 -98.92729137 -94.12663864 -87.3663415 -87.19902769 -87.41923358 -79.90165728 -83.27061178 -97.92832444 -96.51270856 -86.29228515 -97.04999131 -87.61700054 -102.3950804 -109.8183084 -89.72060962 -106.2124507 -104.3946814 -97.23175775 -113.5856281 -96.03594247 -89.14733326 -104.703616 -121.5732584 -99.93797428 -114.3042129 -78.84954727 -103.2281991 -128.2015951 -94.42699371 -103.9150872 -81.92716427 -100.1234421 -100.2498074 -138.3697542 -100.4779671 -112.6106694 -118.3492065 -93.42376516 -93.9574748 -91.68938826 -85.91829157 -102.7823054 -108.8603989 -99.29350704 -105.4119148 -104.8475803 -118.2392459 -114.9315706 -104.5023825 -115.2774196 -93.6488869 -111.6894641 -115.7819675 -98.39662966 -104.1481387 -117.2605802 -126.7892667 -106.9216245 -108.9963278 -122.4931013 -94.24926393 -104.4753435 -120.2328613 -101.1800659 -115.8940486 -95.36951165 -104.3855795 -118.1921456 -110.6871452 -116.0912641 -114.7747094 -125.2496692 -106.7349761 -115.4219948 -114.3395831 -105.917662 -119.364546 -112.6979065 -96.20257895 -107.6177896 -105.9358521 -115.8610687 -99.20501345 -87.68995605 -123.1897901 -98.02895782 -95.48723167 -98.35491481 -92.57220462 -118.5306661 -99.24077403 -112.9958766 -111.3604642 -112.3225746 -74.85394314 -106.244066 -97.04028992 -96.98762067 -106.0505782 -103.6805113 -95.46956312 -92.91983893 -102.6308354 -90.32892705 -95.73816035 -103.2442121 -111.7224927 -109.4991855 -88.00131205 -87.6079082 -112.3614284 -99.97522782 -80.22998372 -85.96033451 -85.40340913 -102.8643718 -106.9974557 -96.14400112 -90.63832437 -108.8355253 -104.3734085 -92.147686 -96.39533626 -111.2250368 -81.65078792 -99.81066025 -98.01276717 -78.07720187 -85.8005895 -99.30102938 -87.2291675 -85.05404714 -99.99921664 -105.1660088 -86.63556105 -99.86613219 -93.77406649 -104.6050015 -90.43610971 -107.7273762 -96.01179742 -112.8139616 -94.65978303 -87.56220328 -85.12668017 -90.07825587 -81.64916045 -89.00651045 -93.38843 -89.3393326 -108.8127612 -79.78539833 -79.33010316 -84.40458774 -74.22738983 -95.13661005 -88.4526456 -80.6699032 -96.01623242 -104.8575109 -83.05433277 -94.47077054 -95.42820344 -83.45761942 -77.16736072 -108.7108433 -88.2118547 -89.09641098 -84.22192739 -96.70630031 -103.73901 -95.14031225 -77.62110462 -89.47014925 -97.66069651 -98.51061704 -105.1499812 -96.00169182 -103.0872119 -86.4214719 -94.98891096 -105.3251198 -84.07151501 -90.96977702 -84.47679852 -87.56417784 -71.36401469 -93.52952914 -85.30760156 -106.4928465 -109.1869557 -95.51556128 -95.57814343 -106.6067326 -103.8086802 -78.22465239 -90.84343693 -102.6572335 -93.37748269 -93.00855817 -133.3098287 -92.02010694 -99.0918937 -89.4371333 -99.32065336 -84.99652824 diff --git a/Code_Python/synthetic.py b/Code_Python/synthetic.py index 6079f08..28581d8 100644 --- a/Code_Python/synthetic.py +++ b/Code_Python/synthetic.py @@ -1,5 +1,5 @@ """ -Signal Filtering/Smoothing and Generation of Synthetic Time-Series. +Signal Filtering and Generation of Synthetic Time-Series. Copyright (c) 2020 Gabriele Gilardi """ @@ -9,7 +9,7 @@ import numpy as np def synthetic_wave(P, A=None, phi=None, num=1000): """ - Generates a multi-sine wave given a periods, amplitudes, and phases. + Generates a multi-sine wave given periods, amplitudes, and phases. P (n, ) Periods A (n, ) Amplitudes @@ -46,10 +46,36 @@ def synthetic_wave(P, A=None, phi=None, num=1000): return t, f +def synthetic_sampling(X, n_reps=1, replace=True): + """ + Generates surrogates of the time-series X using randomized-sampling + (bootstrap) with or without replacement. Input X must be a 1D array. + + X (n, ) Original time-series + idx (n_reps, n) Random index of X + X_synt (n_reps, n) Synthetic time-series + """ + X = X.flatten() # Reshape to (n, ) + n = len(X) + + # Sampling with replacement + if (replace): + idx = np.random.randint(0, n, size=(n_reps, n)) + + # Sampling without replacement + else: + idx = np.argsort(np.random.rand(n_reps, n), axis=1) + + # Synthetic time-series + X_synt = X[idx] + + return X_synt + + def synthetic_FFT(X, n_reps=1): """ - Generates surrogates of the time-serie X using the phase-randomized - Fourier-transform algorithm. Input X needs to be a 1D array. + Generates surrogates of the time-series X using the phase-randomized + Fourier-transform algorithm. Input X must be a 1D array. X (n, ) Original time-series X_fft (n, ) FFT of the original time-series @@ -90,36 +116,10 @@ def synthetic_FFT(X, n_reps=1): return X_synt -def synthetic_sampling(X, n_reps=1, replace=True): - """ - Generates surrogates of the time-serie X using randomized-sampling - (bootstrap) with or without replacement. Input X needs to be a 1D array. - - X (n, ) Original time-series - idx (n_reps, n) Random index of X - X_synt (n_reps, n) Synthetic time-series - """ - X = X.flatten() # Reshape to (n, ) - n = len(X) - - # Sampling with replacement - if (replace): - idx = np.random.randint(0, n, size=(n_reps, n)) - - # Sampling without replacement - else: - idx = np.argsort(np.random.rand(n_reps, n), axis=1) - - # Synthetic time-series - X_synt = X[idx] - - return X_synt - - def synthetic_MEboot(X, n_reps=1, alpha=0.1, bounds=False, scale=False): """ - Generates surrogates of the time-serie X using the maximum entropy - bootstrap algorithm. Input X needs to be a 1D array. + Generates surrogates of the time-series X using the maximum entropy + bootstrap algorithm. Input X must be a 1D array. X (n, ) Original time-series idx (n, ) Original order of X @@ -259,7 +259,7 @@ def value2diff(X, percent=True): Notes: - the discrete difference can be calculated in percent or in value. - dX is one element shorter than X. - - X needs to be a 1D array. + - X must be a 1D array. """ X = X.flatten() # Reshape to (n, ) @@ -276,7 +276,7 @@ def value2diff(X, percent=True): def diff2value(dX, X0, percent=True): """ - Returns array X from the 1st discrete difference using X0 as initial value. + Rebuilds array X from the 1st discrete difference using X0 as initial value. dX (n, ) Discrete differences X0 scalar Initial value @@ -285,7 +285,7 @@ def diff2value(dX, X0, percent=True): Notes: - the discrete difference can be in percent or in value. - X is one element longer than dX. - - dX needs to be a 1D array. + - dX must be a 1D array. If the discrete difference is in percent: X[0] = X0 diff --git a/Code_Python/test.py b/Code_Python/test.py index 5c6b82f..7d043fd 100644 --- a/Code_Python/test.py +++ b/Code_Python/test.py @@ -1,17 +1,12 @@ """ -Signal Filtering/Smoothing and Generation of Synthetic Time-Series. +Signal Filtering and Generation of Synthetic Time-Series. Copyright (c) 2020 Gabriele Gilardi - -ToDo: -- do examples and check type (low pass, etc) -- put type in description -- see paper ZEMA for tests -- check results with ML """ import sys +import warnings import numpy as np import matplotlib.pyplot as plt @@ -20,83 +15,72 @@ import synthetic as syn # Added to avoid message warning "The group delay is singular at frequencies # [....], setting to 0" when plotting the lag for SMA filters. -np.seterr(all='ignore') +# np.seterr(all='ignore') +warnings.filterwarnings('ignore') np.random.seed(1294404794) -# Read data to filter +# Read example to run if len(sys.argv) != 2: - print("Usage: python test.py ") + print("Usage: python test.py ") sys.exit(1) -data_file = sys.argv[1] + '.csv' +example = sys.argv[1] -# Read data from a csv file (one time-series each column) -data = np.loadtxt(data_file, delimiter=',') -print(data.shape) +# Read data from a csv file +data_file = 'spx' +data = np.loadtxt(data_file + '.csv', delimiter=',') -# t, f = syn.synthetic_wave([1., 2., 3.], A=None, phi=None, num=1000) -# plt.plot(t,f) -# plt.show() +# Example with a few filters +if (example == 'Filters'): + spx = flt.Filter(data) + ema = spx.EMA(N=10) + butter = spx.ButterMod(P=10, N=2) + zema = spx.ZEMA2(N=10, K=2.0) + signals = [spx.data, ema, butter, zema] + names = ['SPX', 'EMA', 'ButterMod', 'ZEMA2'] + flt.plot_signals(signals, names=names, start=0, end=200) -spx = flt.Filter(data) -# res = spx.EMA(N=10) -# signals = [spx.data, res[0:400]] -# flt.plot_signals(signals, ['SPX', 'SMA']) +# Example with the three types of Kalman filter +elif (example == 'Kalman'): + spx = flt.Filter(data) + a_type = spx.Kalman(sigma_x=0.1, sigma_v=0.1, dt=1.0, abg_type="a") + ab_type = spx.Kalman(sigma_x=0.1, sigma_v=0.1, dt=1.0, abg_type="ab") + abg_type = spx.Kalman(sigma_x=0.1, sigma_v=0.1, dt=1.0, abg_type="abg") + signals = [spx.data, a_type, ab_type, abg_type] + names = ['SPX', 'a-type', 'ab-type', 'abg-type'] + flt.plot_signals(signals, names=names, start=0, end=200) -res = spx.BandPass(P=10, delta=0.3) +# Example of filter frequency and lag +elif (example == 'Response'): + spx = flt.Filter(data) + band = spx.BandPass(P=10, delta=0.3) + spx.plot_response() -spx.plot_frequency() -spx.plot_lag() +# Example of surrogates time-series using the Fourier-transform algorithm +elif (example == 'FFT_boot'): + pass -# sigma_x = 0.1 -# sigma_v = 0.1 * np.ones(n_samples) -# res = spx.Kalman(sigma_x=sigma_x, sigma_v=sigma_v, dt=1.0, abg_type="abg") -# alpha = 0.5 -# beta = 0.005 -# gamma = 0.0 -# Yc, Yp = spx.ABG(alpha=alpha, beta=beta, gamma=gamma, dt=1.0) -# signals = (spx.data[:, 0], Yc[:, 0], Yp[:, 0]) -# utl.plot_signals(signals, 0, 50) +# Example of surrogates time-series using maximum entropy bootstrap algorithm +elif (example == 'ME_boot'): + pass -# t, f = syn.synthetic_wave([1., 2., 3.], A=None, phi=None, num=100) -# plt.plot(t,f) -# plt.show() -# aa = np.array([ -# [ 0.8252, 0.2820], -# [ 1.3790, 0.0335], -# [-1.0582, -1.3337], -# [-0.4686, 1.1275], -# [-0.2725, 0.3502], -# [ 1.0984, -0.2991], -# [-0.2779, 0.0229], -# [ 0.7015, -0.2620], -# [-2.0518, -1.7502], -# [-0.3538, -0.2857], -# [-0.8236, -0.8314], -# [-1.5771, -0.9792], -# [ 0.5080, -1.1564]]) -# synt_data = syn.synthetic_FFT(aa[0:5,0], n_reps=1) -# print(synt_data) +else: + print("Example not found") + sys.exit(1) + -# plt.plot(synt_data1) -# plt.plot(synt_data2) +# dX = syn.value2diff(data, percent=True) +# # X = syn.diff2value(dX, data[0], percent=True) +# signals = [data, X] +# flt.plot_signals(signals, start=0, end=200) + +# dX = syn.value2diff(data, percent=False) +# dX_synt = syn.synthetic_sampling(dX, n_reps=1, replace=True) +# dX_synt = syn.synthetic_FFT(dX, n_reps=2) +# dX_synt = syn.synthetic_MEboot(dX, n_reps=4, alpha=0.1, bounds=False, scale=False) +# for i in range(dX_synt.shape[0]): +# # aa = dX_synt[:, i] +# aa = syn.diff2value(dX_synt[i, :], data[0], percent=False) +# plt.plot(aa) +# plt.xlim(0, 200) # plt.plot(data) -# names = ['syn1', 'syn2', 'spx'] -# plt.legend(names) # plt.show() -# percent = False -# print(data[0:10, :]) -# bb = syn.value2diff(data, percent) -# print(bb[0:10, :]) -# cc = syn.diff2value(bb, percent) -# print(cc[0:10, :]+1399.48) - -# i = np.arange(aa.shape[1]) -# bb[:, i] = aa[idx[:, i], i] -# aa = np.array([4, 12, 36, 20, 8]) -# bb = syn.synthetic_sampling(aa, n_reps=2, replace=True) -# print(bb) -# aa = np.array([4, 12, 36, 20, 8]) -# W = syn.synthetic_MEboot(aa, n_reps=1, alpha=0.1, bounds=False, scale=False) -# print('W=') -# print(W) - diff --git a/README.md b/README.md index 17f6fb8..a0f0e62 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Signal Smoothing / Filtering and Generation of Synthetic Time-Series +# Signal Filtering and Generation of Synthetic Time-Series ## Reference @@ -10,6 +10,57 @@ ## Characteristics -## Parameters +- The code has been written and tested in Python 3.7.7. +- Implementation of several digital signal filters and functions for the generation of synthetic (surrogate) time-series. +- Filter list (*filters.py*): + - **Generic** Generic filter. + - **SMA** Simple moving average. + - **EMA** Exponential moving average. + - **WMA** Weighted moving average. + - **MSMA** Modified simple moving average. + - **MLSQ** Modified least-squares quadratic. + - **ButterOrig** Butterworth original filter. + - **ButterMod** Butterworth modified filter. + - **SuperSmooth** Supersmoother filter. + - **GaussLow** Gauss low pass filter. + - **GaussHigh** Gauss high pass filter. + - **BandPass** Band-pass filter. + - **BandStop** Band-stop filter. + - **ZEMA1** Zero-lag EMA (type 1). + - **ZEMA2** Zero-lag EMA (type 2). + - **InstTrend** Instantaneous trendline. + - **SincFilter** Sinc function filter. + - **Decycler** De-cycler filter. + - **DecyclerOsc** De-cycle oscillator. + - **ABG** Alpha-beta-gamma filter. + - **Kalman** One-dimensional steady-state Kalman filter. +- Synthetic time-series (*synthetic.py*): + - **synthetic_wave** Generates multi-sine wave given periods, amplitudes, and phases. + - **synthetic_sampling** Generates surrogates using randomized-sampling (bootstrap) with or without replacement. + - **synthetic_FFT** Generates surrogates using the phase-randomized Fourier-transform algorithm. + - **synthetic_MEboot** Generates surrogates using the maximum entropy bootstrap algorithm. +- File *filters.py* includes also functions to plot the filter signal, frequency response, and group delay. +- File *synthetic.py* includes also functions to differentiate, integrate, normalize, and scale the discrete time-series. +- Usage: *python test.py example*. + +## Main Parameters + +`example` Name of the example to run. + +`data_file` File name with the dataset (csv format). The extension is added automatically. + +`X` Dataset to filter/time-series (input). It must be a 1D array, i.e. of shape `(:, )` or `(:, 1)` or `(1, :)`. + +`b` Transfer response coefficients (numerator). + +`a` Transfer response coefficients (denominator). + +`Y` Filtered dataset (output). + +`X_synt` Synthetic time-series (output) + +`n_reps` Number of surrogates/synthetic time-series to generate. ## Examples + +There are five examples: **Filters**, **Kalman**, **Response**, **FFT_boot**, **ME_boot**. For all, the dataset in *spx.csv* is used.