blib/lib/Math/CMA.pm

filter: Statementstmt 100% 43 / 43 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 98.3% 58 / 59 CC 7 SCAR 15.7
CC
7
Cov
100%
central_moving_averages
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.8
↑ Summary
1 package Math::CMA;
2
3 3 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use List::Util qw/min max sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 3 require Exporter;
9
10 3 our @ISA = qw(Exporter);
11
12 3 our %EXPORT_TAGS = ( 'all' => [ qw(
13 central_moving_averages
14 ) ] );
15
16 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
17
18 3 our @EXPORT = qw(
19
20 );
21
22 3 our $VERSION = '0.1';
23
24 101 ▶sub central_moving_averages {
Subroutine central_moving_averages: called CC=7 SCAR=19.5
Pod undocumented
25 101 my $smoothing = shift;
26 101 my @series = @{shift()};
27 101 my $count = min($smoothing, $#series);
28 101 my $left = -$count;
29 101 my $right = $count;
30 101 my $sum = 0;
31 101 my @result;
32
33 101 ▶ return @series unless $count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $count > 0 1 100 101
34
35 # sum first element and window to the right
36 100 for (0 .. $count) {
37 3168 $sum += $series[$_];
38 }
39
40 # head
41 100 ▶ while ($left < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left < 0) 3068 100 3168
42 3068 push @result, $sum / ($right + 1);
43 3068 $left++;
44 3068 ▶ $sum += $series[++$right] if $right < $#series;
Branch: 1 branch
BranchTrueFalseTotal
if $right < $#series 751 2317 3068
45 }
46
47 # middle part
48 100 while (1) {
49 1110 push @result, $sum / ($right - $left + 1);
50 1110 $sum -= $series[$left++];
51 1110 ▶ last if $right >= $#series;
Branch: 1 branch
BranchTrueFalseTotal
if $right >= $#series 100 1010 1110
52 1010 $sum += $series[++$right];
53 }
54
55 # tail
56 100 ▶ while (@result < @series) {
Branch: 1 branch
BranchTrueFalseTotal
if (@result < @series) 751 100 851
57 751 push @result, $sum / ($right - $left + 1);
58 751 $sum -= $series[$left++];
59 }
60
61 100 @result;
62 }
63
64 3 1;
65 __END__
↑ Summary