summaryrefslogtreecommitdiff
path: root/map65/libm65/s3avg.f90
blob: 5a21c31f503ea0264cdc6e4dd7df861331bf5ed6 (plain)
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
module s3avg_mod
  implicit none
contains

subroutine s3avg(nsave, mode65, nutc, nhz, xdt, npol, nflip, ntol, s3, nsum, nkv, decoded)
  use extract_mod
  implicit none

! Save the current synchronized spectra, s3(64,63), for possible
! decoding of average.

  integer,      intent(inout) :: nsave
  integer,      intent(in)    :: mode65, nutc, nhz, npol, nflip, ntol
  real,         intent(in)    :: xdt
  real,         intent(in)    :: s3(64,63)   !Synchronized spectra for 63 symbols
  integer,      intent(out) :: nsum, nkv
  character(len=22), intent(out) :: decoded

  real s3a(64,63,64)                    !Saved spectra
  real s3b(64,63)                       !Average spectra
  integer iutc(64),ihz(64),ipol(64),iflip(64)
  integer :: i,ihzdiff,nadd,ncount,nhist
  integer :: mrs(63), mrs2(63)          !Dummy arguments for extract
  real, parameter :: dtdiff=0.2
  real dt(64)
  logical ltext,first
  data first/.true./
  save first,iutc,ihz,ipol,iflip,s3a,dt

  nsum=0
  nkv=0
  decoded='                      '
  ihzdiff=min(100,ntol)

  if(first) then
     iutc=-1
     ihz=0
     ipol=0
     iflip=0
     first=.false.
  endif

  do i=1,64
     if(nutc.eq.iutc(i) .and. abs(nhz-ihz(i)).le.ihzdiff) then
        nsave=mod(nsave-2+64,64)+1
        go to 10
     endif
  enddo
  
  iutc(nsave)=nutc                          !Save UTC
  ihz(nsave)=nhz                            !Save freq in Hz
  ipol(nsave)=npol                          !Save pol
  iflip(nsave)=nflip
  dt(nsave)=xdt                             !Save DT
  s3a(1:64,1:63,nsave)=s3                   !Save the spectra

10 s3b=0.
  do i=1,64                                 !Accumulate avg spectra
     if(iutc(i).lt.0) cycle
     if(mod(iutc(i),2).ne.mod(nutc,2)) cycle !Use only same sequence
     if(abs(nhz-ihz(i)).gt.ihzdiff) cycle   !Freq must match
     if(abs(xdt-dt(i)).gt.dtdiff) cycle     !DT must match
     ! Opposite sync polarities map s3 columns to complementary symbol positions.
     if(iflip(i).eq.0) cycle
     if(nflip.ne.iflip(i)) cycle
     s3b=s3b + s3a(1:64,1:63,i)
     nsum=nsum+1
  enddo
 
  if(nsum.ge.2) then                        !Try decoding the sverage
     nadd=mode65*nsum
     call extract(s3b,nadd,ncount,nhist,decoded,ltext,mrs,mrs2)     !Extract the message
     nkv=nsum
     if(ncount.lt.0) then 
        nkv=0
        decoded='                      '
     endif
  endif

  return
end subroutine s3avg

end module s3avg_mod