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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/*
Copyright (C) 2014, Youssef Touil <youssef@airspy.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "iqconverter_float.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
#include <malloc.h>
#define _aligned_malloc __mingw_aligned_malloc
#define _aligned_free __mingw_aligned_free
#define _inline inline
#define FIR_STANDARD
#elif defined(__APPLE__)
#include <malloc/malloc.h>
#define _aligned_malloc(size, alignment) malloc(size)
#define _aligned_free(mem) free(mem)
#define _inline inline
#define FIR_STANDARD
#elif defined(__FreeBSD__)
#define USE_SSE2
#include <immintrin.h>
#define _inline inline
#define _aligned_free(mem) free(mem)
void *_aligned_malloc(size_t size, size_t alignment)
{
void *result;
if (posix_memalign(&result, alignment, size) == 0)
return result;
return 0;
}
#elif defined(__GNUC__) && !defined(__MINGW64_VERSION_MAJOR)
#include <malloc.h>
#define _aligned_malloc(size, alignment) memalign(alignment, size)
#define _aligned_free(mem) free(mem)
#define _inline inline
#else
#if (_MSC_VER >= 1800)
//#define USE_SSE2
//#include <immintrin.h>
#endif
#endif
#define SIZE_FACTOR 32
#define DEFAULT_ALIGNMENT 16
#define HPF_COEFF 0.01f
#if defined(_MSC_VER)
#define ALIGNED __declspec(align(DEFAULT_ALIGNMENT))
#else
#define ALIGNED
#endif
iqconverter_float_t *iqconverter_float_create(const float *hb_kernel, int len)
{
int i, j;
size_t buffer_size;
iqconverter_float_t *cnv = (iqconverter_float_t *) _aligned_malloc(sizeof(iqconverter_float_t), DEFAULT_ALIGNMENT);
cnv->len = len / 2 + 1;
cnv->hbc = hb_kernel[len / 2];
buffer_size = cnv->len * sizeof(float);
cnv->fir_kernel = (float *) _aligned_malloc(buffer_size, DEFAULT_ALIGNMENT);
cnv->fir_queue = (float *) _aligned_malloc(buffer_size * SIZE_FACTOR, DEFAULT_ALIGNMENT);
cnv->delay_line = (float *) _aligned_malloc(buffer_size / 2, DEFAULT_ALIGNMENT);
iqconverter_float_reset(cnv);
for (i = 0, j = 0; i < cnv->len; i++, j += 2)
{
cnv->fir_kernel[i] = hb_kernel[j];
}
return cnv;
}
void iqconverter_float_free(iqconverter_float_t *cnv)
{
_aligned_free(cnv->fir_kernel);
_aligned_free(cnv->fir_queue);
_aligned_free(cnv->delay_line);
_aligned_free(cnv);
}
void iqconverter_float_reset(iqconverter_float_t *cnv)
{
cnv->avg = 0.0f;
cnv->fir_index = 0;
cnv->delay_index = 0;
memset(cnv->delay_line, 0, cnv->len * sizeof(float) / 2);
memset(cnv->fir_queue, 0, cnv->len * sizeof(float) * SIZE_FACTOR);
}
static _inline float process_fir_taps(const float *kernel, const float *queue, int len)
{
int i;
#ifdef USE_SSE2
__m128 acc = _mm_set_ps(0, 0, 0, 0);
#else
float sum = 0.0f;
#endif
if (len >= 8)
{
int it = len >> 3;
#ifdef USE_SSE2
for (i = 0; i < it; i++)
{
__m128 head1 = _mm_loadu_ps(queue);
__m128 kern1 = _mm_load_ps(kernel);
__m128 head2 = _mm_loadu_ps(queue + 4);
__m128 kern2 = _mm_load_ps(kernel + 4);
__m128 mul1 = _mm_mul_ps(kern1, head1);
__m128 mul2 = _mm_mul_ps(kern2, head2);
mul1 = _mm_add_ps(mul1, mul2);
acc = _mm_add_ps(acc, mul1);
queue += 8;
kernel += 8;
}
#else
for (i = 0; i < it; i++)
{
sum += kernel[0] * queue[0]
+ kernel[1] * queue[1]
+ kernel[2] * queue[2]
+ kernel[3] * queue[3]
+ kernel[4] * queue[4]
+ kernel[5] * queue[5]
+ kernel[6] * queue[6]
+ kernel[7] * queue[7];
queue += 8;
kernel += 8;
}
#endif
len &= 7;
}
if (len >= 4)
{
#ifdef USE_SSE2
__m128 head = _mm_loadu_ps(queue);
__m128 kern = _mm_load_ps(kernel);
__m128 mul = _mm_mul_ps(kern, head);
acc = _mm_add_ps(acc, mul);
#else
sum += kernel[0] * queue[0]
+ kernel[1] * queue[1]
+ kernel[2] * queue[2]
+ kernel[3] * queue[3];
#endif
kernel += 4;
queue += 4;
len &= 3;
}
#ifdef USE_SSE2
__m128 t = _mm_add_ps(acc, _mm_movehl_ps(acc, acc));
acc = _mm_add_ss(t, _mm_shuffle_ps(t, t, 1));
#ifdef __FreeBSD__
float sum = acc[0];
#else
float sum = acc.m128_f32[0];
#endif
#endif
if (len >= 2)
{
sum += kernel[0] * queue[0]
+ kernel[1] * queue[1];
//kernel += 2;
//queue += 2;
//len &= 1;
}
//if (len >= 1)
//{
// sum += kernel[0] * queue[0];
//}
return sum;
}
static void fir_interleaved_4(iqconverter_float_t *cnv, float *samples, int len)
{
int i;
int fir_index = cnv->fir_index;
int fir_len = cnv->len;
float *fir_kernel = cnv->fir_kernel;
float *fir_queue = cnv->fir_queue;
float *queue;
float acc;
for (i = 0; i < len; i += 2)
{
queue = fir_queue + fir_index;
queue[0] = samples[i];
acc = fir_kernel[0] * (queue[0] + queue[4 - 1])
+ fir_kernel[1] * (queue[1] + queue[4 - 2]);
samples[i] = acc;
if (--fir_index < 0)
{
fir_index = fir_len * (SIZE_FACTOR - 1);
memcpy(fir_queue + fir_index + 1, fir_queue, (fir_len - 1) * sizeof(float));
}
}
cnv->fir_index = fir_index;
}
static void fir_interleaved_8(iqconverter_float_t *cnv, float *samples, int len)
{
int i;
int fir_index = cnv->fir_index;
int fir_len = cnv->len;
float *fir_kernel = cnv->fir_kernel;
float *fir_queue = cnv->fir_queue;
float *queue;
float acc;
for (i = 0; i < len; i += 2)
{
queue = fir_queue + fir_index;
queue[0] = samples[i];
acc = fir_kernel[0] * (queue[0] + queue[8 - 1])
+ fir_kernel[1] * (queue[1] + queue[8 - 2])
+ fir_kernel[2] * (queue[2] + queue[8 - 3])
+ fir_kernel[3] * (queue[3] + queue[8 - 4]);
samples[i] = acc;
if (--fir_index < 0)
{
fir_index = fir_len * (SIZE_FACTOR - 1);
memcpy(fir_queue + fir_index + 1, fir_queue, (fir_len - 1) * sizeof(float));
}
}
cnv->fir_index = fir_index;
}
static void fir_interleaved_12(iqconverter_float_t *cnv, float *samples, int len)
{
int i;
int fir_index = cnv->fir_index;
int fir_len = cnv->len;
float *fir_kernel = cnv->fir_kernel;
float *fir_queue = cnv->fir_queue;
float *queue;
float acc = 0;
for (i = 0; i < len; i += 2)
{
queue = fir_queue + fir_index;
queue[0] = samples[i];
acc = fir_kernel[0] * (queue[0] + queue[12 - 1])
+ fir_kernel[1] * (queue[1] + queue[12 - 2])
+ fir_kernel[2] * (queue[2] + queue[12 - 3])
+ fir_kernel[3] * (queue[3] + queue[12 - 4])
+ fir_kernel[4] * (queue[4] + queue[12 - 5])
+ fir_kernel[5] * (queue[5] + queue[12 - 6]);
samples[i] = acc;
if (--fir_index < 0)
{
fir_index = fir_len * (SIZE_FACTOR - 1);
memcpy(fir_queue + fir_index + 1, fir_queue, (fir_len - 1) * sizeof(float));
}
}
cnv->fir_index = fir_index;
}
static void fir_interleaved_24(iqconverter_float_t *cnv, float *samples, int len)
{
int i;
int fir_index = cnv->fir_index;
int fir_len = cnv->len;
float *fir_kernel = cnv->fir_kernel;
float *fir_queue = cnv->fir_queue;
float *queue;
float acc = 0;
for (i = 0; i < len; i += 2)
{
queue = fir_queue + fir_index;
queue[0] = samples[i];
acc = fir_kernel[0] * (queue[0] + queue[24 - 1])
+ fir_kernel[1] * (queue[1] + queue[24 - 2])
+ fir_kernel[2] * (queue[2] + queue[24 - 3])
+ fir_kernel[3] * (queue[3] + queue[24 - 4])
+ fir_kernel[4] * (queue[4] + queue[24 - 5])
+ fir_kernel[5] * (queue[5] + queue[24 - 6])
+ fir_kernel[6] * (queue[6] + queue[24 - 7])
+ fir_kernel[7] * (queue[7] + queue[24 - 8])
+ fir_kernel[8] * (queue[8] + queue[24 - 9])
+ fir_kernel[9] * (queue[9] + queue[24 - 10])
+ fir_kernel[10] * (queue[10] + queue[24 - 11])
+ fir_kernel[11] * (queue[11] + queue[24 - 12]);
samples[i] = acc;
if (--fir_index < 0)
{
fir_index = fir_len * (SIZE_FACTOR - 1);
memcpy(fir_queue + fir_index + 1, fir_queue, (fir_len - 1) * sizeof(float));
}
}
cnv->fir_index = fir_index;
}
static void fir_interleaved_generic(iqconverter_float_t *cnv, float *samples, int len)
{
int i;
int fir_index = cnv->fir_index;
int fir_len = cnv->len;
float *fir_kernel = cnv->fir_kernel;
float *fir_queue = cnv->fir_queue;
float *queue;
for (i = 0; i < len; i += 2)
{
queue = fir_queue + fir_index;
queue[0] = samples[i];
samples[i] = process_fir_taps(fir_kernel, queue, fir_len);
if (--fir_index < 0)
{
fir_index = fir_len * (SIZE_FACTOR - 1);
memcpy(fir_queue + fir_index + 1, fir_queue, (fir_len - 1) * sizeof(float));
}
}
cnv->fir_index = fir_index;
}
static void fir_interleaved(iqconverter_float_t *cnv, float *samples, int len)
{
switch (cnv->len)
{
case 4:
fir_interleaved_4(cnv, samples, len);
break;
case 8:
fir_interleaved_8(cnv, samples, len);
break;
case 12:
fir_interleaved_12(cnv, samples, len);
break;
case 24:
fir_interleaved_24(cnv, samples, len);
break;
default:
fir_interleaved_generic(cnv, samples, len);
break;
}
}
static void delay_interleaved(iqconverter_float_t *cnv, float *samples, int len)
{
int i;
ALIGNED int index;
ALIGNED int half_len;
ALIGNED float res;
half_len = cnv->len >> 1;
index = cnv->delay_index;
for (i = 0; i < len; i += 2)
{
res = cnv->delay_line[index];
cnv->delay_line[index] = samples[i];
samples[i] = res;
if (++index >= half_len)
{
index = 0;
}
}
cnv->delay_index = index;
}
#define SCALE (0.01f)
static void remove_dc(iqconverter_float_t *cnv, float *samples, int len)
{
int i;
ALIGNED float avg = cnv->avg;
for (i = 0; i < len; i++)
{
samples[i] -= avg;
avg += SCALE * samples[i];
}
cnv->avg = avg;
}
static void translate_fs_4(iqconverter_float_t *cnv, float *samples, int len)
{
int i;
ALIGNED float hbc = cnv->hbc;
#ifdef USE_SSE2
float *buf = samples;
ALIGNED __m128 vec;
ALIGNED __m128 rot = _mm_set_ps(hbc, 1.0f, -hbc, -1.0f);
for (i = 0; i < len / 4; i++, buf +=4)
{
vec = _mm_loadu_ps(buf);
vec = _mm_mul_ps(vec, rot);
_mm_storeu_ps(buf, vec);
}
#else
int j;
for (i = 0; i < len / 4; i++)
{
j = i << 2;
samples[j + 0] = -samples[j + 0];
samples[j + 1] = -samples[j + 1] * hbc;
//samples[j + 2] = samples[j + 2];
samples[j + 3] = samples[j + 3] * hbc;
}
#endif
fir_interleaved(cnv, samples, len);
delay_interleaved(cnv, samples + 1, len);
}
void iqconverter_float_process(iqconverter_float_t *cnv, float *samples, int len)
{
remove_dc(cnv, samples, len);
translate_fs_4(cnv, samples, len);
}
|