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
|
program echosim
! Generate simulated echo-mode files -- self-echo or "measure"
use wavhdr
use julian
parameter (NWAVE=27648,NZ=36000)
type(hdr) h !Header for .wav file
character arg*12,fname*17
complex c0(0:NZ-1)
real*4 level_1,level_2
real*8 f0,dt,twopi,phi,dphi
real wave(NZ)
integer*2 iwave(NZ) !Generated full-length waveform
character*32 isft !WAV LIST/INFO trailer fields
character*24 icrd
character*64 icmt
integer*8 tsec8
integer gmt(9)
integer*4 trailerlen,wav_trailer_length
equivalence (nDop0,iwave(1))
equivalence (nDopAudio0,iwave(3))
equivalence (nfrit0,iwave(5))
equivalence (f10,iwave(7))
equivalence (fspread0,iwave(9))
! Get command-line argument(s)
nargs=iargc()
if(nargs.ne.3 .and. nargs.ne.5) then
print*,'Usage 1: echosim f0 fdop fspread nfiles snr'
print*,'Example: echosim 1500 0.0 4.0 10 -22'
print*,'Usage 2: echosim level_1 level_2 nfiles'
print*,'Example: echosim 30.0 40.0 100'
go to 999
endif
call getarg(1,arg)
read(arg,*) f0 !Tone frequency
call getarg(2,arg)
read(arg,*) fdop !Doppler shift (Hz)
call getarg(3,arg)
read(arg,*) fspread !Frequency spread (Hz) (JHT Lorentzian model)
if(nargs.eq.3) then
level_1=f0
level_2=fdop
nfiles=fspread
snrdb=0.
go to 10
endif
call getarg(4,arg)
read(arg,*) nfiles !Number of files
call getarg(5,arg)
read(arg,*) snrdb !SNR_2500
10 twopi=8.d0*atan(1.d0)
fs=12000.0 !Sample rate (Hz)
dt=1.d0/fs !Sample interval (s)
bandwidth_ratio=2500.0/(fs/2.0)
sig=sqrt(2*bandwidth_ratio) * 10.0**(0.05*snrdb)
if(snrdb.gt.90.0) sig=1.0
dphi=twopi*(f0+fdop)*dt
write(*,1000)
1000 format(' N f0 fDop fSpread SNR File name'/51('-'))
isft='WSJT-X echosim'
tsec8=itime8()
call gmtime(int(tsec8,4),gmt)
write(icrd,1004) gmt(6)+1900,gmt(5)+1,gmt(4),gmt(3),gmt(2),gmt(1)
1004 format(i4.4,'-',i2.2,'-',i2.2,'T',i2.2,':',i2.2,':',i2.2,'Z')
write(icmt,1005) nfiles
1005 format('Mode=Echo; Simulated by echosim; nfiles=',i0)
trailerlen=wav_trailer_length(isft,icrd,icmt)
do ifile=1,nfiles
wave=0.
if(nargs.eq.5) then
phi=0.d0
do i=0,NWAVE-1
phi=phi + dphi
if(phi.gt.twopi) phi=phi-twopi
xphi=phi
c0(i)=cmplx(cos(xphi),sin(xphi))
enddo
c0(NWAVE:)=0.
if(fspread.gt.0.0) call fspread_lorentz(c0,fspread)
wave(1:NWAVE)=sig*imag(c0(0:NWAVE-1))
endif
if(snrdb.lt.90) then
do i=1,NWAVE !Add gaussian noise at specified SNR
xnoise=gran()
wave(i)=wave(i) + xnoise
enddo
do i=NWAVE+1,NZ
xnoise=gran()
wave(i)=xnoise
enddo
endif
gain=100.0
if(nargs.eq.3) then
gain=10.0**(0.05*level_1)
if(mod((ifile-1)/10,2).eq.1) gain=10.0**(0.05*level_2)
endif
if(snrdb.lt.90.0) then
wave=gain*wave
else
datpk=maxval(abs(wave))
fac=32766.9/datpk
wave=fac*wave
endif
if(any(abs(wave).gt.32767.0)) print*,"Warning - data will be clipped."
iwave=nint(wave)
nDop0=nint(fdop)
nDopAudio0=0
nfrit0=0
f10=f0 + fdop
fspread0=fspread
h=default_header(12000,NZ)
h%lenfile=h%lenfile + trailerlen ! Include trailing metadata in the RIFF size
n=3*(ifile-1)
ihr=n/3600
imin=(n-3600*ihr)/60
isec=mod(n,60)
write(fname,1102) ihr,imin,isec
1102 format('000000_',3i2.2,'.wav')
open(10,file=fname,status='replace',access='stream')
write(10) h,iwave !Save to *.wav file
call write_wav_info_trailer(10,isft,icrd,icmt)
close(10)
write(*,1110) ifile,f0,fdop,fspread,snrdb,fname
1110 format(i4,4f7.1,2x,a17)
enddo
999 end program echosim
integer*4 function wav_info_field_bytes(nchar)
! Match BWFFile's NUL-terminated, word-aligned INFO subchunk layout.
integer, intent(in) :: nchar
integer*4 n
n=nchar+1
wav_info_field_bytes=8+n+mod(n,2)
end function wav_info_field_bytes
integer*4 function wav_trailer_length(isft,icrd,icmt)
! Return the complete LIST chunk size, including its 8-byte header.
character(len=*), intent(in) :: isft,icrd,icmt
integer*4 wav_info_field_bytes
wav_trailer_length = 8 + 4 & !'LIST' id+size, 'INFO'
+ wav_info_field_bytes(len_trim(isft)) &
+ wav_info_field_bytes(len_trim(icrd)) &
+ wav_info_field_bytes(len_trim(icmt))
end function wav_trailer_length
subroutine write_wav_info_trailer(lu,isft,icrd,icmt)
! Append LIST/INFO metadata at the current stream position.
integer, intent(in) :: lu
character(len=*), intent(in) :: isft,icrd,icmt
integer*4 listlen,wav_info_field_bytes
listlen = 4 & !'INFO'
+ wav_info_field_bytes(len_trim(isft)) &
+ wav_info_field_bytes(len_trim(icrd)) &
+ wav_info_field_bytes(len_trim(icmt))
write(lu) 'LIST',listlen,'INFO'
call write_wav_info_field(lu,'ISFT',isft(1:len_trim(isft)))
call write_wav_info_field(lu,'ICRD',icrd(1:len_trim(icrd)))
call write_wav_info_field(lu,'ICMT',icmt(1:len_trim(icmt)))
end subroutine write_wav_info_trailer
subroutine write_wav_info_field(lu,id,value)
! Write one NUL-terminated, word-aligned INFO subchunk.
integer, intent(in) :: lu
character*4, intent(in) :: id
character(len=*), intent(in) :: value
integer*4 n
n=len(value)+1
write(lu) id,n,value,char(0)
if(mod(n,2).eq.1) write(lu) char(0)
end subroutine write_wav_info_field
|