-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerMeter.cs
45 lines (35 loc) · 1.13 KB
/
PowerMeter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BangaiO
{
public class PowerMeter
{
public delegate void PowerUpdatedHandler(float db);
public event PowerUpdatedHandler PowerUpdated;
public InputPin<float> Input;
public PowerMeter(int bufferSize)
{
Input = new InputPin<float>(bufferSize);
Input.BufferFilled += new InputPin<float>.BufferFilledHandler(Input_BufferFilled);
}
void Input_BufferFilled(float[] buffer, int bufSize)
{
// TODO: DC removal?
float invSize = 1.0f / bufSize;
float dc = 0;
for (int i = 0; i < bufSize; ++i)
dc += buffer[i] * invSize;
float power = 0.0f;
for (int i = 0; i < bufSize; ++i)
{
float x = buffer[i] - dc;
power += x * x * invSize;
}
float db = 10.0f * (float)Math.Log10(power);
if (PowerUpdated != null)
PowerUpdated(db);
}
}
}