Cross correlation using mathdotnet

Error processing SSI file

Answers

  1. Conor

    • 2018/9/22

    I have tried the above solution with a sine wave that was shifted backwards by 20 time units with respect to a first sine wave. It gave me the correct result that the maximum of the correlation is at -20 (see below). One could discuss whether its appropriate to apply a zero padding, the zeros are not usually part of the signal. The MATLAB cross-correlation is not normalized the same way, it's not a "Pearson correlation" as in the example above.

    The definition of the MATLAB cross-correlation is different: for scaling option "none" its a convolution with the time reversed signal. There are also various scaling options but none of them gives the same result as the Pearson correlation: matlab definition of xcorr

    My result: cross correlation of sin(n*0.1) with sin(n*0.1 - 20*0.1) using the example above:

  2. Harlem

    • 2019/2/23

    I have recently started using Mathdotnet Numerics statistical package to do data analysis in c#. I am looking for the cross correlation function 

  3. Harvey

    • 2018/3/2

    Correlation can be calculated by any of the methods from MathNet.Numerics.Statistics.Correlation, like Pearson or Spearman. But if you're looking for results like the ones provided by Matlab's xcorr or autocorr, then you have to manually calculate the correlation using those methods for each lag/delay value between your input samples. Notice this example includes both, cross and auto correlation.

  4. Leandro

    • 2017/3/18

    Data array to calculate auto correlation for. Return. Double[]. An array with the ACF as a function of the lags k.

  5. Enzo

    • 2021/3/20

    Correlation can be calculated by any of the methods from MathNet.Numerics.Statistics.Correlation, like Pearson or Spearman. But if you're looking for results like the ones provided by Matlab's xcorr or autocorr, then you have to manually calculate the correlation using those methods for each lag/delay value between your input samples. Notice this example includes both, cross and auto correlation.

    double fs = 50; //sampling rate, Hz
    double te = 1; //end time, seconds
    int size = (int)(fs * te); //sample size
    
    var t = Enumerable.Range(0, size).Select(p => p / fs).ToArray();
    var y1 = t.Select(p => p < te / 2 ? 1.0 : 0).ToArray();
    var y2 = t.Select(p => p < te / 2 ? 1.0 - 2*p : 0).ToArray();
    
    var r12 = StatsHelper.CrossCorrelation(y1, y2); // Y1 * Y2
    var r21 = StatsHelper.CrossCorrelation(y2, y1); // Y2 * Y1
    var r11 = StatsHelper.CrossCorrelation(y1, y1); // Y1 * Y1 autocorrelation
    

    StatsHelper:

    public static class StatsHelper
    {
        public static LagCorr CrossCorrelation(double[] x1, double[] x2)
        {
            if (x1.Length != x2.Length)
                throw new Exception("Samples must have same size.");
    
            var len = x1.Length;
            var len2 = 2 * len;
            var len3 = 3 * len;
            var s1 = new double[len3];
            var s2 = new double[len3];
            var cor = new double[len2];
            var lag = new double[len2];
    
            Array.Copy(x1, 0, s1, len, len);
            Array.Copy(x2, 0, s2, 0, len);
    
            for (int i = 0; i < len2; i++)
            {
                cor[i] = Correlation.Pearson(s1, s2);
                lag[i] = i - len;
                Array.Copy(s2,0,s2,1,s2.Length-1);
                s2[0] = 0;
            }
    
            return new LagCorr { Corr = cor, Lag = lag };
        }
    }
    

    LagCorr:

    public class LagCorr
    {
        public double[] Lag { get; set; }
        public double[] Corr { get; set; }
    }
    

    EDIT: Adding Matlab comparison results:

    clear;
    step=0.02;
    t=[0:step:1-step];
    y1=ones(1,50);
    y1(26:50)=0;
    y2=[1-2*t];
    y2(26:50)=0;
    
    [cor12,lags12]=xcorr(y1,y2);
    [cor21,lags21]=xcorr(y2,y1);
    [cor11,lags11]=xcorr(y1,y1);
    [cor22,lags22]=xcorr(y2,y2);
    
    subplot(2,3,1);
    plot(t,y1);
    title('Y1');
    axis([0 1 -0.5 1.5]);
    
    subplot(2,3,2);
    plot(lags12,cor12);
    title('Y1*Y2');
    axis([-30 30 0 15]);
    
    subplot(2,3,3);
    plot(lags11,cor11);
    title('Y1*Y1');
    axis([-30 30 0 30]);
    
    subplot(2,3,4);
    plot(t,y2);
    title('Y2');
    axis([0 1 -0.5 1.5]);
    
    subplot(2,3,5);
    plot(lags21,cor21);
    title('Y2*Y1');
    axis([-30 30 0 15]);
    
    subplot(2,3,6);
    plot(lags22,cor22);
    title('Y2*Y2');
    axis([-30 30 0 10]);
    

  6. Ari

    • 2016/7/28

    Here is a code for cross correlation borrowed from other question. I am running this code using a sliding window that takes 40 points (20 seconds) and slides by 20 (10 seconds) every step. public IList< (double Corr, double Lag)> CalculateCrossCorrelation (double [] x1, double [] x2) { var len = x1.Length; var len2 = 2 * len; var len3 = 3 * len; var s1 = new double [len3]; var s2 = new double [len3]; Array.Copy (x1, 0, s1, len, len); Array.Copy (x2, 0, s2, 0, len); var results = new

  7. Fabbri

    • 2016/7/23

    using Complex = System.Numerics.Complex;. namespace MathNet.Numerics.Statistics. {. /// <summary>. /// A class with correlation measures between two 

  8. Dorian

    • 2015/2/1

    private KeyValuePair<int, double> findMaxCorrelation(List<double> pattern, List<double> signal) { int MaxShift = 0; double MaxCorrelation = .0; int patternLength = pattern.Count; int signalLength = signal.Count; int compensatedSignalLength = signalLength + 2 * patternLength; // signal 111111 // patern 0000 // compensatedsignal 00001111110000 // add size of the signal to ensure the signal can cover the whole range of the pattern List<double> compensatedSignal = new List<double>(signal

  9. Jonas

    • 2021/3/18

    You are using the stats package, you'll probably have more luck here https://numerics.mathdotnet.com/api/MathNet.Numeri It's a pretty 

  10. Mac

    • 2019/11/26

    Hi guys I'm trying to implement a phase shift meter in VB.NET between 2 audio signals. I'm using Math.NET cross-correlation functions however they can't calculate the phase shift or lag between the 2 signals as far as I can tell.

  11. Brandon

    • 2015/4/10

    I suspect that you need to pre-whiten the images. Here's an example with and without prewhitening.

  12. Otis

    • 2017/5/23

    Cross-correlation measures the similarity between a vector x and shifted (lagged) copies of a vector y as a function of the lag. If x and y have different lengths, the function appends zeros to the end of the shorter vector so it has the same length as the other. example. r = xcorr (x) returns the autocorrelation sequence of x.

  13. Aldo

    • 2015/12/29

    Pearson(signal1, signal2)" to calculate the cross-correlation coeffcieint or delay between these 2 signals using this or any other related function?

  14. Riva

    • 2020/5/6

    the fast cross correlation. Arguments for using the FFT to accelerate the cross correlation are often not supported with specific data on computation time (a situation, which this paper remedies) [Lyon 97]. The cross correlation has uses in many fields of scientific endeavor (music, identification of blood flow, astronomical event processing

  15. Pedro

    • 2021/10/14

    Abstract: The paper is concerned with complications of linear recurrent sequences over the field $GF(q)$ and the ring $GR(q^n, p^n)$ with interconnected 

  16. Eduardo

    • 2016/9/19

    Abstract: We study the class of sequences over the prime field with $p$ elements formed by the most significant $p$-ary digits of linear recurrent sequences 

Comments are closed.

More Posts