-
Notifications
You must be signed in to change notification settings - Fork 1
/
Downsampler2x4Neon.hpp
345 lines (263 loc) · 9.44 KB
/
Downsampler2x4Neon.hpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*****************************************************************************
Downsampler2x4Neon.hpp
Author: Laurent de Soras, 2016
--- Legal stuff ---
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*Tab=3***********************************************************************/
#if ! defined (hiir_Downsampler2x4Neon_CODEHEADER_INCLUDED)
#define hiir_Downsampler2x4Neon_CODEHEADER_INCLUDED
/*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
#include "hiir/fnc_neon.h"
#include "hiir/StageProc4Neon.h"
#include <cassert>
namespace hiir
{
/*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
/*
==============================================================================
Name: ctor
Throws: Nothing
==============================================================================
*/
template <int NC>
Downsampler2x4Neon <NC>::Downsampler2x4Neon ()
: _filter ()
{
for (int i = 0; i < NBR_COEFS + 2; ++i)
{
storea (_filter [i]._coef, vdupq_n_f32 (0));
}
clear_buffers ();
}
/*
==============================================================================
Name: set_coefs
Description:
Sets filter coefficients. Generate them with the PolyphaseIir2Designer
class.
Call this function before doing any processing.
Input parameters:
- coef_arr: Array of coefficients. There should be as many coefficients as
mentioned in the class template parameter.
Throws: Nothing
==============================================================================
*/
template <int NC>
void Downsampler2x4Neon <NC>::set_coefs (const double coef_arr [])
{
assert (coef_arr != nullptr);
for (int i = 0; i < NBR_COEFS; ++i)
{
storea (_filter [i + 2]._coef, vdupq_n_f32 (DataType (coef_arr [i])));
}
}
/*
==============================================================================
Name: process_sample
Description:
Downsamples (x2) one pair of vector of 4 samples, to generate one output
vector.
Input parameters:
- in_ptr: pointer on the two vectors to decimate. No alignment constraint.
Returns: Samplerate-reduced vector.
Throws: Nothing
==============================================================================
*/
template <int NC>
float32x4_t Downsampler2x4Neon <NC>::process_sample (const float in_ptr [8])
{
assert (in_ptr != nullptr);
const float32x4_t in_0 = load4u (in_ptr );
const float32x4_t in_1 = load4u (in_ptr + 4);
return process_sample (in_0, in_1);
}
/*
==============================================================================
Name: process_sample
Description:
Downsamples (x2) one pair of vector of 4 samples, to generate one output
vector.
Input parameters:
- in_0: vector at t
- in_1: vector at t + 1
Returns: Samplerate-reduced vector.
Throws: Nothing
==============================================================================
*/
template <int NC>
float32x4_t Downsampler2x4Neon <NC>::process_sample (float32x4_t in_0, float32x4_t in_1)
{
float32x4_t spl_0 = in_1;
float32x4_t spl_1 = in_0;
StageProc4Neon <NBR_COEFS>::process_sample_pos (
NBR_COEFS, spl_0, spl_1, &_filter [0]
);
const float32x4_t sum = spl_0 + spl_1;
const float32x4_t out = sum * vdupq_n_f32 (0.5f);
return out;
}
/*
==============================================================================
Name: process_block
Description:
Downsamples (x2) a block of vectors of 4 samples.
Input and output blocks may overlap, see assert() for details.
Input parameters:
- in_ptr: Input array, containing nbr_spl * 2 vectors.
No alignment constraint.
- nbr_spl: Number of vectors to output, > 0
Output parameters:
- out_ptr: Array for the output vectors, capacity: nbr_spl vectors.
No alignment constraint.
Throws: Nothing
==============================================================================
*/
template <int NC>
void Downsampler2x4Neon <NC>::process_block (float out_ptr [], const float in_ptr [], long nbr_spl)
{
assert (in_ptr != nullptr);
assert (out_ptr != nullptr);
assert (out_ptr <= in_ptr || out_ptr >= in_ptr + nbr_spl * 8);
assert (nbr_spl > 0);
long pos = 0;
do
{
const float32x4_t val = process_sample (in_ptr + pos * 8);
storeu (out_ptr + pos * 4, val);
++ pos;
}
while (pos < nbr_spl);
}
/*
==============================================================================
Name: process_sample_split
Description:
Split (spectrum-wise) in half a pair of vector of 4 samples. The lower part
of the spectrum is a classic downsampling, equivalent to the output of
process_sample().
The higher part is the complementary signal: original filter response
is flipped from left to right, becoming a high-pass filter with the same
cutoff frequency. This signal is then critically sampled (decimation by 2),
flipping the spectrum: Fs/4...Fs/2 becomes Fs/4...0.
Input parameters:
- in_ptr: pointer on the pair of input vectors. No alignment constraint.
Output parameters:
- low: output vector, lower part of the spectrum (downsampling)
- high: output vector, higher part of the spectrum.
Throws: Nothing
==============================================================================
*/
template <int NC>
void Downsampler2x4Neon <NC>::process_sample_split (float32x4_t &low, float32x4_t &high, const float in_ptr [8])
{
assert (in_ptr != nullptr);
const float32x4_t in_0 = load4u (in_ptr );
const float32x4_t in_1 = load4u (in_ptr + 4);
process_sample_split (low, high, in_0, in_1);
}
/*
==============================================================================
Name: process_sample_split
Description:
Split (spectrum-wise) in half a pair of vector of 4 samples. The lower part
of the spectrum is a classic downsampling, equivalent to the output of
process_sample().
The higher part is the complementary signal: original filter response
is flipped from left to right, becoming a high-pass filter with the same
cutoff frequency. This signal is then critically sampled (decimation by 2),
flipping the spectrum: Fs/4...Fs/2 becomes Fs/4...0.
Input parameters:
- in_0: vector at t
- in_1: vector at t + 1
Output parameters:
- low: output vector, lower part of the spectrum (downsampling)
- high: output vector, higher part of the spectrum.
Throws: Nothing
==============================================================================
*/
template <int NC>
void Downsampler2x4Neon <NC>::process_sample_split (float32x4_t &low, float32x4_t &high, float32x4_t in_0, float32x4_t in_1)
{
float32x4_t spl_0 = in_1;
float32x4_t spl_1 = in_0;
StageProc4Neon <NBR_COEFS>::process_sample_pos (
NBR_COEFS, spl_0, spl_1, &_filter [0]
);
const float32x4_t sum = spl_0 + spl_1;
low = sum * vdupq_n_f32 (0.5f);
high = spl_0 - low;
}
/*
==============================================================================
Name: process_block_split
Description:
Split (spectrum-wise) in half a pair of vector of 4 samples. The lower part
of the spectrum is a classic downsampling, equivalent to the output of
process_block().
The higher part is the complementary signal: original filter response
is flipped from left to right, becoming a high-pass filter with the same
cutoff frequency. This signal is then critically sampled (decimation by 2),
flipping the spectrum: Fs/4...Fs/2 becomes Fs/4...0.
Input and output blocks may overlap, see assert() for details.
Input parameters:
- in_ptr: Input array, containing nbr_spl * 2 vectors.
No alignment constraint.
- nbr_spl: Number of vectors for each output, > 0
Output parameters:
- out_l_ptr: Array for the output vectors, lower part of the spectrum
(downsampling). Capacity: nbr_spl vectors.
No alignment constraint.
- out_h_ptr: Array for the output vectors, higher part of the spectrum.
Capacity: nbr_spl vectors.
No alignment constraint.
Throws: Nothing
==============================================================================
*/
template <int NC>
void Downsampler2x4Neon <NC>::process_block_split (float out_l_ptr [], float out_h_ptr [], const float in_ptr [], long nbr_spl)
{
assert (in_ptr != nullptr);
assert (out_l_ptr != nullptr);
assert (out_l_ptr <= in_ptr || out_l_ptr >= in_ptr + nbr_spl * 8);
assert (out_h_ptr != nullptr);
assert (out_h_ptr <= in_ptr || out_h_ptr >= in_ptr + nbr_spl * 8);
assert (out_h_ptr != out_l_ptr);
assert (nbr_spl > 0);
long pos = 0;
do
{
float32x4_t low;
float32x4_t high;
process_sample_split (low, high, in_ptr + pos * 8);
storeu (out_l_ptr + pos * 4, low );
storeu (out_h_ptr + pos * 4, high);
++ pos;
}
while (pos < nbr_spl);
}
/*
==============================================================================
Name: clear_buffers
Description:
Clears filter memory, as if it processed silence since an infinite amount
of time.
Throws: Nothing
==============================================================================
*/
template <int NC>
void Downsampler2x4Neon <NC>::clear_buffers ()
{
for (int i = 0; i < NBR_COEFS + 2; ++i)
{
storea (_filter [i]._mem, vdupq_n_f32 (0));
}
}
/*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
/*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
} // namespace hiir
#endif // hiir_Downsampler2x4Neon_CODEHEADER_INCLUDED
/*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/