-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdspp.cc
More file actions
1957 lines (1790 loc) · 58.1 KB
/
dspp.cc
File metadata and controls
1957 lines (1790 loc) · 58.1 KB
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
/*
* dspp.c -- DSP Pipe - process signals via piped commands
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include "dspp.h"
/* ---------------------------------------------------------------------- */
static const char USAGE_STR[] = "\n"
"Usage: %s <command> [ parameter 1 [ ... parameter n]]\n"
" -h : help\n"
" convert_byte_sInt16 : convert little endian byte stream to internal short ints\n"
" convert_byte_f : convert a signed byte stream to internal floating point\n"
" convert_uByte_f : convert a unsigned byte stream to internal floating point\n"
" convert_uByte_byte : convert a unsigned byte stream to a signed byte stream\n"
" shift_frequency_cc : recenter a signal by x cycles per sample\n"
" shift_frequency_uByte_uByte : recenter a signal (raw RTL) by x cycles per sample\n"
" fsSlash4_byte_byte : mix / shift frequency by fs/4\n"
" decimate_cc : replace every n samples with 1 (complex)\n"
" fmdemod_cf : demodulate FM signal\n"
" decimate_ff : replace every n samples with 1 (real)\n"
" convert_f_uInt16 : convert a float(real) stream into an unsigned short stream\n"
" convert_f_sInt16 : convert a float(real) stream into an signed short stream\n"
" convert_tcp_byte : convert a tcp stream into an unsigned/generic byte stream\n"
" convert_byte_tcp : convert a byte stream to a tcp byte stream\n"
" custom_fir_ff : FIR filter a real stream\n"
" custom_fir_cc : FIR filter a complex stream\n"
" sfir_cc : Smooth FIR filter a complex stream\n"
" comb_byte_c : Comb filter a complex stream\n"
" sfir_ff : Smooth FIR filter a real stream\n"
" real_to_complex_fc : real stream to complex stream\n"
" real_to_quadrature_fc : real stream to complex quadrature stream\n"
" fmmod_fc : real stream FM modulated quadrature (I/Q) stream\n"
" head : take first n bytes of stream\n"
" tail : take bytes after n bytes of stream\n"
" convert_sInt16_f : convert a signed short stream to a float(real) stream\n"
" fft_cc : convert a complex stream to a complex stream in the frequency domain\n"
" tee : tee stream to another stream\n"
" sfir_cc : smooth fir filter, complex stream to complex stream\n"
" sfir_ff : smooth fir filter, float(real) stream to float stream\n"
" real_of_complex_cf : real(float) part of complex stream to float stream\n"
" mag_cf : magnitude of complex number\n"
" gain : multiply float/complex by scalar\n"
" limit_real_stream : limit a floating point stream between -1.0 and 1.0\n"
" dc_removal : remove average value of the stream\n"
" agc : automatic gain control, sustain a fixed average level\n"
" split_stream : split input stream into multiple streams\n"
" FT8Window : find FT8 spots in a FT8 window\n"
" WSPRWindow : find WSPR spots in a WSPR window\n"
" WindowSample : Synchronize a sampling window to a clock\n"
" convert_f_byte : convert a float stream to a signed byte stream\n";
static struct option longOpts[] = {
{ "convert_byte_sInt16" , no_argument, NULL, 1 },
{ "convert_byte_f" , no_argument, NULL, 2 },
{ "convert_uByte_f" , no_argument, NULL, 3 },
{ "shift_frequency_cc" , no_argument, NULL, 4 },
{ "decimate_cc" , no_argument, NULL, 5 },
{ "fmdemod_cf" , no_argument, NULL, 6 },
{ "decimate_ff" , no_argument, NULL, 7 },
{ "convert_f_uInt16" , no_argument, NULL, 8 },
{ "convert_f_sInt16" , no_argument, NULL, 9 },
{ "convert_tcp_byte" , no_argument, NULL, 10 },
{ "convert_byte_tcp" , no_argument, NULL, 11 },
{ "custom_fir_ff" , no_argument, NULL, 12 },
{ "custom_fir_cc" , no_argument, NULL, 13 },
{ "real_to_complex_fc" , no_argument, NULL, 14 },
{ "fmmod_fc" , no_argument, NULL, 15 },
{ "head" , no_argument, NULL, 16 },
{ "tail" , no_argument, NULL, 17 },
{ "convert_sInt16_f" , no_argument, NULL, 18 },
{ "fft_cc" , no_argument, NULL, 19 },
{ "tee" , no_argument, NULL, 20 },
{ "sfir_cc" , no_argument, NULL, 21 },
{ "sfir_ff" , no_argument, NULL, 22 },
{ "real_of_complex_cf" , no_argument, NULL, 23 },
{ "comb_byte_c" , no_argument, NULL, 25 },
{ "mag_cf" , no_argument, NULL, 26 },
{ "gain" , no_argument, NULL, 27 },
{ "limit_real_stream" , no_argument, NULL, 28 },
{ "dc_removal" , no_argument, NULL, 29 },
{ "agc" , no_argument, NULL, 30 },
{ "shift_frequency_uByte_uByte", no_argument, NULL, 31 },
{ "convert_uByte_byte" , no_argument, NULL, 32 },
{ "fsSlash4_byte_byte" , no_argument, NULL, 33 },
{ "split_stream" , no_argument, NULL, 36 },
{ "WSPRWindow" , no_argument, NULL, 39 },
{ "WindowSample" , no_argument, NULL, 40 },
{ "convert_f_byte" , no_argument, NULL, 41 },
{ "FT8Window" , no_argument, NULL, 42 },
{ "real_to_quadrature_fc" , no_argument, NULL, 43 },
{ NULL, 0, NULL, 0 }
};
/* ---------------------------------------------------------------------- */
/*
* convert_byte_sInt16.c -- DSP Pipe - byte stream to short int stream
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
#ifdef LE_MACHINE
int dspp::convert_byte_sInt16() {
union encode {
char bytes[2];
short integer;
} piece;
const int byteBufferSize = sizeof(piece.bytes);
for (;;) {
fread(&piece.bytes, sizeof(char), byteBufferSize, stdin);
fwrite(&piece.integer, sizeof(short), 1, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
#else
int dspp::convert_byte_sInt16() {
union encode {
char bytes[2];
short integer;
} piece;
for (;;) {
fread(&piece.bytes[1], sizeof(char), 1, stdin);
fread(&piece.bytes[0], sizeof(char), 1, stdin);
fwrite(&piece.integer, sizeof(short), 1, stdout);
}
return 0;
}
#endif
/* ---------------------------------------------------------------------- */
/*
* convert_byte_f.c -- DSP Pipe - byte(signed) stream to float
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::convert_byte_f() {
const int BUFFER_SIZE = 4096;
signed char c[BUFFER_SIZE];
float f[BUFFER_SIZE];
int count;
float * fptr;
signed char * cptr;
for (;;) {
count = fread(&c, sizeof(char), BUFFER_SIZE, stdin);
if(count < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, convert_byte_f\n");
fclose(stdout);
return 0;
}
cptr = c;
fptr = f;
for (int i=0; i < BUFFER_SIZE; i++) {
*fptr++ = *cptr++/128.0 ;
}
fwrite(&f, sizeof(float), BUFFER_SIZE, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* convert_f_byte.c -- DSP Pipe - float stream to byte(signed) stream
*
* Copyright (C) 2023
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::convert_f_byte() {
const int BUFFER_SIZE = 4096;
int8_t sbyte[BUFFER_SIZE];
float f[BUFFER_SIZE];
int count;
float * fptr;
int8_t * cptr;
for (;;) {
count = fread(&f, sizeof(float), BUFFER_SIZE, stdin);
cptr = sbyte;
fptr = f;
for (int i=0; i < count; i++) {
*cptr++ = *fptr++ ;
}
fwrite(&sbyte, sizeof(int8_t), count, stdout);
if(count < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, convert_f_byte\n");
fclose(stdout);
return 0;
}
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* convert_aUnsignedByte_f.c -- DSP Pipe - byte(unsigned) stream to float
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::convert_uByte_f() {
const int BUFFER_SIZE = 4096;
unsigned char c[BUFFER_SIZE];
float f[BUFFER_SIZE];
int count;
float * fptr;
unsigned char * cptr;
for (;;) {
count = fread(&c, sizeof(char), BUFFER_SIZE, stdin);
if(count < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, convert_uByte_f\n");
fclose(stdout);
return 0;
}
cptr = c;
fptr = f;
for (int i=0; i < BUFFER_SIZE; i++) {
*fptr++ = (*cptr++ - 128)/128.0;
}
fwrite(&f, sizeof(float), BUFFER_SIZE, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* convert_aUnsignedByte_signedByte.c -- DSP Pipe - byte(unsigned) stream to byte
*
* Copyright (C) 2022
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::convert_uByte_byte() {
const int BUFFER_SIZE = 4096;
unsigned char c[BUFFER_SIZE];
char b[BUFFER_SIZE];
int count;
char * bptr;
unsigned char * cptr;
for (;;) {
count = fread(&c, sizeof(char), BUFFER_SIZE, stdin);
if(count < BUFFER_SIZE) {
fwrite(&b, sizeof(char), count, stdout);
fprintf(stderr, "Short data stream, convert_uByte_byte\n");
fclose(stdout);
return 0;
}
cptr = c;
bptr = b;
for (int i=0; i < BUFFER_SIZE; i++) {
*bptr++ = *cptr++ - 128;
}
fwrite(&b, sizeof(char), BUFFER_SIZE, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* shift_frequency_cc.cc -- DSP Pipe - shift the frequency of a quadature
* by amount cycles per sample
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::shift_frequency_cc(float amount) {
const int BUFFER_SIZE = 4096;
float f[BUFFER_SIZE];
float of[BUFFER_SIZE];
int count;
float * fptr, * ofptr;
float sinDeltaAmount = sin(amount*2.0*M_PI);
float cosDeltaAmount = cos(amount*2.0*M_PI);
float phase = 0.0;
float cosAmount = 1.0;
float sinAmount = 0.0;
float newCosAmount;
float newSinAmount;
float I;
float Q;
fprintf(stderr, "cycles per sample correction: %f\n", amount);
for (;;) {
count = fread(&f, sizeof(float), BUFFER_SIZE, stdin);
if(count == 0) {
fprintf(stderr, "End of data stream, shift_frequency_cc\n");
fclose(stdout);
return 0;
}
fptr = f;
ofptr = of;
for (int i=0; i < count; i+=2) {
/*
Assuming I is r*cos(2*PI*fc*t) and
Q is -r*sin(2*PI*fc*t) where fc is the center frequency the signal r is sampled at
Then to to recenter to a new frequency fs, use the trigonometric identities:
cos(a + b) = cos(a)*cos(b) - sin(a)*sin(b)
sin(a + b) = sin(a)*cos(b) + cos(a)*sin(b)
shifted I = I * cos(2*PI*fs*t) + Q * sin(2*PI*fs*t)
shifted Q = Q * cos(2*PI*fs*t) - I * sin(2*PI*fs*t)
To advance the sin/cos 2*PI*fs*t functions, the sin/cos of sum of angles identities are again
used. Since each sample is separated by a fixed delta of time, successively applying the
identities advances the angle/time.
*/
I = *fptr++;
Q = *fptr++;
*ofptr++ = I * cosAmount + Q * sinAmount;
*ofptr++ = Q * cosAmount - I * sinAmount;
newCosAmount = cosAmount * cosDeltaAmount - sinAmount * sinDeltaAmount;
newSinAmount = sinAmount * cosDeltaAmount + cosAmount * sinDeltaAmount;
cosAmount = newCosAmount;
sinAmount = newSinAmount;
}
fwrite(&of, sizeof(float), count, stdout);
phase += cosDeltaAmount * count / 2.0;
while(phase > M_PI) phase -= 2.0 * M_PI; // taken from CSDR
while(phase < -M_PI) phase += 2.0 * M_PI;
cosAmount = cos(phase);
sinAmount = sin(phase);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* shift_frequency_uByteuByte.cc -- DSP Pipe - shift the frequency of
* raw unsigned RTL byte stream by
* amount cycles per sample
*
* Copyright (C) 2022
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::shift_frequency_uByteuByte(float amount) {
const int BUFFER_SIZE = 4096;
unsigned char b[BUFFER_SIZE];
unsigned char ob[BUFFER_SIZE];
int count;
unsigned char * ubptr, * uobptr;
float sinDeltaAmount = sin(amount*2.0*M_PI);
float cosDeltaAmount = cos(amount*2.0*M_PI);
float phase = 0.0;
float cosAmount = 1.0;
float sinAmount = 0.0;
float newCosAmount;
float newSinAmount;
float I;
float Q;
fprintf(stderr, "cycles per sample correction: %f\n", amount);
for (;;) {
count = fread(&b, sizeof(char), BUFFER_SIZE, stdin);
if(count < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, shift_frequency_uByteuByte\n");
fclose(stdout);
return 0;
}
ubptr = b;
uobptr = ob;
for (int i=0; i < BUFFER_SIZE; i+=2) {
/*
Assuming I is r*cos(2*PI*fc*t) and
Q is -r*sin(2*PI*fc*t) where fc is the center frequency the signal r is sampled at
Then to to recenter to a new frequency fs, use the trigonometric identities:
cos(a + b) = cos(a)*cos(b) - sin(a)*sin(b)
sin(a + b) = sin(a)*cos(b) + cos(a)*sin(b)
shifted I = I * cos(2*PI*fs*t) + Q * sin(2*PI*fs*t)
shifted Q = Q * cos(2*PI*fs*t) - I * sin(2*PI*fs*t)
To advance the sin/cos 2*PI*fs*t functions, the sin/cos of sum of angles identities are again
used. Since each sample is separated by a fixed delta of time, successively applying the
identities advances the angle/time.
*/
I = (*ubptr++ - 128)/128.0;
Q = (*ubptr++ - 128)/128.0;
*uobptr++ = (I * cosAmount + Q * sinAmount)*128.0 + 128;
*uobptr++ = (Q * cosAmount - I * sinAmount)*128.0 + 128;
newCosAmount = cosAmount * cosDeltaAmount - sinAmount * sinDeltaAmount;
newSinAmount = sinAmount * cosDeltaAmount + cosAmount * sinDeltaAmount;
cosAmount = newCosAmount;
sinAmount = newSinAmount;
}
fwrite(&ob, sizeof(char), BUFFER_SIZE, stdout);
phase += cosDeltaAmount * count / 2.0;
while(phase > M_PI) phase -= 2.0 * M_PI; // taken from CSDR
while(phase < -M_PI) phase += 2.0 * M_PI;
cosAmount = cos(phase);
sinAmount = sin(phase);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* fsSlash4_byte_byte.cc -- DSP Pipe - shift the frequency of
* signal by fs/4
*
* Copyright (C) 2022
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::fsSlash4_byte_byte() {
const int BUFFER_SIZE = 8;
char b[BUFFER_SIZE];
char ob[BUFFER_SIZE];
int count;
fprintf(stderr, "fs/4 mix\n");
for (;;) {
count = fread(&b, sizeof(char), BUFFER_SIZE, stdin);
if(count < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, fsSlash_byte_byte\n");
fclose(stdout);
return 0;
}
ob[0] = b[0];
ob[1] = b[1];
ob[2] = -b[3];
ob[3] = b[2];
ob[4] = -b[4];
ob[5] = -b[5];
ob[6] = b[7];
ob[7] = -b[6];
fwrite(&ob, sizeof(char), BUFFER_SIZE, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* decimate_cc.cc -- DSP Pipe - decimate a sample stream 1 for N
* samples
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::decimate_cc(float cutOffFrequency, int M, int amount, int N, const char * window) {
FIRFilter filter(cutOffFrequency, M, amount, N, FIRFilter::HAMMING);
filter.filterSignal();
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* fmdemod_cf.cc -- DSP Pipe - demodulate a FM signal
*
* Copyright (C) 2019
* Mark Broihier
*
*
* This assumes FM modulation has the form of:
* y(t) = Ac*cos(2*PI*fc*t + 2*PI*fd Integral of x(tau)d(tau))
* Ac - carrier amplitude
* fc - carrier frequency
* fd - maximum frequency deviation
*
* If we let s(t) = 2*PI*fd Integral of x(tau)d(tau) and we use the
* trigonometric formula for cos of the sum of two angles, then
* y(t) = Ac[cos(2*PI*fc*t)*cos(s(t)) - sin(2*PI*fc*t)*sin(s(t))]
*
* At the radio receiver we'll have a signal such as:
* r(t) = Ar[cos(2*PI*fc*t)*cos(s(t)) - sin(2*PI*fc*t)*sin(s(t))]
*
* Assuming the receiver processes the RF signal into a quadrature
* signal of I and Q about the carrier frequency fc, then
* I = cos(s(t)) and Q = sin(s(t))
*
* To recover x(tau) we can do the following:
* Q / I = sin(s(t))/cos(s(t)) = tan(s(t))
* so,
* arctan(Q/I) = 2*PI*fd Integral x(tau)d(tau)
* Take the derivative
* arctan(Q/I)' = 2*PI*fd*x(tau)
* x(tau) = 1/(2*PI*fd) arctan(Q/I)'
* = 1/(2*PI*fd) [1/(1 + (Q/I) * (Q/I))]*[I*Q' - Q*I']/(I*I)
*
* The constant, 1/(2*PI*fd), is changed to 1 assuming gain further
* down the processing line.
*
* I' is approximated by I - last value of I and
* Q' is approximated by Q - last value of Q
*
* To avoid a singularity, if any I is less than epsilon,
* the last value of x(t) is used.
*/
/* ---------------------------------------------------------------------- */
int dspp::fmdemod_cf() {
const int BUFFER_SIZE = 2048;
float f[BUFFER_SIZE];
const int HALF_BUFFER_SIZE = BUFFER_SIZE / 2;
float of[HALF_BUFFER_SIZE];
float * fptr, * ofptr;
int fSize = sizeof(f);
int ofSize = sizeof(of);
const float EPSILON = 1.0 / 128.0;
int count = 0;
fprintf(stderr, "demod buffer input size: %d\n", fSize);
float I, Q;
float lastI = 0.0;
float lastQ = 0.0;
float lastOutput = 0.0;
float Isquared;
float Qsquared;
for (;;) {
count = fread(&f, sizeof(char), fSize, stdin);
if(count < fSize) {
fprintf(stderr, "Short data stream, fmdemod_cf\n");
fclose(stdout);
return 0;
}
ofptr = of;
fptr = f;
for (int i = 0; i < HALF_BUFFER_SIZE; i++) {
I = *fptr++;
Q = *fptr++;
if ((fabs(I) > EPSILON) || (fabs(Q) > EPSILON)) {
Isquared = I * I;
Qsquared = Q * Q;
//lastOutput = (I * (Q - lastQ) - Q * (I - lastI)) / ((1.0 + ratio * ratio) * I * I);
lastOutput = (Q*lastI - I*lastQ) / (Isquared + Qsquared);
}
*ofptr++ = lastOutput;
lastI = I;
lastQ = Q;
}
fwrite(&of, sizeof(char), ofSize, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* decimate_ff.cc -- DSP Pipe - decimate real signal
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::decimate_ff(float cutOffFrequency, int M, int amount, int N, const char * window) {
if (strcmp(window, "HAMMING") == 0) {
FIRFilter filter(cutOffFrequency, M, amount, N, FIRFilter::HAMMING, true);
filter.filterReal();
} else {
if (strcmp(window, "AVERAGE") == 0) {
FIRFilter filter(amount);
filter.filterRealWindow();
} else if (strcmp(window, "MAX") == 0) {
FIRFilter filter(amount);
filter.maxRealWindow();
} else {
fprintf(stderr, "Unsupported decimate_ff window type: %s\n", window);
}
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* convert_f_uInt16.cc -- DSP Pipe - float (-1.0 to 1.0)
* to 0 to 65535
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::convert_f_uInt16() {
const int BUFFER_SIZE = 4096;
unsigned short uin[BUFFER_SIZE];
float f[BUFFER_SIZE];
int count;
float * fptr;
unsigned short * iptr;
for (;;) {
count = fread(&f, sizeof(float), BUFFER_SIZE, stdin);
if(count < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, convert_f_uInt16\n");
fclose(stdout);
return 0;
}
iptr = uin;
fptr = f;
for (int i=0; i < BUFFER_SIZE; i++) {
*iptr++ = (*fptr++ + 1.0) * 32767.0;
}
fwrite(&uin, sizeof(short), BUFFER_SIZE, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* convert_f_sInt16.cc -- DSP Pipe - float (-1.0 to 1.0)
* to -32767 to 32767
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::convert_f_sInt16() {
const int BUFFER_SIZE = 4096;
short sin[BUFFER_SIZE];
float f[BUFFER_SIZE];
int count;
float * fptr;
short * iptr;
for (;;) {
count = fread(&f, sizeof(float), BUFFER_SIZE, stdin);
if(count < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, convert_f_sInt16\n");
fclose(stdout);
return 0;
}
iptr = sin;
fptr = f;
for (int i=0; i < BUFFER_SIZE; i++) {
*iptr++ = *fptr++ * 32767.0;
}
fwrite(&sin, sizeof(short), BUFFER_SIZE, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* convert_sInt16_f -- DSP Pipe - signed int -32767 to 32767
* float (-1.0 to 1.0)
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::convert_sInt16_f() {
const int BUFFER_SIZE = 4096;
short sin[BUFFER_SIZE];
float f[BUFFER_SIZE];
int count;
float * fptr;
short * iptr;
float scale = 1.0 / 32767.0;
for (;;) {
count = fread(&sin, sizeof(short), BUFFER_SIZE, stdin);
if(count < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, convert_sInt16_f\n");
fprintf(stderr, "shorts: %d\n", count);
fclose(stdout);
return 0;
}
iptr = sin;
fptr = f;
for (int i=0; i < BUFFER_SIZE; i++) {
*fptr++ = *iptr++ * scale;
}
fwrite(&f, sizeof(float), BUFFER_SIZE, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* convert_tcp_byte.cc -- DSP Pipe - convert a TCP stream connection to a
* byte(generic) stream
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::convert_tcp_byte(const char * IPAddress, int port, int frequency, int sampleRate, int mode, int gain) {
fprintf(stderr, "Creating a client object\n");
RTLTCPClient client(IPAddress, port, frequency, sampleRate, mode, gain);
fprintf(stderr, "Client terminated\n");
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* convert_byte_tcp.cc -- DSP Pipe - convert a byte stream to a TCP stream
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::convert_byte_tcp(const char * IPAddress, int port) {
fprintf(stderr, "Creating a TCP server object\n");
RTLTCPServer client(IPAddress, port);
fprintf(stderr, "Server terminated\n");
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* custom_fir_ff.cc -- DSP Pipe - custom FIR filter
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::custom_fir_ff(const char * filePath, int M, int N, FIRFilter::WindowType window) {
fprintf(stderr, "Creating a custom FIR filter object\n");
FIRFilter customFilter(filePath, M, N, FIRFilter::CUSTOM, true);
customFilter.filterReal();
fprintf(stderr, "Custom FIR filter terminated\n");
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* custom_fir_cc.cc -- DSP Pipe - custom FIR filter
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::custom_fir_cc(const char * filePath, int M, int N, FIRFilter::WindowType window) {
fprintf(stderr, "Creating a custom FIR filter object\n");
FIRFilter customFilter(filePath, M, N, FIRFilter::CUSTOM);
customFilter.filterSignal();
fprintf(stderr, "Custom FIR filter terminated\n");
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* real_to_complex_fc.cc -- DSP Pipe - real stream to complex quadrature
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::real_to_complex_fc() {
const int BUFFER_SIZE = 4096;
float signal[BUFFER_SIZE];
float complexSignal[BUFFER_SIZE*2];
int numberRead = 0;
float * signalPtr;
float * complexSignalPtr;
for (;;) {
numberRead = fread(&signal, sizeof(float), BUFFER_SIZE, stdin);
if(numberRead < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, real_to_complex_fc\n");
fclose(stdout);
return 0;
}
signalPtr = signal;
complexSignalPtr = complexSignal;
for (int i=0; i < BUFFER_SIZE; i++) {
*complexSignalPtr++ = *signalPtr++;
*complexSignalPtr++ = 0.0;
}
fwrite(&complexSignal, sizeof(float), BUFFER_SIZE*2, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* real_to_quadrature_fc.cc -- DSP Pipe - real stream to complex quadrature
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::real_to_quadrature_fc(bool selector) {
const int BUFFER_SIZE = 256;
RealToQuadrature rtqo(BUFFER_SIZE);
if (selector) {
rtqo.processSampleSetHilbert();
} else {
rtqo.processSampleSetDownconversion();
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* real_of_complex_fc.cc -- DSP Pipe - real part of complex stream
*
* Copyright (C) 2019
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::real_of_complex_cf() {
const int BUFFER_SIZE = 4096;
float signal[BUFFER_SIZE];
float complexSignal[BUFFER_SIZE*2];
int numberRead = 0;
float * signalPtr;
float * complexSignalPtr;
for (;;) {
numberRead = fread(&complexSignal, sizeof(float), BUFFER_SIZE*2, stdin);
if(numberRead < BUFFER_SIZE*2) {
fprintf(stderr, "Short data stream, real_of_complex_cf\n");
fclose(stdout);
return 0;
}
signalPtr = signal;
complexSignalPtr = complexSignal;
for (int i=0; i < BUFFER_SIZE; i++) {
*signalPtr++ = *complexSignalPtr++;
complexSignalPtr++;
}
fwrite(&signal, sizeof(float), BUFFER_SIZE, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* mag_cf.cc -- DSP Pipe - magnitude of complex stream
*
* Copyright (C) 2022
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::mag_cf() {
const int BUFFER_SIZE = 4096;
float signal[BUFFER_SIZE*2];
float mag[BUFFER_SIZE];
int numberRead = 0;
float * magPtr;
float * complexSignalPtr;
for (;;) {
numberRead = fread(&signal, sizeof(float), BUFFER_SIZE*2, stdin);
if(numberRead < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, mag_cf\n");
fclose(stdout);
return 0;
}
magPtr = mag;
complexSignalPtr = signal;
for (int i=0; i < BUFFER_SIZE; i++) {
float r = *complexSignalPtr++;
float j = *complexSignalPtr++;
*magPtr++ = sqrt(r * r + j * j);
}
fwrite(&mag, sizeof(float), BUFFER_SIZE, stdout);
}
return 0;
}
/* ---------------------------------------------------------------------- */
/*
* gain.cc -- DSP Pipe - multiply float/complex stream by scalar
*
* Copyright (C) 2022
* Mark Broihier
*
*/
/* ---------------------------------------------------------------------- */
int dspp::gain(float gain) {
const int BUFFER_SIZE = 4096;
float signal[BUFFER_SIZE];
float amplifiedSignal[BUFFER_SIZE];
int numberRead = 0;
float * amplifiedSignalPtr;
float * signalPtr;
for (;;) {
numberRead = fread(&signal, sizeof(float), BUFFER_SIZE, stdin);
if(numberRead < BUFFER_SIZE) {
fprintf(stderr, "Short data stream, gain\n");
fclose(stdout);
return 0;
}