22
22
23
23
24
24
import sys
25
- from scipy .signal import spectrogram , detrend
25
+ from scipy .signal import spectrogram , stft , detrend
26
26
from scipy .linalg import norm
27
27
import matplotlib .pyplot as plt
28
28
import numpy as np
@@ -349,6 +349,8 @@ def QC_daily_spectra(self, pd=[0.004, 0.2], tol=1.5, alpha=0.05,
349
349
psdP = None
350
350
f , t , psdZ = spectrogram (
351
351
self .trZ .data , self .fs , window = wind , nperseg = ws , noverlap = ss )
352
+ self .f = f
353
+ print (f )
352
354
if self .ncomp == 2 or self .ncomp == 4 :
353
355
f , t , psdP = spectrogram (
354
356
self .trP .data , self .fs , window = wind , nperseg = ws , noverlap = ss )
@@ -640,20 +642,37 @@ def average_daily_spectra(self, calc_rotation=True, fig_average=False,
640
642
ws = int (self .window / self .dt )
641
643
642
644
# Number of points in step
643
- ss = int (self .window * (1. - self .overlap )/ self .dt )
645
+ ss = int (self .window * self .overlap / self .dt )
646
+
647
+ # hanning window
648
+ hanning = np .hanning (2 * ss )
649
+ wind = np .ones (ws )
650
+ wind [0 :ss ] = hanning [0 :ss ]
651
+ wind [- ss :ws ] = hanning [ss :ws ]
644
652
645
653
ft1 = None
646
654
ft2 = None
647
655
ftZ = None
648
656
ftP = None
649
- ftZ , f = utils .calculate_windowed_fft (self .trZ , ws , ss )
657
+
658
+ _f , _t , ftZ = stft (
659
+ self .trZ .data , self .fs , return_onesided = False , boundary = None , padded = False ,
660
+ window = wind , nperseg = ws , noverlap = ss )
661
+ ftZ = ftZ .T
650
662
if self .ncomp == 2 or self .ncomp == 4 :
651
- ftP , f = utils .calculate_windowed_fft (self .trP , ws , ss )
663
+ _f , _t , ftP = stft (
664
+ self .trP .data , self .fs , return_onesided = False , boundary = None , padded = False ,
665
+ window = wind , nperseg = ws , noverlap = ss )
666
+ ftP = ftP .T
652
667
if self .ncomp == 3 or self .ncomp == 4 :
653
- ft1 , f = utils .calculate_windowed_fft (self .tr1 , ws , ss )
654
- ft2 , f = utils .calculate_windowed_fft (self .tr2 , ws , ss )
655
-
656
- self .f = f
668
+ _f , _t , ft1 = stft (
669
+ self .tr1 .data , self .fs , return_onesided = False , boundary = None , padded = False ,
670
+ window = wind , nperseg = ws , noverlap = ss )
671
+ _f , _t , ft2 = stft (
672
+ self .tr2 .data , self .fs , return_onesided = False , boundary = None , padded = False ,
673
+ window = wind , nperseg = ws , noverlap = ss )
674
+ ft1 = ft1 .T
675
+ ft2 = ft2 .T
657
676
658
677
# Extract good windows
659
678
c11 = None
@@ -662,18 +681,18 @@ def average_daily_spectra(self, calc_rotation=True, fig_average=False,
662
681
cPP = None
663
682
cZZ = np .abs (
664
683
np .mean (ftZ [self .goodwins , :]* np .conj (ftZ [self .goodwins , :]),
665
- axis = 0 ))[0 :len (f )]
684
+ axis = 0 ))[0 :len (self . f )]
666
685
if self .ncomp == 2 or self .ncomp == 4 :
667
686
cPP = np .abs (
668
687
np .mean (ftP [self .goodwins , :]* np .conj (ftP [self .goodwins , :]),
669
- axis = 0 ))[0 :len (f )]
688
+ axis = 0 ))[0 :len (self . f )]
670
689
if self .ncomp == 3 or self .ncomp == 4 :
671
690
c11 = np .abs (
672
691
np .mean (ft1 [self .goodwins , :]* np .conj (ft1 [self .goodwins , :]),
673
- axis = 0 ))[0 :len (f )]
692
+ axis = 0 ))[0 :len (self . f )]
674
693
c22 = np .abs (
675
694
np .mean (ft2 [self .goodwins , :]* np .conj (ft2 [self .goodwins , :]),
676
- axis = 0 ))[0 :len (f )]
695
+ axis = 0 ))[0 :len (self . f )]
677
696
678
697
# Extract bad windows
679
698
bc11 = None
@@ -683,18 +702,18 @@ def average_daily_spectra(self, calc_rotation=True, fig_average=False,
683
702
if np .sum (~ self .goodwins ) > 0 :
684
703
bcZZ = np .abs (np .mean (
685
704
ftZ [~ self .goodwins , :]* np .conj (ftZ [~ self .goodwins , :]),
686
- axis = 0 ))[0 :len (f )]
705
+ axis = 0 ))[0 :len (self . f )]
687
706
if self .ncomp == 2 or self .ncomp == 4 :
688
707
bcPP = np .abs (np .mean (
689
708
ftP [~ self .goodwins , :]* np .conj (ftP [~ self .goodwins , :]),
690
- axis = 0 ))[0 :len (f )]
709
+ axis = 0 ))[0 :len (self . f )]
691
710
if self .ncomp == 3 or self .ncomp == 4 :
692
711
bc11 = np .abs (np .mean (
693
712
ft1 [~ self .goodwins , :]* np .conj (ft1 [~ self .goodwins , :]),
694
- axis = 0 ))[0 :len (f )]
713
+ axis = 0 ))[0 :len (self . f )]
695
714
bc22 = np .abs (np .mean (
696
715
ft2 [~ self .goodwins , :]* np .conj (ft2 [~ self .goodwins , :]),
697
- axis = 0 ))[0 :len (f )]
716
+ axis = 0 ))[0 :len (self . f )]
698
717
699
718
# Calculate mean of all good windows if component combinations exist
700
719
c12 = None
@@ -705,27 +724,27 @@ def average_daily_spectra(self, calc_rotation=True, fig_average=False,
705
724
cZP = None
706
725
if self .ncomp == 3 or self .ncomp == 4 :
707
726
c12 = np .mean (ft1 [self .goodwins , :] *
708
- np .conj (ft2 [self .goodwins , :]), axis = 0 )[0 :len (f )]
727
+ np .conj (ft2 [self .goodwins , :]), axis = 0 )[0 :len (self . f )]
709
728
c1Z = np .mean (ft1 [self .goodwins , :] *
710
- np .conj (ftZ [self .goodwins , :]), axis = 0 )[0 :len (f )]
729
+ np .conj (ftZ [self .goodwins , :]), axis = 0 )[0 :len (self . f )]
711
730
c2Z = np .mean (ft2 [self .goodwins , :] *
712
- np .conj (ftZ [self .goodwins , :]), axis = 0 )[0 :len (f )]
731
+ np .conj (ftZ [self .goodwins , :]), axis = 0 )[0 :len (self . f )]
713
732
if self .ncomp == 4 :
714
733
c1P = np .mean (ft1 [self .goodwins , :] *
715
- np .conj (ftP [self .goodwins , :]), axis = 0 )[0 :len (f )]
734
+ np .conj (ftP [self .goodwins , :]), axis = 0 )[0 :len (self . f )]
716
735
c2P = np .mean (ft2 [self .goodwins , :] *
717
- np .conj (ftP [self .goodwins , :]), axis = 0 )[0 :len (f )]
736
+ np .conj (ftP [self .goodwins , :]), axis = 0 )[0 :len (self . f )]
718
737
if self .ncomp == 2 or self .ncomp == 4 :
719
738
cZP = np .mean (ftZ [self .goodwins , :] *
720
- np .conj (ftP [self .goodwins , :]), axis = 0 )[0 :len (f )]
739
+ np .conj (ftP [self .goodwins , :]), axis = 0 )[0 :len (self . f )]
721
740
722
741
# Store as attributes
723
742
self .power = Power (c11 , c22 , cZZ , cPP )
724
743
self .cross = Cross (c12 , c1Z , c1P , c2Z , c2P , cZP )
725
744
bad = Power (bc11 , bc22 , bcZZ , bcPP )
726
745
727
746
if fig_average :
728
- plot = plotting .fig_average (f , self .power , bad , self .goodwins ,
747
+ plot = plotting .fig_average (self . f , self .power , bad , self .goodwins ,
729
748
self .ncomp , key = self .key )
730
749
if save :
731
750
fname = self .key + '.' + self .tkey + '.' + 'average.' + form
@@ -739,7 +758,7 @@ def average_daily_spectra(self, calc_rotation=True, fig_average=False,
739
758
if calc_rotation and self .ncomp >= 3 :
740
759
cHH , cHZ , cHP , coh , ph , direc , tilt , coh_value , phase_value = \
741
760
utils .calculate_tilt (
742
- ft1 , ft2 , ftZ , ftP , f , self .goodwins )
761
+ ft1 , ft2 , ftZ , ftP , self . f , self .goodwins )
743
762
self .rotation = Rotation (
744
763
cHH , cHZ , cHP , coh , ph , tilt , coh_value , phase_value , direc )
745
764
@@ -1949,12 +1968,15 @@ def correct_data(self, tfnoise):
1949
1968
ft2 = None
1950
1969
ftZ = None
1951
1970
ftP = None
1952
- ftZ , f = utils . calculate_windowed_fft (trZ , ws , hann = False )
1971
+ ftZ = np . fft . fft (trZ , n = ws )
1953
1972
if self .ncomp == 2 or self .ncomp == 4 :
1954
- ftP , f = utils . calculate_windowed_fft (trP , ws , hann = False )
1973
+ ftP = np . fft . fft (trP , n = ws )
1955
1974
if self .ncomp == 3 or self .ncomp == 4 :
1956
- ft1 , f = utils .calculate_windowed_fft (tr1 , ws , hann = False )
1957
- ft2 , f = utils .calculate_windowed_fft (tr2 , ws , hann = False )
1975
+ ft1 = np .fft .fft (tr1 , n = ws )
1976
+ ft2 = np .fft .fft (tr2 , n = ws )
1977
+
1978
+ # Use one-sided frequency axis to match spectrogram
1979
+ f = np .fft .rfftfreq (ws , d = self .dt )
1958
1980
1959
1981
if not np .allclose (f , tfnoise .f ):
1960
1982
raise (Exception ('Frequency axes are different: ' , f , tfnoise .f ))
0 commit comments