Coverage for /opt/obspy/update-docs/src/obspy/obspy/signal/konnoohmachismoothing : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- #------------------------------------------------------------------- # Filename: konnoohmachismoothing.py # Purpose: Small module to smooth spectra with the so called Konno & Ohmachi # method. # Author: Lion Krischer # Email: krischer@geophysik.uni-muenchen.de # License: GPLv2 # # Copyright (C) 2011 Lion Krischer #--------------------------------------------------------------------- Functions to smooth spectra with the so called Konno & Ohmachi method.
:copyright: The ObsPy Development Team (devs@obspy.org) :license: GNU Lesser General Public License, Version 3 (http://www.gnu.org/copyleft/lesser.html) """
normalize=False): """ Returns the Konno & Ohmachi Smoothing window for every frequency in frequencies.
Returns the smoothing window around the center frequency with one value per input frequency defined as follows (see [Konno1998]_):
[sin(b * log_10(f/f_c)) / (b * log_10(f/f_c)]^4 b = bandwidth f = frequency f_c = center frequency
The bandwidth of the smoothing function is constant on a logarithmic scale. A small value will lead to a strong smoothing, while a large value of will lead to a low smoothing of the Fourier spectra. The default (and generally used) value for the bandwidth is 40. (From the Geopsy documentation - www.geopsy.org)
All parameters need to be positive. This is not checked due to performance reasons and therefore any negative parameters might have unexpected results.
This function might raise some numpy warnings due to divisions by zero and logarithms of zero. This is intentional and faster than prefiltering the special cases. You can disable numpy warnings (they usually do not show up anyways) with:
temp = np.geterr() np.seterr(all='ignore') ...code that raises numpy warning due to division by zero... np.seterr(**temp)
:param frequencies: numpy.ndarray (float32 or float64) All frequencies for which the smoothing window will be returned. :param center_frequency: float >= 0.0 The frequency around which the smoothing is performed. :param bandwidth: float > 0.0 Determines the width of the smoothing peak. Lower values result in a broader peak. Defaults to 40. :param normalize: boolean, optional The Konno-Ohmachi smoothing window is normalized on a logarithmic scale. Set this parameter to True to normalize it on a normal scale. Default to False. """ # If the center_frequency is 0 return an array with zero everywhere except # at zero. # Calculate the bandwidth*log10(f/f_c) # Just the Konno-Ohmachi formulae. # Check if the center frequency is exactly part of the provided # frequencies. This will result in a division by 0. The limit of f->f_c is # one. # Also a frequency of zero will result in a logarithm of -inf. The limit of # f->0 with f_c!=0 is zero. # Normalize to one if wished.
""" Calculates a len(frequencies) x len(frequencies) matrix with the Konno & Ohmachi window for each frequency as the center frequency.
Any spectrum with the same frequency bins as this matrix can later be smoothed by a simple matrix multiplication with this matrix: smoothed_spectrum = np.dot(spectrum, smoothing_matrix)
This also works for many spectra stored in one large matrix and is even more efficient.
This makes it very efficient for smoothing the same spectra again and again but it comes with a high memory consumption for larger frequency arrays!
:param frequencies: numpy.ndarray (float32 or float64) The input frequencies. :param bandwidth: float > 0.0 Determines the width of the smoothing peak. Lower values result in a broader peak. Defaults to 40. :param normalize: boolean, optional The Konno-Ohmachi smoothing window is normalized on a logarithmic scale. Set this parameter to True to normalize it on a normal scale. Default to False. """ # Create matrix to be filled with smoothing entries. frequencies.dtype) bandwidth, normalize=normalize)
enforce_no_matrix=False, max_memory_usage=512, normalize=False): """ Smoothes a matrix containing one spectra per row with the Konno-Ohmachi smoothing window.
All spectra need to have frequency bins corresponding to the same frequencies.
This method first will estimate the memory usage and then either use a fast and memory intensive method or a slow one with a better memory usage.
:param spectra: numpy.ndarray (float32 or float64) One or more spectra per row. If more than one the first spectrum has to be accessible via spectra[0], the next via spectra[1], ... :param frequencies: numpy.ndarray (float32 or float64) Contains the frequencies for the spectra. :param bandwidth: float > 0.0 Determines the width of the smoothing peak. Lower values result in a broader peak. Defaults to 40. :param count: integer, optional How often the apply the filter. For very noisy spectra it is useful to apply is more than once. Defaults to 1. :param enforce_no_matrix: boolean, optional An efficient but memory intensive matrix-multiplication algorithm is used in case more than one spectra is to be smoothed or one spectrum is to be smoothed more than once if enough memory is available. This flag disables the matrix algorithm altogether. Defaults to False :param max_memory_usage: integer, optional Set the maximum amount of extra memory in MB for this method. Decides whether or not the matrix multiplication method is used. Defaults to 512 MB. :param normalize: boolean, optional The Konno-Ohmachi smoothing window is normalized on a logarithmic scale. Set this parameter to True to normalize it on a normal scale. Default to False. """ or (spectra.dtype != np.float32 and spectra.dtype != np.float64): # Spectra and frequencies should have the same dtype. 'will be changed to np.float64 for both.' # Check the dtype to get the correct size. # Calculate the approximate usage needs for the smoothing matrix algorithm. size / 1048576.0 # If smaller than the allowed maximum memory consumption build a smoothing # matrix and apply to each spectrum. Also only use when more then one # spectrum is to be smoothed. and approx_mem_usage < max_memory_usage: # Disable numpy warnings due to possible divisions by zero/logarithms # of zero. normalize=normalize) # Eventually apply more than once. # Otherwise just calculate the smoothing window every time and apply it. else: # Separate case for just one spectrum. # Disable numpy warnings due to possible divisions by # zero/logarithms of zero. frequencies[_i], bandwidth, normalize=normalize) # Reuse smoothing window if more than one spectrum. else: # Disable numpy warnings due to possible divisions by # zero/logarithms of zero. frequencies[_i], bandwidth, normalize=normalize) # Eventually apply more than once. enforce_no_matrix=True, normalize=normalize) |