-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbcf.cpp
179 lines (151 loc) · 4.19 KB
/
bcf.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*=============================================================================
Copyright (c) 2018 Cycfi Research. All rights reserved.
Distributed under the MIT License [ https://opensource.org/licenses/MIT ]
=============================================================================*/
#include <iostream>
#include <cmath>
#include <array>
#include <type_traits>
#include <cstdlib>
template <typename T>
constexpr bool is_pow2(T n)
{
return (n & (n - 1)) == 0;
}
template <std::uint32_t N, typename T = std::uint32_t>
struct bitstream
{
static_assert(is_pow2(N), "N must be a power of 2, except 0");
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static constexpr auto nbits = 8 * sizeof(T);
static constexpr auto array_size = N / nbits;
void clear()
{
bits.fill(0);
}
void set(std::uint32_t i, bool val)
{
auto mask = 1 << (i % nbits);
auto& ref = bits[i / nbits];
ref ^= (-T(val) ^ ref) & mask;
}
bool get(std::uint32_t i) const
{
auto mask = 1 << (i % nbits);
return (bits[i / nbits] & mask) != 0;
}
static std::uint32_t count_bits(std::uint32_t i)
{
// GCC only!!!
return __builtin_popcount(i);
}
static std::uint64_t count_bits(std::uint64_t i)
{
// GCC only!!!
return __builtin_popcountll(i);
}
template <typename F>
void auto_correlate(F f)
{
// The first will always be zero:
f(0, 0);
constexpr auto mid_array = (array_size / 2) - 1;
constexpr auto mid_pos = N / 2;
auto index = 0;
auto shift = 1;
for (auto pos = 1; pos != mid_pos; ++pos)
{
auto* p1 = bits.data();
auto* p2 = bits.data() + index;
auto count = 0;
if (shift == 0)
{
for (auto i = 0; i != mid_array; ++i)
count += count_bits(*p1++ ^ *p2++);
}
else
{
auto shift2 = nbits - shift;
for (auto i = 0; i != mid_array; ++i)
{
auto v = *p2++ >> shift;
v |= *p2 << shift2;
count += count_bits(*p1++ ^ v);
}
}
++shift;
if (shift == nbits)
{
shift = 0;
++index;
}
f(pos, count);
}
}
std::array<T, array_size> bits;
};
struct zero_cross
{
bool operator()(float s)
{
if (s < -0.1f)
y = 0;
else if (s > 0.0f)
y = 1;
return y;
}
bool y = 0;
};
struct noise
{
float operator()() const
{
return (float(rand()) / (RAND_MAX / 2)) - 1.0;
}
};
int main ()
{
constexpr auto pi = M_PI;
constexpr auto sps = 20000; // Samples per second
constexpr auto buff_size = 1024;
////////////////////////////////////////////////////////////////////////////
// Generate a test signal
std::array<float, buff_size> signal;
noise ns; // noise
const float f = 82.41;
float p = float(sps) / f;
for (int i = 0; i < buff_size; i++)
{
signal[i] = 0.1 * ns(); // Noise
signal[i] += 0.3 * sin(2 * pi * i / p); // First harmonic
signal[i] += 0.4 * sin(4 * pi * i / p); // Second harmonic
signal[i] += 0.3 * sin(6 * pi * i / p); // Third harmonic
}
////////////////////////////////////////////////////////////////////////////
// Zero crossing
zero_cross zc;
bitstream<buff_size> bin;
bin.clear();
for (auto i = 0; i != buff_size; ++i)
bin.set(i, zc(signal[i]));
////////////////////////////////////////////////////////////////////////////
// Binary Auto-correlation
std::array<std::uint32_t, buff_size / 2> corr;
bin.auto_correlate(
[&corr](auto pos, auto count)
{
corr[pos] = count;
}
);
////////////////////////////////////////////////////////////////////////////
// Print the signal, zero crossings and correlations
constexpr auto mid_pos = buff_size / 2;
for (int i = 0; i < buff_size; i++)
{
if (i < mid_pos)
std::cout << signal[i] << ", " << bin.get(i) << ", " << float(corr[i])/mid_pos << std::endl;
else
std::cout << signal[i] << ", " << bin.get(i) << std::endl;
}
return 0;
}