-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_abs.py
More file actions
56 lines (45 loc) · 1.15 KB
/
Copy pathexample_abs.py
File metadata and controls
56 lines (45 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from diff_tvr import DiffTVR
import numpy as np
import matplotlib.pyplot as plt
from diff_tvr import *
if __name__ == "__main__":
# Data
dx = 0.01
data = []
for x in np.arange(0,1,dx):
data.append(abs(x-0.5))
data = np.array(data)
# True derivative
deriv_true = []
for x in np.arange(0,1,dx):
if x < 0.5:
deriv_true.append(-1)
else:
deriv_true.append(1)
deriv_true = np.array(deriv_true)
# Add noise
n = len(data)
data_noisy = data + np.random.normal(0,0.05,n)
# Plot true and noisy signal
fig1 = plt.figure()
plt.plot(data)
plt.plot(data_noisy)
plt.title("Signal")
plt.legend(["True","Noisy"])
# Derivative with TVR
diff_tvr = DiffTVR(n,dx)
(deriv,_) = diff_tvr.get_deriv_tvr(
data=data_noisy,
deriv_guess=np.full(n+1,0.0),
alpha=0.2,
no_opt_steps=100
)
# Plot TVR derivative
fig2 = plt.figure()
plt.plot(deriv_true)
plt.plot(deriv)
plt.title("Derivative")
plt.legend(["True","TVR"])
fig1.savefig('signal.png')
fig2.savefig('derivative.png')
plt.show()