I have a List with many samples representing many Amplitude values from a noisy sinusoidal wave. I need to find the Pearson's correlation coefficient for this wave and a given sinusoidal wave (not noisy). (So I can find the right harmonic from my noisy wave). What I really need is to do the same procedure in a method in C#. I know that this can be done with corrcoef command in MATLAB. Any explanation or idea is very welcome. Thanks in advance.
Asked
Active
Viewed 432 times
1
-
Possible duplicate of [Correlation of two arrays in C#](http://stackoverflow.com/q/17447817/2433501). – zelanix Nov 10 '15 at 00:17
1 Answers
1
Why don't you use Math.NET Numerics
Math.NET Numerics aims to provide methods and algorithms for numerical computations in science, engineering and every day use. Covered topics include special functions, linear algebra, probability models, random numbers, interpolation, integration, regression, optimization problems and more.
the class You are looking for is coded like:
namespace MathNet.Numerics.Statistics
{
using System;
using System.Collections.Generic;
using Properties;
/// <summary>
/// A class with correlation measures between two datasets.
/// </summary>
public static class Correlation
{
/// <summary>
/// Computes the Pearson product-moment correlation coefficient.
/// </summary>
/// <param name="dataA">Sample data A.</param>
/// <param name="dataB">Sample data B.</param>
/// <returns>The Pearson product-moment correlation coefficient.</returns>
public static double Pearson(IEnumerable<double> dataA, IEnumerable<double> dataB)
{
int n = 0;
double r = 0.0;
double meanA = dataA.Mean();
double meanB = dataB.Mean();
double sdevA = dataA.StandardDeviation();
double sdevB = dataB.StandardDeviation();
IEnumerator<double> ieA = dataA.GetEnumerator();
IEnumerator<double> ieB = dataB.GetEnumerator();
while (ieA.MoveNext())
{
if (ieB.MoveNext() == false)
{
throw new ArgumentOutOfRangeException("Datasets dataA and dataB need to have the same length.");
}
n++;
r += (ieA.Current - meanA) * (ieB.Current - meanB) / (sdevA * sdevB);
}
if (ieB.MoveNext() == true)
{
throw new ArgumentOutOfRangeException("Datasets dataA and dataB need to have the same length.");
}
return r / (n - 1);
}
}
}
Math.NET Numerics works very well with C# and related .Net languages. When using Visual Studio or another IDE with built-in NuGet support, you can get started quickly by adding a reference to the MathNet.Numerics NuGet package. Alternatively you can grab that package with the command line tool with nuget.exe install MathNet.Numerics -Pre or simply download the Zip package.
I have used this library intensively with good results. So you would use it like:
using MathNet.Numerics.Statistics;
correlation = Correlation.Pearson(arrayOfDoubles1, arrayOfDoubles2);

edgarmtze
- 24,683
- 80
- 235
- 386
-
It seems it does the hard work for me, I was seeking to do it myself, but it'll be ok, now I just have to thresh it myself. Thank you very much sir. – Marcelo Vinícius de Paula Nov 11 '15 at 01:06