module DataLayerProcessorNoiseRemoval

Constants

DEFAULT_NOISE_REMOVAL_LEVEL
DEFAULT_NOISE_REMOVAL_WINDOW_SIZE
NOISE_COEFF
NOISE_POWER_COEFF

Attributes

noise_removal[RW]
noise_removal_level[RW]
noise_removal_window_size[RW]
noise_threshold[RW]

Public Instance Methods

calc_avg_derivative(i_from, i_to) click to toggle source
# File lib/technical_graph/data_layer_processor_noise_removal.rb, line 87
def calc_avg_derivative(i_from, i_to)
  part_array = data.clone_partial_w_fill(i_from, i_to)
  derivatives = Array.new
  (1...part_array.size).each do |i|
    x_len = (part_array[i].x - part_array[i - 1].x).abs
    y_len = (part_array[i].y - part_array[i - 1].y).abs
    derivatives << (x_len / y_len).abs if x_len.abs > 0
  end
  avg_der = derivatives.float_mean
  return avg_der
end
noise?(i) click to toggle source

Check if data at index is noisy

# File lib/technical_graph/data_layer_processor_noise_removal.rb, line 57
def noise?(i)
  i_from = noise_removal_window_from(i)
  i_to = noise_removal_window_to(i)


  # y_mean = part_array.collect { |p| p.y }.float_mean
  # # another algorithm
  # noise_strength = (data[i].y - y_mean).abs / y_mean
  # return noise_strength_enough?(noise_strength)

  # calc. avg 'derivative'
  avg_der = calc_avg_derivative(i_from, i_to)
  current_der = calc_avg_derivative(i-1, i+1)

  # safety
  return false if avg_der == 0 or current_der == 0

  begin
    current_level = Math.log((current_der / avg_der) ** NOISE_POWER_COEFF).abs
  rescue Errno::EDOM
    # can not calculate logarithm
    return false
  rescue Errno::ERANGE
    # can not calculate logarithm
    return false
  end
  logger.debug "noise removal, avg der #{avg_der}, current #{current_der}, current lev #{current_level}, threshold #{noise_threshold}"
  return current_level > noise_threshold
end
noise_removal_initialize(options) click to toggle source
# File lib/technical_graph/data_layer_processor_noise_removal.rb, line 12
def noise_removal_initialize(options)
  @noise_removal = options[:noise_removal] == true
  @noise_removal_level = options[:noise_removal_level] || DEFAULT_NOISE_REMOVAL_LEVEL
  @noise_removal_window_size = options[:noise_removal_window_size] || DEFAULT_NOISE_REMOVAL_WINDOW_SIZE

  @noise_threshold = Math.log(NOISE_COEFF / @noise_removal_level)
end
noise_removal_process() click to toggle source

Smooth values

# File lib/technical_graph/data_layer_processor_noise_removal.rb, line 23
def noise_removal_process
  return if noise_removal == false

  t = Time.now
  new_data = Array.new

  @noises_removed_count = 0

  logger.debug "Noise removal started"

  (0...data.size).each do |i|
    if not noise?(i)
      new_data << data[i]
    else
      @noises_removed_count += 1
    end
  end

  logger.debug "Noise removal completed, removed #{@noises_removed_count}"
  logger.debug " TIME COST #{Time.now - t}"

  @data = new_data
  return new_data
end
noise_removal_window_from(i) click to toggle source
# File lib/technical_graph/data_layer_processor_noise_removal.rb, line 48
def noise_removal_window_from(i)
  return i - (noise_removal_window_size.to_f / 2.0).ceil
end
noise_removal_window_to(i) click to toggle source
# File lib/technical_graph/data_layer_processor_noise_removal.rb, line 52
def noise_removal_window_to(i)
  return i + (noise_removal_window_size.to_f / 2.0).ceil
end