use Statistics::Basic::Correlation; sub xcorr { # given two sets and a phase increment, return # at which phase the best correlation is found # and what that value is my ($xref, $yref, $incdeg ) = @_; my $maxcorr = -1; $incdeg ||= 1; default 1 degree of phase. die "sets of unequal size" if ( $#$xref != $#$yref ); my $indexinc = $incdeg * ( 1 + $#$xref ); my $iters = 360 / $incdeg; { use integer; $indexinc /= 360; $iters++; } $indexinc ||=1; my @copy = @$yref; my $where = 0; my $match = 0; for (my $i = 0; $i < $iters; $i++) { unshift @copy, ( pop @copy ) for ( 1 .. $indexinc ); my $res = correlate( $xref, \@copy ); if ( $res > $maxcorr ) { $maxcorr = $res; $match = $where; } $where += $incdeg; } return ( $match, $maxcorr ); }