forked from NickFoubert/simple-segment
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
66 lines (50 loc) · 1.93 KB
/
example.py
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
57
58
59
60
61
62
63
64
65
from matplotlib.pylab import gca, figure, plot, subplot, title, xlabel, ylabel, xlim,show
from matplotlib.lines import Line2D
import segment
import fit
def draw_plot(data,plot_title):
plot(range(len(data)),data,alpha=0.8,color='red')
title(plot_title)
xlabel("Samples")
ylabel("Signal")
xlim((0,len(data)-1))
def draw_segments(segments):
ax = gca()
for segment in segments:
line = Line2D((segment[0],segment[2]),(segment[1],segment[3]))
ax.add_line(line)
with open("example_data/16265-normalecg.txt") as f:
file_lines = f.readlines()
data = [float(x.split("\t")[2].strip()) for x in file_lines[100:320]]
max_error = 0.005
#sliding window with regression
figure()
segments = segment.slidingwindowsegment(data, fit.regression, fit.sumsquared_error_regr, max_error)
draw_plot(data,"Sliding window with regression")
draw_segments(segments)
#bottom-up with regression
figure()
segments = segment.bottomupsegment(data, fit.regression, fit.sumsquared_error_regr, max_error)
draw_plot(data,"Bottom-up with regression")
draw_segments(segments)
#top-down with regression
figure()
segments = segment.topdownsegment(data, fit.regression, fit.sumsquared_error_regr, max_error)
draw_plot(data,"Top-down with regression")
draw_segments(segments)
#sliding window with simple interpolation
figure()
segments = segment.slidingwindowsegment(data, fit.interpolate, fit.sumsquared_error_int, max_error)
draw_plot(data,"Sliding window with simple interpolation")
draw_segments(segments)
#bottom-up with simple interpolation
figure()
segments = segment.bottomupsegment(data, fit.interpolate, fit.sumsquared_error_int, max_error)
draw_plot(data,"Bottom-up with simple interpolation")
draw_segments(segments)
#top-down with simple interpolation
figure()
segments = segment.topdownsegment(data, fit.interpolate, fit.sumsquared_error_int, max_error)
draw_plot(data,"Top-down with simple interpolation")
draw_segments(segments)
show()