Algorithm::LineSegments - Piecewise linear function approximation
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; }, );
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.
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.
The function line_segment_points by default.
line_segment_points
Copyright (c) 2014 Bjoern Hoehrmann <bjoern@hoehrmann.de>. This module is licensed under the same terms as Perl itself.
To install Algorithm::LineSegments, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Algorithm::LineSegments
CPAN shell
perl -MCPAN -e shell install Algorithm::LineSegments
For more information on module installation, please visit the detailed CPAN module installation guide.