summaryrefslogtreecommitdiff
path: root/map65/libm65/mapsim.f90
blob: 9daa21ed02776b977a9940b49d0605d1af35e6fa (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
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
module dopspread_mod
  implicit none
contains

  subroutine dopspread(cwave, npts, fsample, fspread)
    use four2a_mod
    use gran_interface
    use iso_fortran_env, only: real32, real64
    implicit none
    integer, intent(in) :: npts
    real(real64), intent(in) :: fsample, fspread
    complex(real32), intent(inout) :: cwave(npts)

    integer :: nfft, nh, i
    complex(real32), allocatable :: cspread(:)
    real(real32) :: twopi, df, b, f, x, a, phi1, phi2
    real(real32) :: sum, p, avep, fac
    complex(real32) :: z

    nfft = npts
    nh   = nfft/2
    allocate(cspread(0:nfft-1))

    twopi = 8.0*atan(1.0)
    df    = real(fsample,real32)/real(nfft,real32)

    cspread(0)=1.0
    cspread(nh)=0.
    b=6.0                       !Use truncated Lorenzian shape for fspread
    do i=1,nh
       f=i*df
       x=b*f/fspread
       z=0.
       a=0.
       if(x.lt.3.0) then                          !Cutoff beyond x=3
          a=sqrt(1.111/(1.0+x*x)-0.1)             !Lorentzian amplitude
          phi1=twopi*rran()                       !Random phase
          z=a*cmplx(cos(phi1),sin(phi1))
       endif
       cspread(i)=z
       z=0.
       if(x.lt.3.0) then                !Same thing for negative freqs
          phi2=twopi*rran()
          z=a*cmplx(cos(phi2),sin(phi2))
       endif
       cspread(nfft-i)=z
    enddo
  
    call four2a(cspread,nfft,1,1,1)             !Transform to time domain
  
    sum=0.
    do i=0,nfft-1
       p=real(cspread(i))**2 + aimag(cspread(i))**2
       sum=sum+p
    enddo
    avep=sum/nfft
    fac=sqrt(1.0/avep)
    cspread=fac*cspread                   !Normalize to constant avg power
    cwave=cspread*cwave                   !Apply Rayleigh fading
    
    deallocate(cspread)
    return
  end subroutine dopspread
  
end module dopspread_mod

program mapsim

! Generate simulated data for testing of MAP65
  use iso_c_binding
  use gen_q65_cwave_mod
  use four2a_mod
  use cgen65_mod
  use noisegen_mod
  use dopspread_mod
  use iso_fortran_env, only: real32, real64, int16
  implicit none

  integer :: NMAX
	
  real(real32), allocatable :: d4(:,:)                  !Floating-point data
  integer(int16), allocatable ::  id4(:,:)               !i*2 data, dual polarization
  integer(int16), allocatable ::  id2(:,:)               !i*2 data, single polarization
  complex(real32), allocatable ::  cwave(:)                !i*2 data, single polarization
  complex(real32) z,zx,zy
  real(real32) :: dt0, fa, fb, snrdb, rmsdb, rad, fac, pol, a, b, sig, snrdbx
  real(real32) :: rms
  real(real64) :: fcenter, fsample, twopi, phi, dphi, xphi
  real(real64) :: f, dt, samfac, fdop

  logical bq65
  character(len=24) msg0  !w3sz msg0 was size 22
  character(len=24)message
  character(len=24)msgsent
  character(len=8)arg
  character(len=11)fname
  character(len=2)mode
  character(len=24) msg_list(60) ! was *16
  integer(int16) hhmm
  integer :: nargs, nsigs, npol, nfiles, isig, nmin, ifile
  integer :: ntone_spacing, ntxfreq, nsendingsh, nwave, npts
  integer :: ilist, i0, i, j
  integer :: sRatekHz
  
  data msg_list/                                          &
      ! 'CQ K1WDG/R','CQ YL/EA8DBM','<YL/EA8DBM> OH3LWP KP11', &
      ! 'CQ PJ4/KA1ABC','CQ KH1/KH7Z','CQ QU1RK FN20',   &
      ! '<K1WDG/P> W3SZ fn20','<PJ4/KA1ABC> WA9XYZ -10','WA9XYZ <KH1/KH7Z> -12', &
      ! '<YW18FIFA> KA1ABC 73','CQ TEST KA1GT FN54','CQ YW18FIFA',   &
      ! 'PJ4/KA1ABC <WA9XYZ> -11','<WA9XYZ> PJ4/KA1ABC 73','<WA9XYZ> PJ4/KA1ABC RR73',  &
      ! '<YW18FIFA> KA1ABC FN42','<PJ4/KA1ABC> WA9XYZ R-09','<PJ4/KA1ABC> WA9XYZ EN90',  &
      ! 'CQ DL4KGC JN68','CQ HB9Q JN47','CQ G3LTF IO91',   &
      ! 'CQ K1JT/P FN20','CQ W3SZ/2 FN20','CQ DL3WDG JN68',   &   
      ! 'CQ GM/N5L IO98','CQ 3DA0RS JJ22','CQ W7RPI/2 FN31',   &         
      ! 'CQ 123 456 78','CQ DX 167 DXP','CQYDHRT 678',   &
      ! 'CQ K1JT FN20','CQ W3SZ FN10','CQ DL3WDG JN68',   &
      ! 'CQ DG2YCB JO42','CQ ON4AOI JO21','CQ KA1GT FN54',   &
      ! 'CQ DL4KGC JN68','CQ HB9Q JN47','CQ G3LTF IO91',   &
       'W1AAA K2BBB EM00','W2CCC K3DDD EM01','W3EEE K4FFF EM02',   &
       'W5GGG K6HHH EM03','W7III K8JJJ EM04','W9KKK K0LLL EM05',   &
       'G0MMM F1NNN JN06','G2OOO F3PPP JN07','G4QQQ F5RRR JN08',   &
       'G6SSS F7TTT JN09','W1XAA K2XBB EM10','W2XCC K3XDD EM11',   &
       'W3XEE K4XFF EM12','W5XGG K6XHH EM13','W7XII K8XJJ EM14',   &
       'W9XKK K0XLL EM15','G0XMM F1XNN JN16','G2XOO F3XPP JN17',   &
       'G4XQQ F5XRR JN18','G6XSS F7XTT JN19','W1YAA K2YBB EM20',   &
       'W2YCC K3YDD EM21','W3YEE K4YFF EM22','W5YGG K6YHH EM23',   &
       'W7YII K8YJJ EM24','W9YKK K0YLL EM25','G0YMM F1YNN JN26',   &
       'G2YOO F3YPP JN27','G4YQQ F5YRR JN28','G6YSS F7YTT JN29',   &
       'W1ZAA K2ZBB EM30','W2ZCC K3ZDD EM31','W3ZEE K4ZFF EM32',   &
       'W5ZGG K6ZHH EM33','W7ZII K8ZJJ EM34','W9ZKK K0ZLL EM35',   &
       'G0ZMM F1ZNN JN36','G2ZOO F3ZPP JN37','G4ZQQ F5ZRR JN38',   &
       'G6ZSS F7ZTT JN39','W1AXA K2BXB EM40','W2CXC K3DXD EM41',   &
       'W3EXE K4FXF EM42','W5GXG K6HXH EM43','W7IXI K8JXJ EM44',   &
       'W9KXK K0LXL EM45','G0MXM F1NXN JN46','G2OXO F3PXP JN47',   &
       'G4QXQ F5RXR JN48','G6SXS F7TXT JN49','W1AYA K2BYB EM50',   &
       'W2CYC K3DYD EM51','W3EYE K4FYF EM52','W5GYG K6HYH EM53',   &
       'W7IYI K8JYJ EM54','W9KYK K0LYL EM55','G0MYM F1NYN JN56',   &
       'G2OYO F3PYP JN57','G4QYQ F5RYR JN58','G6SYS F7TYT JN59'/
    
  nargs=command_argument_count()
  if(nargs.ne.13) then
     print*,'Usage:   mapsim "message"     mode DT  fa fb nsigs pol fDop SNR nfiles fcenter HHmm sRatekHz'
     print*,'Example: mapsim "CQ K1ABC FN42" B 2.5 -20 20  21    45  0.0 -20   1   1296.1  1803 96'
     print*,' '
     print*,'         mode = A B C for JT65; QA-QE for Q65-60A' 
     print*,'         fa = lowest freq in kHz, relative to center'
     print*,'         fb = highest freq in kHz, relative to center'
     print*,'         message = "list" to use callsigns from list'
     print*,'         pol = -1 to generate a range of polarization angles.'
     print*,'         SNR = 0 to generate a range of SNRs.'
     print*,'         sRateKHz must be one of 96, 128, 192,256'
     go to 999
  endif

  call get_command_argument(1,msg0)
  call get_command_argument(2,mode)                !JT65 sub-mode (A B C QA-QE)
  call get_command_argument(3,arg)
  read(arg,*) dt0                    !Time delay
  call get_command_argument(4,arg)
  read(arg,*) fa                     !Lowest freq (kHz, relative to fcenter)
  call get_command_argument(5,arg)
  read(arg,*) fb                     !Highest freq
  call get_command_argument(6,arg)
  read(arg,*) nsigs                  !Number of signals in each file
  call get_command_argument(7,arg)
  read(arg,*) npol                   !Polarization in degrees
  pol=npol
  call get_command_argument(8,arg)
  read(arg,*) fdop                   !Doppler spread
  call get_command_argument(9,arg)
  read(arg,*) snrdb                  !S/N
  call get_command_argument(10,arg)
  read(arg,*) nfiles                 !Number of files                !S/N
  call get_command_argument(11,arg) ! added by w3sz
  read(arg,*) fcenter     ! added by w3sz
  call get_command_argument(12,arg) ! added by w3sz
  read(arg,*) hhmm     ! added by w3sz
  call get_command_argument(13,arg) ! added by w3sz
  read(arg,*) sRatekHz     ! added by w3sz

  if (sRatekHz /= 96 .and. sRatekHz /= 128 .and. sRatekHz /= 192 .and. sRatekHz /= 256) then
    write(*,*) 'ERROR: Invalid sample rate ', sRatekHz, ' kHz'
    write(*,*) 'Allowed sample rates are: 96, 128, 192, 256 kHz'
    stop
  endif

  fsample = 1000.d0 * sRatekHz
  dt      = 1.d0 / fsample
  NMAX = 60 * sRatekHz * 1000
  npts = NMAX
  allocate(d4(4, NMAX))
  allocate(id4(4,NMAX))
  allocate(id2(2,NMAX))
  allocate(cwave(NMAX))

  message=msg0                       !Transmitted message
  rmsdb=25.
  rms=10.0**(0.05*rmsdb)
  ! w3sz fcenter=144.125d0                  !Center frequency (MHz)
  twopi=8.d0*atan(1.d0)
  rad=360.0/twopi
  samfac = fsample / 96000.d0
  bq65=(mode(1:1).eq.'Q')
  ntone_spacing=1
  ntxfreq=1270
  fac=1.0/32767.0
  if(mode(1:1).eq.'B' .or. mode(2:2).eq.'B') ntone_spacing=2
  if(mode(1:1).eq.'C' .or. mode(2:2).eq.'C') ntone_spacing=4
  if(mode(2:2).eq.'D') ntone_spacing=8
  if(mode(2:2).eq.'E') ntone_spacing=16
  nsendingsh=0 ! Initialize nsendingsh (was implicit?)

  write(*,1000)
1000 format('File N Mode  DT    freq   pol   fDop   SNR   Message'/68('-'))

  do ifile=1,nfiles
     ilist=0
     nmin=ifile-1
     if(mode(2:2).eq.' ') nmin=2*nmin
     write(fname, '(I6.6, "_", I4.4)') ifile, hhmm !Create the output filenames  ! w3sz was nmin
     open(10,file=fname//'.iq',access='stream',status='replace')
     open(11,file=fname//'.tf2',access='stream',status='replace')

     call noisegen(d4,npts)                      !Generate Gaussuian noise

     if(msg0(1:4).ne.'list') then
        call generate_cwave(message,msgsent,cwave,nwave)
     endif

     if(fdop.gt.0.0) call dopspread(cwave,npts,fsample,fdop)

     do isig=1,nsigs

        if(msg0(1:4).eq.'list') then
           ilist=ilist+1
           message=msg_list(ilist)
           call generate_cwave(message,msgsent,cwave,nwave)
        endif

        if(npol.lt.0) pol=(isig-1)*180.0/nsigs
        a=cos(pol/rad)
        b=sin(pol/rad)
        f=1000.0*(fa+fb)/2.0
        if(nsigs.gt.1) f=1000.0*(fa + (isig-1)*(fb-fa)/(nsigs-1.0))
        dphi=twopi*f*dt + 0.5*twopi

        snrdbx=snrdb
        if(snrdb.eq.0.0) snrdbx=-15.0 - 15.0*(isig-1.0)/nsigs
        sig = sqrt(2.2*2500.0/fsample) * 10.0**(0.05*snrdbx)
        write(*,1020) ifile,isig,mode,dt0,0.001*f,nint(pol),fDop,snrdbx,msgsent
1020    format(i3,i3,2x,a2,f6.2,f8.3,i5,2f7.1,2x,a24)  !w3sz was a22

        phi=0.
!        i0=fsample*(3.5d0+0.05d0*(isig-1))
        i0=fsample*(1.d0 + dt0)
        do i=1,nwave
           phi=phi + dphi
           if(phi.lt.-twopi) phi=phi+twopi
           if(phi.gt.twopi) phi=phi-twopi
           xphi=phi
           z = sig * cwave(i) * cmplx( real(cos(xphi),kind=real32), &
              real(-sin(xphi),kind=real32), kind=real32 )
           zx=a*z
           zy=b*z
           j=i+i0
           d4(1,j)=d4(1,j) + real(zx)
           d4(2,j)=d4(2,j) + aimag(zx)
           d4(3,j)=d4(3,j) + real(zy)
           d4(4,j)=d4(4,j) + aimag(zy)
        enddo
     enddo

     do i=1,npts
        id4(1,i)=nint(rms*d4(1,i))
        id4(2,i)=nint(rms*d4(2,i))
        id4(3,i)=nint(rms*d4(3,i))
        id4(4,i)=nint(rms*d4(4,i))
        id2(1,i)=id4(1,i)
        id2(2,i)=id4(2,i)
     enddo

     write(*,*) 'DEBUG: fsample =', fsample, ' sRatekHz =', sRatekHz, &
           ' NMAX =', NMAX, ' npts =', npts

     write(10) fcenter,id2(1:2,1:npts)
     write(11) fcenter,id4(1:4,1:npts)
     close(10)
     close(11)
  enddo

  deallocate(d4)
  deallocate(id4)
  deallocate(id2)
  deallocate(cwave)

999 continue
contains

  subroutine generate_cwave(message,msgsent,cwave,nwave)
    character(len=*),intent(inout) :: message
    character(len=*),intent(out)   :: msgsent
    complex,         intent(out) :: cwave(:)
    integer,         intent(out) :: nwave

    ! cgen65 defines only 22 characters; blank-fill so the a24 output is fully defined
    msgsent = ' '
    if(bq65) then
       call gen_q65_cwave(message,ntxfreq,ntone_spacing,fsample,msgsent, &
            cwave,nwave)
    else
       call cgen65(message,ntone_spacing,samfac,nsendingsh,msgsent, &
            cwave,nwave)
    endif
  end subroutine generate_cwave

end program mapsim