0

I have a signal that I store it real time in a list in c#. I also have a reference signal which I want to compare with the real time signal. The two signals haven't the same size. How can I align those two signals? How can I correlate them in c# I mean is there a function to calculate the correlation?

snake plissken
  • 2,649
  • 10
  • 43
  • 64

3 Answers3

3

I think that you need to reconsider your data-structure. It's not clear if the signals;

  1. cover different time-spans;
  2. have different time-steps between readings;
  3. or both.

In the first case, you can simply crop the series so that they have the same length and cover the same time-span. However, a list of numbers can only contain the values, not the times they are for. If you do not store this information elsewhere in the program then you will not be able to do the clipping.

In the second case, you need to choose an appropriate series or times and conform both of your signals to that. This will likely take the form of a series of lerp operations to fill in the target points.

// x - the first value
// y - the second value
// t - the distance from the first to the second value, normalized to 0..1
public static float Lerp(float x, float y, float t) {

    return x + t * (y - x);
}

As you can see, performing the Lerp requires t, which can be computed from the time-values of the two known points.

A better data-structure might be a mapping of times to values:

var signal = new Dictionary<DateTime, double>();

This will allow you to keep track of when a reading happens more easily.

There is already a question about performing the actual correlation on StackOverflow.

As an aside, this is something which R makes considerably easier - take a look at the zoo package for inspiration.

Community
  • 1
  • 1
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
2

You will not be able to find correlation between to arrays of different length. You need either to make shorter array longer, or longer array shorter. I propose you to consider Autoregressive conditional heteroskedasticity and/or Vector autoregression in order to make manipulations with prolonging or cutting short your array. After that you can apply correlation calculation.

Yuriy Zaletskyy
  • 4,983
  • 5
  • 34
  • 54
1

Use following VB.NET code to make arrays of same size. It will basically stretch/compress an array and at the same time it will maintain the linear sequence of values.

e.g. {2, 4, 6} will be stretched to size 9 as {2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0}

Public Sub Process
        Dim requiredLength As Integer = 10
        Dim originalArr() As Double = {2.2, 3.3, 4.4, 4}

        Dim lcm As Integer = GetLCM(originalArr.Length - 1, requiredLength - 1)

        Dim finalArr(requiredLength - 1) As Double
        Dim finalIndex As Integer = 0
        Dim originalIndex As Integer = 0
        Dim currentValue As Double

        Dim portionRatio As Integer = (lcm / (originalArr.Length - 1))
        Dim portionValue As Double = 0
        For i = 0 To lcm
            If portionRatio = 1 Or (i + 1) Mod portionRatio = 1 Then
                currentValue = originalArr(originalIndex)

                If originalIndex < originalArr.Length - 1 Then portionValue = (originalArr(originalIndex + 1) - originalArr(originalIndex)) / (lcm - 1)

                originalIndex += 1
            Else
                currentValue += portionValue
            End If

            If i Mod (lcm / (requiredLength - 1)) = 0 Then
                finalArr(finalIndex) = currentValue
                finalIndex += 1
            End If
        Next
End Sub

Private Function GetGCD(num1 As Integer, num2 As Integer) As Integer
    While (Not num1 = num2)
        If num1 > num2 Then num1 = num1 - num2
        If num2 > num1 Then num2 = num2 - num1
    End While

    Return num1
End Function

Private  Function GetLCM(num1 As Integer, num2 As Integer) As Integer
    Return (num1 * num2) / GetGCD(num1, num2)
End Function

If you plot array on excel chart before and after the process. You will see chart line will have same shape.

Then use any correlation coefficient formula to compare it with required array.