-
Notifications
You must be signed in to change notification settings - Fork 1
/
FsBy4CarrierDetector.cs
116 lines (93 loc) · 2.86 KB
/
FsBy4CarrierDetector.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
namespace UnderwaterVideo2
{
public class FsBy4CarrierDetector
{
#region Properties
int[] ring1;
int[] ring2;
int ringHead;
int ringTail;
int ringSize;
double s1;
double s2;
double s;
double sPrev;
int cycle;
int smpCount;
public double Threshold { get; set; }
#endregion
#region Constructor
public FsBy4CarrierDetector(int rSize, double threshold)
{
if (rSize < 4) throw new ArgumentOutOfRangeException("rSize should be greater or equal 4");
ringSize = rSize;
ring1 = new int[ringSize];
ring2 = new int[ringSize];
Threshold = threshold;
Init();
}
#endregion
#region Methods
public void Init()
{
s1 = 0;
s2 = 0;
s = 0;
sPrev = -1;
Array.Clear(ring1, 0, ringSize);
Array.Clear(ring2, 0, ringSize);
ringHead = ringSize - 1;
ringTail = 0;
cycle = 0;
smpCount = 0;
}
public bool ProcessSample(short a)
{
bool result = false;
if (smpCount == 0)
{
ring1[ringHead] = a;
ring2[ringHead] = a;
s1 += a - ring1[ringTail];
s2 += a - ring2[ringTail];
}
else if (smpCount == 1)
{
ring1[ringHead] = a;
ring2[ringHead] = -a;
s1 += a - ring1[ringTail];
s2 += - a - ring2[ringTail];
}
else if (smpCount == 2)
{
ring1[ringHead] = -a;
ring2[ringHead] = -a;
s1 += -a - ring1[ringTail];
s2 += -a - ring2[ringTail];
}
else if (smpCount == 3)
{
ring1[ringHead] = -a;
ring2[ringHead] = a;
s1 += -a - ring1[ringTail];
s2 += a - ring2[ringTail];
}
ringHead = (ringHead + 1) % ringSize;
ringTail = (ringTail + 1) % ringSize;
if (++smpCount >= 4)
{
smpCount = 0;
if (++cycle >= ringSize)
{
s = Math.Sqrt(s1 * s1 + s2 * s2) / ringSize;
cycle = 0;
result = (s - sPrev) >= Threshold;
sPrev = s;
}
}
return result;
}
#endregion
}
}