Class: Net::NTP::Check::AutoBandPass

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ntp/check/offset.rb

Overview

The filtering works like this: This assumes you are passing in a 9 length Array. Other lengths may work, but YMMV. This then gets the average of the middle three of the sorted Array. Using this average, and an arbitrary range (+/- 200ms), we delete any values outside of that range. This effectively is a lazy-man’s band-pass filter.

Constant Summary collapse

FILTER_RANGE =
0.2

Class Method Summary collapse

Class Method Details

.apply_band(values, average) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/net/ntp/check/offset.rb', line 95

def self.apply_band(values, average)
  values.dup.each do |o|
    upper_bound = (average + FILTER_RANGE)
    lower_bound = (average - FILTER_RANGE)
    values.delete(o) if o < lower_bound || o > upper_bound
  end
  values
end

.average(values) ⇒ Object



104
105
106
# File 'lib/net/ntp/check/offset.rb', line 104

def self.average(values)
  values.reduce { |a, e| a + e } / values.length.to_f
end

.filter(values) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/net/ntp/check/offset.rb', line 83

def self.filter(values)
  v = values.sort
  Net::NTP::Check.logger.debug("AutoBandPass values: #{v}")
  middle = values.length / 2.0
  middle.ceil unless values.length % 2
  avg = average(v[(middle - 1).floor..(middle + 1).floor])
  Net::NTP::Check.logger.debug("AutoBandPass mid values avg: #{avg}")
  v = apply_band(v, avg)
  Net::NTP::Check.logger.debug("AutoBandPass values with filter: #{v}")
  average(v)
end