The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Algorithm::LineSegments - Piecewise linear function approximation

SYNOPSIS

  use Algorithm::LineSegments;
  my @points = line_segment_points(
    points => \@numbers,
    continue => sub {
      my ($segment_count, $cost_factor) = @_;
      return 0 if $segment_count <= 10;
      return 1;
    },
  );

DESCRIPTION

This module takes discrete data points like time series data and computes a piecewise linear function, line segments, approximating them. It does this by merging groups of adjacent points into lines, always picking the pair that produces the smallest error, until it is told to stop.

FUNCTIONS

line_segment_points(%options)

Returns a list of [[$x0, $y0], [$x1, $y1]] pairs describing line segments. Options are

points

Array of numbers, the input data.

continue

A callback function that is called with the number of remaining segments and the cost of the current merge with the expectation the callback returns a true value if it should perform the merge and continue, and a false value if it should stop merging and return. The default is to merge until only three line segments are left.

cost

A callback function that is called with two list references of points and it should return a number indicating how costly it is, how much of an error it introduces, if all points are made into a single line segment. The default projects all data points to the unit range 0 .. 1 based on the maximum and minimum value and computes the euclidean distance between the points and the corresponding points on a line that would cover them all.

EXPORTS

The function line_segment_points by default.

AUTHOR / COPYRIGHT / LICENSE

  Copyright (c) 2014 Bjoern Hoehrmann <bjoern@hoehrmann.de>.
  This module is licensed under the same terms as Perl itself.