/
RunningAverage.cs
63 lines (61 loc) · 2.47 KB
/
RunningAverage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Linq;
using OpenCV.Net;
using System.ComponentModel;
using System.Reactive.Linq;
namespace Bonsai.Dsp
{
/// <summary>
/// Represents an operator that computes the running average of all the arrays in the sequence.
/// </summary>
[Description("Computes the running average of all the arrays in the sequence.")]
public class RunningAverage : ArrayTransform
{
/// <summary>
/// Gets or sets the weight to assign to each new array in the sequence.
/// This parameter determines how fast the average forgets previous values.
/// </summary>
[Range(0, 1)]
[Editor(DesignTypes.SliderEditor, DesignTypes.UITypeEditor)]
[Description("The weight to assign to each new array in the sequence. This parameter determines how fast the average forgets previous values.")]
public double Alpha { get; set; }
/// <summary>
/// Computes the running average of all the arrays in an observable sequence.
/// </summary>
/// <typeparam name="TArray">
/// The type of the array-like objects in the <paramref name="source"/> sequence.
/// </typeparam>
/// <param name="source">
/// A sequence of multi-channel array values.
/// </param>
/// <returns>
/// A sequence of multi-channel arrays, where each element represents the weighted
/// sum of the corresponding input value and the accumulated average.
/// </returns>
public override IObservable<TArray> Process<TArray>(IObservable<TArray> source)
{
var outputFactory = ArrFactory<TArray>.TemplateFactory;
var accumulatorFactory = ArrFactory<TArray>.TemplateSizeChannelFactory;
return Observable.Defer(() =>
{
TArray accumulator = null;
return source.Select(input =>
{
if (accumulator == null)
{
accumulator = accumulatorFactory(input, Depth.F32);
CV.Convert(input, accumulator);
return input;
}
else
{
var output = outputFactory(input);
CV.RunningAvg(input, accumulator, Alpha);
CV.Convert(accumulator, output);
return output;
}
});
});
}
}
}