Пример #1
0
def smooth_spectrum(spec, freqs, nfft, bandwidth=20):
    """
    Smooths the amplitude spectrum following the algorithm of
    Konno and Ohmachi.

    Args:
        spec (numpy.ndarray):
            Spectral amplitude data.
        freqs (numpy.ndarray):
            Frequencies.
        nfft (int):
            Number of data points for the fourier transform.
        bandwidth (float):
            Konno-Omachi smoothing bandwidth parameter.

    Returns:
        numpy.ndarray: Smoothed amplitude data and frequencies.
    """

    # Do a maximum of 301 K-O frequencies in the range of the fft freqs
    nkofreqs = min(nfft, 302) - 1
    ko_freqs = np.logspace(np.log10(freqs[1]), np.log10(freqs[-1]), nkofreqs)
    # An array to hold the output
    spec_smooth = np.empty_like(ko_freqs)

    # Konno Omachi Smoothing
    konno_ohmachi_smooth(spec.astype(np.double), freqs, ko_freqs, spec_smooth,
                         bandwidth)
    return spec_smooth, ko_freqs
Пример #2
0
def fft_smooth(trace, nfft, bandwidth=20):
    """
    Pads a trace to the nearest upper power of 2, takes the FFT, and
    smooths the amplitude spectra following the algorithm of
    Konno and Ohmachi.

    Args:
        trace (StationTrace):
            Trace of strong motion data.
        nfft (int):
            Number of data points for the fourier transform.
        bandwidth (float):
            Konno-Omachi smoothing bandwidth parameter.

    Returns:
        numpy.ndarray: Smoothed amplitude data and frequencies.
    """

    # Compute the FFT, normalizing by the number of data points
    dt = trace.stats.delta
    spec = abs(np.fft.rfft(trace.data, n=nfft)) * dt

    # Get the frequencies associated with the FFT
    freqs = np.fft.rfftfreq(nfft, dt)

    # Do a maximum of 301 K-O frequencies in the range of the fft freqs
    nkofreqs = min(nfft, 302) - 1
    ko_freqs = np.logspace(np.log10(freqs[1]), np.log10(freqs[-1]), nkofreqs)
    # An array to hold the output
    spec_smooth = np.empty_like(ko_freqs)

    # Konno Omachi Smoothing
    konno_ohmachi_smooth(
        spec.astype(np.double), freqs, ko_freqs, spec_smooth, bandwidth)
    return spec_smooth, ko_freqs
Пример #3
0
def calculate_fas(stream, imcs, periods, smoothing, bandwidth):
    """
    Calculate the fourier amplitude spectra.

    This process requires getting the fourier amplitude spectra, applying
    the geometric mean to the horizontal traces, smoothing and returning the
    smoothed value for each requested period.

    Args:
        stream (obspy.core.stream.Stream): streams of strong ground motion.
            Traces in stream must be in units of g.
        imcs (list): list of imcs. (Currently only the geometric mean is
            supported and this is ignored. In the future it will account for
            channels and rotated channels.)

    Returns:
        dictionary: Dictionary of fas for the geometric mean.
            Units are in cm/s. The dictionary is structured as:
            fas_dict = {
                    <period> : <value,...
            }
    """
    fas_dict = {}
    sampling_rate = None
    # check units and add channel pga
    for trace in stream:
        if trace.stats['units'] != 'g' and trace.stats['units'] != 'cm/s/s':
            raise PGMException('Invalid units for sa: %r. '
                               'Units must be g (cm/s/s)' % trace.stats['units'])
        if 'Z' not in trace.stats['channel'].upper():
            sampling_rate = trace.stats.sampling_rate
    if sampling_rate is None:
        raise PGMException('No horizontal channels')

    spec_stream = Stream()
    for trace in stream:
        nfft = len(trace.data)
        # the fft scales so the factor of 1/nfft is applied
        spectra = abs(np.fft.rfft(trace.data, n=nfft)) / nfft
        spec_trace = Trace(data=spectra, header=trace.stats)
        spec_stream.append(spec_trace)

    ## The imc is always geometric mean. However, the combined stream is
    ## required rather than the single maximum value
    gm_trace = calculate_geometric_mean(spec_stream, return_combined=True)
    freqs = np.fft.rfftfreq(nfft, 1 / trace.stats.sampling_rate)

    fas_frequencies = 1 / np.asarray(list(periods))
    smoothed_values = np.empty_like(fas_frequencies)

    if smoothing.lower() == 'konno_ohmachi':
        konno_ohmachi_smooth(gm_trace.astype(np.double), freqs,
                fas_frequencies, smoothed_values, bandwidth)
    else:
        raise PGMException('Not a valid smoothing option: %r' % smoothing)

    for idx, freq in enumerate(fas_frequencies):
        fas_dict[1/freq] = smoothed_values[idx]

    return fas_dict
Пример #4
0
 def _pick_spectra(self, data):
     freqs = data['freqs']
     spectra = data['spectra']
     fas_frequencies = 1 / np.asarray([self.period])
     smoothed_values = np.empty_like(fas_frequencies)
     if self.smoothing.lower() == 'konno_ohmachi':
         konno_ohmachi_smooth(spectra.astype(np.double), freqs,
                              fas_frequencies, smoothed_values,
                              self.bandwidth)
     return smoothed_values[0]
    def get_pick(self):
        """
        Performs smoothing and picks values for each defined period.

        Returns:
            picked: Dictionary of values for each period.
        """
        freqs = self.reduction_data[0]
        fas_frequencies = 1 / np.asarray([self.period])
        smoothed_values = np.empty_like(fas_frequencies)
        if self.smoothing.lower() == 'konno_ohmachi':
            konno_ohmachi_smooth(self.reduction_data[1].astype(np.double), freqs,
                fas_frequencies, smoothed_values, self.bandwidth)
        picked_value = smoothed_values[0]
        picked = {'': picked_value}
        return picked
Пример #6
0
    def get_pick(self):
        """
        Performs smoothing and picks values for each defined period.

        Returns:
            picked: Dictionary of values for each period.
        """
        freqs = self.reduction_data[0]
        fas_frequencies = 1 / np.asarray([self.period])
        smoothed_values = np.empty_like(fas_frequencies)
        if self.smoothing.lower() == 'konno_ohmachi':
            konno_ohmachi_smooth(self.reduction_data[1].astype(np.double),
                                 freqs, fas_frequencies, smoothed_values,
                                 self.bandwidth)
        picked_value = smoothed_values[0]
        picked = {'': picked_value}
        return picked