-
Notifications
You must be signed in to change notification settings - Fork 0
/
liquid.h
8823 lines (7883 loc) · 479 KB
/
liquid.h
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2007 - 2020 Joseph Gaeddert
*
* 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.
*/
#ifndef __LIQUID_H__
#define __LIQUID_H__
#ifdef __cplusplus
extern "C" {
# define LIQUID_USE_COMPLEX_H 0
#else
# define LIQUID_USE_COMPLEX_H 1
#endif // __cplusplus
// common headers
#include <inttypes.h>
//
// Make sure the version and version number macros weren't defined by
// some prevoiusly included header file.
//
#ifdef LIQUID_VERSION
# undef LIQUID_VERSION
#endif
#ifdef LIQUID_VERSION_NUMBER
# undef LIQUID_VERSION_NUMBER
#endif
//
// Compile-time version numbers
//
// LIQUID_VERSION = "X.Y.Z"
// LIQUID_VERSION_NUMBER = (X*1000000 + Y*1000 + Z)
//
#define LIQUID_VERSION "1.3.2"
#define LIQUID_VERSION_NUMBER 1003002
//
// Run-time library version numbers
//
extern const char liquid_version[];
const char * liquid_libversion(void);
int liquid_libversion_number(void);
// run-time library validation
#define LIQUID_VALIDATE_LIBVERSION \
if (LIQUID_VERSION_NUMBER != liquid_libversion_number()) { \
fprintf(stderr,"%s:%u: ", __FILE__,__LINE__); \
fprintf(stderr,"error: invalid liquid runtime library\n"); \
exit(1); \
} \
// basic error types
#define LIQUID_NUM_ERRORS 12
typedef enum {
// everything ok
LIQUID_OK=0,
// internal logic error; this is a bug with liquid and should be reported immediately
LIQUID_EINT,
// invalid object, examples:
// - destroy() method called on NULL pointer
LIQUID_EIOBJ,
// invalid parameter, or configuration; examples:
// - setting bandwidth of a filter to a negative number
// - setting FFT size to zero
// - create a spectral periodogram object with window size greater than nfft
LIQUID_EICONFIG,
// input out of range; examples:
// - try to take log of -1
// - try to create an FFT plan of size zero
LIQUID_EIVAL,
// invalid vector length or dimension; examples
// - trying to refer to the 17th element of a 2 x 2 matrix
// - trying to multiply two matrices of incompatible dimensions
LIQUID_EIRANGE,
// invalid mode; examples:
// - try to create a modem of type 'LIQUID_MODEM_XXX' which does not exit
LIQUID_EIMODE,
// unsupported mode (e.g. LIQUID_FEC_CONV_V27 with 'libfec' not installed)
LIQUID_EUMODE,
// object has not been created or properly initialized
// - try to run firfilt_crcf_execute(NULL, ...)
// - try to modulate using an arbitrary modem without initializing the constellation
LIQUID_ENOINIT,
// not enough memory allocated for operation; examples:
// - try to factor 100 = 2*2*5*5 but only give 3 spaces for factors
LIQUID_EIMEM,
// file input/output; examples:
// - could not open a file for writing because of insufficient permissions
// - could not open a file for reading because it does not exist
// - try to read more data than a file has space for
// - could not parse line in file (improper formatting)
LIQUID_EIO,
} liquid_error_code;
// error descriptions
extern const char * liquid_error_str[LIQUID_NUM_ERRORS];
const char * liquid_error_info(liquid_error_code _code);
#define LIQUID_CONCAT(prefix, name) prefix ## name
#define LIQUID_VALIDATE_INPUT
/*
* Compile-time complex data type definitions
*
* Default: use the C99 complex data type, otherwise
* define complex type compatible with the C++ complex standard,
* otherwise resort to defining binary compatible array.
*/
#if LIQUID_USE_COMPLEX_H==1
# include <complex.h>
# define LIQUID_DEFINE_COMPLEX(R,C) typedef R _Complex C
#elif defined _GLIBCXX_COMPLEX || defined _LIBCPP_COMPLEX
# define LIQUID_DEFINE_COMPLEX(R,C) typedef std::complex<R> C
#else
# define LIQUID_DEFINE_COMPLEX(R,C) typedef struct {R real; R imag;} C;
#endif
//# define LIQUID_DEFINE_COMPLEX(R,C) typedef R C[2]
LIQUID_DEFINE_COMPLEX(float, liquid_float_complex);
LIQUID_DEFINE_COMPLEX(double, liquid_double_complex);
//
// MODULE : agc (automatic gain control)
//
// available squelch modes
typedef enum {
LIQUID_AGC_SQUELCH_UNKNOWN=0, // unknown/unavailable squelch mode
LIQUID_AGC_SQUELCH_ENABLED, // squelch enabled but signal not detected
LIQUID_AGC_SQUELCH_RISE, // signal first hit/exceeded threshold
LIQUID_AGC_SQUELCH_SIGNALHI, // signal level high (above threshold)
LIQUID_AGC_SQUELCH_FALL, // signal first dropped below threshold
LIQUID_AGC_SQUELCH_SIGNALLO, // signal level low (below threshold)
LIQUID_AGC_SQUELCH_TIMEOUT, // signal level low (below threshold for a certain time)
LIQUID_AGC_SQUELCH_DISABLED, // squelch not enabled
} agc_squelch_mode;
#define LIQUID_AGC_MANGLE_CRCF(name) LIQUID_CONCAT(agc_crcf, name)
#define LIQUID_AGC_MANGLE_RRRF(name) LIQUID_CONCAT(agc_rrrf, name)
// large macro
// AGC : name-mangling macro
// T : primitive data type
// TC : input/output data type
#define LIQUID_AGC_DEFINE_API(AGC,T,TC) \
\
/* Automatic gain control (agc) for level correction and signal */ \
/* detection */ \
typedef struct AGC(_s) * AGC(); \
\
/* Create automatic gain control object. */ \
AGC() AGC(_create)(void); \
\
/* Destroy object, freeing all internally-allocated memory. */ \
int AGC(_destroy)(AGC() _q); \
\
/* Print object properties to stdout, including received signal */ \
/* strength indication (RSSI), loop bandwidth, lock status, and squelch */ \
/* status. */ \
int AGC(_print)(AGC() _q); \
\
/* Reset internal state of agc object, including gain estimate, input */ \
/* signal level estimate, lock status, and squelch mode */ \
/* If the squelch mode is disabled, it stays disabled, but all enabled */ \
/* modes (e.g. LIQUID_AGC_SQUELCH_TIMEOUT) resets to just */ \
/* LIQUID_AGC_SQUELCH_ENABLED. */ \
int AGC(_reset)(AGC() _q); \
\
/* Execute automatic gain control on an single input sample */ \
/* _q : automatic gain control object */ \
/* _x : input sample */ \
/* _y : output sample */ \
int AGC(_execute)(AGC() _q, \
TC _x, \
TC * _y); \
\
/* Execute automatic gain control on block of samples pointed to by _x */ \
/* and store the result in the array of the same length _y. */ \
/* _q : automatic gain control object */ \
/* _x : input data array, [size: _n x 1] */ \
/* _n : number of input, output samples */ \
/* _y : output data array, [size: _n x 1] */ \
int AGC(_execute_block)(AGC() _q, \
TC * _x, \
unsigned int _n, \
TC * _y); \
\
/* Lock agc object. When locked, the agc object still makes an estimate */ \
/* of the signal level, but the gain setting is fixed and does not */ \
/* change. */ \
/* This is useful for providing coarse input signal level correction */ \
/* and quickly detecting a packet burst but not distorting signals with */ \
/* amplitude variation due to modulation. */ \
int AGC(_lock)(AGC() _q); \
\
/* Unlock agc object, and allow amplitude correction to resume. */ \
int AGC(_unlock)(AGC() _q); \
\
/* Set loop filter bandwidth: attack/release time. */ \
/* _q : automatic gain control object */ \
/* _bt : bandwidth-time constant, _bt > 0 */ \
int AGC(_set_bandwidth)(AGC() _q, float _bt); \
\
/* Get the agc object's loop filter bandwidth. */ \
float AGC(_get_bandwidth)(AGC() _q); \
\
/* Get the input signal's estimated energy level, relative to unity. */ \
/* The result is a linear value. */ \
float AGC(_get_signal_level)(AGC() _q); \
\
/* Set the agc object's estimate of the input signal by specifying an */ \
/* explicit linear value. This is useful for initializing the agc */ \
/* object with a preliminary estimate of the signal level to help gain */ \
/* convergence. */ \
/* _q : automatic gain control object */ \
/* _x2 : signal level of input, _x2 > 0 */ \
int AGC(_set_signal_level)(AGC() _q, \
float _x2); \
\
/* Get the agc object's estimated received signal strength indication */ \
/* (RSSI) on the input signal. */ \
/* This is similar to getting the signal level (above), but returns the */ \
/* result in dB rather than on a linear scale. */ \
float AGC(_get_rssi)(AGC() _q); \
\
/* Set the agc object's estimated received signal strength indication */ \
/* (RSSI) on the input signal by specifying an explicit value in dB. */ \
/* _q : automatic gain control object */ \
/* _rssi : signal level of input [dB] */ \
int AGC(_set_rssi)(AGC() _q, float _rssi); \
\
/* Get the gain value currently being applied to the input signal */ \
/* (linear). */ \
float AGC(_get_gain)(AGC() _q); \
\
/* Set the agc object's internal gain by specifying an explicit linear */ \
/* value. */ \
/* _q : automatic gain control object */ \
/* _gain : gain to apply to input signal, _gain > 0 */ \
int AGC(_set_gain)(AGC() _q, \
float _gain); \
\
/* Get the ouput scaling applied to each sample (linear). */ \
float AGC(_get_scale)(AGC() _q); \
\
/* Set the agc object's output scaling (linear). Note that this does */ \
/* affect the response of the AGC. */ \
/* _q : automatic gain control object */ \
/* _gain : gain to apply to input signal, _gain > 0 */ \
int AGC(_set_scale)(AGC() _q, \
float _scale); \
\
/* Estimate signal level and initialize internal gain on an input */ \
/* array. */ \
/* _q : automatic gain control object */ \
/* _x : input data array, [size: _n x 1] */ \
/* _n : number of input, output samples */ \
int AGC(_init)(AGC() _q, \
TC * _x, \
unsigned int _n); \
\
/* Enable squelch mode. */ \
int AGC(_squelch_enable)(AGC() _q); \
\
/* Disable squelch mode. */ \
int AGC(_squelch_disable)(AGC() _q); \
\
/* Return flag indicating if squelch is enabled or not. */ \
int AGC(_squelch_is_enabled)(AGC() _q); \
\
/* Set threshold for enabling/disabling squelch. */ \
/* _q : automatic gain control object */ \
/* _thresh : threshold for enabling squelch [dB] */ \
int AGC(_squelch_set_threshold)(AGC() _q, \
T _thresh); \
\
/* Get squelch threshold (value in dB) */ \
T AGC(_squelch_get_threshold)(AGC() _q); \
\
/* Set timeout before enabling squelch. */ \
/* _q : automatic gain control object */ \
/* _timeout : timeout before enabling squelch [samples] */ \
int AGC(_squelch_set_timeout)(AGC() _q, \
unsigned int _timeout); \
\
/* Get squelch timeout (number of samples) */ \
unsigned int AGC(_squelch_get_timeout)(AGC() _q); \
\
/* Get squelch status (e.g. LIQUID_AGC_SQUELCH_TIMEOUT) */ \
int AGC(_squelch_get_status)(AGC() _q); \
// Define agc APIs
LIQUID_AGC_DEFINE_API(LIQUID_AGC_MANGLE_CRCF, float, liquid_float_complex)
LIQUID_AGC_DEFINE_API(LIQUID_AGC_MANGLE_RRRF, float, float)
//
// MODULE : audio
//
// CVSD: continuously variable slope delta
typedef struct cvsd_s * cvsd;
// create cvsd object
// _num_bits : number of adjacent bits to observe (4 recommended)
// _zeta : slope adjustment multiplier (1.5 recommended)
// _alpha : pre-/post-emphasis filter coefficient (0.9 recommended)
// NOTE: _alpha must be in [0,1]
cvsd cvsd_create(unsigned int _num_bits,
float _zeta,
float _alpha);
// destroy cvsd object
void cvsd_destroy(cvsd _q);
// print cvsd object parameters
void cvsd_print(cvsd _q);
// encode/decode single sample
unsigned char cvsd_encode(cvsd _q, float _audio_sample);
float cvsd_decode(cvsd _q, unsigned char _bit);
// encode/decode 8 samples at a time
void cvsd_encode8(cvsd _q, float * _audio, unsigned char * _data);
void cvsd_decode8(cvsd _q, unsigned char _data, float * _audio);
//
// MODULE : buffer
//
// circular buffer
#define LIQUID_CBUFFER_MANGLE_FLOAT(name) LIQUID_CONCAT(cbufferf, name)
#define LIQUID_CBUFFER_MANGLE_CFLOAT(name) LIQUID_CONCAT(cbuffercf, name)
// large macro
// CBUFFER : name-mangling macro
// T : data type
#define LIQUID_CBUFFER_DEFINE_API(CBUFFER,T) \
\
/* Circular buffer object for storing and retrieving samples in a */ \
/* first-in/first-out (FIFO) manner using a minimal amount of memory */ \
typedef struct CBUFFER(_s) * CBUFFER(); \
\
/* Create circular buffer object of a particular maximum storage length */ \
/* _max_size : maximum buffer size, _max_size > 0 */ \
CBUFFER() CBUFFER(_create)(unsigned int _max_size); \
\
/* Create circular buffer object of a particular maximum storage size */ \
/* and specify the maximum number of elements that can be read at any */ \
/* any given time */ \
/* _max_size : maximum buffer size, _max_size > 0 */ \
/* _max_read : maximum size that will be read from buffer */ \
CBUFFER() CBUFFER(_create_max)(unsigned int _max_size, \
unsigned int _max_read); \
\
/* Destroy cbuffer object, freeing all internal memory */ \
void CBUFFER(_destroy)(CBUFFER() _q); \
\
/* Print cbuffer object properties to stdout */ \
void CBUFFER(_print)(CBUFFER() _q); \
\
/* Print cbuffer object properties and internal state */ \
void CBUFFER(_debug_print)(CBUFFER() _q); \
\
/* Clear internal buffer */ \
void CBUFFER(_reset)(CBUFFER() _q); \
\
/* Get the number of elements currently in the buffer */ \
unsigned int CBUFFER(_size)(CBUFFER() _q); \
\
/* Get the maximum number of elements the buffer can hold */ \
unsigned int CBUFFER(_max_size)(CBUFFER() _q); \
\
/* Get the maximum number of elements you may read at once */ \
unsigned int CBUFFER(_max_read)(CBUFFER() _q); \
\
/* Get the number of available slots (max_size - size) */ \
unsigned int CBUFFER(_space_available)(CBUFFER() _q); \
\
/* Return flag indicating if the buffer is full or not */ \
int CBUFFER(_is_full)(CBUFFER() _q); \
\
/* Write a single sample into the buffer */ \
/* _q : circular buffer object */ \
/* _v : input sample */ \
void CBUFFER(_push)(CBUFFER() _q, \
T _v); \
\
/* Write a block of samples to the buffer */ \
/* _q : circular buffer object */ \
/* _v : array of samples to write to buffer */ \
/* _n : number of samples to write */ \
void CBUFFER(_write)(CBUFFER() _q, \
T * _v, \
unsigned int _n); \
\
/* Remove and return a single element from the buffer by setting the */ \
/* value of the output sample pointed to by _v */ \
/* _q : circular buffer object */ \
/* _v : pointer to sample output */ \
void CBUFFER(_pop)(CBUFFER() _q, \
T * _v); \
\
/* Read buffer contents by returning a pointer to the linearized array; */ \
/* note that the returned pointer is only valid until another operation */ \
/* is performed on the circular buffer object */ \
/* _q : circular buffer object */ \
/* _num_requested : number of elements requested */ \
/* _v : output pointer */ \
/* _num_read : number of elements referenced by _v */ \
void CBUFFER(_read)(CBUFFER() _q, \
unsigned int _num_requested, \
T ** _v, \
unsigned int * _num_read); \
\
/* Release _n samples from the buffer */ \
/* _q : circular buffer object */ \
/* _n : number of elements to release */ \
void CBUFFER(_release)(CBUFFER() _q, \
unsigned int _n); \
// Define buffer APIs
LIQUID_CBUFFER_DEFINE_API(LIQUID_CBUFFER_MANGLE_FLOAT, float)
LIQUID_CBUFFER_DEFINE_API(LIQUID_CBUFFER_MANGLE_CFLOAT, liquid_float_complex)
// Windowing functions
#define LIQUID_WINDOW_MANGLE_FLOAT(name) LIQUID_CONCAT(windowf, name)
#define LIQUID_WINDOW_MANGLE_CFLOAT(name) LIQUID_CONCAT(windowcf, name)
// large macro
// WINDOW : name-mangling macro
// T : data type
#define LIQUID_WINDOW_DEFINE_API(WINDOW,T) \
\
/* Sliding window first-in/first-out buffer with a fixed size */ \
typedef struct WINDOW(_s) * WINDOW(); \
\
/* Create window buffer object of a fixed length */ \
WINDOW() WINDOW(_create)(unsigned int _n); \
\
/* Recreate window buffer object with new length. */ \
/* This extends an existing window's size, similar to the standard C */ \
/* library's realloc() to n samples. */ \
/* If the size of the new window is larger than the old one, the newest */ \
/* values are retained at the beginning of the buffer and the oldest */ \
/* values are truncated. If the size of the new window is smaller than */ \
/* the old one, the oldest values are truncated. */ \
/* _q : old window object */ \
/* _n : new window length */ \
WINDOW() WINDOW(_recreate)(WINDOW() _q, unsigned int _n); \
\
/* Destroy window object, freeing all internally memory */ \
int WINDOW(_destroy)(WINDOW() _q); \
\
/* Print window object to stdout */ \
int WINDOW(_print)(WINDOW() _q); \
\
/* Print window object to stdout (with extra information) */ \
int WINDOW(_debug_print)(WINDOW() _q); \
\
/* Reset window object (initialize to zeros) */ \
int WINDOW(_reset)(WINDOW() _q); \
\
/* Read the contents of the window by returning a pointer to the */ \
/* aligned internal memory array. This method guarantees that the */ \
/* elements are linearized. This method should only be used for */ \
/* reading; writing values to the buffer has unspecified results. */ \
/* Note that the returned pointer is only valid until another operation */ \
/* is performed on the window buffer object */ \
/* _q : window object */ \
/* _v : output pointer (set to internal array) */ \
int WINDOW(_read)(WINDOW() _q, \
T ** _v); \
\
/* Index single element in buffer at a particular index */ \
/* This retrieves the \(i^{th}\) sample in the window, storing the */ \
/* output value in _v. */ \
/* This is equivalent to first invoking read() and then indexing on the */ \
/* resulting pointer; however the result is obtained much faster. */ \
/* Therefore setting the index to 0 returns the oldest value in the */ \
/* window. */ \
/* _q : window object */ \
/* _i : index of element to read */ \
/* _v : output value pointer */ \
int WINDOW(_index)(WINDOW() _q, \
unsigned int _i, \
T * _v); \
\
/* Shifts a single sample into the right side of the window, pushing */ \
/* the oldest (left-most) sample out of the end. Unlike stacks, the */ \
/* window object has no equivalent "pop" method, as values are retained */ \
/* in memory until they are overwritten. */ \
/* _q : window object */ \
/* _v : single input element */ \
int WINDOW(_push)(WINDOW() _q, \
T _v); \
\
/* Write array of elements onto window buffer */ \
/* Effectively, this is equivalent to pushing each sample one at a */ \
/* time, but executes much faster. */ \
/* _q : window object */ \
/* _v : input array of values to write */ \
/* _n : number of input values to write */ \
int WINDOW(_write)(WINDOW() _q, \
T * _v, \
unsigned int _n); \
// Define window APIs
LIQUID_WINDOW_DEFINE_API(LIQUID_WINDOW_MANGLE_FLOAT, float)
LIQUID_WINDOW_DEFINE_API(LIQUID_WINDOW_MANGLE_CFLOAT, liquid_float_complex)
//LIQUID_WINDOW_DEFINE_API(LIQUID_WINDOW_MANGLE_UINT, unsigned int)
// wdelay functions : windowed-delay
// Implements an efficient z^-k delay with minimal memory
#define LIQUID_WDELAY_MANGLE_FLOAT(name) LIQUID_CONCAT(wdelayf, name)
#define LIQUID_WDELAY_MANGLE_CFLOAT(name) LIQUID_CONCAT(wdelaycf, name)
//#define LIQUID_WDELAY_MANGLE_UINT(name) LIQUID_CONCAT(wdelayui, name)
// large macro
// WDELAY : name-mangling macro
// T : data type
#define LIQUID_WDELAY_DEFINE_API(WDELAY,T) \
\
/* Efficient digital delay line using a minimal amount of memory */ \
typedef struct WDELAY(_s) * WDELAY(); \
\
/* Create delay buffer object with a particular number of samples of */ \
/* delay */ \
/* _delay : number of samples of delay in the wdelay object */ \
WDELAY() WDELAY(_create)(unsigned int _delay); \
\
/* Re-create delay buffer object, adjusting the delay size, preserving */ \
/* the internal state of the object */ \
/* _q : old delay buffer object */ \
/* _delay : delay for new object */ \
WDELAY() WDELAY(_recreate)(WDELAY() _q, \
unsigned int _delay); \
\
/* Destroy delay buffer object, freeing internal memory */ \
void WDELAY(_destroy)(WDELAY() _q); \
\
/* Print delay buffer object's state to stdout */ \
void WDELAY(_print)(WDELAY() _q); \
\
/* Clear/reset state of object */ \
void WDELAY(_reset)(WDELAY() _q); \
\
/* Read delayed sample at the head of the buffer and store it to the */ \
/* output pointer */ \
/* _q : delay buffer object */ \
/* _v : value of delayed element */ \
void WDELAY(_read)(WDELAY() _q, \
T * _v); \
\
/* Push new sample into delay buffer object */ \
/* _q : delay buffer object */ \
/* _v : new value to be added to buffer */ \
void WDELAY(_push)(WDELAY() _q, \
T _v); \
// Define wdelay APIs
LIQUID_WDELAY_DEFINE_API(LIQUID_WDELAY_MANGLE_FLOAT, float)
LIQUID_WDELAY_DEFINE_API(LIQUID_WDELAY_MANGLE_CFLOAT, liquid_float_complex)
//LIQUID_WDELAY_DEFINE_API(LIQUID_WDELAY_MANGLE_UINT, unsigned int)
//
// MODULE : channel
//
#define LIQUID_CHANNEL_MANGLE_CCCF(name) LIQUID_CONCAT(channel_cccf,name)
// large macro
// CHANNEL : name-mangling macro
// TO : output data type
// TC : coefficients data type
// TI : input data type
#define LIQUID_CHANNEL_DEFINE_API(CHANNEL,TO,TC,TI) \
\
/* Channel emulation */ \
typedef struct CHANNEL(_s) * CHANNEL(); \
\
/* Create channel object with default parameters */ \
CHANNEL() CHANNEL(_create)(void); \
\
/* Destroy channel object, freeing all internal memory */ \
int CHANNEL(_destroy)(CHANNEL() _q); \
\
/* Print channel object internals to standard output */ \
int CHANNEL(_print)(CHANNEL() _q); \
\
/* Include additive white Gausss noise impairment */ \
/* _q : channel object */ \
/* _N0dB : noise floor power spectral density [dB] */ \
/* _SNRdB : signal-to-noise ratio [dB] */ \
int CHANNEL(_add_awgn)(CHANNEL() _q, \
float _N0dB, \
float _SNRdB); \
\
/* Include carrier offset impairment */ \
/* _q : channel object */ \
/* _frequency : carrier frequency offset [radians/sample] */ \
/* _phase : carrier phase offset [radians] */ \
int CHANNEL(_add_carrier_offset)(CHANNEL() _q, \
float _frequency, \
float _phase); \
\
/* Include multi-path channel impairment */ \
/* _q : channel object */ \
/* _h : channel coefficients (NULL for random) */ \
/* _h_len : number of channel coefficients */ \
int CHANNEL(_add_multipath)(CHANNEL() _q, \
TC * _h, \
unsigned int _h_len); \
\
/* Include slowly-varying shadowing impairment */ \
/* _q : channel object */ \
/* _sigma : standard deviation for log-normal shadowing */ \
/* _fd : Doppler frequency, 0 <= _fd < 0.5 */ \
int CHANNEL(_add_shadowing)(CHANNEL() _q, \
float _sigma, \
float _fd); \
\
/* Apply channel impairments on single input sample */ \
/* _q : channel object */ \
/* _x : input sample */ \
/* _y : pointer to output sample */ \
int CHANNEL(_execute)(CHANNEL() _q, \
TI _x, \
TO * _y); \
\
/* Apply channel impairments on block of samples */ \
/* _q : channel object */ \
/* _x : input array, [size: _n x 1] */ \
/* _n : input array, length */ \
/* _y : output array, [size: _n x 1] */ \
int CHANNEL(_execute_block)(CHANNEL() _q, \
TI * _x, \
unsigned int _n, \
TO * _y); \
LIQUID_CHANNEL_DEFINE_API(LIQUID_CHANNEL_MANGLE_CCCF,
liquid_float_complex,
liquid_float_complex,
liquid_float_complex)
//
// time-varying multi-path channel
//
#define LIQUID_TVMPCH_MANGLE_CCCF(name) LIQUID_CONCAT(tvmpch_cccf,name)
// large macro
// TVMPCH : name-mangling macro
// TO : output data type
// TC : coefficients data type
// TI : input data type
#define LIQUID_TVMPCH_DEFINE_API(TVMPCH,TO,TC,TI) \
\
/* Time-varying multipath channel emulation */ \
typedef struct TVMPCH(_s) * TVMPCH(); \
\
/* Create time-varying multi-path channel emulator object, specifying */ \
/* the number of coefficients, the standard deviation of coefficients, */ \
/* and the coherence time. The larger the standard deviation, the more */ \
/* dramatic the frequency response of the channel. The shorter the */ \
/* coeherent time, the faster the channel effects. */ \
/* _n : number of coefficients, _n > 0 */ \
/* _std : standard deviation, _std >= 0 */ \
/* _tau : normalized coherence time, 0 < _tau < 1 */ \
TVMPCH() TVMPCH(_create)(unsigned int _n, \
float _std, \
float _tau); \
\
/* Destroy channel object, freeing all internal memory */ \
int TVMPCH(_destroy)(TVMPCH() _q); \
\
/* Reset object */ \
int TVMPCH(_reset)(TVMPCH() _q); \
\
/* Print channel object internals to standard output */ \
int TVMPCH(_print)(TVMPCH() _q); \
\
/* Push sample into emulator */ \
/* _q : channel object */ \
/* _x : input sample */ \
int TVMPCH(_push)(TVMPCH() _q, \
TI _x); \
\
/* Compute output sample */ \
/* _q : channel object */ \
/* _y : output sample */ \
int TVMPCH(_execute)(TVMPCH() _q, \
TO * _y); \
\
/* Apply channel impairments on a block of samples */ \
/* _q : channel object */ \
/* _x : input array, [size: _n x 1] */ \
/* _n : input array length */ \
/* _y : output array, [size: _n x 1] */ \
int TVMPCH(_execute_block)(TVMPCH() _q, \
TI * _x, \
unsigned int _n, \
TO * _y); \
LIQUID_TVMPCH_DEFINE_API(LIQUID_TVMPCH_MANGLE_CCCF,
liquid_float_complex,
liquid_float_complex,
liquid_float_complex)
//
// MODULE : dotprod (vector dot product)
//
#define LIQUID_DOTPROD_MANGLE_RRRF(name) LIQUID_CONCAT(dotprod_rrrf,name)
#define LIQUID_DOTPROD_MANGLE_CCCF(name) LIQUID_CONCAT(dotprod_cccf,name)
#define LIQUID_DOTPROD_MANGLE_CRCF(name) LIQUID_CONCAT(dotprod_crcf,name)
// large macro
// DOTPROD : name-mangling macro
// TO : output data type
// TC : coefficients data type
// TI : input data type
#define LIQUID_DOTPROD_DEFINE_API(DOTPROD,TO,TC,TI) \
\
/* Vector dot product operation */ \
typedef struct DOTPROD(_s) * DOTPROD(); \
\
/* Run dot product without creating object. This is less efficient than */ \
/* creating the object as it is an unoptimized portable implementation */ \
/* that doesn't take advantage of processor extensions. It is meant to */ \
/* provide a baseline for performance comparison and a convenient way */ \
/* to invoke a dot product operation when fast operation is not */ \
/* necessary. */ \
/* _v : coefficients array [size: _n x 1] */ \
/* _x : input array [size: _n x 1] */ \
/* _n : dotprod length, _n > 0 */ \
/* _y : output sample pointer */ \
void DOTPROD(_run)( TC * _v, \
TI * _x, \
unsigned int _n, \
TO * _y); \
\
/* This provides the same unoptimized operation as the 'run()' method */ \
/* above, but with the loop unrolled by a factor of 4. It is marginally */ \
/* faster than 'run()' without unrolling the loop. */ \
/* _v : coefficients array [size: _n x 1] */ \
/* _x : input array [size: _n x 1] */ \
/* _n : dotprod length, _n > 0 */ \
/* _y : output sample pointer */ \
void DOTPROD(_run4)( TC * _v, \
TI * _x, \
unsigned int _n, \
TO * _y); \
\
/* Create vector dot product object */ \
/* _v : coefficients array [size: _n x 1] */ \
/* _n : dotprod length, _n > 0 */ \
DOTPROD() DOTPROD(_create)(TC * _v, \
unsigned int _n); \
\
/* Re-create dot product object of potentially a different length with */ \
/* different coefficients. If the length of the dot product object does */ \
/* not change, not memory reallocation is invoked. */ \
/* _q : old dotprod object */ \
/* _v : coefficients array [size: _n x 1] */ \
/* _n : dotprod length, _n > 0 */ \
DOTPROD() DOTPROD(_recreate)(DOTPROD() _q, \
TC * _v, \
unsigned int _n); \
\
/* Destroy dotprod object, freeing all internal memory */ \
void DOTPROD(_destroy)(DOTPROD() _q); \
\
/* Print dotprod object internals to standard output */ \
void DOTPROD(_print)(DOTPROD() _q); \
\
/* Execute dot product on an input array */ \
/* _q : dotprod object */ \
/* _x : input array [size: _n x 1] */ \
/* _y : output sample pointer */ \
void DOTPROD(_execute)(DOTPROD() _q, \
TI * _x, \
TO * _y); \
LIQUID_DOTPROD_DEFINE_API(LIQUID_DOTPROD_MANGLE_RRRF,
float,
float,
float)
LIQUID_DOTPROD_DEFINE_API(LIQUID_DOTPROD_MANGLE_CCCF,
liquid_float_complex,
liquid_float_complex,
liquid_float_complex)
LIQUID_DOTPROD_DEFINE_API(LIQUID_DOTPROD_MANGLE_CRCF,
liquid_float_complex,
float,
liquid_float_complex)
//
// sum squared methods
//
float liquid_sumsqf(float * _v,
unsigned int _n);
float liquid_sumsqcf(liquid_float_complex * _v,
unsigned int _n);
//
// MODULE : equalization
//
// least mean-squares (LMS)
#define LIQUID_EQLMS_MANGLE_RRRF(name) LIQUID_CONCAT(eqlms_rrrf,name)
#define LIQUID_EQLMS_MANGLE_CCCF(name) LIQUID_CONCAT(eqlms_cccf,name)
// large macro
// EQLMS : name-mangling macro
// T : data type
#define LIQUID_EQLMS_DEFINE_API(EQLMS,T) \
\
/* Least mean-squares equalization object */ \
typedef struct EQLMS(_s) * EQLMS(); \
\
/* Create LMS EQ initialized with external coefficients */ \
/* _h : filter coefficients; set to NULL for {1,0,0...},[size: _n x 1] */ \
/* _n : filter length */ \
EQLMS() EQLMS(_create)(T * _h, \
unsigned int _n); \
\
/* Create LMS EQ initialized with square-root Nyquist prototype filter */ \
/* as initial set of coefficients. This is useful for applications */ \
/* where the baseline matched filter is a good starting point, but */ \
/* where equalization is needed to properly remove inter-symbol */ \
/* interference. */ \
/* The filter length is \(2 k m + 1\) */ \
/* _type : filter type (e.g. LIQUID_FIRFILT_RRC) */ \
/* _k : samples/symbol */ \
/* _m : filter delay (symbols) */ \
/* _beta : rolloff factor (0 < beta <= 1) */ \
/* _dt : fractional sample delay */ \
EQLMS() EQLMS(_create_rnyquist)(int _type, \
unsigned int _k, \
unsigned int _m, \
float _beta, \
float _dt); \
\
/* Create LMS EQ initialized with low-pass filter */ \
/* _n : filter length */ \
/* _fc : filter cut-off normalized to sample rate, 0 < _fc <= 0.5 */ \
EQLMS() EQLMS(_create_lowpass)(unsigned int _n, \
float _fc); \
\
/* Re-create EQ initialized with external coefficients */ \
/* _q : equalizer object */ \
/* _h : filter coefficients (NULL for {1,0,0...}), [size: _n x 1] */ \
/* _h_len : filter length */ \
EQLMS() EQLMS(_recreate)(EQLMS() _q, \
T * _h, \
unsigned int _h_len); \
\
/* Destroy equalizer object, freeing all internal memory */ \
int EQLMS(_destroy)(EQLMS() _q); \
\
/* Reset equalizer object, clearing internal state */ \
int EQLMS(_reset)(EQLMS() _q); \
\
/* Print equalizer internal state */ \
int EQLMS(_print)(EQLMS() _q); \
\
/* Get equalizer learning rate */ \
float EQLMS(_get_bw)(EQLMS() _q); \
\
/* Set equalizer learning rate */ \
/* _q : equalizer object */ \
/* _lambda : learning rate, _lambda > 0 */ \
int EQLMS(_set_bw)(EQLMS() _q, \
float _lambda); \
\
/* Push sample into equalizer internal buffer */ \
/* _q : equalizer object */ \
/* _x : input sample */ \
int EQLMS(_push)(EQLMS() _q, \
T _x); \
\
/* Push block of samples into internal buffer of equalizer object */ \
/* _q : equalizer object */ \
/* _x : input sample array, [size: _n x 1] */ \
/* _n : input sample array length */ \
int EQLMS(_push_block)(EQLMS() _q, \
T * _x, \
unsigned int _n); \
\
/* Execute internal dot product and return result */ \
/* _q : equalizer object */ \
/* _y : output sample */ \
int EQLMS(_execute)(EQLMS() _q, \
T * _y); \
\
/* Execute equalizer with block of samples using constant */ \
/* modulus algorithm, operating on a decimation rate of _k */ \
/* samples. */ \
/* _q : equalizer object */ \
/* _k : down-sampling rate */ \
/* _x : input sample array [size: _n x 1] */ \
/* _n : input sample array length */ \
/* _y : output sample array [size: _n x 1] */ \
int EQLMS(_execute_block)(EQLMS() _q, \
unsigned int _k, \
T * _x, \
unsigned int _n, \
T * _y); \
\
/* Step through one cycle of equalizer training */ \
/* _q : equalizer object */ \
/* _d : desired output */ \
/* _d_hat : actual output */ \
int EQLMS(_step)(EQLMS() _q, \
T _d, \
T _d_hat); \
\
/* Step through one cycle of equalizer training (blind) */ \
/* _q : equalizer object */ \
/* _d_hat : actual output */ \
int EQLMS(_step_blind)(EQLMS() _q, \
T _d_hat); \
\
/* Get equalizer's internal coefficients */ \
/* _q : equalizer object */ \
/* _w : weights, [size: _p x 1] */ \
int EQLMS(_get_weights)(EQLMS() _q, \
T * _w); \
\
/* Train equalizer object on group of samples */ \
/* _q : equalizer object */ \
/* _w : input/output weights, [size: _p x 1] */ \
/* _x : received sample vector,[size: _n x 1] */ \
/* _d : desired output vector, [size: _n x 1] */ \
/* _n : input, output vector length */ \
int EQLMS(_train)(EQLMS() _q, \
T * _w, \
T * _x, \
T * _d, \
unsigned int _n); \
LIQUID_EQLMS_DEFINE_API(LIQUID_EQLMS_MANGLE_RRRF, float)
LIQUID_EQLMS_DEFINE_API(LIQUID_EQLMS_MANGLE_CCCF, liquid_float_complex)
// recursive least-squares (RLS)
#define LIQUID_EQRLS_MANGLE_RRRF(name) LIQUID_CONCAT(eqrls_rrrf,name)
#define LIQUID_EQRLS_MANGLE_CCCF(name) LIQUID_CONCAT(eqrls_cccf,name)
// large macro
// EQRLS : name-mangling macro
// T : data type
#define LIQUID_EQRLS_DEFINE_API(EQRLS,T) \
\
/* Recursive least mean-squares equalization object */ \
typedef struct EQRLS(_s) * EQRLS(); \
\