class Geospatial::Peaks

Attributes

derivative[R]

Public Class Methods

new(values) click to toggle source
# File lib/geospatial/histogram.rb, line 110
def initialize(values)
        @values = values
        @derivative = []
        
        s = @values.size
        
        @values.size.times do |i|
                # Apply the Laplacian of Gaussians to compute the gradient changes:
                # @derivative << (@values[i-2] * -1) + (@values[i-1] * -1) + (@values[i] * 4) + (@values[i+1-s] * -1) + (@values[i+2-s] * -1)
                @derivative << (2.0 * @values[i-1]) + (-2.0 * @values[i+1-s])
        end
end

Public Instance Methods

each() { |x1| ... } click to toggle source
# File lib/geospatial/histogram.rb, line 125
def each
        return to_enum unless block_given?
        
        @derivative.each_with_index do |y2, x2|
                x1 = x2 - 1
                y1 = @derivative[x1]
                
                if (y1 <= 0 and y2 > 0) or (y1 > 0 and y2 <= 0)
                        # There has been a zero crossing, so we have a peak somewhere here:
                        g = (y2.to_f - y1.to_f)
                        m = (-y1.to_f / g)
                        
                        yield x1 + m, g
                end
        end
end
peaks() click to toggle source
# File lib/geospatial/histogram.rb, line 142
def peaks
        self.class.new(@derivative)
end
segments() { |up, down| ... } click to toggle source
# File lib/geospatial/histogram.rb, line 146
def segments
        return to_enum(:segments) unless block_given?
        
        peaks = self.peaks
        gradients = peaks.to_a
        
        return if gradients.empty?
        
        index, gradient = gradients.first
        
        if gradient > 0
                gradients.push gradients.shift
        end
        
        gradients.each_slice(2) do |up, down|
                yield up, down
        end
end