summaryrefslogtreecommitdiff
path: root/map65/libm65/demod64a.f90
blob: 40160cab9f473baf44a4765d879a1a362fdfa0d5 (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
84
85
86
87
88
89
90
91
92
module demod64a_mod
  implicit none
contains  

subroutine demod64a(s3,nadd,mrsym,mrprob,mr2sym,mr2prob,ntest,nlow,mrs,mrs2)

!  Demodulate the 64-bin spectra for each of 63 symbols in a frame.

!  Parameters
!     nadd     number of spectra already summed
!     mrsym    most reliable symbol value
!     mr2sym   second most likely symbol value
!     mrprob   probability that mrsym was the transmitted value
!     mr2prob  probability that mr2sym was the transmitted value
!     mrs      most reliable symbol value (1-based, for deep search)
!     mrs2     second most likely symbol value (1-based, for deep search)
  use iso_fortran_env, only: real32, real64
  implicit none

  real(real32), intent(in)    :: s3(64,63)
  integer, intent(in)   :: nadd
  integer, intent(out)  :: mrsym(63), mrprob(63)
  integer, intent(out)  :: mr2sym(63), mr2prob(63)
  integer, intent(out)  :: ntest, nlow
  integer, intent(out)  :: mrs(63), mrs2(63)

  real(real64) fs(64)
  integer :: i,i1,i2,j
  real(real64) ::afac,scale,ave,s1,s2,p2,psum,x,fsum,p1,sum

  afac=1.1 * float(nadd)**0.64
  scale=255.999

!  Compute average spectral value
  sum=0.
  do j=1,63
     do i=1,64
        sum=sum+s3(i,j)
     enddo
  enddo
  ave=sum/(64.*63.)
  i1=1                                      !Silence warning
  i2=1

!  Compute probabilities for most reliable symbol values
  do j=1,63
     s1=-1.e30
     fsum=0.
     psum=0.
     do i=1,64
        x=min(afac*s3(i,j)/ave,50.d0)
        fs(i)=exp(x)
        fsum=fsum+fs(i)
        psum=psum + s3(i,j)
        if(s3(i,j).gt.s1) then
           s1=s3(i,j)
           i1=i                              !Most reliable
        endif
     enddo

     s2=-1.e30
     do i=1,64
        if(i.ne.i1 .and. s3(i,j).gt.s2) then
           s2=s3(i,j)
           i2=i                              !Second most reliable
        endif
     enddo
!     p1=fs(i1)/fsum                          !Normalized probabilities
!     p2=fs(i2)/fsum
     p1=s1/psum
     p2=s2/psum
     mrsym(j)=i1-1
     mr2sym(j)=i2-1
     mrprob(j)=scale*p1
     mr2prob(j)=scale*p2
     mrs(j)=i1
     mrs2(j)=i2
  enddo
  
  sum=0.
  nlow=0
  do j=1,63
     sum=sum+mrprob(j)
     if(mrprob(j).le.5) nlow=nlow+1
  enddo
!  ntest=sum/63
  ntest=sum

  return
end subroutine demod64a

end module demod64a_mod