-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcwdetect.c
4447 lines (4253 loc) · 117 KB
/
cwdetect.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
// Copyright (c) <2012> <Leif Asbrink>
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
#include "osnum.h"
#include "globdef.h"
#include "uidef.h"
#include "sigdef.h"
#include "screendef.h"
#include "fft1def.h"
#include "options.h"
#include "llsqdef.h"
#include "fft3def.h"
#define MIN_DASHNO 8
#define CW_CORRELATE_GOODLEN 25
#define MIN_CORRELATE_SEP 70
#define MAX_CORRSUM 10
#define OVFL_PROTECT 0.00000000000000001
#define MORSE_CLOCK_COH_TIME 16
#define ZZ 0.000001
// name data length
// CW_DASH |---_| 4
// CW_DOT |-_| 2
// CW_SPACE |__| 2
// CW_WORDSEP |____| 4
void check_cw(int num,int type);
void first_morse_decode(void);
void continued_morse_decode(void);
void show_cw(char *caller);
// *********************************************************
// *********************************************************
// These defines allow a lot of information to be written to dmp
// define DUMPFILE 1 in main.c to use this.
#if DUMPFILE == TRUE
#define PR00 0
#define PR01 0
#define PR02 1
#define PR03 0
#define PR04 0
#define PR05 0
#define PR06 0
#define PR07 0
#define PR08 0
#define PR09 0
#define PR10 0
#else
#define PR00 0
#define PR01 0
#define PR02 0
#define PR03 0
#define PR04 0
#define PR05 0
#define PR06 0
#define PR07 0
#define PR08 0
#define PR09 0
#define PR10 0
#endif
#define PRT00 if(PR00 != 0)DEB
#define PRT01 if(PR01 != 0)DEB
#define PRT02 if(PR02 != 0)DEB
#define PRT03 if(PR03 != 0)DEB
#define PRT04 if(PR04 != 0)DEB
#define PRT05 if(PR05 != 0)DEB
#define PRT06 if(PR06 != 0)DEB
#define PRT07 if(PR07 != 0)DEB
#define PRT08 if(PR08 != 0)DEB
#define PRT09 if(PR09 != 0)DEB
#define PRT10 if(PR10 != 0)DEB
// *********************************************************
// *********************************************************
// float[2] baseb_out=data to send to loudspeaker
// float[2] baseb=complex amplitude of first level coherent data.
// float[2] baseb_raw=complex amplitude of baseband signal. Raw data, pol. adapted.
// float[2] baseb_raw_orthog=complex amplitude of pol. orthog signal. Raw data.
// float[2] baseb_carrier=phase of carrier. Complex data, cosines and sines.
// float[1] baseb_carrier_ampl=amplitude of carrier
// float[1] baseb_totpwr=total power of baseb_raw
// float[2] baseb_envelope=complex amplitude from fitted dots and dashes.
// float[1] baseb_upthreshold=forward peak detector for power
// float[1] baseb_threshold=combined forward and backward peak detector at -6dB
// float[2] baseb_fit=fitted dots and dashes.
// float[2] baseb_tmp=array for intermediate data in complex format
// float[2] baseb_sho1=array for showing intermediate data in complex format
// float[2] baseb_sho2=array for showing intermediate data in complex format
// float[1] baseb_agc_level=used only when AGC is enabled.
// short int[1] baseb_ramp=indicator for time of power above/below threshold.
// short_int[1] baseb_clock=CW code clock
// float[2] baseb_tmp=for debug during development
// baseb_pa The starting point of the latest mix2.size/2 points.
// baseb_pb The point up to which thresholds exist.
// baseb_pc The point up to which cw speed statistics and ramp is collected.
// baseb_pd A key up region close before baseb_pc
// baseb_pe The point up to which we have run first detect.
// baseb_pf
// baseb_px The oldest point that contains valid data.
int best_corr_ia;
int best_corr_ib;
float best_corr_ampl;
int corr_len;
int no_of_corr;
int msig_ptr;
float msig_signal;
float msig_noise;
#define MAX_GUESS_DATA 100
typedef struct {
float pos;
float re;
float im;
float noise;
int wei;
} GUESS_DATA;
GUESS_DATA msig[MAX_GUESS_DATA];
typedef struct {
int pos;
int sep;
float fpos;
float val;
} CORR_INFO;
CORR_INFO corr[MAX_CORRSUM];
CORR_INFO tmpcorr;
void show_msig(int ic)
{
int k;
for(k=0; k<ic; k++)
{
fprintf( dmp,"\nmsig[%d] [%.1f]=%f, (%f,%f) W=%d",k,msig[k].pos,
msig[k].pos/cwbit_pts,ZZ*msig[k].re,ZZ*msig[k].im,msig[k].wei);
}
}
void fit_msig(void)
{
int i, k, m;
float t1, r1, re, im, midpoint;
// The structure msig[0] to msig[msig_ptr-1] contains complex amplitudes
// that are hopefully reasonably well synchronized to the Morse symbol clock.
// msig[].wei is > 0 for those points that the current guess assumes
// the key is pressed. Fit a slowly varying complex amplitude to
// all points with wei > 0.
// The time interval contained in msig is typically 30 ticks of the
// morse symbol clock.
// Over a short time like this, the complex amplitude of the
// carrier can not change very much so we should only fit a
// small number of parameters to describe it.
t1=msig[msig_ptr-1].pos-msig[0].pos;
if(t1<0)t1+=baseband_size;
t1/=2;
midpoint=msig[0].pos+t1;
if(midpoint >= baseband_size)midpoint+=baseband_size;
t1=sqrt(t1);
llsq_neq=0;
for(i=0; i<msig_ptr; i++)
{
if(msig[i].wei > 0)llsq_neq++;
}
llsq_errors=fftn_tmp;
llsq_derivatives=&llsq_errors[2*llsq_neq];
k=0;
for(i=0; i<msig_ptr; i++)
{
if(msig[i].wei > 0)
{
llsq_errors[2*k ]=msig[i].re*msig[i].wei;
llsq_errors[2*k+1]=msig[i].im*msig[i].wei;
llsq_derivatives[k]=t1*msig[i].wei;
r1=msig[i].pos-midpoint;
if(r1<-(baseband_size>>1))r1+=baseband_size;
llsq_derivatives[llsq_neq+k]=r1*msig[i].wei;
llsq_derivatives[2*llsq_neq+k]=r1*r1*msig[i].wei;
llsq_derivatives[3*llsq_neq+k]=r1*r1*r1*msig[i].wei/t1;
k++;
}
}
if(3*llsq_neq+k > 2*fftn_tmp_size)
{
lirerr(93657);
return;
}
llsq_npar=4;
if(llsq2() != 0)
{
lirerr(93610);
return;
}
// compute signal and noise
msig_noise=0;
msig_signal=0;
k=0;
m=0;
for(i=0; i<msig_ptr; i++)
{
if(msig[i].wei > 0)
{
llsq_errors[2*k ]=msig[i].re*msig[i].wei;
llsq_errors[2*k+1]=msig[i].im*msig[i].wei;
llsq_derivatives[k]=t1*msig[i].wei;
r1=msig[i].pos-midpoint;
if(r1<-(baseband_size>>1))r1+=baseband_size;
re=t1*llsq_steps[0]+r1*llsq_steps[2]+r1*r1*llsq_steps[4]+r1*r1*r1*llsq_steps[6]/t1;
im=t1*llsq_steps[1]+r1*llsq_steps[3]+r1*r1*llsq_steps[5]+r1*r1*r1*llsq_steps[7]/t1;
msig_signal+=msig[i].wei*(re*re+im*im);
re-=msig[i].re;
im-=msig[i].im;
// Dashes integrate 3 times longer so their noise is sqrt(3) smaller.
// Take that into account.
// A region with noise only would give Ndots=n1*n1+n2*n2+n3*n3 when
// evaluated as a dot (assume n1=n2=n3 on the average) so the
// noise contribution from a false dash would be Ndots=3*n*n.
// When evaluated as a dash, Ndash=3*n4*n4, because we have three parts
// with the same complex amplitude n4. n4 is n/sqrt(3) on the average
// because of the longer integration time so when evaluating as a
// dash we would get 3 times less noise when there is no signal.
// Therefore, multiply the contribution to msig_noise by 9 for a dash.
re*=msig[i].wei;
im*=msig[i].wei;
msig_noise+=msig[i].wei*(re*re+im*im);
m+=msig[i].wei;
k++;
}
else
{
if(msig[i].wei == 0)msig_noise+=msig[i].noise;
}
}
msig_signal=sqrt(msig_signal/m);
msig_noise=sqrt(msig_noise/msig_ptr);
}
void store_msig(int k)
{
int j;
switch (cw[k].type)
{
case CW_DOT:
// CW_DOT |-_| 2
// ^
msig[msig_ptr].pos=cw[k].midpoint;
msig[msig_ptr].wei=1;
msig[msig_ptr].re=cw[k].raw_re;
msig[msig_ptr].im=cw[k].raw_im;
msig_ptr++;
cg_wave_midpoint=cw[k].midpoint+cwbit_pts;
if(cg_wave_midpoint >= baseband_size)cg_wave_midpoint-=baseband_size;
msig[msig_ptr].pos=cg_wave_midpoint;
wb_investigate_region(cg_wave_midpoint, 1);
msig[msig_ptr].re=reg_dot_re[0];
msig[msig_ptr].im=reg_dot_im[0];
msig[msig_ptr].noise=reg_dot_re[0]*reg_dot_re[0]+reg_dot_im[0]*reg_dot_im[0];
msig[msig_ptr].wei=0;
msig_ptr++;
break;
case CW_SPACE:
// CW_SPACE |__| 2
// ^
cg_wave_midpoint=cw[k].midpoint;
msig[msig_ptr].pos=cg_wave_midpoint;
wb_investigate_region(cg_wave_midpoint, 1);
msig[msig_ptr].re=reg_dot_re[0];
msig[msig_ptr].im=reg_dot_im[0];
msig[msig_ptr].noise=reg_dot_re[0]*reg_dot_re[0]+reg_dot_im[0]*reg_dot_im[0];
msig[msig_ptr].wei=0;
msig_ptr++;
cg_wave_midpoint+=cwbit_pts;
if(cg_wave_midpoint >= baseband_size)cg_wave_midpoint-=baseband_size;
msig[msig_ptr].pos=cg_wave_midpoint;
wb_investigate_region(cg_wave_midpoint, 1);
msig[msig_ptr].re=reg_dot_re[0];
msig[msig_ptr].im=reg_dot_im[0];
msig[msig_ptr].noise=reg_dot_re[0]*reg_dot_re[0]+reg_dot_im[0]*reg_dot_im[0];
msig[msig_ptr].wei=0;
msig_ptr++;
break;
case CW_DASH:
// CW_DASH |---_| 4
// ^
msig[msig_ptr].pos=cw[k].midpoint-cwbit_pts;
msig[msig_ptr].wei=0;
msig[msig_ptr].re=0;
msig[msig_ptr].im=0;
msig[msig_ptr].noise=0;
msig_ptr++;
msig[msig_ptr].pos=cw[k].midpoint;
msig[msig_ptr].wei=3;
msig[msig_ptr].re=cw[k].raw_re;
msig[msig_ptr].im=cw[k].raw_im;
msig_ptr++;
msig[msig_ptr].pos=cw[k].midpoint+cwbit_pts;
cg_wave_midpoint=msig[msig_ptr].pos;
msig[msig_ptr].wei=0;
msig[msig_ptr].re=0;
msig[msig_ptr].im=0;
msig[msig_ptr].noise=0;
msig_ptr++;
cg_wave_midpoint+=cwbit_pts;
if(cg_wave_midpoint >= baseband_size)cg_wave_midpoint-=baseband_size;
msig[msig_ptr].pos=cg_wave_midpoint;
wb_investigate_region(cg_wave_midpoint, 1);
msig[msig_ptr].re=reg_dot_re[0];
msig[msig_ptr].im=reg_dot_im[0];
msig[msig_ptr].noise=reg_dot_re[0]*reg_dot_re[0]+reg_dot_im[0]*reg_dot_im[0];
msig[msig_ptr].wei=0;
msig_ptr++;
break;
case CW_WORDSEP:
// CW_WORDSEP |____| 4
// ^
cg_wave_midpoint=cw[k].midpoint;
wb_investigate_region(cg_wave_midpoint, 3);
cg_wave_midpoint-=cwbit_pts;
if(cg_wave_midpoint < 0)cg_wave_midpoint+=baseband_size;
for(j=0; j<3; j++)
{
msig[msig_ptr].pos=cg_wave_midpoint;
msig[msig_ptr].re=reg_dot_re[j];
msig[msig_ptr].im=reg_dot_im[j];
msig[msig_ptr].noise=reg_dot_re[j]*reg_dot_re[j]+reg_dot_im[j]*reg_dot_im[j];
msig[msig_ptr].wei=0;
msig_ptr++;
cg_wave_midpoint+=cwbit_pts;
if(cg_wave_midpoint >= baseband_size)cg_wave_midpoint-=baseband_size;
}
msig[msig_ptr].pos=cg_wave_midpoint;
wb_investigate_region(cg_wave_midpoint, 1);
msig[msig_ptr].re=reg_dot_re[0];
msig[msig_ptr].im=reg_dot_im[0];
msig[msig_ptr].noise=reg_dot_re[0]*reg_dot_re[0]+reg_dot_im[0]*reg_dot_im[0];
msig[msig_ptr].wei=0;
msig_ptr++;
break;
default:
lirerr(874529);
break;
}
}
void fill_known_high_msig(void)
{
int k, dist;
// ptr points to a decoded Morse part in the CW array.
// Step downwards through decoded parts until the first
// undecoded is encountered or until the total distance is
// 10 parts.
show_cw("----- fill high guesses -----");
dist=0;
k=cw_ptr+1;
if(cw[k].unkn == 0)lirerr(8835263);
store_msig(k);
k++;
while(k<no_of_cwdat && cw[k].unkn == 0 && dist < 10)
{
dist+=cw[k].len;
store_msig(k);
k++;
}
}
void fill_known_low_msig(void)
{
int k, dist;
// ptr points to a decoded Morse part in the CW array.
// Step downwards through decoded parts until the first
// undecoded is encountered or until the total distance is
// 10 parts.
dist=0;
k=cw_ptr;
while(k>0 && cw[k].unkn == 0 && dist < 10)
{
dist+=cw[k].len;
k--;
}
// fill known info into msig.
while(k<cw_ptr || cw[k].unkn == 0)
{
store_msig(k);
k++;
}
}
char check_hguess3(int val, int bits, int charval_dwn_rev, int charbits_dwn)
{
int i, m, charbits, charval;
charbits=bits+charbits_dwn+2;
if(charbits > 6)return FALSE;
charval=val<<2;
m=charval_dwn_rev;
for(i=0; i<charbits_dwn; i++)
{
charval<<=1;
charval+=m&1;
m>>=1;
}
if(morsascii[charbits-1][charval] == 242 ||
morsascii[charbits-1][charval] == 243)return FALSE;
return TRUE;
}
char check_hguess4(int val, int bits, int charval_dwn_rev, int charbits_dwn)
{
int i, m, charbits, charval;
charbits=bits+charbits_dwn+1;
if(charbits > 6)return FALSE;
charval=val<<1;
charval++;
m=charval_dwn_rev;
for(i=0; i<charbits_dwn; i++)
{
charval<<=1;
charval+=m&1;
m>>=1;
}
if(morsascii[charbits-1][charval] == 242 ||
morsascii[charbits-1][charval] == 243)return FALSE;
return TRUE;
}
char check_hguess3a(int val, int bits, int charval_dwn_rev, int charbits_dwn)
{
int i, m, charbits, charval;
charbits=bits+charbits_dwn+1;
if(charbits > 6)return FALSE;
charval=val<<1;
m=charval_dwn_rev;
for(i=0; i<charbits_dwn; i++)
{
charval<<=1;
charval+=m&1;
m>>=1;
}
if(morsascii[charbits-1][charval] == 242 ||
morsascii[charbits-1][charval] == 243)return FALSE;
return TRUE;
}
char check_hguess4a(int val, int bits, int charval_dwn_rev, int charbits_dwn)
{
int i, m, charbits, charval;
charbits=bits+charbits_dwn;
if(charbits > 6)return FALSE;
charval=val;
m=charval_dwn_rev;
for(i=0; i<charbits_dwn; i++)
{
charval<<=1;
charval+=m&1;
m>>=1;
}
if(morsascii[charbits-1][charval] == 242 ||
morsascii[charbits-1][charval] == 243)return FALSE;
return TRUE;
}
int part_guesses(void)
{
// Look for long undecoded regions where a decision might be certain
// enough based on an extrapolation from the surrounding decoded
// regions.
return 0;
}
int character_guesses(void)
{
char lguess_flag[7],hguess_flag[5],guess_flag[7][5];
int charval_up, charbits_up;
int charval_dwn, charval_dwn_rev, charbits_dwn;
int added_parts;
int i, j, k, m, ia, ib, ic, gap, iptr;
int nlg, nhg, mlg, mhg;
int val, bits;
float r1, t1;
float dash_re[3], dash_im[3];
float dot_re[20], dot_im[20],dot_pwr[20];
// We have dashes and hopefully some dots and separators.
// Look for a space or a word separator and try to decode upwards.
show_cw(" char guesses");
cw_ptr=0;
added_parts=0;
region_guesses:;
show_cw(" char guesses");
while(cw_ptr < no_of_cwdat &&
cw[cw_ptr].type != CW_WORDSEP &&
cw[cw_ptr].type != CW_SPACE)cw_ptr++;
fprintf( dmp,"\nCHARGUESS cw_ptr=%d",cw_ptr);
if(cw_ptr < no_of_cwdat)
{
// We have a separator here!
// Check for an undecoded region above it.
iptr=cw_ptr+1;
while(iptr < no_of_cwdat && cw[iptr].unkn == 0)
{
if( cw[iptr].type == CW_WORDSEP || cw[iptr].type == CW_SPACE)cw_ptr=iptr;
iptr++;
}
fprintf( dmp,"\ncw_ptr %d iptr %d",cw_ptr,iptr);
if(iptr < no_of_cwdat-1)
{
charval_up=0;
charbits_up=0;
k=cw_ptr;
k++;
while(k < iptr)
{
charbits_up++;
charval_up<<=1;
if(cw[k].type==CW_DASH)charval_up++;
k++;
}
gap=cw[iptr].unkn;
fprintf( dmp,"\ngap=%d iptr %d",gap,iptr);
fprintf( dmp,"\nbits %d val %d %c", charbits_up,charval_up,
morsascii[charbits_up-1][charval_up]);
if(charbits_up > 6 ||
morsascii[charbits_up-1][charval_up] == 243 ||
gap >= 16 ||
gap <= 4 ||
cw[iptr].type == CW_WORDSEP ||
cw[iptr].type == CW_SPACE)
{
cw_ptr=iptr;
fprintf( dmp,"\nERROR below");
// This is an error. Do nothing for now.
goto region_guesses;
}
// We here have a sequence like this:
// known-_??????????????_-known
// gap is the length of the undecoded region and it has to be
// an even number.
msig_ptr=0;
// This is a small gap. Use the information from decoded dots and
// dashes from both sides of the region.
cw_ptr=iptr-1;
fill_known_low_msig();
ia=msig_ptr;
msig_ptr+=gap;
ib=msig_ptr;
fill_known_high_msig();
ic=msig_ptr;
cg_wave_midpoint=msig[ib].pos-cwbit_pts;
ib--;
if(cg_wave_midpoint < 0)cg_wave_midpoint+=baseband_size;
wb_investigate_region(cg_wave_midpoint, 1);
msig[ib].pos=cg_wave_midpoint;
msig[ib].re=reg_dot_re[0];
msig[ib].im=reg_dot_im[0];
msig[ib].noise=reg_dot_re[0]*reg_dot_re[0]+reg_dot_im[0]*reg_dot_im[0];
msig[ib].wei=0;
// We will fill in different guesses in the region ia to ib.
// First we fill in what is known above the undecoded region.
// note that we do not trust the symbol clock speed very much -
// we only use it 4 positions into the unknown region from
// either side. The operator could use hand keying and
// the length of dots, dashes and separators could be
// deviating from the nominal values.
t1=msig[ia-1].pos;
for(k=ia; k<ib; k++)
{
t1+=cwbit_pts;
if(t1>=baseband_size)t1-=baseband_size;
msig[k].pos=t1;
msig[k].re=0;
msig[k].im=k;
msig[k].wei=-1;
}
ib--;
t1=cg_wave_midpoint;
k=1+(ia+ib)/2;
t1-=(ib-k+2)*cwbit_pts;
if(t1<0)t1+=baseband_size;
while(k<=ib)
{
t1+=cwbit_pts;
if(t1>=baseband_size)t1-=baseband_size;
msig[k].pos=t1;
k++;
}
// The Morse code goes in units of 2 or 4.
// The region has key-down at both ends so we know that the first
// pand the last points in the gap region must be key-up.
// Here are all possible guesses for the beginning:
// ****-_____?????_-**** lguess 0
// ****-___-_?????_-**** lguess 1
// ****-_-___?????_-**** lguess 2
// ****-_-_-_?????_-**** lguess 3
// ****-_---_?????_-**** lguess 4
// ****-___--?????_-**** lguess 5
// ****-_-_--?????_-**** lguess 6
// ia^
// 0123
//
// Here are all possible guesses for the end:
// ****-_?????____-**** hguess 0
// ****-_?????-___-**** hguess 1
// ****-_?????__-_-**** hguess 2
// ****-_?????-_-_-**** hguess 3
// ****-_?????---_-**** hguess 4
// ib^
// 210
//
// In all we have 35 guesses, but many of them are likely
// to be impossible since the decoded data would not fit
// the Morse alphabet.
for(nlg=0; nlg<7; nlg++)lguess_flag[nlg]=TRUE;
for(nhg=0; nhg<5; nhg++)hguess_flag[nhg]=TRUE;
// Fill in acceptable guesses in lguess_flag.
if(charbits_up == 6)
{
// This is the maximum length for a Morse character.
// Skip if not legal, force key-up otherwise.
if(morsascii[charbits_up-1][charval_up] == 243)
{
fprintf( dmp,"\nERROR above");
goto skip_guesses;
}
lguess_flag[2]=FALSE;
lguess_flag[3]=FALSE;
lguess_flag[4]=FALSE;
lguess_flag[6]=FALSE;
}
else
{
if(charbits_up == 5)
{
// Skip guesses that would add more than one part.
lguess_flag[3]=FALSE;
lguess_flag[6]=FALSE;
// In case the character is not legal, but adding one part
// could make it legal, skip guesses that do not add one part.
if(morsascii[charbits_up-1][charval_up] == 242)
{
lguess_flag[0]=FALSE;
lguess_flag[1]=FALSE;
lguess_flag[5]=FALSE;
}
// Check whether adding a dot would give a legal character.
if(morsascii[charbits_up][charval_up<<1] == 243)
{
lguess_flag[2]=FALSE;
}
// Check whether adding a dash would give a legal character.
if(morsascii[charbits_up][(charval_up<<1)+1] == 243)
{
lguess_flag[4]=FALSE;
}
}
else
{
if(charbits_up == 4)
{
if(morsascii[charbits_up][charval_up<<1] == 243)
{
lguess_flag[2]=FALSE;
}
if(morsascii[charbits_up+1][charval_up<<2] == 243)
{
lguess_flag[3]=FALSE;
}
if(morsascii[charbits_up][(charval_up<<1)+1] == 243)
{
lguess_flag[4]=FALSE;
}
if(morsascii[charbits_up+1][(charval_up<<2)+1] == 243)
{
lguess_flag[6]=FALSE;
}
}
}
}
// Look for a separator upwards.
k=iptr+1;
while( k < no_of_cwdat &&
cw[k].unkn==0 &&
cw[k].type != CW_WORDSEP &&
cw[k].type != CW_SPACE)k++;
if( k == no_of_cwdat || cw[k].unkn != 0)goto skip_guesses;
// There is a known separator above the current region.
// Decode it downwards.
charbits_dwn=0;
charval_dwn_rev=0;
while(k > iptr)
{
charval_dwn_rev<<=1;
k--;
if(cw[k].type==CW_DASH)charval_dwn_rev++;
charbits_dwn++;
}
if(charbits_dwn > 6)goto skip_guesses;
k=0;
m=charval_dwn_rev;
for(i=0; i<charbits_dwn; i++)
{
k<<=1;
k+=m&1;
m>>=1;
}
charval_dwn=k;
if(charbits_dwn==6)
{
if(morsascii[charbits_dwn-1][charval_dwn] == 243)
{
goto skip_guesses;
}
hguess_flag[2]=FALSE;
hguess_flag[3]=FALSE;
hguess_flag[4]=FALSE;
}
else
{
if(charbits_dwn==5)
{
hguess_flag[3]=FALSE;
if(morsascii[charbits_dwn-1][charval_dwn] == 243)
{
hguess_flag[0]=FALSE;
hguess_flag[1]=FALSE;
}
if(morsascii[charbits_dwn][charval_dwn] == 243)
{
hguess_flag[2]=FALSE;
}
if(morsascii[charbits_dwn][charval_dwn+32] == 243)
{
hguess_flag[4]=FALSE;
}
}
else
{
if(charbits_dwn==4)
{
if(morsascii[charbits_dwn+1][charval_dwn] == 243)
{
hguess_flag[3]=FALSE;
}
}
}
}
for(nlg=0;nlg<7;nlg++)
{
for(nhg=0;nhg<5;nhg++)
{
guess_flag[nlg][nhg]=lguess_flag[nlg]&hguess_flag[nhg];
}
}
if(gap == 8)
{
// The regions touch each other.
// We can not combine lguesses 5 and 6 with hguesses 0,2 or 4
for(i=5; i<=6; i++)
{
for(j=0; j<=4; j+=2)
{
guess_flag[i][j]=FALSE;
}
}
// When we combine lguesses and hguesses that both contain no
// space or word separator, the entire region has to be a valid
// Morse code.
// We have to check l=3,4,6 combined with h=3,4
// **************************************************
// lguess=3
val=charval_up<<2;
bits=charbits_up+2;
guess_flag[3][3]=check_hguess3(val, bits, charval_dwn_rev, charbits_dwn);
guess_flag[3][4]=check_hguess4(val, bits, charval_dwn_rev, charbits_dwn);
// lguess=4
val=(charval_up<<1)+1;
bits=charbits_up+1;
guess_flag[4][3]=check_hguess3(val, bits, charval_dwn_rev, charbits_dwn);
guess_flag[4][4]=check_hguess4(val, bits, charval_dwn_rev, charbits_dwn);
// lguess=6
val=(charval_up<<2)+1;
bits=charbits_up+2;
guess_flag[6][3]=check_hguess3a(val, bits, charval_dwn_rev, charbits_dwn);
}
else
{
if(gap == 6)
{
// Two positions overlap. Make all combinations that have
// different patterns in the overlapping region illegal.
guess_flag[0][1]=FALSE;
guess_flag[0][3]=FALSE;
guess_flag[0][4]=FALSE;
guess_flag[1][0]=FALSE;
guess_flag[1][2]=FALSE;
guess_flag[1][4]=FALSE;
guess_flag[2][1]=FALSE;
guess_flag[2][3]=FALSE;
guess_flag[2][4]=FALSE;
guess_flag[3][0]=FALSE;
guess_flag[3][2]=FALSE;
guess_flag[3][4]=FALSE;
guess_flag[4][0]=FALSE;
guess_flag[4][2]=FALSE;
guess_flag[4][4]=FALSE;
guess_flag[5][0]=FALSE;
guess_flag[5][1]=FALSE;
guess_flag[5][2]=FALSE;
guess_flag[5][3]=FALSE;
guess_flag[6][0]=FALSE;
guess_flag[6][1]=FALSE;
guess_flag[6][2]=FALSE;
guess_flag[6][3]=FALSE;
// When we combine lguesses and hguesses without any space
// or word separator, the entire region has to be a valid
// Morse code.
// We have to check lguess=3,4,6 when combined with hguess=2,3,4
// **************************************************
// lguess=3: only hguess=3 is possible.
val=charval_up<<1;
bits=charbits_up+1;
guess_flag[3][3]=check_hguess3(val, bits, charval_dwn_rev, charbits_dwn);
// lguess=4: only hguess=3 is possible.
val=(charval_up<<1)+1;
bits=charbits_up+1;
guess_flag[4][3]=check_hguess3a(val, bits, charval_dwn_rev, charbits_dwn);
// lguess=6: only hguess=4 is possible.
val=(charval_up<<2)+1;
bits=charbits_up+2;
guess_flag[6][4]=check_hguess4a(val, bits, charval_dwn_rev, charbits_dwn);
}
}
fprintf( dmp,"\nib-ia %d, gap %d",ib-ia,gap);
fprintf( dmp,"\nia %d ib %d",ia,ib);
for(k=1; k<msig_ptr; k++)
fprintf( dmp,"\n%d %f (%f,%f) %d %f",k,msig[k].pos,ZZ*msig[k].re,ZZ*msig[k].im,msig[k].wei,msig[k].pos-msig[k-1].pos);
for(k=0; k<7; k++)fprintf( dmp,"\nlguess_flag[%d] %d",k,lguess_flag[k]);
for(k=0; k<5; k++)fprintf( dmp,"\nhguess_flag[%d] %d",k,hguess_flag[k]);
fprintf( dmp,"\n");
for(i=0; i<7; i++)
{
fprintf( dmp,"\n");
for(j=0; j<5; j++)fprintf( dmp,"[%d][%d] %d ",i,j,guess_flag[i][j]);
}
fprintf( dmp,"\n");
show_msig(ic);
// *****************************************************************
// Prepare to set up msig for all allowed guesses.
// Dashes may be placed at ia+1 and at ib-2.
cg_wave_midpoint=msig[ia+1].pos;
wb_investigate_region(cg_wave_midpoint, -5);
dash_re[0]=reg_dash_re;
dash_im[0]=reg_dash_im;
cg_wave_midpoint=msig[ia+3].pos;
wb_investigate_region(cg_wave_midpoint, -5);
dash_re[1]=reg_dash_re;
dash_im[1]=reg_dash_im;
cg_wave_midpoint=msig[ib-2].pos;
wb_investigate_region(cg_wave_midpoint, -5);
dash_re[2]=reg_dash_re;
dash_im[2]=reg_dash_im;
for(k=ia; k<ib; k++)
{
cg_wave_midpoint=msig[k].pos;
wb_investigate_region(cg_wave_midpoint, 1);
dot_re[k-ia]=reg_dot_re[0];
dot_im[k-ia]=reg_dot_im[0];
dot_pwr[k-ia]=reg_dot_re[0]*reg_dot_re[0]+reg_dot_im[0]*reg_dot_im[0];
}
// Fill in all allowed guesses in msig one by one.
// Fit a smooth function to the carrier at those points where there
// is a key down according to the current guess.
// Compute the associated RMS error (noise) and average carrier
// amplitude (signal.) Remember S and N for each valid guess.
r1=0;
mlg=-1;
mhg=-1;
for(nlg=0; nlg<7; nlg++)
{
if(lguess_flag[nlg] == TRUE)
{
for(k=0; k<4; k++)
{
msig[ia+k].wei=0;
msig[ia+k].re=dot_re[k];
msig[ia+k].im=dot_im[k];
msig[ia+k].noise=dot_pwr[k];
}
switch (nlg)
{
case 0:
// ****-_____?????_-**** lguess 0
// ia^
// 0123
break;
case 1:
// ****-___-_?????_-**** lguess 1
// ia^
// 0123
msig[ia+2].wei=1;
break;
case 2:
// ****-_-___?????_-**** lguess 2
// ia^
// 0123
msig[ia].wei=1;
break;
case 3:
// ****-_-_-_?????_-**** lguess 3
// ia^
// 0123
msig[ia].wei=1;
msig[ia+2].wei=1;
break;
case 4:
// ****-_---_?????_-**** lguess 4
// ia^
// 0123
msig[ia ].wei=0;
msig[ia ].re=0;
msig[ia ].im=0;
msig[ia ].noise=0;
msig[ia+1].wei=3;
msig[ia+1].re=dash_re[0];
msig[ia+1].im=dash_im[0];
msig[ia+1].noise=dash_re[0]*dash_re[0]+dash_im[0]*dash_im[0];
msig[ia+2].wei=0;
msig[ia+2].re=0;
msig[ia+2].im=0;
msig[ia+2].noise=0;
break;
case 5:
// ****-___--?????_-**** lguess 5
// ia^
// 0123
msig[ia+2].wei=1;
msig[ia+3].wei=1;
break;
case 6:
// ****-_-_--?????_-**** lguess 6
// ia^
// 0123
msig[ia ].wei=1;
msig[ia+2].wei=1;
msig[ia+3].wei=1;
break;
}
for(nhg=0; nhg<5; nhg++)
{
if(guess_flag[nlg][nhg]==TRUE)
{
for(k=ib-ia-3; k<ib-ia; k++)
{
msig[ia+k].wei=0;
msig[ia+k].re=dot_re[k];
msig[ia+k].im=dot_im[k];