suppose you have two sets @x and @y, then to employ correlation to check for cross-correlation between them, you have to simulate the phasing yourself as far as I know; something like this is what is on my mind:
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 ); }
(updated the code)

More update:

- the arrays are passed by reference i.e. \@x and \@y

- the approach does not take into account potential change of amplitude of one of the signals relative to the other, which is not taken into account by correlation alone. To compensate if necessary, find s(x)/s(y) where s() is standard deviation found using Statistics::Basic::StdDev and multiply all @copy by that before iterating through the phase shifts.

One world, one people


In reply to Re: Calculating cross-correlation by anonymized user 468275
in thread Calculating cross-correlation by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.