Waveform filter programs
evelo.f
Go to the documentation of this file.
1 c this is <evelo.f>
2 c------------------------------------------------------------------------------
3 c
4 c 28/01/99 by Thomas Forbriger (IfG Stuttgart)
5 c
6 c calculate EnVELOpe of time series
7 c
8 c ----
9 c This program is free software; you can redistribute it and/or modify
10 c it under the terms of the GNU General Public License as published by
11 c the Free Software Foundation; either version 2 of the License, or
12 c (at your option) any later version.
13 c
14 c This program is distributed in the hope that it will be useful,
15 c but WITHOUT ANY WARRANTY; without even the implied warranty of
16 c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 c GNU General Public License for more details.
18 c
19 c You should have received a copy of the GNU General Public License
20 c along with this program; if not, write to the Free Software
21 c Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 c ----
23 c
24 c REVISIONS and CHANGES
25 c 28/01/99 V1.0 Thomas Forbriger
26 c 23/02/99 V1.1 there was a factor of two missing
27 c 28/02/99 V1.2 allow calculation of hilbert transform
28 c 01/03/99 V1.3 calculate spectral coefficients
29 c 25/06/01 V1.4 depart from GSE sampling rate standard formst if
30 c necessary
31 c 04/12/09 V1.5 corrected comment
32 c 05/07/14 V1.6 provide access to other data formats
33 c 14/02/17 V1.7 fix option default (set envelope to .false.)
34 c
35 c==============================================================================
36 c
37  program evelo
38 c
39  character*79 version
40  parameter(version=
41  & 'EVELO V1.7 calculate EnVELOpe of time series')
42 c
43  integer maxsamples,nsamp,nspa,i, npow, limit
44  parameter(maxsamples=310000)
45 c
46  real fdata(maxsamples)
47  real scalefact,pi
48  integer idata(maxsamples)
49  equivalence(fdata,idata)
50  double complex ftarray(maxsamples)
51  parameter(pi=3.14159265358979311)
52 c
53  character*80 infile
54  character*80 outfile
55  character*80 comment
56 c
57  integer luin, luout
58  parameter(luin=10, luout=11)
59 c
60  logical last, hilbert, realspec, imagspec, ampspec, phasespec
61  logical envelope, pi4shift
62 c
63  real tanf,dt
64  real c1,c2,c3,readversion
65  integer nstack,ierr
66  character*1 cs
67  character*14 timestamp
68  character*10 code
69  character*132 wid2line
70  character*20 srctype
71  character*6 srcdate
72  character*10 srctime
73 c
74  character*80 inputformat, outputformat
75 c
76  integer maxfree, maxreadfree, nfree, lenmax
77  parameter(maxfree=200, maxreadfree=maxfree-1)
78  character*80 free(maxfree)
79 c commandline
80  integer maxopt, lastarg, iargc
81  character*80 argument
82  parameter(maxopt=11)
83  character*3 optid(maxopt)
84  character*40 optarg(maxopt)
85  logical optset(maxopt), opthasarg(maxopt)
86 c debugging
87  logical debug, verbose
88 c here are the keys to our commandline options
89  data optid/'-d', '-v', '-H', '-R', '-I', '-A', '-P', '-4',
90  & '-ty', '-it', '-ot'/
91  data opthasarg/8*.false.,3*.true./
92  data optarg/8*'-',3*'sff'/
93 c
94 c------------------------------------------------------------------------------
95 c basic information
96 c
97 c
98  argument=' '
99  if (iargc().eq.1) call getarg(1, argument)
100  if ((argument(1:5).eq.'-help').or.(iargc().lt.2)) then
101  print *,version
102  print *,'Usage: evelo infile outfile [-H|-R|-I|-A|-P|-4]'
103  print *,' [-ty f] [-it f] [-ot f]'
104  print *,' or: evelo -help'
105  if (argument(1:5).ne.'-help') stop 'ERROR: wrong number of arguments'
106  print *,' '
107  print *,'calculate EnVELOpe of time series'
108  print *,' '
109  print *,'-H Calculate Hilbert transform of input'
110  print *,' signal rather than envelope.'
111  print *,'-R calculate real part of Fourier spectrum'
112  print *,'-I calculate imaginary part of Fourier spectrum'
113  print *,'-A calculate Fourier amplitude spectrum'
114  print *,'-P calculate Fourier phase spectrum'
115  print *,'-4 phase shift by pi/4'
116  print *,' '
117  print *,'-ty f choose file format ''f'' instead of SFF'
118  print *,' for input and output files'
119  print *,'-it f choose input file format ''f'' instead of SFF'
120  print *,'-ot f choose output file format ''f'' instead of SFF'
121  print *,' '
122  print *,'The switches are exclusive with priority in the order'
123  print *,'listed above.'
124  print *,' '
125  print *,'In case of Fourier output one second means one Hertz.'
126  print *,' '
127  print *,'The output Fourier spectrum is normalized to be'
128  print *,'equivalent to the corresponding integral transform.'
129  print *,'Thus Fourier coeffients and spectral amplitudes are'
130  print *,'given in [amplitude]/Hz, where [amplitude] is'
131  print *,'the unit of the input samples.'
132  print *,' '
133  call sff_help_formats
134  stop
135  endif
136 c
137 c------------------------------------------------------------------------------
138 c read command line arguments
139 c
140  call tf_cmdline(3, lastarg, maxopt, optid,
141  & optarg, optset, opthasarg)
142  debug=optset(1)
143  verbose=optset(2)
144  hilbert=optset(3)
145 c
146  envelope=.false.
147  realspec=.false.
148  imagspec=.false.
149  ampspec=.false.
150  phasespec=.false.
151  pi4shift=.false.
152 c
153  if (.not.(hilbert)) then
154  realspec=optset(4)
155  if (.not.(realspec)) then
156  imagspec=optset(5)
157  if (.not.(imagspec)) then
158  ampspec=optset(6)
159  if (.not.(ampspec)) then
160  phasespec=optset(7)
161  if (.not.(phasespec)) then
162  pi4shift=optset(8)
163  if (.not.(pi4shift)) envelope=.true.
164  endif
165  endif
166  endif
167  endif
168  endif
169  inputformat=optarg(9)
170  outputformat=optarg(9)
171  if (optset(10)) inputformat=optarg(10)
172  if (optset(11)) outputformat=optarg(11)
173 c
174 c
175  call getarg(1, infile)
176  call getarg(2, outfile)
177 c
178  call sff_select_input_format(inputformat, ierr)
179  if (ierr.ne.0) stop 'ERROR: selected input format is not supported'
180  call sff_select_output_format(outputformat, ierr)
181  if (ierr.ne.0) stop 'ERROR: selected output format is not supported'
182 c
183 c------------------------------------------------------------------------------
184 c go
185 c
186  call sff_ropenfs(luin, infile, readversion, timestamp, code,
187  & nfree, free, lenmax, maxreadfree,
188  & srctype, cs, c1, c2, c3, srcdate, srctime, ierr)
189  if (ierr.ne.0) stop 'ERROR: opening input file'
190 c
191  nfree=min(nfree+1, maxfree)
192  free(nfree)=version
193  comment='ERROR in algorithm selection code'
194  if (envelope) comment='calculate envelope'
195  if (hilbert) comment='calculate Hilbert transform'
196  if (pi4shift) comment='shift by pi/4'
197  if (realspec) comment='calculate real part of Fourier spectrum'
198  if (imagspec) comment=
199  & 'calculate imaginary part of Fourier spectrum'
200  if (ampspec) comment='calculate Fourier amplitude spectrum'
201  if (phasespec) comment='calculate Fourier phase spectrum'
202  print *,comment
203  if (comment(1:6).eq.'ERROR ') stop
204  nfree=min(nfree+1, maxfree)
205  free(nfree)=comment
206 
207  if (envelope) print *,'envelop option (default) is selected'
208  if (hilbert) print *,'hilbert option -H is selected'
209  if (pi4shift) print *,'pi4shift option -4 is selected'
210  if (realspec) print *,'realspec option -R is selected'
211  if (imagspec) print *,'imagspec option -I is selected'
212  if (ampspec) print *,'ampspec option -A is selected'
213  if (phasespec) print *,'phasespec option -P is selected'
214 c
215  call sff_wopenfs(luout, outfile,
216  & free, nfree, srctype, cs, c1, c2, c3, srcdate, srctime, ierr)
217  if (ierr.ne.0) stop 'ERROR: opening output file'
218 c
219  last=.false.
220  do while(.not.(last))
221  nsamp=maxsamples
222  call sff_rtracefi(luin, tanf, dt,
223  & wid2line, nsamp, fdata, idata, code, last,
224  & nfree, free, maxreadfree, lenmax,
225  & cs, c1, c2, c3, nstack, ierr)
226  if (ierr.ne.0) stop 'ERROR: reading input file'
227 c
228  nfree=min(nfree+1, maxfree)
229  free(nfree)=version
230  nfree=min(nfree+1, maxfree)
231  free(nfree)=comment
232 c
233  npow=0
234  nspa=2**npow
235  do while (nspa.lt.nsamp)
236  npow=npow+1
237  nspa=2**npow
238  enddo
239  if (nspa.gt.maxsamples) stop 'ERROR: too many samples'
240 c
241  do i=1,nspa
242  ftarray(i)=(0.d0,0.d0)
243  if (i.le.nsamp) ftarray(i)=cmplx(fdata(i), 0.)
244  enddo
245 c
246  call tf_dfork(nspa, ftarray, -1.d0)
247 c
248  if (hilbert) then
249  limit=nspa/2+1
250  do i=2,limit
251  ftarray(i)=(0.d0,1.d0)*ftarray(i)
252  enddo
253  ftarray(limit)=(0.d0,0.d0)
254  do i=limit+2,nspa
255  ftarray(i)=(0.d0,-1.d0)*ftarray(i)
256  enddo
257  elseif (pi4shift) then
258  limit=nspa/2+1
259  do i=2,limit
260  ftarray(i)=sqrt(1/2.)*(1.d0,1.d0)*ftarray(i)
261  enddo
262  ftarray(limit)=(0.d0,0.d0)
263  do i=limit+2,nspa
264  ftarray(i)=sqrt(1/2.)*(1.d0,-1.d0)*ftarray(i)
265  enddo
266  elseif (envelope) then
267  limit=nspa/2+1
268  do i=limit+1,nspa
269  ftarray(i)=(0.d0,0.d0)
270  enddo
271  endif
272 c
273  if ((envelope).or.(hilbert).or.(pi4shift))
274  & call tf_dfork(nspa, ftarray, 1.d0)
275 c
276  scalefact=dt*sqrt(float(nspa))
277  if (hilbert.or.pi4shift) then
278  do i=1,nsamp
279  fdata(i)=sngl(real(ftarray(i)))
280  enddo
281  elseif (envelope) then
282  do i=1,nsamp
283  fdata(i)=sngl(2.d0*abs(ftarray(i)))
284  enddo
285  elseif (realspec) then
286  nsamp=nspa
287  do i=1,nsamp
288  fdata(i)=sngl(scalefact*real(ftarray(i)))
289  enddo
290  elseif (imagspec) then
291  nsamp=nspa
292  do i=1,nsamp
293  fdata(i)=sngl(scalefact*imag(ftarray(i)))
294  enddo
295  elseif (ampspec) then
296  nsamp=nspa
297  do i=1,nsamp
298  fdata(i)=sngl(scalefact*abs(ftarray(i)))
299  enddo
300  elseif (phasespec) then
301  nsamp=nspa
302  do i=1,nsamp
303  fdata(i)=sngl(atan2(imag(ftarray(i)),real(ftarray(i))))*
304  & 180./pi
305  enddo
306  else
307  stop 'ERROR in selection code'
308  endif
309 c
310  if ((phasespec).or.(ampspec).or.(realspec).or.(imagspec)) then
311  call sff_modwid2samps(wid2line,nsamp)
312  if ((nsamp*dt).lt.1.e3) then
313  call sff_modwid2samprat(wid2line, (nsamp*dt))
314  else
315  write(wid2line(58:68), '(e11.6)') (nsamp*dt)
316  endif
317  endif
318 c
319  call sff_wtracefi(luout,
320  & wid2line, nsamp, fdata, idata, last,
321  & nfree, free, cs, c1, c2, c3, nstack, ierr)
322  if (ierr.ne.0) stop 'ERROR: writing output file'
323  enddo
324 c
325  stop
326  end
327 c
328 c ----- END OF evelo.f -----
program evelo
Definition: evelo.f:37