complexeva 0.2.1__tar.gz → 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: complexeva
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Home-page:
5
5
  Author: Don Yin
6
6
  Author-email: don_yin@outlook.com
@@ -1,6 +1,6 @@
1
- from .higuchi import hfd
1
+ from .higuchi import hfd, hfd_hilbert
2
2
  from .lempel_ziv import lempel_ziv
3
3
  from .compression import compression_complexity
4
4
  from .lyapunov_exponent import lyapunov_exponent
5
5
 
6
- __all__ = ["compression_complexity", "lyapunov_exponent", "hfd", "lempel_ziv"]
6
+ __all__ = ["compression_complexity", "lyapunov_exponent", "hfd", "hfd_hilbert", "lempel_ziv"]
@@ -0,0 +1,75 @@
1
+ #!/Users/donyin/miniconda3/envs/rotation-1/bin/python
2
+
3
+
4
+ import numpy
5
+ from numba import njit
6
+ from scipy.signal import hilbert
7
+
8
+
9
+ @njit
10
+ def compute_L_x(X, k_max=16):
11
+ k_max = min(k_max, len(X) // 2) if k_max is None else k_max
12
+ N = len(X)
13
+ L = numpy.zeros(k_max)
14
+ x = numpy.zeros(k_max)
15
+ for k in range(1, k_max + 1):
16
+ Lk = numpy.zeros(k)
17
+ for m in range(k):
18
+ Lmk = 0.0
19
+ n_max = (N - m) // k
20
+ for i in range(1, n_max):
21
+ Lmk += numpy.abs(X[m + i * k] - X[m + (i - 1) * k])
22
+ Lmk *= (N - 1) / (n_max * k)
23
+ Lk[m] = Lmk
24
+ L[k - 1] = numpy.log(Lk.mean())
25
+ x[k - 1] = numpy.log(1.0 / k)
26
+ return x, L
27
+
28
+
29
+ def hfd(X, k_max=16):
30
+ """expect a 1d numpy array as input"""
31
+ assert X.ndim == 1, f"X must be a 1d numpy array, got shape {X.shape}"
32
+ x, L = compute_L_x(X, k_max)
33
+ A = numpy.column_stack((x, numpy.ones_like(x)))
34
+ beta, _, _, _ = numpy.linalg.lstsq(A, L, rcond=None)
35
+ return beta[0]
36
+
37
+
38
+ def hfd_hilbert(X, k_max=16):
39
+ """
40
+ Calculate Higuchi Fractal Dimension on the instantaneous frequency
41
+ obtained from Hilbert transform of the input signal.
42
+
43
+ Args:
44
+ X (numpy.ndarray): Input 1D signal
45
+ k_max (int): Maximum k value for HFD calculation
46
+
47
+ Returns:
48
+ float: Higuchi Fractal Dimension of the instantaneous frequency
49
+ """
50
+ assert X.ndim == 1, f"X must be a 1d numpy array, got shape {X.shape}"
51
+ analytic_signal = hilbert(X)
52
+ instantaneous_phase = numpy.angle(analytic_signal)
53
+ instantaneous_freq = numpy.diff(instantaneous_phase)
54
+ instantaneous_freq = numpy.append(instantaneous_freq, instantaneous_freq[-1])
55
+ return hfd(instantaneous_freq, k_max=k_max)
56
+
57
+
58
+ if __name__ == "__main__":
59
+ import time
60
+
61
+ signal = numpy.random.rand(1000) # shape: (1000,)
62
+
63
+ # Test plain HFD speed
64
+ start_time = time.time()
65
+ complexity_plain = hfd(signal)
66
+ plain_time = time.time() - start_time
67
+
68
+ # Test Hilbert HFD speed
69
+ start_time = time.time()
70
+ complexity_hilbert = hfd_hilbert(signal)
71
+ hilbert_time = time.time() - start_time
72
+
73
+ print(f"Plain HFD: {complexity_plain:.6f}, Time: {plain_time:.6f}s")
74
+ print(f"Hilbert HFD: {complexity_hilbert:.6f}, Time: {hilbert_time:.6f}s")
75
+ print(f"Speed ratio (Hilbert/Plain): {hilbert_time/plain_time:.2f}x")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: complexeva
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Home-page:
5
5
  Author: Don Yin
6
6
  Author-email: don_yin@outlook.com
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="complexeva",
5
- version="0.2.1",
5
+ version="0.2.2",
6
6
  packages=find_packages(),
7
7
  install_requires=[],
8
8
  author="Don Yin",
@@ -1,39 +0,0 @@
1
- #!/Users/donyin/miniconda3/envs/rotation-1/bin/python
2
-
3
-
4
- import numpy
5
- from numba import njit
6
-
7
-
8
- @njit
9
- def compute_L_x(X, k_max=16):
10
- N = len(X)
11
- L = numpy.zeros(k_max)
12
- x = numpy.zeros(k_max)
13
- for k in range(1, k_max + 1):
14
- Lk = numpy.zeros(k)
15
- for m in range(k):
16
- Lmk = 0.0
17
- n_max = (N - m) // k
18
- for i in range(1, n_max):
19
- Lmk += numpy.abs(X[m + i * k] - X[m + (i - 1) * k])
20
- Lmk *= (N - 1) / (n_max * k)
21
- Lk[m] = Lmk
22
- L[k - 1] = numpy.log(Lk.mean())
23
- x[k - 1] = numpy.log(1.0 / k)
24
- return x, L
25
-
26
-
27
- def hfd(X, k_max=16):
28
- """expect a 1d numpy array as input"""
29
- assert X.ndim == 1, f"X must be a 1d numpy array, got shape {X.shape}"
30
- x, L = compute_L_x(X, k_max)
31
- A = numpy.column_stack((x, numpy.ones_like(x)))
32
- beta, _, _, _ = numpy.linalg.lstsq(A, L, rcond=None)
33
- return beta[0]
34
-
35
-
36
- if __name__ == "__main__":
37
- signal = numpy.random.rand(1000) # shape: (1000,)
38
- complexity = hfd(signal)
39
- print(complexity)
File without changes
File without changes