/
tf_misfit.py
1555 lines (1313 loc) · 58.6 KB
/
tf_misfit.py
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------
# Filename: tf_misfit.py
# Purpose: Various Time Frequency Misfit Functions
# Author: Martin van Driel
# Email: vandriel@sed.ethz.ch
#
# Copyright (C) 2012 Martin van Driel
# --------------------------------------------------------------------
"""
Various Time Frequency Misfit Functions based on [Kristekova2006]_ and
[Kristekova2009]_.
:copyright:
The ObsPy Development Team (devs@obspy.org)
:license:
GNU Lesser General Public License, Version 3
(https://www.gnu.org/copyleft/lesser.html)
"""
import numpy as np
from obspy.imaging.cm import obspy_sequential, obspy_divergent
from obspy.signal import util
def _pcolormesh_same_dim(ax, x, y, v, **kwargs):
# x, y, v must have the same dimension
try:
return ax.pcolormesh(x, y, v, shading='nearest', **kwargs)
except TypeError:
# matplotlib versions < 3.3
return ax.pcolormesh(x, y, v[:-1, :-1], **kwargs)
def cwt(st, dt, w0, fmin, fmax, nf=100, wl='morlet'):
"""
Continuous Wavelet Transformation in the Frequency Domain.
.. seealso:: [Kristekova2006]_, eq. (4)
:param st: time dependent signal.
:param dt: time step between two samples in st (in seconds)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param fmin: minimum frequency (in Hz)
:param fmax: maximum frequency (in Hz)
:param nf: number of logarithmically spaced frequencies between fmin and
fmax
:param wl: wavelet to use, for now only 'morlet' is implemented
:return: time frequency representation of st, type numpy.ndarray of complex
values, shape = (nf, len(st)).
"""
npts = len(st) * 2
tmax = (npts - 1) * dt
t = np.linspace(0., tmax, npts)
f = np.logspace(np.log10(fmin), np.log10(fmax), nf)
cwt = np.zeros((npts // 2, nf), dtype=complex)
if wl == 'morlet':
def psi(t):
return np.pi ** (-.25) * np.exp(1j * w0 * t) * \
np.exp(-t ** 2 / 2.)
def scale(f):
return w0 / (2 * np.pi * f)
else:
raise ValueError('wavelet type "' + wl + '" not defined!')
nfft = util.next_pow_2(npts) * 2
sf = np.fft.fft(st, n=nfft)
# Ignore underflows.
with np.errstate(under="ignore"):
for n, _f in enumerate(f):
a = scale(_f)
# time shift necessary, because wavelet is defined around t = 0
psih = psi(-1 * (t - t[-1] / 2.) / a).conjugate() / np.abs(a) ** .5
psihf = np.fft.fft(psih, n=nfft)
tminin = int(t[-1] / 2. / (t[1] - t[0]))
cwt[:, n] = np.fft.ifft(psihf * sf)[tminin:tminin + npts // 2] * \
(t[1] - t[0])
return cwt.T
def tfem(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True):
"""
Time Frequency Envelope Misfit
.. seealso:: [Kristekova2009]_, Table 1. and 2.
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:return: time frequency representation of Envelope Misfit,
type numpy.ndarray with shape (nf, len(st1)) for single component data
and (number of components, nf, len(st1)) for multicomponent data
"""
if len(st1.shape) == 1:
w_1 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_2 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_1[0] = cwt(st1, dt, w0, fmin, fmax, nf)
w_2[0] = cwt(st2, dt, w0, fmin, fmax, nf)
else:
w_1 = np.zeros((st1.shape[0], nf, st1.shape[1]), dtype=complex)
w_2 = np.zeros((st2.shape[0], nf, st2.shape[1]), dtype=complex)
for i in np.arange(st1.shape[0]):
w_1[i] = cwt(st1[i], dt, w0, fmin, fmax, nf)
w_2[i] = cwt(st2[i], dt, w0, fmin, fmax, nf)
if st2_isref:
ar = np.abs(w_2)
else:
if np.abs(w_1).max() > np.abs(w_2).max():
ar = np.abs(w_1)
else:
ar = np.abs(w_2)
_tfem = (np.abs(w_1) - np.abs(w_2))
if norm == 'global':
if len(st1.shape) == 1:
return _tfem[0] / np.max(ar)
else:
return _tfem / np.max(ar)
elif norm == 'local':
if len(st1.shape) == 1:
return _tfem[0] / ar[0]
else:
return _tfem / ar
else:
raise ValueError('norm "' + norm + '" not defined!')
def tfpm(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True):
"""
Time Frequency Phase Misfit
.. seealso:: [Kristekova2009]_, Table 1. and 2.
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:return: time frequency representation of Phase Misfit,
type numpy.ndarray with shape (nf, len(st1)) for single component data
and (number of components, nf, len(st1)) for multicomponent data
"""
if len(st1.shape) == 1:
w_1 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_2 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_1[0] = cwt(st1, dt, w0, fmin, fmax, nf)
w_2[0] = cwt(st2, dt, w0, fmin, fmax, nf)
else:
w_1 = np.zeros((st1.shape[0], nf, st1.shape[1]), dtype=complex)
w_2 = np.zeros((st2.shape[0], nf, st2.shape[1]), dtype=complex)
for i in np.arange(st1.shape[0]):
w_1[i] = cwt(st1[i], dt, w0, fmin, fmax, nf)
w_2[i] = cwt(st2[i], dt, w0, fmin, fmax, nf)
if st2_isref:
_ar = np.abs(w_2)
else:
if np.abs(w_1).max() > np.abs(w_2).max():
_ar = np.abs(w_1)
else:
_ar = np.abs(w_2)
_tfpm = np.angle(w_1 / w_2) / np.pi
if norm == 'global':
if len(st1.shape) == 1:
return _ar[0] * _tfpm[0] / np.max(_ar)
else:
return _ar * _tfpm / np.max(_ar)
elif norm == 'local':
if len(st1.shape) == 1:
return _tfpm[0]
else:
return _tfpm
else:
raise ValueError('norm "' + norm + '" not defined!')
def tem(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True):
"""
Time-dependent Envelope Misfit
.. seealso:: [Kristekova2009]_, Table 1. and 2.
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:return: Time-dependent Envelope Misfit, type numpy.ndarray with shape
(len(st1),) for single component data and (number of components,
len(st1)) for multicomponent data
"""
if len(st1.shape) == 1:
w_1 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_2 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_1[0] = cwt(st1, dt, w0, fmin, fmax, nf)
w_2[0] = cwt(st2, dt, w0, fmin, fmax, nf)
else:
w_1 = np.zeros((st1.shape[0], nf, st1.shape[1]), dtype=complex)
w_2 = np.zeros((st2.shape[0], nf, st2.shape[1]), dtype=complex)
for i in np.arange(st1.shape[0]):
w_1[i] = cwt(st1[i], dt, w0, fmin, fmax, nf)
w_2[i] = cwt(st2[i], dt, w0, fmin, fmax, nf)
if st2_isref:
_ar = np.abs(w_2)
else:
if np.abs(w_1).max() > np.abs(w_2).max():
_ar = np.abs(w_1)
else:
_ar = np.abs(w_2)
_tem = np.sum((np.abs(w_1) - np.abs(w_2)), axis=1)
if norm == 'global':
if len(st1.shape) == 1:
return _tem[0] / np.max(np.sum(_ar, axis=1))
else:
return _tem / np.max(np.sum(_ar, axis=1))
elif norm == 'local':
if len(st1.shape) == 1:
return _tem[0] / np.sum(_ar, axis=1)[0]
else:
return _tem / np.sum(_ar, axis=1)
else:
raise ValueError('norm "' + norm + '" not defined!')
def tpm(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True):
"""
Time-dependent Phase Misfit
.. seealso:: [Kristekova2009]_, Table 1. and 2.
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:return: Time-dependent Phase Misfit, type numpy.ndarray with shape
(len(st1),) for single component data and (number of components,
len(st1)) for multicomponent data
"""
if len(st1.shape) == 1:
w_1 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_2 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_1[0] = cwt(st1, dt, w0, fmin, fmax, nf)
w_2[0] = cwt(st2, dt, w0, fmin, fmax, nf)
else:
w_1 = np.zeros((st1.shape[0], nf, st1.shape[1]), dtype=complex)
w_2 = np.zeros((st2.shape[0], nf, st2.shape[1]), dtype=complex)
for i in np.arange(st1.shape[0]):
w_1[i] = cwt(st1[i], dt, w0, fmin, fmax, nf)
w_2[i] = cwt(st2[i], dt, w0, fmin, fmax, nf)
if st2_isref:
_ar = np.abs(w_2)
else:
if np.abs(w_1).max() > np.abs(w_2).max():
_ar = np.abs(w_2)
else:
_ar = np.abs(w_1)
_tpm = np.angle(w_1 / w_2) / np.pi
_tpm = np.sum(_ar * _tpm, axis=1)
if norm == 'global':
if len(st1.shape) == 1:
return _tpm[0] / np.max(np.sum(_ar, axis=1))
else:
return _tpm / np.max(np.sum(_ar, axis=1))
elif norm == 'local':
if len(st1.shape) == 1:
return _tpm[0] / np.sum(_ar, axis=1)[0]
else:
return _tpm / np.sum(_ar, axis=1)
else:
raise ValueError('norm "' + norm + '" not defined!')
def fem(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True):
"""
Frequency-dependent Envelope Misfit
.. seealso:: [Kristekova2009]_, Table 1. and 2.
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:return: Frequency-dependent Envelope Misfit, type numpy.ndarray with shape
(nf,) for single component data and (number of components, nf) for
multicomponent data
"""
if len(st1.shape) == 1:
w_1 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_2 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_1[0] = cwt(st1, dt, w0, fmin, fmax, nf)
w_2[0] = cwt(st2, dt, w0, fmin, fmax, nf)
else:
w_1 = np.zeros((st1.shape[0], nf, st1.shape[1]), dtype=complex)
w_2 = np.zeros((st2.shape[0], nf, st2.shape[1]), dtype=complex)
for i in np.arange(st1.shape[0]):
w_1[i] = cwt(st1[i], dt, w0, fmin, fmax, nf)
w_2[i] = cwt(st2[i], dt, w0, fmin, fmax, nf)
if st2_isref:
_ar = np.abs(w_2)
else:
if np.abs(w_1).max() > np.abs(w_2).max():
_ar = np.abs(w_1)
else:
_ar = np.abs(w_2)
_tem = np.abs(w_1) - np.abs(w_2)
_tem = np.sum(_tem, axis=2)
if norm == 'global':
if len(st1.shape) == 1:
return _tem[0] / np.max(np.sum(_ar, axis=2))
else:
return _tem / np.max(np.sum(_ar, axis=2))
elif norm == 'local':
if len(st1.shape) == 1:
return _tem[0] / np.sum(_ar, axis=2)[0]
else:
return _tem / np.sum(_ar, axis=2)
else:
raise ValueError('norm "' + norm + '" not defined!')
def fpm(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True):
"""
Frequency-dependent Phase Misfit
.. seealso:: [Kristekova2009]_, Table 1. and 2.
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:return: Frequency-dependent Phase Misfit, type numpy.ndarray with shape
(nf,) for single component data and (number of components, nf) for
multicomponent data
"""
if len(st1.shape) == 1:
w_1 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_2 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_1[0] = cwt(st1, dt, w0, fmin, fmax, nf)
w_2[0] = cwt(st2, dt, w0, fmin, fmax, nf)
else:
w_1 = np.zeros((st1.shape[0], nf, st1.shape[1]), dtype=complex)
w_2 = np.zeros((st2.shape[0], nf, st2.shape[1]), dtype=complex)
for i in np.arange(st1.shape[0]):
w_1[i] = cwt(st1[i], dt, w0, fmin, fmax, nf)
w_2[i] = cwt(st2[i], dt, w0, fmin, fmax, nf)
if st2_isref:
_ar = np.abs(w_2)
else:
if np.abs(w_1).max() > np.abs(w_2).max():
_ar = np.abs(w_1)
else:
_ar = np.abs(w_2)
_tpm = np.angle(w_1 / w_2) / np.pi
_tpm = np.sum(_ar * _tpm, axis=2)
if norm == 'global':
if len(st1.shape) == 1:
return _tpm[0] / np.max(np.sum(_ar, axis=2))
else:
return _tpm / np.max(np.sum(_ar, axis=2))
elif norm == 'local':
if len(st1.shape) == 1:
return _tpm[0] / np.sum(_ar, axis=2)[0]
else:
return _tpm / np.sum(_ar, axis=2)
else:
raise ValueError('norm "' + norm + '" not defined!')
def em(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True):
"""
Single Valued Envelope Misfit
.. seealso:: [Kristekova2009]_, Table 1. and 2.
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:return: Single Valued Envelope Misfit
"""
if len(st1.shape) == 1:
w_1 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_2 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_1[0] = cwt(st1, dt, w0, fmin, fmax, nf)
w_2[0] = cwt(st2, dt, w0, fmin, fmax, nf)
else:
w_1 = np.zeros((st1.shape[0], nf, st1.shape[1]), dtype=complex)
w_2 = np.zeros((st2.shape[0], nf, st2.shape[1]), dtype=complex)
for i in np.arange(st1.shape[0]):
w_1[i] = cwt(st1[i], dt, w0, fmin, fmax, nf)
w_2[i] = cwt(st2[i], dt, w0, fmin, fmax, nf)
if st2_isref:
_ar = np.abs(w_2)
else:
if np.abs(w_1).max() > np.abs(w_2).max():
_ar = np.abs(w_1)
else:
_ar = np.abs(w_2)
_em = (np.sum(np.sum((np.abs(w_1) - np.abs(w_2)) ** 2, axis=2),
axis=1)) ** .5
if norm == 'global':
if len(st1.shape) == 1:
return _em[0] / (np.sum(_ar ** 2)) ** .5
else:
return _em / ((np.sum(np.sum(_ar ** 2, axis=2),
axis=1)) ** .5).max()
elif norm == 'local':
if len(st1.shape) == 1:
return _em[0] / (np.sum(_ar ** 2)) ** .5
else:
return _em / (np.sum(np.sum(_ar ** 2, axis=2), axis=1)) ** .5
else:
raise ValueError('norm "' + norm + '" not defined!')
def pm(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True):
"""
Single Valued Phase Misfit
.. seealso:: [Kristekova2009]_, Table 1. and 2.
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:return: Single Valued Phase Misfit
"""
if len(st1.shape) == 1:
w_1 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_2 = np.zeros((1, nf, st1.shape[0]), dtype=complex)
w_1[0] = cwt(st1, dt, w0, fmin, fmax, nf)
w_2[0] = cwt(st2, dt, w0, fmin, fmax, nf)
else:
w_1 = np.zeros((st1.shape[0], nf, st1.shape[1]), dtype=complex)
w_2 = np.zeros((st2.shape[0], nf, st2.shape[1]), dtype=complex)
for i in np.arange(st1.shape[0]):
w_1[i] = cwt(st1[i], dt, w0, fmin, fmax, nf)
w_2[i] = cwt(st2[i], dt, w0, fmin, fmax, nf)
if st2_isref:
_ar = np.abs(w_2)
else:
if np.abs(w_1).max() > np.abs(w_2).max():
_ar = np.abs(w_1)
else:
_ar = np.abs(w_2)
_pm = np.angle(w_1 / w_2) / np.pi
_pm = (np.sum(np.sum((_ar * _pm) ** 2, axis=2), axis=1)) ** .5
if norm == 'global':
if len(st1.shape) == 1:
return _pm[0] / (np.sum(_ar ** 2)) ** .5
else:
return _pm / ((np.sum(np.sum(_ar ** 2, axis=2),
axis=1)) ** .5).max()
elif norm == 'local':
if len(st1.shape) == 1:
return _pm[0] / (np.sum(_ar ** 2)) ** .5
else:
return _pm / (np.sum(np.sum(_ar ** 2, axis=2), axis=1)) ** .5
else:
raise ValueError('norm "' + norm + '" not defined!')
def tfeg(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True, a=10., k=1.):
"""
Time Frequency Envelope Goodness-of-Fit
.. seealso:: [Kristekova2009]_, Eq.(15)
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:param a: Maximum value of Goodness-of-Fit for perfect agreement
:param k: sensitivity of Goodness-of-Fit to the misfit
:return: time frequency representation of Envelope Goodness-of-Fit,
type numpy.ndarray with shape (nf, len(st1)) for single component data
and (number of components, nf, len(st1)) for multicomponent data
"""
_tfem = tfem(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0,
norm=norm, st2_isref=st2_isref)
return a * np.exp(-np.abs(_tfem) ** k)
def tfpg(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True, a=10., k=1.):
"""
Time Frequency Phase Goodness-of-Fit
.. seealso:: [Kristekova2009]_, Eq.(16)
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:param a: Maximum value of Goodness-of-Fit for perfect agreement
:param k: sensitivity of Goodness-of-Fit to the misfit
:return: time frequency representation of Phase Goodness-of-Fit,
type numpy.ndarray with shape (nf, len(st1)) for single component data
and (number of components, nf, len(st1)) for multicomponent data
"""
_tfpm = tfpm(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0,
norm=norm, st2_isref=st2_isref)
return a * (1 - np.abs(_tfpm) ** k)
def teg(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True, a=10., k=1.):
"""
Time-dependent Envelope Goodness-of-Fit
.. seealso:: [Kristekova2009]_, Eq.(15)
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:param a: Maximum value of Goodness-of-Fit for perfect agreement
:param k: sensitivity of Goodness-of-Fit to the misfit
:return: time dependent Envelope Goodness-of-Fit, type numpy.ndarray with
shape (len(st1),) for single component data and (number of components,
len(st1)) for multicomponent data
"""
_tem = tem(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
return a * np.exp(-np.abs(_tem) ** k)
def tpg(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True, a=10., k=1.):
"""
Time-dependent Phase Goodness-of-Fit
.. seealso:: [Kristekova2009]_, Eq.(16)
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:param a: Maximum value of Goodness-of-Fit for perfect agreement
:param k: sensitivity of Goodness-of-Fit to the misfit
:return: time dependent Phase Goodness-of-Fit, type numpy.ndarray with
shape (len(st1),) for single component data and (number of components,
len(st1)) for multicomponent data
"""
_tpm = tpm(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
return a * (1 - np.abs(_tpm) ** k)
def feg(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True, a=10., k=1.):
"""
Frequency-dependent Envelope Goodness-of-Fit
.. seealso:: [Kristekova2009]_, Eq.(15)
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:param a: Maximum value of Goodness-of-Fit for perfect agreement
:param k: sensitivity of Goodness-of-Fit to the misfit
:return: frequency dependent Envelope Goodness-of-Fit, type numpy.ndarray
with shape (nf,) for single component data and (number of components,
nf) for multicomponent data
"""
_fem = fem(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
return a * np.exp(-np.abs(_fem) ** k)
def fpg(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True, a=10., k=1.):
"""
Frequency-dependent Phase Goodness-of-Fit
.. seealso:: [Kristekova2009]_, Eq.(16)
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:param a: Maximum value of Goodness-of-Fit for perfect agreement
:param k: sensitivity of Goodness-of-Fit to the misfit
:return: frequency dependent Phase Goodness-of-Fit, type numpy.ndarray
with shape (nf,) for single component data and (number of components,
nf) for multicomponent data
"""
_fpm = fpm(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
return a * (1 - np.abs(_fpm) ** k)
def eg(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True, a=10., k=1.):
"""
Single Valued Envelope Goodness-of-Fit
.. seealso:: [Kristekova2009]_, Eq.(15)
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:param a: Maximum value of Goodness-of-Fit for perfect agreement
:param k: sensitivity of Goodness-of-Fit to the misfit
:return: Single Valued Envelope Goodness-of-Fit
"""
_em = em(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
return a * np.exp(-np.abs(_em) ** k)
def pg(st1, st2, dt=0.01, fmin=1., fmax=10., nf=100, w0=6, norm='global',
st2_isref=True, a=10., k=1.):
"""
Single Valued Phase Goodness-of-Fit
.. seealso:: [Kristekova2009]_, Eq.(16)
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:param a: Maximum value of Goodness-of-Fit for perfect agreement
:param k: sensitivity of Goodness-of-Fit to the misfit
:return: Single Valued Phase Goodness-of-Fit
"""
_pm = pm(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
return a * (1 - np.abs(_pm) ** k)
def plot_tf_misfits(st1, st2, dt=0.01, t0=0., fmin=1., fmax=10., nf=100, w0=6,
norm='global', st2_isref=True, left=0.1, bottom=0.1,
h_1=0.2, h_2=0.125, h_3=0.2, w_1=0.2, w_2=0.6, w_cb=0.01,
d_cb=0.0, show=True, plot_args=['k', 'r', 'b'], ylim=0.,
clim=0., cmap=obspy_divergent):
"""
Plot all time frequency misfits and the time series in one plot (per
component).
:param st1: signal 1 of two signals to compare, type numpy.ndarray with
shape (number of components, number of time samples) or (number of
timesamples, ) for single component data
:param st2: signal 2 of two signals to compare, type and shape as st1
:param dt: time step between two samples in st1 and st2
:param t0: starting time for plotting
:param fmin: minimal frequency to be analyzed
:param fmax: maximal frequency to be analyzed
:param nf: number of frequencies (will be chosen with logarithmic spacing)
:param w0: parameter for the wavelet, tradeoff between time and frequency
resolution
:param norm: 'global' or 'local' normalization of the misfit
:type st2_isref: bool
:param st2_isref: True if st2 is a reference signal, False if none is a
reference
:param left: plot distance from the left of the figure
:param bottom: plot distance from the bottom of the figure
:param h_1: height of the signal axes
:param h_2: height of the TEM and TPM axes
:param h_3: height of the TFEM and TFPM axes
:param w_1: width of the FEM and FPM axes
:param w_2: width of the TFEM, TFPM, signal etc. axes
:param w_cb: width of the colorbar axes
:param d_cb: distance of the colorbar axes to the other axes
:param show: show figure or return
:param plot_args: list of plot arguments passed to the signal 1/2 and
TEM/TPM/FEM/FPM plots
:param ylim: limits in misfit for TEM/TPM/FEM/FPM
:param clim: limits of the colorbars
:param cmap: colormap for TFEM/TFPM, either a string or
matplotlib.cm.Colormap instance
:return: If show is False, returns a matplotlib.pyplot.figure object
(single component data) or a list of figure objects (multi component
data)
.. rubric:: Example
For a signal with pure phase error
.. seealso:: [Kristekova2006]_, Fig.(4)
>>> import numpy as np
>>> from scipy.signal import hilbert
>>> tmax = 6.
>>> dt = 0.01
>>> npts = int(tmax / dt + 1)
>>> t = np.linspace(0., tmax, npts)
>>> A1 = 4.
>>> t1 = 2.
>>> f1 = 2.
>>> phi1 = 0.
>>> phase_shift = 0.1
>>> H1 = (np.sign(t - t1) + 1)/ 2
>>> st1 = (A1 * (t - t1) * np.exp(-2*(t - t1)) *
... np.cos(2. * np.pi * f1 * (t - t1) + phi1 * np.pi) * H1)
>>> # Reference signal
>>> st2 = st1.copy()
>>> # Distorted signal:
>>> # generate analytical signal (hilbert transform) and add phase shift
>>> st1 = hilbert(st1)
>>> st1 = np.real(np.abs(st1) * np.exp((np.angle(st1) +
... phase_shift * np.pi) * 1j))
>>> plot_tf_misfits(st1, st2, dt=dt, fmin=1., fmax=10.) # doctest: +SKIP
.. plot::
import numpy as np
from scipy.signal import hilbert
from obspy.signal.tf_misfit import plot_tf_misfits
tmax = 6.
dt = 0.01
npts = int(tmax / dt + 1)
t = np.linspace(0., tmax, npts)
A1 = 4.
t1 = 2.
f1 = 2.
phi1 = 0.
phase_shift = 0.1
H1 = (np.sign(t - t1) + 1)/ 2
st1 = (A1 * (t - t1) * np.exp(-2*(t - t1)) *
np.cos(2. * np.pi * f1 * (t - t1) + phi1 * np.pi) * H1)
# Reference signal
st2 = st1.copy()
# Distorted signal:
# generate analytical signal (hilbert transform) and add phase shift
st1 = hilbert(st1)
st1 = np.real(np.abs(st1) * np.exp((np.angle(st1) +
phase_shift * np.pi) * 1j))
plot_tf_misfits(st1, st2, dt=dt, fmin=1., fmax=10.)
"""
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
npts = st1.shape[-1]
tmax = (npts - 1) * dt
t = np.linspace(0., tmax, npts) + t0
f = np.logspace(np.log10(fmin), np.log10(fmax), nf)
# compute time frequency misfits
_tfem = tfem(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0,
norm=norm, st2_isref=st2_isref)
_tem = tem(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
_fem = fem(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
_em = em(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
_tfpm = tfpm(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0,
norm=norm, st2_isref=st2_isref)
_tpm = tpm(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
_fpm = fpm(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
_pm = pm(st1, st2, dt=dt, fmin=fmin, fmax=fmax, nf=nf, w0=w0, norm=norm,
st2_isref=st2_isref)
if len(st1.shape) == 1:
_tfem = _tfem.reshape((1, nf, npts))
_tem = _tem.reshape((1, npts))
_fem = _fem.reshape((1, nf))
_em = _em.reshape((1, 1))
_tfpm = _tfpm.reshape((1, nf, npts))
_tpm = _tpm.reshape((1, npts))
_fpm = _fpm.reshape((1, nf))
_pm = _pm.reshape((1, 1))
st1 = st1.reshape((1, npts))