| 1 | package Feed::PhaseCheck; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | our @EXPORT_OK = qw(compare_feeds); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # ABSTRACT: Finds the relative time delay between two feed segments. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Feed::PhaseCheck | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Finds the relative time delay between two feed segments. | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Accomplished by shifting one feed relative to the other and then computing the error (absolute difference). | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | The shift that yields the lowest error corresponds to the relative delay between he two input feeds. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | The output consists of the delay found, and the error in delayed point. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | use Feed::PhaseCheck qw(compare_feeds); | ||||||||||||||||||||||||||||
| 32 | my $sample = { | ||||||||||||||||||||||||||||
| 33 | "1451276654" => "1.097655", | ||||||||||||||||||||||||||||
| 34 | "1451276655" => "1.09765", | ||||||||||||||||||||||||||||
| 35 | #... | ||||||||||||||||||||||||||||
| 36 | "1451276763" => "1.0976", | ||||||||||||||||||||||||||||
| 37 | "1451276764" => "1.097595" | ||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||
| 39 | my $compare_to = { | ||||||||||||||||||||||||||||
| 40 | "1451276629" => "1.09765", | ||||||||||||||||||||||||||||
| 41 | "1451276630" => "1.09764916666667", | ||||||||||||||||||||||||||||
| 42 | #... | ||||||||||||||||||||||||||||
| 43 | "1451276791" => "1.097595", | ||||||||||||||||||||||||||||
| 44 | "1451276792" => "1.097595" | ||||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||
| 46 | my $max_delay_check = 30; # seconds | ||||||||||||||||||||||||||||
| 47 | my ($errors,$delay_with_min_err) = compare_feeds($sample,$compare_to,$max_delay_check); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 METHODS | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head2 compare_feeds | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub compare_feeds { | ||||||||||||||||||||||||||
|
Subroutine
compare_feeds: called CC=24 SCAR=41.3
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 1 | my $sample = shift; | |||||||||||||||||||||||||||
| 59 | 1 | my $main_data = shift; | |||||||||||||||||||||||||||
| 60 | 1 | ▶ | my $max_delay_check = shift || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | if ($max_delay_check !~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | if (ref $sample ne 'HASH' || scalar keys %$sample < 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $sample ne "HASH" or scalar keys %$sample < 2
MC/DC0%0/2
ref $sample ne "HASH" or scalar keys %$sample < 2
ref $sample ne "HASH"
scalar keys %$sample < 2
Truth table33%1/3
A or B
A= ref $sample ne "HASH"B= scalar keys %$sample < 2
| |||||||||||||||||||||||||||||
| 67 | 0 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | if (ref $main_data ne 'HASH' || scalar keys %$main_data < 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $main_data ne "HASH" or scalar keys %$main_data < 2
MC/DC0%0/2
ref $main_data ne "HASH" or scalar keys %$main_data < 2
ref $main_data ne "HASH"
scalar keys %$main_data < 2
Truth table33%1/3
A or B
A= ref $main_data ne "HASH"B= scalar keys %$main_data < 2
| |||||||||||||||||||||||||||||
| 71 | 0 | return; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | my @main_epoches = sort keys %$main_data; | |||||||||||||||||||||||||||
| 75 | 1 | foreach (@main_epoches) { | |||||||||||||||||||||||||||
| 76 | 164 | ▶ | if (int($_) != $_ || abs($main_data->{$_}) != $main_data->{$_}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
int $_ != $_ or abs $main_data->{$_} != $main_data->{$_}
MC/DC0%0/2
int $_ != $_ or abs $main_data->{$_} != $main_data->{$_}
int $_ != $_
abs $main_data->{$_} != $main_data->{$_}
Truth table33%1/3
A or B
A= int $_ != $_B= abs $main_data->{$_} != $main_data->{$_}
| |||||||||||||||||||||||||||||
| 77 | 0 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | my @sample_epoches = sort keys %$sample; | |||||||||||||||||||||||||||
| 82 | 1 | foreach (@sample_epoches) { | |||||||||||||||||||||||||||
| 83 | 26 | ▶ | if (int($_) != $_ || abs($sample->{$_}) != $sample->{$_}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
int $_ != $_ or abs $sample->{$_} != $sample->{$_}
MC/DC0%0/2
int $_ != $_ or abs $sample->{$_} != $sample->{$_}
int $_ != $_
abs $sample->{$_} != $sample->{$_}
Truth table33%1/3
A or B
A= int $_ != $_B= abs $sample->{$_} != $sample->{$_}
| |||||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | if ($sample_epoches[0] < $main_epoches[0] || $sample_epoches[-1] > $main_epoches[-1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sample_epoches[0] < $main_epoches[0] or $sample_epoches[-1] > $main_epoches[-1]
MC/DC0%0/2
$sample_epoches[0] < $main_epoches[0] or $sample_epoches[-1] > $main_epoches[-1]
$sample_epoches[0] < $main_epoches[0]
$sample_epoches[-1] > $main_epoches[-1]
Truth table33%1/3
A or B
A= $sample_epoches[0] < $main_epoches[0]B= $sample_epoches[-1] > $main_epoches[-1]
| |||||||||||||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 1 | my %main_data = %$main_data; | |||||||||||||||||||||||||||
| 92 | 1 | my %error = (); | |||||||||||||||||||||||||||
| 93 | 1 | my ($min_error, $delay_for_min_error); | |||||||||||||||||||||||||||
| 94 | 1 | ▶ | my $delay1 = $sample_epoches[0] - $main_epoches[0] < $max_delay_check ? $sample_epoches[0] - $main_epoches[0] : $max_delay_check; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | my $delay2 = $main_epoches[-1] - $sample_epoches[-1] < $max_delay_check ? $main_epoches[-1] - $sample_epoches[-1] : $max_delay_check; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | for (my $delay = -$delay1; $delay <= $delay2; $delay++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 54 | $error{$delay} = 0; | |||||||||||||||||||||||||||
| 98 | 54 | foreach my $epoch (@sample_epoches) { | |||||||||||||||||||||||||||
| 99 | 1404 | my $sample_epoch = $epoch + $delay; | |||||||||||||||||||||||||||
| 100 | 1404 | ▶ | if (!defined $main_data{$sample_epoch}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | for (my $i = 1; $i < scalar keys @main_epoches; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($main_epoches[$i] > $sample_epoch) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | $main_data{$sample_epoch} = _interpolate( | ||||||||||||||||||||||||||||
| 104 | $main_epoches[$i - 1], | ||||||||||||||||||||||||||||
| 105 | $main_data{$main_epoches[$i - 1]}, | ||||||||||||||||||||||||||||
| 106 | 0 | $main_epoches[$i], $main_data{$main_epoches[$i]}, | |||||||||||||||||||||||||||
| 107 | $sample_epoch | ||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||
| 109 | 0 | last; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 1404 | $error{$delay} += ($main_data{$sample_epoch} - $sample->{$epoch})**2; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 54 | ▶ | if (!defined $min_error || $error{$delay} < $min_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $min_error or $error{$delay} < $min_error
MC/DC100%2/2
not defined $min_error or $error{$delay} < $min_error
not defined $min_error
$error{$delay} < $min_error
Truth table100%3/3
not(A) or B
A= not defined $min_errorB= $error{$delay} < $min_error
| |||||||||||||||||||||||||||||
| 116 | 25 | $min_error = $error{$delay}; | |||||||||||||||||||||||||||
| 117 | 25 | $delay_for_min_error = $delay; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | # $error{$delay} =~ s/(\d{8}).+?e/$1e/; | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | return (\%error, $delay_for_min_error); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub _interpolate { | ||||||||||||||||||||||||||
|
Subroutine
_interpolate: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 126 | 0 | my ($x1, $y1, $x2, $y2, $x) = @_; | |||||||||||||||||||||||||||
| 127 | 0 | my $y = $y1 + ($x - $x1) * ($y2 - $y1) / ($x2 - $x1); | |||||||||||||||||||||||||||
| 128 | 0 | return $y; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Maksym Kotielnikov, C<< <maksym at binary.com> >> | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 BUGS | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Please report any bugs or feature requests to C<bug-feed-phasecheck at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 138 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Feed-PhaseCheck>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 139 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | perldoc Feed::PhaseCheck | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | You can also look for information at: | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =over 4 | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Feed-PhaseCheck> | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | L<http://annocpan.org/dist/Feed-PhaseCheck> | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | L<http://cpanratings.perl.org/d/Feed-PhaseCheck> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | L<http://search.cpan.org/dist/Feed-PhaseCheck/> | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =back | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 2 | 1; # End of Feed::PhaseCheck | |||||||||||||||||||||||||||