summaryrefslogtreecommitdiff
path: root/map65/libm65/symspec.f90
blob: abab5ef07e0b1220d0813199cce9535b8885d0fd (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
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
module symspec_mod
  implicit none
contains

subroutine symspec(k,nxpol,ndiskdat,nb,nbslider,idphi,    &
     iqadjust,iqapply,gainx,gainy,phasex,phasey,rejectx,rejecty,  &
     pxdb,pydb,ssz5a,nkhz,ihsym,nzap,slimit,lstrong) bind(C, name='symspec_')

  use iso_c_binding
  use datcom_ptrs_mod
  use npar_ptrs_mod,  only: get_fcenter, nrate_active, nfft_active, nsmax_active
  use four2a_mod
  use timf2_mod
  use iqcal_mod
  use iqfix_mod
  use iso_fortran_env, only: real32, real64

  ! C-facing arguments (keep these as in your modern version)
  integer(c_int),    intent(in)    :: k, nxpol, ndiskdat, nb, nbslider
  integer(c_int),    intent(in)    :: idphi
  integer(c_int),    intent(in)    :: iqadjust, iqapply
  real(c_float),     intent(inout) :: gainx, gainy, phasex, phasey
  real(c_float),     intent(out)   :: rejectx, rejecty, pxdb, pydb
  real(c_float),     intent(out)   :: ssz5a(*)
  integer(c_int),    intent(out)   :: nkhz
  integer(c_int),    intent(inout) :: ihsym, nzap
  real(c_float),     intent(inout) :: slimit
  integer(c_signed_char), intent(out) :: lstrong(0:1023)

  integer, parameter :: live_lookahead_samples=512

  
  ! ---- from legacy body, adapted to modules ----
  ! (no COMMON, no local fcenter, no logical*1 lstrong)


  real(real64) :: ts, hsym
  integer :: i, ipkx, ipky, iqadjust0, iqapply0, j, ja, jb, k0, k1, kstep, &
             mm, nadjx, nadjy, nblk, nblks, nfft2, npts, nsum, nwindow, n, nfast
  real(real32) :: fac, faclim, peaklimit, px, py, q, rejectx0, rms, rmsx, rmsy, &
             s135, s45, sigmas, sx, sy, u, x1, x2, x3, x4, dphi, pi
 
  real(real32), allocatable :: w(:), w2a(:), w2b(:)
  complex,      allocatable :: cx(:), cy(:), cx00(:), cy00(:)

  complex :: z, zfac, zsumx, zsumy
  real(c_double) :: fcenter
  complex :: cx0(0:1023), cx1(0:1023), cy0(0:1023), cy1(0:1023)
 
  data rms/999.0/, k0/99999999/, k1/0/, nadjx/0/, nadjy/0/
  save

nfast = 1

! Stop when we�re near the end of the allocated minute buffer.
! nsmax_active = 60 * nrate_active, set from C++ at startup.
if (k.gt.nsmax_active-9000) goto 999

if (k.lt.nfft_active) then
   ihsym = 0
   goto 999
endif

  if(k0.eq.99999999) then
     ! First-time initialization: allocate buffers at runtime NFFT and
     ! build the window(s). nfft_active is set from C++ via
     ! set_runtime_params_() before any call into this routine.
     if(.not.allocated(w))   allocate(w(nfft_active))
     if(.not.allocated(w2a)) allocate(w2a(nfft_active))
     if(.not.allocated(w2b)) allocate(w2b(nfft_active))
     if(.not.allocated(cx))  allocate(cx(nfft_active))
     if(.not.allocated(cy))  allocate(cy(nfft_active))
     if(.not.allocated(cx00)) allocate(cx00(nfft_active))
     if(.not.allocated(cy00)) allocate(cy00(nfft_active))

     pi=4.0*atan(1.0)
     w2a=0.
     w2b=0.
     do i=1,nfft_active
        w(i)=(sin(i*pi/nfft_active))**2                   !Window for nfast=1
        if(i.lt.17833) w2a(i)=(sin(i*pi/17832.925))**2    !Window a for nfast=2
        j=i-8916
        if(j.gt.0 .and. j.lt.17833) w2b(i)=(sin(j*pi/17832.925))**2    ! b
     enddo
     w2a=sqrt(2.0)*w2a
     w2b=sqrt(2.0)*w2b
  endif

 
! Symbol length in samples, based on the active runtime rate.
hsym = 2048.d0 * dble(nrate_active) / 11025.d0
 
if(nrate_active.eq.95238)   hsym=2048.d0*95238.1d0/11025.d0

if (k.lt.k0) then
   ! Perform the legacy reset
   ts    = 1.d0 - hsym
   savg  = 0.
   ihsym = 0
   k1    = 0
 !  if (ndiskdat.eq.0) dd(1:4,k+1:5760000)=0.
   if (ndiskdat.eq.0) dd(1:4,k+live_lookahead_samples+1:nsmax_active)=0.
endif

k0 = k

  
  nzap=0
  sigmas=1.5*(10.0**(0.01*nbslider)) + 0.7
  peaklimit=sigmas*max(10.0,rms)
  faclim=3.0
  px=0.
  py=0.

  iqapply0=0
  iqadjust0=0
  if(iqadjust.ne.0) iqapply0=0
  nwindow=2
!  nwindow=0                                    !### No windowing ###
  nfft2=1024
  kstep=nfft2
  if(nwindow.ne.0) kstep=nfft2/2
  nblks = (k - k1) / kstep
  
  do nblk=1,nblks
     j=k1+1
     do i=0,nfft2-1
        cx0(i)=cmplx(dd(1,j+i),dd(2,j+i))
        if(nxpol.ne.0) cy0(i)=cmplx(dd(3,j+i),dd(4,j+i))
     enddo
     call timf2(k,nxpol,nfft2,nwindow,nb,peaklimit,iqadjust0,iqapply0,       &
          faclim,cx0,cy0,gainx,gainy,phasex,phasey,cx1,cy1,slimit,lstrong,   &
          px,py,nzap)

     do i=0,kstep-1
        dd(1,j+i)=real(cx1(i))
        dd(2,j+i)=aimag(cx1(i))
        if(nxpol.ne.0) then
           dd(3,j+i)=real(cy1(i))
           dd(4,j+i)=aimag(cy1(i))
        endif
     enddo
     k1=k1+kstep
  enddo

  npts=nfft_active                            !Samples used in each half-symbol FFT

  ts=ts+hsym
  ja=ts   ! ts already set from ihsym above; do not integrate it again                             !Index of first sample
  jb=ja+npts-1                        !Last sample

  i=0
  fac=0.0002
  dphi=idphi/57.2957795
  zfac=fac*cmplx(cos(dphi),sin(dphi))

  do j=ja,jb                          !Copy data into cx, cy
     x1=dd(1,j)
     x2=dd(2,j)
     if(nxpol.ne.0) then 
        x3=dd(3,j)
        x4=dd(4,j)
     else
        x3=0.
        x4=0.
     endif
     i=i+1
     cx(i)=fac*cmplx(x1,x2)
     cy(i)=zfac*cmplx(x3,x4)          !NB: cy includes dphi correction
  enddo

  if(nzap/178.lt.50 .and. (ndiskdat.eq.0 .or. ihsym.lt.280)) then
     nsum=nblks*kstep - nzap
     if(nsum.le.0) nsum=1
     rmsx=sqrt(0.5*px/nsum)
     rmsy=sqrt(0.5*py/nsum)
     rms=rmsx
     if(nxpol.ne.0) rms=sqrt((px+py)/(4.0*nsum))
  endif
  pxdb=0.
  pydb=0.
  if(rmsx.gt.1.0) pxdb=20.0*log10(rmsx)
  if(rmsy.gt.1.0) pydb=20.0*log10(rmsy)
  if(pxdb.gt.60.0) pxdb=60.0
  if(pydb.gt.60.0) pydb=60.0

  cx00=cx
  if(nxpol.ne.0) cy00=cy

  do mm=1,nfast
     ihsym=ihsym+1
     if(nfast.eq.1) then
        cx=w*cx00                           !Apply window for 2nd forward FFT
        if(nxpol.ne.0) cy=w*cy00
     else
        if(mm.eq.1) then
           cx=w2a*cx00
           if(nxpol.ne.0) cy=w2a*cy00
        else
           cx=w2b*cx00
           if(nxpol.ne.0) cy=w2b*cy00
        endif
     endif

     call four2a(cx,nfft_active,1,1,1)          !Second forward FFT (X)
     if(iqadjust.eq.0) nadjx=0
     if(iqadjust.ne.0 .and. nadjx.lt.50) call iqcal(nadjx,cx,nfft_active,    &
          gainx,phasex,zsumx,ipkx,rejectx0)
     if(iqapply.ne.0) call iqfix(cx,nfft_active,gainx,phasex)

     if(nxpol.ne.0) then
        call four2a(cy,nfft_active,1,1,1)       !Second forward FFT (Y)
        if(iqadjust.eq.0) nadjy=0
        if(iqadjust.ne.0 .and. nadjy.lt.50) call iqcal(nadjy,cy,nfft_active, &
             gainy,phasey,zsumy,ipky,rejecty)
        if(iqapply.ne.0) call iqfix(cy,nfft_active,gainy,phasey)
     endif

     n=min(322,ihsym)
     do i=1,nfft_active
        sx=real(cx(i))**2 + aimag(cx(i))**2  
        ss(1,n,i)=sx                    ! Pol = 0
        savg(1,i)=savg(1,i) + sx
       
        if(nxpol.ne.0) then
           z=cx(i) + cy(i)
           s45=0.5*(real(z)**2 + aimag(z)**2)
           ss(2,n,i)=s45                   ! Pol = 45
           savg(2,i)=savg(2,i) + s45

           sy=real(cy(i))**2 + aimag(cy(i))**2
           ss(3,n,i)=sy                    ! Pol = 90
           savg(3,i)=savg(3,i) + sy
        
           z=cx(i) - cy(i)
           s135=0.5*(real(z)**2 + aimag(z)**2)
           ss(4,n,i)=s135                  ! Pol = 135
           savg(4,i)=savg(4,i) + s135

           z=cx(i)*conjg(cy(i))
           q=sx - sy
           u=2.0*real(z)
           ssz5a(i)=0.707*sqrt(q*q + u*u)    !Spectrum of linear polarization
! Leif's formula:
!     ssz5a(i)=0.5*(sx+sy) + (real(z)**2 + aimag(z)**2 - sx*sy)/(sx+sy)
        else
           ssz5a(i)=sx
        endif
     enddo
  enddo

! NOTE: ipkx and ipky are 0 when no peak was found. The reject ratio
! calculation is only valid when both are non-zero. The guard
! (ipkx.ne.0 .and. ipky.ne.0) ensures that indices such as
! 1+NFFT-ipkx never evaluate to NFFT+1. No out-of-bounds access occurs.

  if(ihsym.eq.278) then
     if(iqadjust.ne.0 .and. ipkx.ne.0 .and. ipky.ne.0) then
        rejectx=10.0*log10(savg(1,1+nfft_active-ipkx)/savg(1,1+ipkx))
        rejecty=10.0*log10(savg(3,1+nfft_active-ipky)/savg(3,1+ipky))
     endif
  endif

  fcenter=get_fcenter()
  nkhz=nint(1000.d0*(fcenter-int(fcenter)))
  if(fcenter.eq.0.d0) nkhz=125

999 return
end subroutine symspec


end module symspec_mod