-
Notifications
You must be signed in to change notification settings - Fork 51
/
Teensy_Convolution_SDR.ino8884691144188022409.tmp
15113 lines (13649 loc) · 553 KB
/
Teensy_Convolution_SDR.ino8884691144188022409.tmp
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
/*********************************************************************************************
(c) Frank DD4WH 2019_09_30
"TEENSY CONVOLUTION SDR"
SOFTWARE FOR A FAST CONVOLUTION-BASED RADIO
HARDWARE NEEDED:
- simple quadrature sampling detector board producing baseband IQ signals (Softrock, Elektor SDR etc.)
(IQ boards with up to 192kHz bandwidth supported --> which basically means nearly 100% of the existing boards on the market)
- Teensy audio board
- Teensy 3.6 (No, Teensy 3.1/3.2/3.5/4.0 not supported)
HARDWARE OPTIONAL:
- Preselection: switchable RF lowpass or bandpass filter
- digital step attenuator: PE4306 used in my setup
SOFTWARE:
- FFT Fast Convolution = Digital Convolution
- with overlap - save = overlap-discard complex bandpass main filtering
- spectral NR uses FFT-iFFT overlap-add with 50% overlap
- in floating point 32bit
- tested on Teensy 3.6 (using its FPU)
- compile with 180MHz F_CPU, other speeds not supported
Part of the evolution of this project has been documented here:
https://forum.pjrc.com/threads/40188-Fast-Convolution-filtering-in-floating-point-with-Teensy-3-6/page2
HISTORY OF IMPLEMENTED FEATURES
- 12kHz to 30MHz Receive PLUS 76 - 108MHz: undersampling-by-3 with slightly reduced sensitivity (-9dB)
- I & Q - correction in software (manual correction or automatic correction)
- efficient frequency translation without multiplication
- efficient spectrum display using a 256 point FFT on the first 256 samples of every 4096 sample-cycle
- efficient AM demodulation with ARM functions
- efficient DC elimination after AM demodulation
- implemented nine different AM demodulation algorithms for comparison (only two could stand the test and one algorithm was finally left in the implementation)
- real SAM - synchronous AM demodulation with phase determination by atan2f implemented from the wdsp lib
- Stereo-SAM and sideband-selected SAM
- sample rate from 48k to 234k and decimation-by-8 for efficient realtime calculations
- spectrum Zoom function 1x, 2x, 4x, 512x, 1024x, 2048x, 4096x --> 4096x zoom with sub-Hz resolution
- Automatic gain control (high end algorithm by Warren Pratt, wdsp)
- plays MP3 and M4A (iTunes files) from SD card with the awesome lib by Frank Bösing (his old MP3 lib, not the new one)
- automatic IQ amplitude and phase imbalance correction
- dynamic frequency indicator figures and graticules on spectrum display x-axis
- kind of menu system now working with many variables that can be set by the encoders
- EEPROM save & load of important settings
- wideband FM demodulation with deemphasis
- automatic codec gain adjustment depending on the sample input level
- spectrum display AGC to allow display of very small signals
- spectrum display in WFM activated (alpha version . . .)
- optimized automatic test whether mirror rejection is working - if not, codec is restarted automatically until we have working mirror rejection
- display mirror rejection check ("IQtest" in red box)
- activated integrated codec 5-band graphic equalizer
- added digital attenuator PE4306 bit-banging SPI control [0 -31dB attenuation possible]
- added backlight control for TFT in the menu
- added analog gain display (analog codec gain AND attenuation displayed)
- fixed major bug associated with too small "string" variables for printing, leading to annoying audio clicks
- STEREO FM reception implemented and disabled spectrum display in WFM STEREO mode, because the digital noise of the refresh of the spectrum display does seriously distort audio
- manual notch filter implemented [in the frequency domain: simply deletes bins before the iFFT]
- bandwidth adjustment of manual notch filter implemented
- graphical display of manual notch filters in the frequency domain
- Michaels excellent noise blanker is working! Eliminates noise impulses very nicely and effectively!
- leaky LMS algorithm from the wdsp lib implemented (but not working as expected . . .)
- switched to complex filter coefficients for general filter in the fast convolution process
- freely adjustable bandpasses & passband tuning in AM/SAM/SSB . . .
- rebuilt convolution with more flexible choice of FFT size --> now default FFT size is 512, because of memory constraints when using 1024 . . .
- decimation and interpolation filters are calculated with new algorithm and are calculated on-the-fly when changing filter characteristics --> much less hiss and ringing of the filters
- Blackman-Harris four-term window for main FIR filter (as in PowerSDR)
- first test of a 110kHz lowpass filter in the WFM path for FM (stereo) reception on VHF --> does work properly but causes strange effects (button swaps) because of memory constraints when assigning the FIR instances
- changed default to 512tap FFT in order to have enough memory for MP3 playing and other things
- updated Arduino to version 1.8.5 and Teensyduino to version 1.40 and had to change some of the code
- implemented spectral noise reduction in the frequency domain by implementing another FFT-iFFT-overlap-add chain on the real audio output after the main filter
- spectral weighting algorithm Kim et al. 2002 implemented[working!]
- spectral weighting algorithm Romanin et al. 2009 / Schmitt et al. 2002 implemented (minimum statistics)[obsolete]
- spectral weighting algorithm Romanin et al. 2009 implemented (voice activity detector)[working, without VAD now]
- fixed bug in alias filter switching when changing bandpass filter coefficients
- adjustment in finer filter frequency steps when below 500Hz (switch to 50Hz steps instead of 100Hz)
- fixed several bugs in band switching and mode switching
- final tweak of spectral NR algorithms finished (many parameters eliminated from menu)
- for comparison added LMS and leaky LMS to NR menu choice (four NR algorithms to choose from: Kim, Romanin, leaky LMS, LMS)
- changed spectral NR Romanin to newest version by Michael DL2FW [the final UHSDR version, 22.2.2018]
- analog clock design
- spectrum display FFT windowing bug fixed (thanks, Bob Larkin!)
- ZoomFFT repaired and now fully functional for all magnifications (up to 2048x), additional IIR filters added, also added higher refresh rate!
- incorporated many good ideas by Bob Larkin, thanks!
- experimental new sample rates up to 353ksps . . . https://forum.pjrc.com/threads/42336-Reset-audio-board-codec-SGTL5000-in-realtime-processing/page3?highlight=sample+rate
- add possibility to use PCB hardware by DO7JBH https://github.com/do7jbh/SSR-2
- bugfix array out-of-bound, thanks bicycleguy for pointing me to this bug!
- atan2f approximation: https://www.mikrocontroller.net/topic/atan2-funktion-mit-lookup-table-fuer-arm --> thanks Frank B for the hint !
- bugfix band vs. bands --> cleanup and changed int band to int current_band
- integrated automatic crc check on eePROM load and save (by Mike / bicycleguy, thanks!) - no more need to uncomment/comment during first time use of the software
- added support for Bob Larkins RF Octave frontend filters http://www.janbob.com/electron/FilterBP1/FiltBP1.html
- bugfix: only use local loop variables
- bugfix: software now usable on different hardware versions: DO7JBH, DD4WH
- CW decoder (modified version of Lofturs excellent implementation) taken from UHSDR
TODO:
- ABANDONED UNTIL RELIABLE AUDIO AVAILABLE: get this software to run on the T4 with software switches, so we can use ONE software for T3.6 AND T4.0
- fix bug in Zoom_FFT --> lowpass IIR filters run with different sample rates, but are calculated for a fixed sample rate of 48ksps
- implement separate interrupt to cope with UI (encoders, buttons, calculation of filter coefficients) in order to free audio interrupt
- SSB autotune algorithm taken from Robert Dick
- RTTY decoder
- BPSK decoder
- UKW DX filters for WFM prior to FM demodulation (110kHz, 80kHz, 57kHz)
- test dBm measurement according to filter passband
- RDS decoding in wide FM reception mode ;-): very hard, but could be barely possible
- finetune AGC parameters and make AGC HANG TIME, AGC HANG THRESHOLD and AGC HANG DECAY user-adjustable
- record and playback IQ audio stream ;-)
- read stations´ frequencies from SD card and display station names when tuned to a frequency
- implement Motorola C-QUAM AM Stereo demodulation
- CW peak filter (independently adjustable from notch filter)
some parts of the code modified from and/or inspired by the following open sources:
Teensy SDR (rheslip & DD4WH): https://github.com/DD4WH/Teensy-SDR-Rx [GNU GPL]
UHSDR (M0NKA, KA7OEI, DF8OE, DB4PLE, DL2FW, DD4WH & other contributors): https://github.com/df8oe/UHSDR [GNU GPL]
libcsdr (András Retzler): https://github.com/simonyiszk/csdr [BSD / GPL]
wdsp (Warren Pratt): http://svn.tapr.org/repos_sdr_hpsdr/trunk/W5WC/PowerSDR_HPSDR_mRX_PS/Source/wdsp/ [GNU GPL]
Wheatley (2011): cuteSDR https://github.com/satrian/cutesdr-se [BSD]
Robert Dick (1999): Tune SSB Automatically. in QEX: http://www.arrl.org/files/file/QEX%20Binaries/1999/ssbtune.zip ["code is in the public domain . . .", thus I assume GNU GPL]
sample-rate-change-on-the-fly code by Frank Bösing [MIT]
GREAT THANKS FOR ALL THE HELP AND INPUT BY WALTER, WMXZ !
Audio queue optimized by Pete El Supremo 2016_10_27, thanks Pete!
An important hint on the implementation came from Alberto I2PHD, thanks for that!
Thanks to Brian, bmillier for helping with codec restart code for the SGTL 5000 codec in the Teensy audio board!
Thanks a lot to Michael DL2FW - without you the spectral noise reduction would not have been possible! Also you contributed the state-of-the-art Noise Blanker
Bob Larkin, W7PUA, found a significant bug in the spectrum display FFT windowing and added lots of other very useful things, thanks a lot, Bob!
and of course a great Thank You to Paul Stoffregen @ pjrc.com for providing the Teensy platform and its excellent audio library !
Audio processing in float32_t with the NEW ARM CMSIS lib, --> https://forum.pjrc.com/threads/40590-Teensy-Convolution-SDR-(Software-Defined-Radio)?p=129081&viewfull=1#post129081
*********************************************************************************
**
** Project.........: Read Hand Sent Morse Code (tolerant of considerable jitter)
**
** Copyright (c) 2016 Loftur E. Jonasson (tf3lj [at] arrl [dot] net)
**
** https://sites.google.com/site/lofturj/cwreceive#TOC-Take-two-Fast-Fourier-Transform-and-Colour-Graphics
** Substantive portions of the methodology used here to decode Morse Code are found in:
**
** "MACHINE RECOGNITION OF HAND-SENT MORSE CODE USING THE PDP-12 COMPUTER"
** by Joel Arthur Guenther, Air Force Institute of Technology,
** Wright-Patterson Air Force Base, Ohio
** December 1973
** http://www.dtic.mil/dtic/tr/fulltext/u2/786492.pdf
**
** Platform........: Teensy 3.1 / 3.2 and the Teensy Audio Shield
**
** Initial version.: 0.00, 2016-01-25 Loftur Jonasson, TF3LJ / VE2LJX
**
*********************************************************************************
GNU GPL LICENSE v3
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
************************************************************************************************************************************/
/* If you use the hardware made by Frank DD4WH uncomment the next line */
//#define HARDWARE_DD4WH
/* If you use the hardware made by FrankB uncomment the next line */
//#define HARDWARE_FRANKB
/* If you use the hardware made by Dante DO7JBH [https://github.com/do7jbh/SSR-2], uncomment the next line */
#define HARDWARE_DO7JBH
/* only for debugging */
//#define DEBUG
/* this prints out the ADC and DAC levels when NOT in SAM mode, primarily for debugging hardware
recommendation: leave this commented */
//#define USE_ADC_DAC_display
/* only for support of the hardware RF frontend filters designed by Bob Larkin, W7PUA
http://www.janbob.com/electron/FilterBP1/FiltBP1.html
adjust cutoff frequencies according to your needs in function setfreq */
#define USE_BOBS_FILTER
/* flag to indicate to use the changes introduced by Bob Larkin, W7PUA
recommendation: leave this uncommented */
#define USE_W7PUA
/* use faster log calculations
recommendation: leave this uncommented */
#define USE_LOG10FAST
/* use faster atan2f calculation
recommendation: leave this uncommented */
#define USE_ATAN2FAST
#define MP3 1
#if defined(__IMXRT1062__)
#define T4
#endif
/* this allows simultaneous calculation of sin and cos to save processor time for SAM demodulation */
extern "C"
{
void sincosf(float err, float *s, float *c);
}
#include <Audio.h>
#include <Time.h>
#include <TimeLib.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Metro.h>
#include <Bounce.h>
#include <arm_math.h>
#include <arm_const_structs.h>
#include <si5351.h>
#include <Encoder.h>
#if defined(MP3)
#include <play_sd_mp3.h> //mp3 decoder by Frank B
#include <play_sd_aac.h> // AAC decoder by Frank B
#endif
#include <util/crc16.h> //mdrhere
#if defined(T4)
#include <SPIN.h>
#include <ILI9341_t3n.h>
#include <ili9341_t3n_font_Arial.h>
#else
#include <EEPROM.h>
#include <ILI9341_t3.h>
#include "font_Arial.h"
#endif
// CW DECODER STUFF
#define CW_DECODER_BLOCKSIZE_MIN 8
#define CW_DECODER_BLOCKSIZE_MAX 256
#define CW_DECODER_BLOCKSIZE_DEFAULT 128 //88
//#define CW_DECODER_THRESH_MIN 1000
//#define CW_DECODER_THRESH_MAX 50000
//#define CW_DECODER_THRESH_DEFAULT 32000
//#define SIGNAL_TAU 0.01
#define SIGNAL_TAU 0.1
#define ONEM_SIGNAL_TAU (1.0 - SIGNAL_TAU)
#define CW_TIMEOUT 3 // Time, in seconds, to trigger display of last Character received
#define ONE_SECOND (12000 / cw_decoder_config.blocksize) // sample rate / decimation rate / block size
#define CW_SPIKECANCEL_MAX_DURATION 8 // Cancel transients/spikes/drops that have max duration of number chosen.
// Typically 4 or 8 to select at time periods of 4 or 8 times 2.9ms.
// 0 to deselect.
#define CW_SIG_BUFSIZE 256 // Size of a circular buffer of decoded input levels and durations
#define CW_DATA_BUFSIZE 40 // Size of a buffer of accumulated dot/dash information. Max is DATA_BUFSIZE-2
// Needs to be significantly longer than longest symbol 'sos'= ~30.
#define DIGIMODE_OFF 0
#define CW 1
#define RTTY 2
#define EFR 3
#define RTTY_OSSI 4
#define DCF77 5
#define PSK 6
#define DIGIMODE_LAST 4
uint8_t digimode = 0;
float lastII = 0;
float lastQQ = 0;
float RXbit = 0;
float bitSampleTimer=0;
float Tsample=1.0 / 12000.0;
float CP_buffer[256];
float CP_buffer_old = 0.0;
// for EFR
//float bitSamplePeriod=1.0/1000.0 ;
// for RTTY
float bitSamplePeriod=1.0/500.0;
// print stuff for text terminal
#define termChrXwidth 9
//#define termChrYwidth 9
#define termChrYwidth 10
//#define termNrows 20
//#define termNrows 16
#define termNrows 4 // 15
#define termNcols 28 // 34
#define CW_x_start spectrum_x + 2 // 1
#define CW_y_start spectrum_y - 1 // 55
//#define font Arial_6
int termCursorXpos = 0;
int termCursorYpos = 0;
uint16_t termColor = 0x10000;
char termCharStore[termNcols][termNrows] ;
int16_t termCharColorStore[termNcols][termNrows] ;
#define RTTYuartFullTime 10
#define RTTYuartHalfTime 6
#define LFcode 10
#define CRcode 13
#define UU 'y'
typedef struct
{
float32_t sampling_freq;
float32_t target_freq;
uint8_t speed;
float32_t thresh;
uint8_t blocksize;
// uint8_t AGC_enable;
uint8_t noisecancel_enable;
uint8_t spikecancel;
#define CW_SPIKECANCEL_MODE_OFF 0
#define CW_SPIKECANCEL_MODE_SPIKE 1
#define CW_SPIKECANCEL_MODE_SHORT 2
bool atc_enable;
bool snap_enable;
bool show_CW_LED; // menu choice whether the user wants the CW LED indicator to be working or not
} cw_config_t;
typedef struct
{
int a;
float32_t b;
float32_t sin;
float32_t cos;
float32_t r;
float32_t buf[3];
} Goertzel;
Goertzel cw_goertzel;
cw_config_t cw_decoder_config =
{ .sampling_freq = 12000.0, .target_freq = 700, //700.0,
.speed = 25,
// .average = 2,
.thresh = 2.8, //32000,
.blocksize = CW_DECODER_BLOCKSIZE_DEFAULT,
// .AGC_enable = 0,
.noisecancel_enable = 1,
.spikecancel = 0,
.atc_enable = false,
.snap_enable = false,
.show_CW_LED = false // menu choice whether the user wants the CW LED indicator to be working or not
};
typedef enum {
RTTY_STOP_1 = 0,
RTTY_STOP_1_5,
RTTY_STOP_2,
RTTY_STOP_NUM
} rtty_stop_t;
typedef struct
{
float32_t speed;
rtty_stop_t stopbits;
uint16_t shift;
float32_t samplerate;
} rtty_mode_config_t;
typedef enum {
RTTY_SPEED_45,
RTTY_SPEED_50,
RTTY_SPEED_200,
RTTY_SPEED_NUM
} rtty_speed_t;
typedef enum {
RTTY_SHIFT_85,
RTTY_SHIFT_170,
RTTY_SHIFT_200,
RTTY_SHIFT_340,
RTTY_SHIFT_425,
RTTY_SHIFT_450,
RTTY_SHIFT_850,
RTTY_SHIFT_NUM
} rtty_shift_t;
typedef struct
{
rtty_speed_t id;
float32_t value;
char* label;
} rtty_speed_item_t;
// TODO: Probably we should define just a few for the various value types and let
// the id be an uint32_t
typedef struct
{
rtty_shift_t id;
uint32_t value;
char* label;
} rtty_shift_item_t;
typedef struct
{
rtty_shift_t shift_idx;
rtty_speed_t speed_idx;
rtty_stop_t stopbits_idx;
bool atc_disable; // should the automatic level control be turned off?
} rtty_ctrl_t;
rtty_ctrl_t rtty_ctrl_config =
{
.shift_idx = RTTY_SHIFT_450,
.speed_idx = RTTY_SPEED_50,
.stopbits_idx = RTTY_STOP_1_5,
.atc_disable = false
};
// bits 0-4 -> baudot, bit 5 1 == LETTER, 0 == NUMBER/FIGURE
const uint8_t Ascii2Baudot[128] =
{
0,
0,
0,
0,
0,
0,
0,
0b001011, // BEL N
0,
0,
0b000010, // \n NL
0,
0,
0b001000, // \r NL
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0b100100, // N
0, // !
0, // "
0, // #
0, // $
0, // %
0, // &
0b000101, // ' N
0b001111, // ( N
0b010010, // ) N
0, // *
0b010001, // + N
0b001100, // , N
0b000011, // - N
0b011100, // . N
0b011101, // / N
0b010110, // 0 N
0b010111, // 1 N
0b010011, // 2 N
0b000001, // 3 N
0b001010, // 4 N
0b010000, // 5 N
0b010101, // 6 N
0b000111, // 7 N
0b000110, // 8 N
0b011000, // 9 N
0b001110, // : N
0, // ;
0, // <
0b011110, // =
0, // >
0b011001, // ? N
0, // @
0b100011, // A L
0b111001, // B L
0b101110, // C L
0b101001, // D L
0b100001, // E L
0b101101, // F L
0b111010, // G L
0b110100, // H L
0b100110, // I L
0b101011, // J L
0b101111, // K L
0b110010, // L L
0b111100, // M L
0b101100, // N L
0b111000, // O L
0b110110, // P L
0b110111, // Q L
0b101010, // R L
0b100101, // S L
0b110000, // T L
0b100111, // U L
0b111110, // V L
0b110011, // W L
0b111101, // X L
0b110101, // Y L
0b110001, // Z L
0,
0,
0,
0,
0,
0,
0b100011, // A L
0b111001, // B L
0b101110, // C L
0b101001, // D L
0b100001, // E L
0b101101, // F L
0b111010, // G L
0b110100, // H L
0b100110, // I L
0b101011, // J L
0b101111, // K L
0b110010, // L L
0b111100, // M L
0b101100, // N L
0b111000, // O L
0b110110, // P L
0b110111, // Q L
0b101010, // R L
0b100101, // S L
0b110000, // T L
0b100111, // U L
0b111110, // V L
0b110011, // W L
0b111101, // X L
0b110101, // Y L
0b110001, // Z L
0,
0,
0,
0,
0,
};
#define RTTY_SYMBOL_CODE (0b11011)
#define RTTY_LETTER_CODE (0b11111)
// RTTY Experiment based on code from the DSP Tutorial at http://dp.nonoo.hu/projects/ham-dsp-tutorial/18-rtty-decoder-using-iir-filters/
// Used with permission from Norbert Varga, HA2NON under GPLv3 license
const rtty_speed_item_t rtty_speeds[RTTY_SPEED_NUM] =
{
{ .id =RTTY_SPEED_45, .value = 45.45, .label = "45" },
{ .id =RTTY_SPEED_50, .value = 50, .label = "50" },
{ .id =RTTY_SPEED_200, .value = 200, .label = "200" },
};
const rtty_shift_item_t rtty_shifts[RTTY_SHIFT_NUM] =
{
{ RTTY_SHIFT_85, 85, " 85" },
{ RTTY_SHIFT_170, 170, "170" },
{ RTTY_SHIFT_200, 200, "200" },
{ RTTY_SHIFT_340, 340, "340" },
{ RTTY_SHIFT_425, 425, "425" },
{ RTTY_SHIFT_450, 450, "450" },
{ RTTY_SHIFT_850, 850, "850" },
};
typedef struct
{
float32_t gain;
float32_t coeffs[4];
uint16_t freq; // center freq
} rtty_bpf_config_t;
typedef struct
{
float32_t gain;
float32_t coeffs[2];
} rtty_lpf_config_t;
typedef struct
{
float32_t xv[5];
float32_t yv[5];
} rtty_bpf_data_t;
typedef struct
{
float32_t xv[3];
float32_t yv[3];
} rtty_lpf_data_t;
static float32_t RttyDecoder_bandPassFreq(float32_t sampleIn, const rtty_bpf_config_t* coeffs, rtty_bpf_data_t* data) {
data->xv[0] = data->xv[1]; data->xv[1] = data->xv[2]; data->xv[2] = data->xv[3]; data->xv[3] = data->xv[4];
data->xv[4] = sampleIn / coeffs->gain; // gain at centre
data->yv[0] = data->yv[1]; data->yv[1] = data->yv[2]; data->yv[2] = data->yv[3]; data->yv[3] = data->yv[4];
data->yv[4] = (data->xv[0] + data->xv[4]) - 2 * data->xv[2]
+ (coeffs->coeffs[0] * data->yv[0]) + (coeffs->coeffs[1] * data->yv[1])
+ (coeffs->coeffs[2] * data->yv[2]) + (coeffs->coeffs[3] * data->yv[3]);
return data->yv[4];
}
static float32_t RttyDecoder_lowPass(float32_t sampleIn, const rtty_lpf_config_t* coeffs, rtty_lpf_data_t* data) {
data->xv[0] = data->xv[1]; data->xv[1] = data->xv[2];
data->xv[2] = sampleIn / coeffs->gain; // gain at DC
data->yv[0] = data->yv[1]; data->yv[1] = data->yv[2];
data->yv[2] = (data->xv[0] + data->xv[2]) + 2 * data->xv[1]
+ (coeffs->coeffs[0] * data->yv[0]) + (coeffs->coeffs[1] * data->yv[1]);
return data->yv[2];
}
typedef enum {
RTTY_RUN_STATE_WAIT_START = 0,
RTTY_RUN_STATE_BIT,
} rtty_run_state_t;
typedef enum {
RTTY_MODE_LETTERS = 0,
RTTY_MODE_SYMBOLS
} rtty_charSetMode_t;
typedef struct {
rtty_bpf_data_t bpfSpaceData;
rtty_bpf_data_t bpfMarkData;
rtty_lpf_data_t lpfData;
rtty_bpf_config_t *bpfSpaceConfig;
rtty_bpf_config_t *bpfMarkConfig;
rtty_lpf_config_t *lpfConfig;
uint16_t oneBitSampleCount;
int32_t DPLLOldVal;
int32_t DPLLBitPhase;
uint8_t byteResult;
uint16_t byteResultp;
rtty_charSetMode_t charSetMode;
rtty_run_state_t state;
const rtty_mode_config_t* config_p;
} rtty_decoder_data_t;
rtty_decoder_data_t rttyDecoderData;
// this is for 12ksps sample rate
// for filter designing, see http://www-users.cs.york.ac.uk/~fisher/mkfilter/
// order 2 Butterworth, freqs: 865-965 Hz, centre: 915 Hz
static rtty_bpf_config_t rtty_bp_12khz_915 =
{
.gain = 1.513364755e+03,
.coeffs = { -0.9286270861, 3.3584472566, -4.9635817596, 3.4851652468 },
.freq = 915
};
// order 2 Butterworth, freqs: 1315-1415 Hz, centre 1365Hz
static rtty_bpf_config_t rtty_bp_12khz_1365 =
{
.gain = 1.513365019e+03,
.coeffs = { -0.9286270861, 2.8583904591, -4.1263569881, 2.9662407442 },
.freq = 1365
};
// order 2 Butterworth, freqs: 1035-1135 Hz, centre: 1085Hz
static rtty_bpf_config_t rtty_bp_12khz_1085 =
{
.gain = 1.513364927e+03,
.coeffs = { -0.9286270861, 3.1900687350, -4.6666321298, 3.3104336142 },
.freq = 1085
};
// order 2 Butterworth, freqs: 1065-1165 Hz, centre: 1115Hz
// for 200Hz shift
static rtty_bpf_config_t rtty_bp_12khz_1115 =
{
.gain = 1.513364944e+03,
.coeffs = { -0.9286270861, 3.1576917276, -4.6112830458, 3.2768349860 },
.freq = 1115
};
// for 340Hz shift --> 915 + 340 = 1255Hz
// order 2 Butterworth, freqs: 1205-1305 Hz, centre: 1255Hz
//
static rtty_bpf_config_t rtty_bp_12khz_1255 =
{
.gain = 1.513364944e+03,
.coeffs = { -0.9286270861, 2.9964316664, -4.3440155011, 3.1094904013 },
.freq = 1255
};
// for 85Hz shift --> 915 + 85Hz = space = 1000Hz
// 3dB bandwidth 50Hz
// order 2 Butterworth, freqs: 975-1025 Hz, centre: 1000Hz
static rtty_bpf_config_t rtty_bp_12khz_1000 =
{
.gain = 5.944465260e+03,
.coeffs = { -0.9636529842, 3.3693752166, -4.9084595657, 3.4323354886 },
.freq = 1000
};
// for 425Hz shift --> 915 + 425Hz = space = 1340Hz
// 3dB bandwidth 100Hz
// order 2 Butterworth, freqs: 1290 - 1390 Hz, centre: 1340Hz
static rtty_bpf_config_t rtty_bp_12khz_1340 =
{
.gain = 1.513365018e+03,
.coeffs = { -0.9286270862, 2.8906128091, -4.1762457780, 2.9996788796 },
.freq = 1340
};
// for 850Hz shift --> 915 + 850Hz = space = 1765Hz
// 3dB bandwidth 100Hz
// order 2 Butterworth, freqs: 1715 - 1815 Hz, centre: 1765Hz
static rtty_bpf_config_t rtty_bp_12khz_1765 =
{
.gain = 1.513365057e+03,
.coeffs = { -0.9286270862, 2.1190223173, -3.1352567157, 2.1989754113 },
.freq = 1765
};
static rtty_lpf_config_t rtty_lp_12khz_50 =
{
.gain = 5.944465310e+03,
.coeffs = { -0.9636529842, 1.9629800894 }
};
static rtty_mode_config_t rtty_mode_current_config;
int RTTY_marker_0 = 915; // RTTY_mark
int RTTY_marker_1 = RTTY_marker_0 + rtty_shifts[rtty_ctrl_config.shift_idx].value;
int is_usb_demod = 1;
float hz_per_pixel = 1.0;
float RTTY_marker_0_offset = 127;
float RTTY_marker_1_offset = 127;
time_t getTeensy3Time()
{
return Teensy3Clock.get();
}
// Settings for the hardware QSD
// Joris PCB uses a 27MHz crystal and CLOCK 2 output
// Elektor SDR PCB uses a 25MHz crystal and the CLOCK 1 output
//#define Si_5351_clock SI5351_CLK1
#if defined(HARDWARE_DO7JBH) || defined(HARDWARE_FRANKB)
#define Si_5351_crystal 25000000
#else
#define Si_5351_crystal 27000000
#endif
#define Si_5351_clock SI5351_CLK2
// Europe uses 9 kHz AM spacing, N.A. uses 10 (AM_SPACING_EU==0). Others??? <PUA>
#define AM_SPACING_EU 1
unsigned long long calibration_factor = 1000000000 ;// 10002285;
long calibration_constant = 0;
// this is for the Joris PCB !
//long calibration_constant = 108000; // this is for the Elektor PCB !
unsigned long long hilfsf = 1000000000;
#ifdef HARDWARE_DO7JBH
// Optical Encoder connections
Encoder tune (16, 17);
Encoder filter (4, 5);
Encoder encoder3 (1, 2); //(26, 28);
Si5351 si5351;
#define MASTER_CLK_MULT 4 // QSD frontend requires 4x clock
#define BACKLIGHT_PIN 0 // unfortunately connected to 3V3 in DO7JBHs PCB
#define TFT_DC 20
#define TFT_CS 21
#define TFT_RST 35 // 255 = unused. connect to 3.3V
#define TFT_MOSI 7
#define TFT_SCLK 14
#define TFT_MISO 12
// pins for digital attenuator board PE4306
//#define ATT_LE 24
//#define ATT_DATA 25
//#define ATT_CLOCK 28
// dummy definitions for Dantes hardware
#define ATT_LE 40
#define ATT_DATA 41
#define ATT_CLOCK 42
// prop shield LC used for audio speaker amp
//#define AUDIO_AMP_ENABLE 39
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
// push-buttons
#define BUTTON_1_PIN A22 // encoder2 button = button3SW
#define BUTTON_2_PIN 37 // BAND+ = button2SW
#define BUTTON_3_PIN 30 // ???
#define BUTTON_4_PIN 36 //
#define BUTTON_5_PIN 38 // this is the pushbutton pin of the tune encoder
#define BUTTON_6_PIN 8 // this is the pushbutton pin of the filter encoder
#define BUTTON_7_PIN 39 // this is the menu button pin
#define BUTTON_8_PIN 33 //27 // this is the pushbutton pin of encoder 3
const int8_t On_set = 25; // hold switched on
Bounce button1 = Bounce(BUTTON_1_PIN, 50);
Bounce button2 = Bounce(BUTTON_2_PIN, 50);
Bounce button3 = Bounce(BUTTON_3_PIN, 50);
Bounce button4 = Bounce(BUTTON_4_PIN, 50);
Bounce button5 = Bounce(BUTTON_5_PIN, 50);
Bounce button6 = Bounce(BUTTON_6_PIN, 50);
Bounce button7 = Bounce(BUTTON_7_PIN, 50);
Bounce button8 = Bounce(BUTTON_8_PIN, 50);
#elif defined(HARDWARE_FRANKB)
Si5351 si5351;
// Optical Encoder connections
Encoder tune (2, 3);
Encoder filter (1, 2);
Encoder encoder3 (15, 16); //(26, 28);
#define MASTER_CLK_MULT 4 // QSD frontend requires 4x clock
#define PCF8574_ADR 0x20
#define PCF8574_INT 22
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_SENSE 24 //read 0: Card inserted, 1: no Card
#define ENCODER_1_A 2
#define ENCODER_1_B 3
#define ENCODER_2_A 4
#define ENCODER_2_B 14
#define ENCODER_3_A 15
#define ENCODER_3_B 16
#define TFT_DC 10 // only CS pin
#define TFT_CS 9 // using standard pin
#define TFT_RST 255 // no reset
#define TFT_TOUCH_IRQ 5
#define TFT_TOUCH_CS 6
#define LED_PIN 13
ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC, TFT_RST);
#else
// Optical Encoder connections
Encoder tune (16, 17);
Encoder filter (1, 2);
Encoder encoder3 (5, 4); //(26, 28);
Si5351 si5351;
#define MASTER_CLK_MULT 4 // QSD frontend requires 4x clock
#define BACKLIGHT_PIN 3
#define TFT_DC 20
#define TFT_CS 21
#define TFT_RST 32 // 255 = unused. connect to 3.3V
#define TFT_MOSI 7
#define TFT_SCLK 14
#define TFT_MISO 12
// pins for digital attenuator board PE4306
#define ATT_LE 24
#define ATT_DATA 25
#define ATT_CLOCK 28
// prop shield LC used for audio speaker amp
//#define AUDIO_AMP_ENABLE 39
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
// push-buttons
#define BUTTON_1_PIN 33
#define BUTTON_2_PIN 34
#define BUTTON_3_PIN 35
#define BUTTON_4_PIN 36
#define BUTTON_5_PIN 38 // this is the pushbutton pin of the tune encoder
#define BUTTON_6_PIN 0 // this is the pushbutton pin of the filter encoder
#define BUTTON_7_PIN 37 // this is the menu button pin
#define BUTTON_8_PIN 8 //27 // this is the pushbutton pin of encoder 3
Bounce button1 = Bounce(BUTTON_1_PIN, 50);
Bounce button2 = Bounce(BUTTON_2_PIN, 50);
Bounce button3 = Bounce(BUTTON_3_PIN, 50);
Bounce button4 = Bounce(BUTTON_4_PIN, 50);
Bounce button5 = Bounce(BUTTON_5_PIN, 50);
Bounce button6 = Bounce(BUTTON_6_PIN, 50);
Bounce button7 = Bounce(BUTTON_7_PIN, 50);
Bounce button8 = Bounce(BUTTON_8_PIN, 50);
#endif
Metro five_sec = Metro(2000); // Set up a Metro
Metro ms_500 = Metro(500); // Set up a Metro
Metro encoder_check = Metro(100); // Set up a Metro
//Metro dbm_check = Metro(25);
uint8_t wait_flag = 0;
#ifdef HARDWARE_DO7JBH
const uint8_t Band1 = 26; // band selection pins for LPF relays, used with 2N7000: HIGH means LPF is activated
const uint8_t Band2 = 27; // always use only one LPF with HIGH, all others have to be LOW
// not used
const uint8_t Band3 = 30;
const uint8_t Band4 = 57; // 29: > 5.4MHz
const uint8_t Band5 = 26; // LW
#endif
#ifdef HARDWARE_DD4WH
const uint8_t Band1 = 31; // band selection pins for LPF relays, used with 2N7000: HIGH means LPF is activated
const uint8_t Band2 = 30; // always use only one LPF with HIGH, all others have to be LOW
const uint8_t Band3 = 27;
const uint8_t Band4 = 29; // 29: > 5.4MHz
const uint8_t Band5 = 26; // LW
#endif
#ifdef USE_BOBS_FILTER
const uint8_t Band_3M5_7M3 = 31;
const uint8_t Band_7M3_15M = 28;
const uint8_t Band_15M_30M = 29;
#endif
// this audio comes from the codec by I2S
AudioInputI2S i2s_in;
AudioRecordQueue Q_in_L;
AudioRecordQueue Q_in_R;
#if defined(MP3)
AudioPlaySdMp3 playMp3;
AudioPlaySdAac playAac;
#endif
AudioMixer4 mixleft;
AudioMixer4 mixright;
AudioPlayQueue Q_out_L;
AudioPlayQueue Q_out_R;
AudioOutputI2S i2s_out;
#ifdef USE_W7PUA
// Added hardware: A pair of 100K resistors connected to the L&R input pins of the Codec.
// These two resisors are connected together on the other end and go through a 2.2nF cap
// to the A21 DAC pin on the Teensy 3.6.
AudioPlayQueue queueTP; // Output packets for Twin Peak test
AudioOutputAnalog dac1; // Generate 24 KHz signal
AudioConnection patchCord21(queueTP, 0, dac1, 0);
#endif
AudioConnection patchCord1(i2s_in, 0, Q_in_L, 0);
AudioConnection patchCord2(i2s_in, 1, Q_in_R, 0);
//AudioConnection patchCord3(Q_out_L, 0, i2s_out, 1);
//AudioConnection patchCord4(Q_out_R, 0, i2s_out, 0);
AudioConnection patchCord3(Q_out_L, 0, mixleft, 0);
AudioConnection patchCord4(Q_out_R, 0, mixright, 0);
#if defined(MP3)
AudioConnection patchCord5(playMp3, 0, mixleft, 1);
AudioConnection patchCord6(playMp3, 1, mixright, 1);
AudioConnection patchCord7(playAac, 0, mixleft, 2);
AudioConnection patchCord8(playAac, 1, mixright, 2);
#endif
AudioConnection patchCord9(mixleft, 0, i2s_out, 1);
AudioConnection patchCord10(mixright, 0, i2s_out, 0);
AudioControlSGTL5000 sgtl5000_1;
int idx_t = 0;
//int idx = 0;
int32_t sum;
float32_t mean;
int n_L;
int n_R;
long int n_clear;
//float32_t notch_amp[1024];
//float32_t FFT_magn[4096];
float32_t FFT_spec[256];
float32_t FFT_spec_old[256];
int16_t pixelnew[256];
int16_t pixelold[256];
float32_t LPF_spectrum = 0.2;
float32_t spectrum_display_scale = 30.0; // 30.0
uint8_t show_spectrum_flag = 1;
uint8_t display_S_meter_or_spectrum_state = 0;
uint8_t bitnumber = 16; // test, how restriction to twelve bit alters sound quality
int16_t spectrum_brightness = 255;
uint8_t spectrum_mov_average = 0;
uint16_t SPECTRUM_DELETE_COLOUR = ILI9341_BLACK;
uint16_t SPECTRUM_DRAW_COLOUR = ILI9341_WHITE;
#define SPECTRUM_ZOOM_MIN 0
#define SPECTRUM_ZOOM_1 0
#define SPECTRUM_ZOOM_2 1