コード例 #1
0
import numpy as np
from obspy.core import Trace, UTCDateTime

x = np.zeros(200)
x[100] = 1
tr = Trace(x)
tr.stats.network = "XX"
tr.stats.station = "SDFD1"
print tr
print "showing original trace"
tr.plot()
tr.stats.sampling_rate = 20
tr.stats.starttime = UTCDateTime(2011, 2, 21, 8)
print tr
tr.plot()

tr.filter("lowpass", freq=1)
print "showing filtered trace"
tr.plot()
tr.trim(tr.stats.starttime + 4.5, tr.stats.endtime - 2)
print "showing trimmed trace"
tr.plot()

tr.data = tr.data * 500
tr2 = tr.copy()
tr2.data = tr2.data + np.random.randn(len(tr2))
tr2.stats.station = "SDFD2"
print tr2
print "showing trace with gaussian noise added"
tr2.plot()
コード例 #2
0
ファイル: nowhiten.py プロジェクト: RDePlaen/MSNoise
def nowhiten(data, Nfft, delta, freqmin, freqmax, plot=False):
    """This function takes 1-dimensional *data* timeseries array
    and returns the fft between *freqmin* and *freqmax*.

    :type data: :class:`numpy.ndarray`
    :param data: Contains the 1D time series to whiten
    :type Nfft: int
    :para Nfft: The number of points to compute the FFT
    :type delta: float
    :param delta: The sampling frequency of the `data`
    :type freqmin: float
    :param freqmin: The lower frequency bound
    :type freqmax: float
    :param freqmax: The upper frequency bound
    :type plot: bool
    :param plot: Whether to show a raw plot of the action (default: False)

    :rtype: :class:`numpy.ndarray`
    :returns: The FFT of the input trace between the frequency bounds
"""

    if plot:
        plt.subplot(211)
        plt.plot(np.arange(len(data)) * delta, data)
        plt.xlim(0, len(data) * delta)
        plt.title('Input trace')

    stats = {'sampling_rate': 20.}
    trdata=Trace(data, header=stats)
    # Napod = 100
    # Nfft = int(Nfft)
    # freqVec = scipy.fftpack.fftfreq(Nfft,d=delta)[:Nfft/2]
    #
    # J = np.where((freqVec >= freqmin) & (freqVec <= freqmax))[0]
    # low = J[0] - Napod
    # if low <= 0:
    #     low = 1
    #
    # porte1 = J[0]
    # porte2 = J[-1]
    # high = J[-1] + Napod
    # if high > Nfft / 2:
    #     high = Nfft // 2
    trdata.filter("bandpass",freqmin=freqmin,freqmax=freqmax,zerophase=True)
    FFTRawSign = scipy.fftpack.fft(trdata, Nfft)
    
    if plot:
        plt.subplot(212)
        axis = np.arange(len(FFTRawSign))
        plt.plot(axis[1:], np.abs(FFTRawSign[1:]))
        plt.xlim(0, max(axis))
        plt.title('FFTRawSign')

    # # Left tapering:
    # FFTRawSign[0:low] *= 0
    # FFTRawSign[low:porte1] = np.cos(np.linspace(np.pi / 2., np.pi, porte1 - low)) ** 2 * FFTRawSign[low:porte1]
    # # Pass band:
    # FFTRawSign[porte1:porte2] = FFTRawSign[porte1:porte2]
    # # Right tapering:
    # FFTRawSign[porte2:high] = np.cos(np.linspace(0., np.pi / 2., high - porte2)) ** 2 * FFTRawSign[porte2:high]
    # FFTRawSign[high:Nfft+1] *= 0
    
    # Hermitian symmetry (because the input is real)
    #FFTRawSign[-Nfft/2+1:] = FFTRawSign[1:Nfft/2].conjugate()[::-1]

    # if plot:
    #     plt.subplot(413)
    #     axis = np.arange(len(FFTRawSign))
    #     plt.axvline(low, c='g')
    #     plt.axvline(porte1, c='g')
    #     plt.axvline(porte2, c='r')
    #     plt.axvline(high, c='r')
    #
    #     plt.axvline(Nfft - high, c='r')
    #     plt.axvline(Nfft - porte2, c='r')
    #     plt.axvline(Nfft - porte1, c='g')
    #     plt.axvline(Nfft - low, c='g')
    #
    #     plt.plot(axis, np.abs(FFTRawSign))
    #     plt.xlim(0, max(axis))
    #
    #     wdata = np.real(scipy.fftpack.ifft(FFTRawSign))
    #     plt.subplot(414)
    #     plt.plot(np.arange(len(wdata)) * delta, wdata)
    #     plt.xlim(0, len(wdata) * delta)
    #     plt.show()
    
    return FFTRawSign
コード例 #3
0
    def test_sonic(self):
#        for i in xrange(100):
        np.random.seed(2348)

        geometry = np.array([[0.0, 0.0, 0.0],
                             [-5.0, 7.0, 0.0],
                             [5.0, 7.0, 0.0],
                             [10.0, 0.0, 0.0],
                             [5.0, -7.0, 0.0],
                             [-5.0, -7.0, 0.0],
                             [-10.0, 0.0, 0.0]])

        geometry /= 100      # in km
        slowness = 1.3       # in s/km
        baz_degree = 20.0    # 0.0 > source in x direction
        baz = baz_degree * np.pi / 180.
        df = 100             # samplerate
        # SNR = 100.         # signal to noise ratio
        amp = .00001         # amplitude of coherent wave
        length = 500         # signal length in samples

        coherent_wave = amp * np.random.randn(length)

        # time offsets in samples
        dt = df * slowness * (np.cos(baz) * geometry[:, 1] + np.sin(baz) *
                              geometry[:, 0])
        dt = np.round(dt)
        dt = dt.astype('int32')
        max_dt = np.max(dt) + 1
        min_dt = np.min(dt) - 1
        trl = list()
        for i in xrange(len(geometry)):
            tr = Trace(coherent_wave[-min_dt + dt[i]:-max_dt + dt[i]].copy())
                # + amp / SNR * \
                # np.random.randn(length - abs(min_dt) - abs(max_dt)))
            tr.stats.sampling_rate = df
            tr.stats.coordinates = AttribDict()
            tr.stats.coordinates.x = geometry[i, 0]
            tr.stats.coordinates.y = geometry[i, 1]
            tr.stats.coordinates.elevation = geometry[i, 2]
            # lowpass random signal to f_nyquist / 2
            tr.filter("lowpass", freq=df / 4.)
            trl.append(tr)

        st = Stream(trl)

        stime = UTCDateTime(1970, 1, 1, 0, 0)
        etime = UTCDateTime(1970, 1, 1, 0, 0) + \
                (length - abs(min_dt) - abs(max_dt)) / df

        win_len = 2.
        step_frac = 0.2
        sll_x = -3.0
        slm_x = 3.0
        sll_y = -3.0
        slm_y = 3.0
        sl_s = 0.1

        frqlow = 1.0
        frqhigh = 8.0
        prewhiten = 0

        semb_thres = -1e99
        vel_thres = -1e99

        # out returns: rel. power, abs. power, backazimuth, slowness
        out = sonic(st, win_len, step_frac, sll_x, slm_x, sll_y, slm_y, sl_s,
                    semb_thres, vel_thres, frqlow, frqhigh, stime, etime,
                    prewhiten, coordsys='xy', verbose=False)

        # returns baz
        np.testing.assert_almost_equal(out[:, 3].mean(), 18.434948822922024)
        # slowness ~= 1.3
        np.testing.assert_almost_equal(out[:, 4].mean(), 1.26491106407)
コード例 #4
0
def nowhiten(data, Nfft, delta, freqmin, freqmax, plot=False):
    """This function takes 1-dimensional *data* timeseries array
    and returns the fft between *freqmin* and *freqmax*.

    :type data: :class:`numpy.ndarray`
    :param data: Contains the 1D time series to whiten
    :type Nfft: int
    :para Nfft: The number of points to compute the FFT
    :type delta: float
    :param delta: The sampling frequency of the `data`
    :type freqmin: float
    :param freqmin: The lower frequency bound
    :type freqmax: float
    :param freqmax: The upper frequency bound
    :type plot: bool
    :param plot: Whether to show a raw plot of the action (default: False)

    :rtype: :class:`numpy.ndarray`
    :returns: The FFT of the input trace between the frequency bounds
"""

    if plot:
        plt.subplot(211)
        plt.plot(np.arange(len(data)) * delta, data)
        plt.xlim(0, len(data) * delta)
        plt.title('Input trace')

    stats = {'sampling_rate': 20.}
    trdata = Trace(data, header=stats)
    # Napod = 100
    # Nfft = int(Nfft)
    # freqVec = scipy.fftpack.fftfreq(Nfft,d=delta)[:Nfft/2]
    #
    # J = np.where((freqVec >= freqmin) & (freqVec <= freqmax))[0]
    # low = J[0] - Napod
    # if low <= 0:
    #     low = 1
    #
    # porte1 = J[0]
    # porte2 = J[-1]
    # high = J[-1] + Napod
    # if high > Nfft / 2:
    #     high = Nfft // 2
    trdata.filter("bandpass",
                  freqmin=freqmin,
                  freqmax=freqmax,
                  corners=4,
                  zerophase=True)
    #Raph    FFTRawSign = scipy.fftpack.fft(trdata, Nfft)

    if plot:
        plt.subplot(212)
        axis = np.arange(len(FFTRawSign))
        plt.plot(axis[1:], np.abs(FFTRawSign[1:]))
        plt.xlim(0, max(axis))
        plt.title('FFTRawSign')

    # # Left tapering:
    # FFTRawSign[0:low] *= 0
    # FFTRawSign[low:porte1] = np.cos(np.linspace(np.pi / 2., np.pi, porte1 - low)) ** 2 * FFTRawSign[low:porte1]
    # # Pass band:
    # FFTRawSign[porte1:porte2] = FFTRawSign[porte1:porte2]
    # # Right tapering:
    # FFTRawSign[porte2:high] = np.cos(np.linspace(0., np.pi / 2., high - porte2)) ** 2 * FFTRawSign[porte2:high]
    # FFTRawSign[high:Nfft+1] *= 0

    # Hermitian symmetry (because the input is real)
    #FFTRawSign[-Nfft/2+1:] = FFTRawSign[1:Nfft/2].conjugate()[::-1]

    # if plot:
    #     plt.subplot(413)
    #     axis = np.arange(len(FFTRawSign))
    #     plt.axvline(low, c='g')
    #     plt.axvline(porte1, c='g')
    #     plt.axvline(porte2, c='r')
    #     plt.axvline(high, c='r')
    #
    #     plt.axvline(Nfft - high, c='r')
    #     plt.axvline(Nfft - porte2, c='r')
    #     plt.axvline(Nfft - porte1, c='g')
    #     plt.axvline(Nfft - low, c='g')
    #
    #     plt.plot(axis, np.abs(FFTRawSign))
    #     plt.xlim(0, max(axis))
    #
    #     wdata = np.real(scipy.fftpack.ifft(FFTRawSign))
    #     plt.subplot(414)
    #     plt.plot(np.arange(len(wdata)) * delta, wdata)
    #     plt.xlim(0, len(wdata) * delta)
    #     plt.show()


#Raph    return FFTRawSign
    return trdata
コード例 #5
0
ファイル: test_sonic.py プロジェクト: kasra-hosseini/obspy
    def test_sonic(self):
        #        for i in xrange(100):
        np.random.seed(2348)

        geometry = np.array(
            [
                [0.0, 0.0, 0.0],
                [-5.0, 7.0, 0.0],
                [5.0, 7.0, 0.0],
                [10.0, 0.0, 0.0],
                [5.0, -7.0, 0.0],
                [-5.0, -7.0, 0.0],
                [-10.0, 0.0, 0.0],
            ]
        )

        geometry /= 100  # in km
        slowness = 1.3  # in s/km
        baz_degree = 20.0  # 0.0 > source in x direction
        baz = baz_degree * np.pi / 180.0
        df = 100  # samplerate
        # SNR = 100.         # signal to noise ratio
        amp = 0.00001  # amplitude of coherent wave
        length = 500  # signal length in samples

        coherent_wave = amp * np.random.randn(length)

        # time offsets in samples
        dt = df * slowness * (np.cos(baz) * geometry[:, 1] + np.sin(baz) * geometry[:, 0])
        dt = np.round(dt)
        dt = dt.astype("int32")
        max_dt = np.max(dt) + 1
        min_dt = np.min(dt) - 1
        trl = list()
        for i in xrange(len(geometry)):
            tr = Trace(coherent_wave[-min_dt + dt[i] : -max_dt + dt[i]].copy())
            # + amp / SNR * \
            # np.random.randn(length - abs(min_dt) - abs(max_dt)))
            tr.stats.sampling_rate = df
            tr.stats.coordinates = AttribDict()
            tr.stats.coordinates.x = geometry[i, 0]
            tr.stats.coordinates.y = geometry[i, 1]
            tr.stats.coordinates.elevation = geometry[i, 2]
            # lowpass random signal to f_nyquist / 2
            tr.filter("lowpass", freq=df / 4.0)
            trl.append(tr)

        st = Stream(trl)

        stime = UTCDateTime(1970, 1, 1, 0, 0)
        etime = UTCDateTime(1970, 1, 1, 0, 0) + (length - abs(min_dt) - abs(max_dt)) / df

        win_len = 2.0
        step_frac = 0.2
        sll_x = -3.0
        slm_x = 3.0
        sll_y = -3.0
        slm_y = 3.0
        sl_s = 0.1

        frqlow = 1.0
        frqhigh = 8.0
        prewhiten = 0

        semb_thres = -1e99
        vel_thres = -1e99

        # out returns: rel. power, abs. power, backazimuth, slowness
        out = sonic(
            st,
            win_len,
            step_frac,
            sll_x,
            slm_x,
            sll_y,
            slm_y,
            sl_s,
            semb_thres,
            vel_thres,
            frqlow,
            frqhigh,
            stime,
            etime,
            prewhiten,
            coordsys="xy",
            verbose=False,
        )

        # returns baz
        np.testing.assert_almost_equal(out[:, 3].mean(), 18.434948822922024)
        # slowness ~= 1.3
        np.testing.assert_almost_equal(out[:, 4].mean(), 1.26491106407)