summaryrefslogtreecommitdiff
path: root/tests/unit/q65/test_map65_wideband_sync_rows.f90
blob: 185bd96ffcfbea3ae066af425f852db881684183 (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
program test_map65_wideband_sync_rows
  use wideband_sync, only: candidate,get_candidates,init_wideband_sync,outside_row_count,sync,wb_sync
  use npar_ptrs_mod, only: nfft_active, nrate_active
  implicit none

  integer, parameter :: jz=280, lag=11
  integer :: expanded_sync(22)
  integer :: ia, ib, i, j, k, ncand, offset, peak(1), target_bin
  real :: angle_error,ccf_before,df,response(4),xdt_before
  real, allocatable :: savg(:,:), ss(:,:,:)
  type(candidate) :: candidates(50)
  logical :: found

  expanded_sync = [1,27,37,40,46,69,72,82,85,104,111,121,146,159,175,192, &
       198,211,221,237,243,272]
  nrate_active=96000
  nfft_active=32768
  df=real(nrate_active)/real(nfft_active)
  ia=nint(1000.0/df)+1
  ib=nint(2000.0/df)+1
  target_bin=(ia+ib)/2

  allocate(ss(4,322,nfft_active),savg(4,nfft_active))
  ss=1.0
  savg=real(jz)
  do i=ia,ib
     do j=1,jz
        ss(1,j,i)=1.0+0.001*real(mod(37*j+17*i,23))
     enddo
     savg(1,i)=sum(ss(1,1:jz,i))
  enddo

  do j=1,size(expanded_sync)
     k=expanded_sync(j)+lag
     do offset=0,2
        if (k+offset <= jz) ss(1,k+offset,target_bin)= &
             ss(1,k+offset,target_bin)+50.0
     enddo
  enddo
  savg(1,target_bin)=sum(ss(1,1:jz,target_bin))

  call init_wideband_sync()
  call wb_sync(ss,savg,.false.,.true.,jz,1,2)
  peak=maxloc(sync(ia:ib)%ccfmax)
  if (peak(1)+ia-1 /= target_bin) error stop 'late Q65 partial sync peak was not found'
  if (sync(target_bin)%iflip /= 0) error stop 'late Q65 partial sync was misclassified'
  if (abs(sync(target_bin)%xdt-(lag*2048.0/11025.0-1.0)) > 0.001) &
       error stop 'late Q65 partial sync used the wrong lag'

  ccf_before=sync(target_bin)%ccfmax
  xdt_before=sync(target_bin)%xdt
  ss(1,jz+1:322,ia:ib)=1.0e20
  call wb_sync(ss,savg,.false.,.true.,jz,1,2)

  if (sync(target_bin)%ccfmax /= ccf_before) &
       error stop 'unavailable spectrum rows changed Q65 sync strength'
  if (sync(target_bin)%xdt /= xdt_before) &
       error stop 'unavailable spectrum rows changed Q65 sync lag'

  if (outside_row_count(24,300,280) /= 24) &
       error stop 'early Q65 carrier baseline used the wrong row count'
  if (outside_row_count(-1,276,280) /= 5) &
       error stop 'negative leading rows changed the carrier baseline count'

  do i=ia,ib
     do j=1,jz
        ss(:,j,i)=1.0+0.001*[real(mod(37*j+17*i,23)),real(mod(31*j+13*i,19)), &
             real(mod(29*j+11*i,17)),real(mod(23*j+7*i,13))]
     enddo
     savg(:,i)=[sum(ss(1,1:jz,i)),sum(ss(2,1:jz,i)), &
          sum(ss(3,1:jz,i)),sum(ss(4,1:jz,i))]
  enddo
  response=[cos(22.5*acos(-1.0)/180.0)**2,cos(-22.5*acos(-1.0)/180.0)**2, &
       cos(-67.5*acos(-1.0)/180.0)**2,cos(-112.5*acos(-1.0)/180.0)**2]
  do j=1,size(expanded_sync)
     k=expanded_sync(j)+lag
     do offset=0,2
        if (k+offset <= jz) ss(:,k+offset,target_bin)=ss(:,k+offset,target_bin)+50.0*response
     enddo
  enddo
  do i=1,4
     savg(i,target_bin)=sum(ss(i,1:jz,target_bin))
  enddo

  call wb_sync(ss,savg,.true.,.false.,jz,1,2)
  ccf_before=sync(target_bin)%ccfmax
  call get_candidates(ss,savg,.true.,jz,1,2,0,1,candidates,ncand)
  if (sync(target_bin)%ccfmax <= ccf_before) &
       error stop 'continuous polarization did not improve the production Q65 score'
  angle_error=abs(modulo(sync(target_bin)%pol-22.5+90.0,180.0)-90.0)
  if (angle_error > 0.05) error stop 'continuous polarization recovered the wrong physical angle'
  angle_error=abs(modulo(sync(target_bin)%combine_pol-22.5+90.0,180.0)-90.0)
  if (angle_error > 0.05) error stop 'continuous polarization selected the wrong combining angle'
  found=.false.
  do i=1,ncand
     if (candidates(i)%iflip == 0 .and. &
          abs(candidates(i)%f-0.001*(target_bin-1)*df) < 0.0005*df) found=.true.
  enddo
  if (.not.found) error stop 'continuous polarization score did not reach Q65 candidate admission'

  savg(2,ia:ib)=4.0*real(jz)
  savg(4,ia:ib)=0.0
  call wb_sync(ss,savg,.true.,.false.,jz,1,2)
  ccf_before=sync(target_bin)%ccfmax
  call get_candidates(ss,savg,.true.,jz,1,2,0,1,candidates,ncand)
  if (sync(target_bin)%ccfmax /= ccf_before) &
       error stop 'invalid covariance did not preserve the legacy Q65 score'

  call test_candidate_search()

contains

  subroutine reset_search_floor()
    integer :: bin,row
    ss(:,1:jz,ia:ib)=1.0
    savg(:,ia:ib)=real(jz)
    do bin=ia,ib
       do row=1,jz
          ss(1,row,bin)=1.0+0.002*real(mod(17*row+13*bin,23))
       enddo
       savg(1,bin)=sum(ss(1,1:jz,bin))
    enddo
  end subroutine reset_search_floor

  subroutine add_search_sync(bin,level)
    integer, intent(in) :: bin
    real, intent(in) :: level
    integer :: symbol,row,offset
    do symbol=1,size(expanded_sync)
       row=expanded_sync(symbol)+lag
       do offset=0,2
          if(row+offset<=jz) ss(1,row+offset,bin)=ss(1,row+offset,bin)+level
       enddo
    enddo
    savg(1,bin)=sum(ss(1,1:jz,bin))
  end subroutine add_search_sync

  subroutine add_jt65_sync(bin,ooo)
    integer, intent(in) :: bin
    logical, intent(in) :: ooo
    integer, parameter :: standard_symbols(63) = [ &
         1,4,5,9,10,11,12,13,14,16,18,22,24,25,28,32, &
         33,34,37,38,39,40,42,43,45,46,47,48,52,53,55,57, &
         59,60,63,64,66,68,70,73,80,81,89,90,92,95,97,98, &
         100,102,104,107,108,111,114,119,120,121,122,123,124,125,126]
    integer :: symbol,row
    do symbol=1,126
       if(ooo .eqv. any(standard_symbols==symbol)) cycle
       row=2*(symbol-1)+1+lag
       ss(1,row:row+1,bin)=ss(1,row:row+1,bin)+50.0
    enddo
    savg(1,bin)=sum(ss(1,1:jz,bin))
  end subroutine add_jt65_sync

  subroutine assert_one_at(bin,description)
    integer, intent(in) :: bin
    character(len=*), intent(in) :: description
    integer :: index
    logical :: matches
    matches=.false.
    do index=1,ncand
       if(candidates(index)%iflip==0 .and. &
            abs(candidates(index)%f-0.001*(bin-1)*df)<0.0005*df .and. &
            abs(candidates(index)%xdt-(lag*2048.0/11025.0-1.0))<0.001) matches=.true.
    enddo
    if(.not.matches) then
       print '(a)', 'FAIL: '//description
       error stop 1
    endif
  end subroutine assert_one_at

  subroutine test_candidate_search()
    type(candidate) :: first
    integer :: first_count, second_bin

    second_bin=target_bin+120
    call reset_search_floor()
    call add_search_sync(target_bin,50.0)
    call get_candidates(ss,savg,.false.,jz,1,2,0,1,candidates,ncand)
    if(ncand/=1) error stop 'one MAP65 Q65 sync candidate was not isolated'
    call assert_one_at(target_bin,'one MAP65 Q65 candidate frequency and time')
    first_count=ncand
    first=candidates(1)

    call reset_search_floor()
    call add_search_sync(second_bin,50.0)
    call get_candidates(ss,savg,.false.,jz,1,2,0,1,candidates,ncand)
    if(ncand/=1) error stop 'alternate MAP65 candidate was not isolated'
    call assert_one_at(second_bin,'alternate MAP65 candidate frequency and time')

    call reset_search_floor()
    call add_search_sync(target_bin,50.0)
    call get_candidates(ss,savg,.false.,jz,1,2,0,1,candidates,ncand)
    if(ncand/=first_count .or. candidates(1)%f/=first%f .or. &
         candidates(1)%xdt/=first%xdt .or. candidates(1)%snr/=first%snr .or. &
         candidates(1)%iflip/=first%iflip) error stop 'MAP65 A-B-A candidate changed'

    ss(2:4,1:jz,ia:ib)=1.0e20
    savg(2:4,ia:ib)=1.0e20
    call get_candidates(ss,savg,.false.,jz,1,2,0,1,candidates,ncand)
    if(ncand/=first_count .or. candidates(1)%f/=first%f .or. &
         candidates(1)%snr/=first%snr) error stop 'inactive MAP65 polarization affected search'

    call reset_search_floor()
    call add_search_sync(target_bin,50.0)
    call add_search_sync(second_bin,45.0)
    call get_candidates(ss,savg,.false.,jz,1,2,0,1,candidates,ncand)
    if(ncand/=2) error stop 'separated MAP65 Q65 signals did not survive'

    call reset_search_floor()
    call add_search_sync(target_bin,50.0)
    call add_search_sync(target_bin+2,20.0)
    call get_candidates(ss,savg,.false.,jz,1,2,0,1,candidates,ncand)
    if(ncand/=1) error stop 'nearby MAP65 peaks did not collapse to one candidate'
    call assert_one_at(target_bin,'stronger MAP65 nearby peak survived')

    call reset_search_floor()
    ss(1,1:jz,target_bin)=ss(1,1:jz,target_bin)+80.0
    savg(1,target_bin)=sum(ss(1,1:jz,target_bin))
    call get_candidates(ss,savg,.false.,jz,1,2,0,1,candidates,ncand)
    if(ncand/=0) error stop 'continuous MAP65 carrier entered the Q65 candidate list'

    call reset_search_floor()
    call add_jt65_sync(second_bin,.false.)
    call get_candidates(ss,savg,.false.,jz,1,2,0,1,candidates,ncand)
    if(sync(second_bin)%iflip/=1) error stop 'standard JT65 sync fixture was not recognized'
    if(ncand/=0) error stop 'standard JT65 sync entered Q65-only search'

    call reset_search_floor()
    call add_jt65_sync(second_bin,.true.)
    call get_candidates(ss,savg,.false.,jz,1,2,0,1,candidates,ncand)
    if(sync(second_bin)%iflip/=-1) error stop 'OOO JT65 sync fixture was not recognized'
    if(ncand/=0) error stop 'OOO JT65 sync entered Q65-only search'

    call reset_search_floor()
    call add_search_sync(target_bin,50.0)
    call get_candidates(ss,savg,.false.,jz,2,3,0,1,candidates,ncand)
    if(ncand/=0) error stop 'MAP65 candidate outside search band was admitted'
  end subroutine test_candidate_search
end program test_map65_wideband_sync_rows