mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-06 11:32:59 +00:00
Correlation
This commit is contained in:
parent
2844eb60ec
commit
23f68b2dbb
@ -1,13 +1,23 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
using Esiur.Analysis.DSP;
|
||||||
|
|
||||||
namespace Esiur.Analysis.Test
|
namespace Esiur.Analysis.Test
|
||||||
{
|
{
|
||||||
internal static class Program
|
internal static class Program
|
||||||
{
|
{
|
||||||
|
private const int V = -1;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The main entry point for the application.
|
/// The main entry point for the application.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[STAThread]
|
[STAThread]
|
||||||
static void Main()
|
static void Main()
|
||||||
{
|
{
|
||||||
|
var signalA = new double[] { 1, 1, 1, V, 1, 1, V, V, 1, V, 1, V, V, V, V };
|
||||||
|
var signalB = new double[] { 1, V, V, V, V, 1, V, V, V, V, 1, V, V, V, V };
|
||||||
|
var cor = signalA.CrossCorrelation(signalB, true);
|
||||||
|
Debug.WriteLine(cor);
|
||||||
|
|
||||||
// To customize application configuration such as set high DPI settings or default font,
|
// To customize application configuration such as set high DPI settings or default font,
|
||||||
// see https://aka.ms/applicationconfiguration.
|
// see https://aka.ms/applicationconfiguration.
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
|
@ -1,21 +1,22 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Esiur.Analysis.DSP;
|
||||||
|
|
||||||
namespace Esiur.Analysis.DSP
|
namespace Esiur.Analysis.DSP
|
||||||
{
|
{
|
||||||
public static class Functions
|
public static class Functions
|
||||||
{
|
{
|
||||||
public static double[] ConvolveMany(params double[][] signals)
|
public static double[] MultipleConvolution(params double[][] signals)
|
||||||
{
|
{
|
||||||
var rt = signals[0];
|
var rt = signals[0];
|
||||||
|
|
||||||
for (var i = 1; i < signals.Length; i++)
|
for (var i = 1; i < signals.Length; i++)
|
||||||
rt = rt.Convolve(signals[i]);
|
rt = rt.Convolution(signals[i]);
|
||||||
|
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
public static double[] Convolve(this double[] signal, double[] filter)
|
public static double[] Convolution(this double[] signal, double[] filter)
|
||||||
{
|
{
|
||||||
var length = signal.Length + filter.Length - 1;
|
var length = signal.Length + filter.Length - 1;
|
||||||
var rt = new double[length];
|
var rt = new double[length];
|
||||||
@ -37,20 +38,46 @@ namespace Esiur.Analysis.DSP
|
|||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double[] CrossCorrelate(this double[] signal, double[] filter)
|
public static double[] CrossCorrelation(this double[] signal, double[] filter, bool cyclic = false)
|
||||||
{
|
{
|
||||||
var length = signal.Length + filter.Length - 1;
|
|
||||||
var rt = new double[length];
|
|
||||||
for (var i = 0; i < length; i++)
|
|
||||||
{
|
|
||||||
for (var j = 0; j < signal.Length && j + i < filter.Length; j++)
|
|
||||||
{
|
|
||||||
rt[i] = signal[j] * filter[i + j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return rt;
|
if (cyclic)
|
||||||
|
{
|
||||||
|
var length = signal.Length + filter.Length - 1;
|
||||||
|
var rt = new double[length];
|
||||||
|
|
||||||
|
for (var i = 0; i < length; i++)
|
||||||
|
{
|
||||||
|
for (var j = 0; j < signal.Length; j++)
|
||||||
|
{
|
||||||
|
rt[i] += signal[j] * filter[(i + j) % filter.Length];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rt;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var length = signal.Length + filter.Length - 1;
|
||||||
|
var rt = new double[length];
|
||||||
|
|
||||||
|
for (var i = 0; i < length; i++)
|
||||||
|
{
|
||||||
|
for (var j = 0; j < signal.Length; j++)
|
||||||
|
{
|
||||||
|
if (i + j < filter.Length)
|
||||||
|
rt[i] += signal[j] * filter[i + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static double[] AutoCorrelation(this double[] signal, bool cyclic = false)
|
||||||
|
{
|
||||||
|
return signal.CrossCorrelation(signal, cyclic);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user