-
Notifications
You must be signed in to change notification settings - Fork 1
/
peak_detection.rb
74 lines (63 loc) · 2.06 KB
/
peak_detection.rb
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
66
67
68
69
70
71
72
73
74
require "logger"
module Tilingol
class PeakDetection
def initialize(freq_window, moving_average_window, peak_threshold, debug = false)
@freq_window = freq_window
@moving_average_window = moving_average_window
@peak_threshold = peak_threshold
@frequency = 0
@currentSec = 0
@flag = false # to avoid extensive check of peak status
@data = []
@eam = []
@growth = []
@logger = Logger.new("data_#{Time.now.to_i}.csv")
@logger.formatter = proc do |severity, datetime, progname, msg|
"#{msg}\n"
end
@logger.level = debug ? Logger::DEBUG : Logger::INFO
end
def collect_frequency
now = Time.now.to_i
@currentSec = now if @currentSec == 0
if now < (@currentSec + @freq_window)
@frequency += 1
else
update_window(@frequency)
@frequency = 1
@currentSec = now
@flag = true
end
end
def is_this_a_peak?
if @flag && @growth.size > 0
@flag = false
return @growth.last > @peak_threshold
end
return false
end
private
def update_window(frequency)
data_entry = ""
data_entry += @currentSec.to_s
@data << frequency
@data.shift if @data.size > @moving_average_window
data_entry += "," + @data.last.to_s
if @data.size >= @moving_average_window
if @eam.size == 0
@eam << @data[0..@moving_average_window].inject {|sum, i| sum + i } / @moving_average_window.to_f
else
@eam << (@data.last - @eam.last.to_f) * (2/(@moving_average_window.to_f + 1)) + @eam.last.to_f
end
@eam.shift if @eam.size > @moving_average_window
data_entry += "," + @eam.last.to_s
end
if @eam.size >= @moving_average_window
@growth << (@eam.last - @eam.first) / @eam.first.to_f
@growth.shift if @growth.size > @moving_average_window
data_entry += "," + @growth.last.to_s
end
@logger.debug(data_entry)
end
end
end