-
Notifications
You must be signed in to change notification settings - Fork 62
/
sbitx.c
1576 lines (1334 loc) · 41.2 KB
/
sbitx.c
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <math.h>
#include <complex.h>
#include <fftw3.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <linux/types.h>
#include <linux/limits.h>
#include <stdint.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include "sdr.h"
#include "sdr_ui.h"
#include "sound.h"
#include "i2cbb.h"
#include "si5351.h"
#include "ini.h"
#define DEBUG 0
char audio_card[32];
static int tx_shift = 512;
FILE *pf_debug = NULL;
//this is for processing FT8 decodes
//unsigned int wallclock = 0;
#define TX_LINE 4
#define TX_POWER 27
#define BAND_SELECT 5
#define LPF_A 5
#define LPF_B 6
#define LPF_C 10
#define LPF_D 11
#define SBITX_DE (0)
#define SBITX_V2 (1)
int sbitx_version = SBITX_V2;
int fwdpower, vswr;
float fft_bins[MAX_BINS]; // spectrum ampltiudes
int spectrum_plot[MAX_BINS];
fftw_complex *fft_spectrum;
fftw_plan plan_spectrum;
float spectrum_window[MAX_BINS];
void set_rx1(int frequency);
void tr_switch(int tx_on);
// Wisdom Defines for the FFTW and FFTWF libraries
// Options for WISDOM_MODE from least to most rigorous are FFTW_ESTIMATE, FFTW_MEASURE, FFTW_PATIENT, and FFTW_EXHAUSTIVE
// The FFTW_ESTIMATE mode seems to make completely incorrect Wisdom plan choices sometimes, and is not recommended.
// Wisdom plans found in an existing Wisdom file will negate the need for time consuming Wisdom plan calculations
// if the Wisdom plans in the file were generated at the same or more rigorous level.
#define WISDOM_MODE FFTW_MEASURE
#define PLANTIME -1 // spend no more than plantime seconds finding the best FFT algorithm. -1 turns the platime cap off.
char wisdom_file[] = "sbitx_wisdom.wis";
fftw_complex *fft_out; // holds the incoming samples in freq domain (for rx as well as tx)
fftw_complex *fft_in; // holds the incoming samples in time domain (for rx as well as tx)
fftw_complex *fft_m; // holds previous samples for overlap and discard convolution
fftw_plan plan_fwd, plan_tx;
int bfo_freq = 40035000;
int freq_hdr = -1;
int si570_xtal = 0;
static double volume = 100.0;
static int tx_drive = 40;
static int rx_gain = 100;
static int rx_vol = 100;
static int tx_gain = 100;
static int tx_compress = 0;
static double spectrum_speed = 0.3;
static int in_tx = 0;
static int rx_tx_ramp = 0;
static int sidetone = 2000000000;
struct vfo tone_a, tone_b, am_carrier; //these are audio tone generators
static int tx_use_line = 0;
struct rx *rx_list = NULL;
struct rx *tx_list = NULL;
struct filter *tx_filter; //convolution filter
static double tx_amp = 0.0;
static double alc_level = 1.0;
static int tr_relay = 0;
static int rx_pitch = 700; //used only to offset the lo for CW,CWR
static int bridge_compensation = 100;
static double voice_clip_level = 0.04;
static int in_calibration = 1; // this turns off alc, clipping et al
static int multicast_socket = -1;
#define MUTE_MAX 6
static int mute_count = 50;
FILE *pf_record;
int16_t record_buffer[1024];
int32_t modulation_buff[MAX_BINS];
/* the power gain of the tx varies widely from
band to band. these data structures help in flattening
the gain */
struct power_settings {
int f_start;
int f_stop;
int max_watts;
double scale;
};
struct power_settings band_power[] ={
{ 3500000, 4000000, 37, 0.002},
{ 7000000, 7300009, 40, 0.0015},
{10000000, 10200000, 35, 0.0019},
{14000000, 14300000, 35, 0.0025},
{18000000, 18200000, 20, 0.0023},
{21000000, 21450000, 20, 0.003},
{24800000, 25000000, 20, 0.0034},
{28000000, 29700000, 20, 0.0037}
};
#define CMD_TX (2)
#define CMD_RX (3)
#define TUNING_SHIFT (0)
#define MDS_LEVEL (-135)
struct Queue qremote;
void radio_tune_to(u_int32_t f){
if (rx_list->mode == MODE_CW)
si5351bx_setfreq(2, f + bfo_freq - 24000 + TUNING_SHIFT - rx_pitch);
else if (rx_list->mode == MODE_CWR)
si5351bx_setfreq(2, f + bfo_freq - 24000 + TUNING_SHIFT + rx_pitch);
else
si5351bx_setfreq(2, f + bfo_freq - 24000 + TUNING_SHIFT);
// printf("Setting radio rx_pitch %d\n", rx_pitch);
}
void fft_init(){
// int mem_needed;
//printf("initializing the fft\n");
fflush(stdout);
// mem_needed = sizeof(fftw_complex) * MAX_BINS;
fft_m = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * MAX_BINS/2);
fft_in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * MAX_BINS);
fft_out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * MAX_BINS);
fft_spectrum = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * MAX_BINS);
memset(fft_spectrum, 0, sizeof(fftw_complex) * MAX_BINS);
memset(fft_in, 0, sizeof(fftw_complex) * MAX_BINS);
memset(fft_out, 0, sizeof(fftw_complex) * MAX_BINS);
memset(fft_m, 0, sizeof(fftw_complex) * MAX_BINS/2);
fftw_set_timelimit(PLANTIME);
fftwf_set_timelimit(PLANTIME);
int e = fftw_import_wisdom_from_filename(wisdom_file);
if (e == 0)
{
printf("Generating Wisdom File...\n");
}
plan_fwd = fftw_plan_dft_1d(MAX_BINS, fft_in, fft_out, FFTW_FORWARD, WISDOM_MODE); // Was FFTW_ESTIMATE N3SB
plan_spectrum = fftw_plan_dft_1d(MAX_BINS, fft_in, fft_spectrum, FFTW_FORWARD, WISDOM_MODE); // Was FFTW_ESTIMATE N3SB
fftw_export_wisdom_to_filename(wisdom_file);
//zero up the previous 'M' bins
for (int i= 0; i < MAX_BINS/2; i++){
__real__ fft_m[i] = 0.0;
__imag__ fft_m[i] = 0.0;
}
make_hann_window(spectrum_window, MAX_BINS);
}
void fft_reset_m_bins(){
//zero up the previous 'M' bins
memset(fft_in, 0, sizeof(fftw_complex) * MAX_BINS);
memset(fft_out, 0, sizeof(fftw_complex) * MAX_BINS);
memset(fft_m, 0, sizeof(fftw_complex) * MAX_BINS/2);
memset(fft_spectrum, 0, sizeof(fftw_complex) * MAX_BINS);
memset(tx_list->fft_time, 0, sizeof(fftw_complex) * MAX_BINS);
memset(tx_list->fft_freq, 0, sizeof(fftw_complex) * MAX_BINS);
/* for (int i= 0; i < MAX_BINS/2; i++){
__real__ fft_m[i] = 0.0;
__imag__ fft_m[i] = 0.0;
}
*/
}
int mag2db(double mag){
int m = abs(mag) * 10000000;
int c = 31;
int p = 0x80000000;
while(c > 0){
if (p & m)
break;
c--;
p = p >> 1;
}
return c;
}
void set_spectrum_speed(int speed){
spectrum_speed = speed;
for (int i = 0; i < MAX_BINS; i++)
fft_bins[i] = 0;
}
void spectrum_reset(){
for (int i = 0; i < MAX_BINS; i++)
fft_bins[i] = 0;
}
void spectrum_update(){
//we are only using the lower half of the bins,
//so this copies twice as many bins,
//it can be optimized. leaving it here just in case
//someone wants to try I Q channels
//in hardware
// this has been hand optimized to lower
//the inordinate cpu usage
for (int i = 1269; i < 1803; i++){
fft_bins[i] = ((1.0 - spectrum_speed) * fft_bins[i]) +
(spectrum_speed * cabs(fft_spectrum[i]));
int y = power2dB(cnrmf(fft_bins[i]));
spectrum_plot[i] = y;
}
}
/*
static int create_mcast_socket(){
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len = sizeof(client_addr);
char buffer[MAX_BUFFER_SIZE];
// Create a UDP socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Error creating mcast socket");
return -1;
}
// Set up the server address structure
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(MULTICAST_PORT);
// Bind the socket to the server address
if (bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Error binding mcast socket");
close(sockfd);
return -1;
}
// Set up the multicast group membership
struct ip_mreq mreq;
inet_pton(AF_INET, MULTICAST_ADDR, &(mreq.imr_multiaddr.s_addr));
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) {
perror("Error adding multicast group membership");
close(sockfd);
exit(EXIT_FAILURE);
}
printf("Listening for multicast on %s:%d...\n", MULTICAST_ADDR, PORT);
return socketfd;
}
*/
int remote_audio_output(int16_t *samples){
int length = q_length(&qremote);
for (int i = 0; i < length; i++){
samples[i] = q_read(&qremote) / 32786;
}
return length;
}
static int prev_lpf = -1;
void set_lpf_40mhz(int frequency){
int lpf = 0;
if (frequency < 5500000)
lpf = LPF_D;
else if (frequency < 10500000)
lpf = LPF_C;
else if (frequency < 18500000)
lpf = LPF_B;
else if (frequency < 30000000)
lpf = LPF_A;
if (lpf == prev_lpf){
#if DEBUG > 0
puts("LPF not changed");
#endif
return;
}
#if DEBUG > 0
printf("##################Setting LPF to %d\n", lpf);
#endif
digitalWrite(LPF_A, LOW);
digitalWrite(LPF_B, LOW);
digitalWrite(LPF_C, LOW);
digitalWrite(LPF_D, LOW);
#if DEBUG > 0
printf("################ setting %d high\n", lpf);
#endif
digitalWrite(lpf, HIGH);
prev_lpf = lpf;
}
void set_rx1(int frequency){
if (frequency == freq_hdr)
return;
radio_tune_to(frequency);
freq_hdr = frequency;
set_lpf_40mhz(frequency);
}
void set_volume(double v){
volume = v;
}
FILE *wav_start_writing(const char* path)
{
char subChunk1ID[4] = { 'f', 'm', 't', ' ' };
uint32_t subChunk1Size = 16; // 16 for PCM
uint16_t audioFormat = 1; // PCM = 1
uint16_t numChannels = 1;
uint16_t bitsPerSample = 16;
uint32_t sampleRate = 12000;
uint16_t blockAlign = numChannels * bitsPerSample / 8;
uint32_t byteRate = sampleRate * blockAlign;
char subChunk2ID[4] = { 'd', 'a', 't', 'a' };
uint32_t subChunk2Size = 0Xffffffff; //num_samples * blockAlign;
char chunkID[4] = { 'R', 'I', 'F', 'F' };
uint32_t chunkSize = 4 + (8 + subChunk1Size) + (8 + subChunk2Size);
char format[4] = { 'W', 'A', 'V', 'E' };
FILE* f = fopen(path, "w");
// NOTE: works only on little-endian architecture
fwrite(chunkID, sizeof(chunkID), 1, f);
fwrite(&chunkSize, sizeof(chunkSize), 1, f);
fwrite(format, sizeof(format), 1, f);
fwrite(subChunk1ID, sizeof(subChunk1ID), 1, f);
fwrite(&subChunk1Size, sizeof(subChunk1Size), 1, f);
fwrite(&audioFormat, sizeof(audioFormat), 1, f);
fwrite(&numChannels, sizeof(numChannels), 1, f);
fwrite(&sampleRate, sizeof(sampleRate), 1, f);
fwrite(&byteRate, sizeof(byteRate), 1, f);
fwrite(&blockAlign, sizeof(blockAlign), 1, f);
fwrite(&bitsPerSample, sizeof(bitsPerSample), 1, f);
fwrite(subChunk2ID, sizeof(subChunk2ID), 1, f);
fwrite(&subChunk2Size, sizeof(subChunk2Size), 1, f);
return f;
}
void wav_record(int32_t *samples, int count){
int16_t *w;
int32_t *s;
int i = 0, j = 0;
int decimation_factor = 96000 / 12000;
if (!pf_record)
return;
w = record_buffer;
while(i < count){
*w++ = *samples / 32786;
samples += decimation_factor;
i += decimation_factor;
j++;
}
fwrite(record_buffer, j, sizeof(int16_t), pf_record);
}
/*
The sound process is called by the duplex sound system for each block of samples
In this demo, we read and equivalent block from the file instead of processing from
the input I and Q signals.
*/
int32_t in_i[MAX_BINS];
int32_t in_q[MAX_BINS];
int32_t out_i[MAX_BINS];
int32_t out_q[MAX_BINS];
short is_ready = 0;
void tx_init(int frequency, short mode, int bpf_low, int bpf_high){
//we assume that there are 96000 samples / sec, giving us a 48khz slice
//the tuning can go up and down only by 22 KHz from the center_freq
tx_filter = filter_new(1024, 1025);
// filter_tune(tx_filter, (1.0 * bpf_low)/96000.0, (1.0 * bpf_high)/96000.0 , 5);
}
struct rx *add_tx(int frequency, short mode, int bpf_low, int bpf_high){
//we assume that there are 96000 samples / sec, giving us a 48khz slice
//the tuning can go up and down only by 22 KHz from the center_freq
struct rx *r = malloc(sizeof(struct rx));
r->low_hz = bpf_low;
r->high_hz = bpf_high;
r->tuned_bin = 512;
//create fft complex arrays to convert the frequency back to time
r->fft_time = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * MAX_BINS);
r->fft_freq = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * MAX_BINS);
int e = fftw_import_wisdom_from_filename(wisdom_file);
if (e == 0)
{
printf("Generating Wisdom File...\n");
}
r->plan_rev = fftw_plan_dft_1d(MAX_BINS, r->fft_freq, r->fft_time, FFTW_BACKWARD, WISDOM_MODE); // Was FFTW_ESTIMATE N3SB
fftw_export_wisdom_to_filename(wisdom_file);
r->output = 0;
r->next = NULL;
r->mode = mode;
r->filter = filter_new(1024, 1025);
filter_tune(r->filter, (1.0 * bpf_low)/96000.0, (1.0 * bpf_high)/96000.0 , 5);
if (abs(bpf_high - bpf_low) < 1000){
r->agc_speed = 10;
r->agc_threshold = -60;
r->agc_loop = 0;
}
else {
r->agc_speed = 10;
r->agc_threshold = -60;
r->agc_loop = 0;
}
//the modems drive the tx at 12000 Hz, this has to be upconverted
//to the radio's sampling rate
r->next = tx_list;
tx_list = r;
}
struct rx *add_rx(int frequency, short mode, int bpf_low, int bpf_high){
//we assume that there are 96000 samples / sec, giving us a 48khz slice
//the tuning can go up and down only by 22 KHz from the center_freq
struct rx *r = malloc(sizeof(struct rx));
r->low_hz = bpf_low;
r->high_hz = bpf_high;
r->tuned_bin = 512;
r->agc_gain = 0.0;
//create fft complex arrays to convert the frequency back to time
r->fft_time = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * MAX_BINS);
r->fft_freq = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * MAX_BINS);
int e = fftw_import_wisdom_from_filename(wisdom_file);
if (e == 0)
{
printf("Generating Wisdom File...\n");
}
r->plan_rev = fftw_plan_dft_1d(MAX_BINS, r->fft_freq, r->fft_time, FFTW_BACKWARD, WISDOM_MODE); // Was FFTW_ESTIMATE N3SB
fftw_export_wisdom_to_filename(wisdom_file);
r->output = 0;
r->next = NULL;
r->mode = mode;
r->filter = filter_new(1024, 1025);
filter_tune(r->filter, (1.0 * bpf_low)/96000.0, (1.0 * bpf_high)/96000.0 , 5);
if (abs(bpf_high - bpf_low) < 1000){
r->agc_speed = 300;
r->agc_threshold = -60;
r->agc_loop = 0;
r->signal_avg = 0;
}
else {
r->agc_speed = 300;
r->agc_threshold = -60;
r->agc_loop = 0;
r->signal_avg = 0;
}
// the modems are driven by 12000 samples/sec
// the queue is for 20 seconds, 5 more than 15 sec needed for the FT8
r->next = rx_list;
rx_list = r;
}
int count = 0;
double agc2(struct rx *r){
int i;
double signal_strength, agc_gain_should_be;
//do nothing if agc is off
if (r->agc_speed == -1){
for (i=0; i < MAX_BINS/2; i++)
__imag__ (r->fft_time[i+(MAX_BINS/2)]) *=10000000;
return 10000000;
}
//find the peak signal amplitude
signal_strength = 0.0;
for (i=0; i < MAX_BINS/2; i++){
double s = cimag(r->fft_time[i+(MAX_BINS/2)]) * 1000;
if (signal_strength < s)
signal_strength = s;
}
//also calculate the moving average of the signal strength
r->signal_avg = (r->signal_avg * 0.93) + (signal_strength * 0.07);
if (signal_strength == 0)
agc_gain_should_be = 10000000;
else
agc_gain_should_be = 100000000000/signal_strength;
r->signal_strength = signal_strength;
// printf("Agc temp, g:%g, s:%g, f:%g ", r->agc_gain, signal_strength, agc_gain_should_be);
double agc_ramp = 0.0;
// climb up the agc quickly if the signal is louder than before
if (agc_gain_should_be < r->agc_gain){
r->agc_gain = agc_gain_should_be;
//reset the agc to hang count down
r->agc_loop = r->agc_speed;
}
else if (r->agc_loop <= 0){
agc_ramp = (agc_gain_should_be - r->agc_gain) / (MAX_BINS/2);
}
if (agc_ramp != 0){
for (i = 0; i < MAX_BINS/2; i++){
__imag__ (r->fft_time[i+(MAX_BINS/2)]) *= r->agc_gain;
}
r->agc_gain += agc_ramp;
}
else
for (i = 0; i < MAX_BINS/2; i++)
__imag__ (r->fft_time[i+(MAX_BINS/2)]) *= r->agc_gain;
r->agc_loop--;
//printf("%d:s meter: %d %d %d \n", count++, (int)r->agc_gain, (int)r->signal_strength, r->agc_loop);
return 100000000000 / r->agc_gain;
}
void my_fftw_execute(fftw_plan f){
fftw_execute(f);
}
static int32_t rx_am_avg = 0;
void rx_am(int32_t *input_rx, int32_t *input_mic,
int32_t *output_speaker, int32_t *output_tx, int n_samples)
{
int i, j = 0;
double i_sample, q_sample;
//STEP 1: first add the previous M samples to
for (i = 0; i < MAX_BINS/2; i++)
fft_in[i] = fft_m[i];
//STEP 2: then add the new set of samples
// m is the index into incoming samples, starting at zero
// i is the index into the time samples, picking from
// the samples added in the previous step
int m = 0;
//gather the samples into a time domain array
for (i= MAX_BINS/2; i < MAX_BINS; i++){
i_sample = (1.0 *input_rx[j])/200000000.0;
q_sample = 0;
j++;
__real__ fft_m[m] = i_sample;
__imag__ fft_m[m] = q_sample;
__real__ fft_in[i] = i_sample;
__imag__ fft_in[i] = q_sample;
m++;
}
// STEP 3: convert the time domain samples to frequency domain
my_fftw_execute(plan_fwd);
//STEP 3B: this is a side line, we use these frequency domain
// values to paint the spectrum in the user interface
// I discovered that the raw time samples give horrible spectrum
// and they need to be multiplied wiht a window function
// they use a separate fft plan
// NOTE: the spectrum update has nothing to do with the actual
// signal processing. If you are not showing the spectrum or the
// waterfall, you can skip these steps
for (i = 0; i < MAX_BINS; i++)
__real__ fft_in[i] *= spectrum_window[i];
my_fftw_execute(plan_spectrum);
// the spectrum display is updated
spectrum_update();
struct rx *r = rx_list;
//STEP 4: we rotate the bins around by r-tuned_bin
for (i = 0; i < MAX_BINS; i++){
int b = i + r->tuned_bin;
if (b >= MAX_BINS)
b = b - MAX_BINS;
if (b < 0)
b = b + MAX_BINS;
r->fft_freq[i] = fft_out[b];
// r->fft_freq[i] = fft_out[i];
}
// STEP 6: apply the filter to the signal,
// in frequency domain we just multiply the filter
// coefficients with the frequency domain samples
for (i = 0; i < MAX_BINS; i++)
r->fft_freq[i] *= r->filter->fir_coeff[i];
//STEP 7: convert back to time domain
my_fftw_execute(r->plan_rev);
//STEP 8 : AGC
agc2(r);
//do an independent am detection (this takes 12 khz of b/w)
for (i= MAX_BINS/2; i < MAX_BINS; i++){
int32_t sample;
sample = abs(r->fft_time[i]) * 1000000;
rx_am_avg = (rx_am_avg* 5 + sample)/6;
//keep transmit buffer empty
output_speaker[i] = sample;
// output_speaker[i] = abs(input_rx[i]);
output_tx[i] = 0;
}
// for (i = 0; i < n_samples; i++)
// output_speaker[i] = rx_am_avg = ((rx_am_avg * 9) + abs(input_rx[i]))/10;
}
//TODO : optimize the memory copy and moves to use the memcpy
void rx_linear(int32_t *input_rx, int32_t *input_mic,
int32_t *output_speaker, int32_t *output_tx, int n_samples)
{
int i, j = 0;
double i_sample, q_sample;
//STEP 1: first add the previous M samples to
for (i = 0; i < MAX_BINS/2; i++)
fft_in[i] = fft_m[i];
//STEP 2: then add the new set of samples
// m is the index into incoming samples, starting at zero
// i is the index into the time samples, picking from
// the samples added in the previous step
int m = 0;
//gather the samples into a time domain array
for (i= MAX_BINS/2; i < MAX_BINS; i++){
i_sample = (1.0 *input_rx[j])/200000000.0;
q_sample = 0;
j++;
__real__ fft_m[m] = i_sample;
__imag__ fft_m[m] = q_sample;
__real__ fft_in[i] = i_sample;
__imag__ fft_in[i] = q_sample;
m++;
}
// STEP 3: convert the time domain samples to frequency domain
my_fftw_execute(plan_fwd);
//STEP 3B: this is a side line, we use these frequency domain
// values to paint the spectrum in the user interface
// I discovered that the raw time samples give horrible spectrum
// and they need to be multiplied wiht a window function
// they use a separate fft plan
// NOTE: the spectrum update has nothing to do with the actual
// signal processing. If you are not showing the spectrum or the
// waterfall, you can skip these steps
for (i = 0; i < MAX_BINS; i++)
__real__ fft_in[i] *= spectrum_window[i];
my_fftw_execute(plan_spectrum);
// the spectrum display is updated
spectrum_update();
// ... back to the actual processing, after spectrum update
// we may add another sub receiver within the pass band later,
// hence, the linkced list of receivers here
// at present, we handle just the first receiver
struct rx *r = rx_list;
//STEP 4: we rotate the bins around by r-tuned_bin
int shift = r->tuned_bin;
if (r->mode == MODE_AM)
shift = 0;
for (i = 0; i < MAX_BINS; i++){
int b = i + shift;
if (b >= MAX_BINS)
b = b - MAX_BINS;
if (b < 0)
b = b + MAX_BINS;
r->fft_freq[i] = fft_out[b];
}
// STEP 5:zero out the other sideband
if (r->mode == MODE_LSB || r->mode == MODE_CWR)
for (i = 0; i < MAX_BINS/2; i++){
__real__ r->fft_freq[i] = 0;
__imag__ r->fft_freq[i] = 0;
}
else if (r->mode != MODE_AM)
for (i = MAX_BINS/2; i < MAX_BINS; i++){
__real__ r->fft_freq[i] = 0;
__imag__ r->fft_freq[i] = 0;
}
// STEP 6: apply the filter to the signal,
// in frequency domain we just multiply the filter
// coefficients with the frequency domain samples
for (i = 0; i < MAX_BINS; i++)
r->fft_freq[i] *= r->filter->fir_coeff[i];
//STEP 7: convert back to time domain
my_fftw_execute(r->plan_rev);
//STEP 8 : AGC
agc2(r);
//STEP 9: send the output back to where it needs to go
int is_digital = 0;
if (rx_list->output == 0){
if (r->mode == MODE_AM)
for (i= 0; i < MAX_BINS/2; i++){
int32_t sample;
sample = cabs(r->fft_time[i+(MAX_BINS/2)]);
//keep transmit buffer empty
output_speaker[i] = sample;
output_tx[i] = 0;
}
else
for (i= 0; i < MAX_BINS/2; i++){
int32_t sample;
sample = cimag(r->fft_time[i+(MAX_BINS/2)]);
//keep transmit buffer empty
output_speaker[i] = sample;
output_tx[i] = 0;
}
//push the samples to the remote audio queue, decimated to 16000 samples/sec
for (i = 0; i < MAX_BINS/2; i += 6)
q_write(&qremote, output_speaker[i]);
}
if (mute_count){
memset(output_speaker, 0, MAX_BINS/2 * sizeof(int32_t));
mute_count--;
}
//push the data to any potential modem
modem_rx(rx_list->mode, output_speaker, MAX_BINS/2);
}
void read_power(){
uint8_t response[4];
int16_t vfwd, vref;
char buff[20];
if (!in_tx)
return;
if(i2cbb_read_i2c_block_data(0x8, 0, 4, response) == -1)
return;
vfwd = vref = 0;
memcpy(&vfwd, response, 2);
memcpy(&vref, response+2, 2);
// printf("%d:%d\n", vfwd, vref);
if (vref >= vfwd)
vswr = 100;
else
vswr = (10*(vfwd + vref))/(vfwd-vref);
//here '400' is the scaling factor as our ref power output is 40 watts
//this calculates the power as 1/10th of a watt, 400 = 40 watts
int fwdvoltage = (vfwd * 40)/bridge_compensation;
fwdpower = (fwdvoltage * fwdvoltage)/400;
int rf_v_p2p = (fwdvoltage * 126)/400;
if (rf_v_p2p > 135 && !in_calibration){
alc_level *= 135.0 / (1.0 * rf_v_p2p);
printf("ALC tripped, to %d percent\n", (int)(100 * alc_level));
}
/* else if (alc_level < 0.95){
printf("alc releasing to ");
alc_level *= 1.02;
}
*/
// printf("alc: %g\n", alc_level);
}
static int tx_process_restart = 0;
void tx_process(
int32_t *input_rx, int32_t *input_mic,
int32_t *output_speaker, int32_t *output_tx,
int n_samples)
{
int i;
double i_sample, q_sample, i_carrier;
struct rx *r = tx_list;
//fix the burst at the start of transmission
if (tx_process_restart){
fft_reset_m_bins();
tx_process_restart = 0;
}
if (mute_count && (r->mode == MODE_USB || r->mode == MODE_LSB
|| r->mode == MODE_AM)){
memset(input_mic, 0, n_samples * sizeof(int32_t));
mute_count--;
}
//first add the previous M samples
for (i = 0; i < MAX_BINS/2; i++)
fft_in[i] = fft_m[i];
int m = 0;
int j = 0;
//double max = -10.0, min = 10.0;
//gather the samples into a time domain array
for (i= MAX_BINS/2; i < MAX_BINS; i++){
if (r->mode == MODE_2TONE)
i_sample = (1.0 * (vfo_read(&tone_a)
+ vfo_read(&tone_b) )) / 50000000000.0;
else if (r->mode == MODE_CALIBRATE)
i_sample = (1.0 * (vfo_read(&tone_a))) / 30000000000.0;
else if (r->mode == MODE_CW || r->mode == MODE_CWR || r->mode == MODE_FT8)
i_sample = modem_next_sample(r->mode) / 3;
else if (r->mode == MODE_AM){
//double modulation = (1.0 * vfo_read(&tone_a)) / 1073741824.0;
double modulation = (1.0 * input_mic[j]) / 200000000.0;
if (modulation < -1.0)
modulation = -1.0;
i_carrier= (1.0 * vfo_read(&am_carrier))/ 50000000000.0 ;
i_sample = (1.0 + modulation) * i_carrier;
}
else
i_sample = (1.0 * input_mic[j]) / 2000000000.0;
//clip the overdrive to prevent damage up the processing chain, PA
if (r->mode == MODE_USB || r->mode == MODE_LSB || r->mode == MODE_AM){
if (i_sample < (-1.0 * voice_clip_level))
i_sample = -1.0 * voice_clip_level;
else if (i_sample > voice_clip_level)
i_sample = voice_clip_level;
}
//don't echo the voice modes
if (r->mode == MODE_USB || r->mode == MODE_LSB || r->mode == MODE_AM
|| r->mode == MODE_NBFM)
output_speaker[j] = 0;
else
output_speaker[j] = i_sample * sidetone;
q_sample = 0;
j++;
__real__ fft_m[m] = i_sample;
__imag__ fft_m[m] = q_sample;
__real__ fft_in[i] = i_sample;
__imag__ fft_in[i] = q_sample;
m++;
}
//push the samples to the remote audio queue, decimated to 16000 samples/sec
for (i = 0; i < MAX_BINS/2; i += 6)
q_write(&qremote, output_speaker[i]);
//convert to frequency
fftw_execute(plan_fwd);
// NOTE: fft_out holds the fft output (in freq domain) of the
// incoming mic samples
// the naming is unfortunate
// apply the filter
for (i = 0; i < MAX_BINS; i++)
fft_out[i] *= tx_filter->fir_coeff[i];
// the usb extends from 0 to MAX_BINS/2 - 1,
// the lsb extends from MAX_BINS - 1 to MAX_BINS/2 (reverse direction)
// zero out the other sideband
// TBD: Something strange is going on, this should have been the otherway
if (r->mode == MODE_LSB || r->mode == MODE_CWR)
// zero out the LSB
for (i = 0; i < MAX_BINS/2; i++){
__real__ fft_out[i] = 0;
__imag__ fft_out[i] = 0;
}
else if (r->mode != MODE_AM)
// zero out the USB
for (i = MAX_BINS/2; i < MAX_BINS; i++){
__real__ fft_out[i] = 0;
__imag__ fft_out[i] = 0;
}
//now rotate to the tx_bin
//rememeber the AM is already a carrier modulated at 24 KHz
int shift = tx_shift;
if (r->mode == MODE_AM)
shift = 0;
for (i = 0; i < MAX_BINS; i++){
int b = i + shift;
if (b >= MAX_BINS)
b = b - MAX_BINS;
if (b < 0)
b = b + MAX_BINS;
r->fft_freq[b] = fft_out[i];
}
// the spectrum display is updated
//spectrum_update();
//convert back to time domain
fftw_execute(r->plan_rev);
int min = 10000000;
int max = -10000000;
float scale = volume;
for (i= 0; i < MAX_BINS/2; i++){
double s = creal(r->fft_time[i+(MAX_BINS/2)]);
output_tx[i] = s * scale * tx_amp * alc_level;
if (min > output_tx[i])
min = output_tx[i];
if (max < output_tx[i])
max = output_tx[i];
//output_tx[i] = 0;
}
// printf("min %d, max %d\n", min, max);
read_power();
sdr_modulation_update(output_tx, MAX_BINS/2, tx_amp);
}
/*
This is called each time there is a block of signal samples ready
either from the mic or from the rx IF
*/
void sound_process(
int32_t *input_rx, int32_t *input_mic,
int32_t *output_speaker, int32_t *output_tx,
int n_samples)
{
if (in_tx)
tx_process(input_rx, input_mic, output_speaker, output_tx, n_samples);
else
rx_linear(input_rx, input_mic, output_speaker, output_tx, n_samples);
if (pf_record)
wav_record(in_tx == 0 ? output_speaker : input_mic, n_samples);