Coverage for /opt/obspy/update-docs/src/obspy/obspy/signal/cpxtrace : 70%

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
#!/usr/bin/env python #-------------------------------------------------------------------- # Filename: cpxtrace.py # Author: Conny Hammer # Email: conny.hammer@geo.uni-potsdam.de # # Copyright (C) 2008-2012 Conny Hammer #------------------------------------------------------------------- Complex Trace Analysis
:copyright: The ObsPy Development Team (devs@obspy.org) :license: GNU Lesser General Public License, Version 3 (http://www.gnu.org/copyleft/lesser.html) """
""" Envelope of a signal.
Computes the envelope of the given data which can be windowed or not. The envelope is determined by the absolute value of the analytic signal of the given data.
If data are windowed the analytic signal and the envelope of each window is returned.
:type data: :class:`~numpy.ndarray` :param data: Data to make envelope of. :return: **A_cpx, A_abs** - Analytic signal of input data, Envelope of input data. """ else: A_cpx = signal.hilbert(data, nfft) A_abs = abs(signal.hilbert(data, nfft))
""" Normalized envelope of a signal.
Computes the normalized envelope of the given data which can be windowed or not. In order to obtain a normalized measure of the signal envelope the instantaneous bandwidth of the smoothed envelope is normalized by the Nyquist frequency and is integrated afterwards.
The time derivative of the normalized envelope is returned if input data are windowed only.
:type data: :class:`~numpy.ndarray` :param data: Data to make normalized envelope of. :param fs: Sampling frequency. :param smoothie: Window length for moving average. :param fk: Coefficients for calculating time derivatives (calculated via central difference). :return: **Anorm[, dAnorm]** - Normalized envelope of input data, Time derivative of normalized envelope (windowed only). """ # Differentiation of original signal, dA/dt #A_win_add = append(append([row[0]]*(size(fk)/2),row), # [row[size(row)-1]]*(size(fk)/2)) # Better, because faster, calculation of A_win_add A_win_smooth, [A_win_smooth[size(A_win_smooth) - 1]] * \ (size(fk) // 2))) #t = t[size(fk) // 2:(size(t) - size(fk) // 2)] # correct start and end values of time derivative # (dA/dt) / 2*PI*smooth(A)*fs/2 # Integral within window #Anorm = util.smooth(Anorm,smoothie) #Anorm_add = np.append(np.append([Anorm[0]] * (size(fk) // 2), Anorm), # [Anorm[size(Anorm) - 1]] * (size(fk) // 2)) # faster alternative to calculate Anorm_add [Anorm[np.size(Anorm) - 1]] * (np.size(fk) // 2))) # correct start and end values of time derivative #dAnorm = dAnorm[size(fk) // 2:(size(dAnorm) - size(fk) // 2)] else: Anorm = np.zeros(1, dtype='float64') A_win_smooth = util.smooth(x[1], smoothie) # Differentiation of original signal, dA/dt #A_win_add = np.append(np.append([x[1][0]] * (size(fk) // 2), x[1]), # [x[1][size(x[1]) - 1]] * (size(fk) // 2)) # Better, because faster, calculation of A_win_add A_win_add = np.hstack(([A_win_smooth[0]] * (size(fk) // 2), \ A_win_smooth, [A_win_smooth[size(A_win_smooth) - 1]] * \ (size(fk) // 2))) t = signal.lfilter(fk, 1, A_win_add) #t = t[size(fk) // 2:(size(t) - size(fk) // 2)] # correct start and end values of time derivative t = t[size(fk) - 1:size(t)] A_win_smooth[A_win_smooth < 1] = 1 # (dA/dt) / 2*PI*smooth(A)*fs/2 t_ = t / (2. * pi * (A_win_smooth) * (fs / 2.0)) # Integral within window t_ = cumtrapz(t_, dx=(1.0 / fs)) t_ = np.concatenate((t_[0:1], t_)) Anorm = ((np.exp(np.mean(t_))) - 1) * 100 return Anorm
""" Centroid time of a signal.
Computes the centroid time of the given data which can be windowed or not. The centroid time is determined as the time in the processed window where 50 per cent of the area below the envelope is reached.
The time derivative of the centroid time is returned if input data are windowed only.
:type data: :class:`~numpy.ndarray` :param data: Data to determine centroid time of. :param fk: Coefficients for calculating time derivatives (calculated via central difference). :return: **centroid[, dcentroid]** - Centroid time input data, Time derivative of centroid time (windowed only). """ # Integral within window # Estimate energy centroid (t - (t - sum(row[0:k - 1]))) (float(k - 1) + float(frac)) / float(size(row)) #centroid_add = np.append(np.append([centroid[0]] * (size(fk) // 2), \ # centroid), [centroid[size(centroid) - 1]] * (size(fk) // 2)) centroid, [centroid[np.size(centroid) - 1]] * \ (np.size(fk) // 2))) #dcentroid = dcentroid[size(fk) // 2:(size(dcentroid) - size(fk) // 2)] # correct start and end values of time derivative else: centroid = np.zeros(1, dtype='float64') # Integral within window half = 0.5 * sum(x[1]) # Estimate energy centroid for k in xrange(2, size(x[1])): t = sum(x[1][0:k]) if (t >= half): frac = (half - (t - sum(x[1][0:k - 1]))) / \ (t - (t - sum(x[1][0:k - 1]))) centroid = (float(k) + float(frac)) / float(size(x[1])) break return centroid
""" Instantaneous frequency of a signal.
Computes the instantaneous frequency of the given data which can be windowed or not. The instantaneous frequency is determined by the time derivative of the analytic signal of the input data.
:type data: :class:`~numpy.ndarray` :param data: Data to determine instantaneous frequency of. :param fs: Sampling frequency. :param fk: Coefficients for calculating time derivatives (calculated via central difference). :return: **omega[, domega]** - Instantaneous frequency of input data, Time derivative of instantaneous frequency (windowed only). """ #f_add = np.append(np.append([f[0]] * (size(fk) // 2), f), # [f[size(f) - 1]] * (size(fk) // 2)) # faster alternative to calculate f_add [f[np.size(f) - 1]] * (np.size(fk) // 2))) #fd = fd[size(fk) // 2:(size(fd) - size(fk) // 2)] # correct start and end values of time derivative #h_add = np.append(np.append([h[0]] * (size(fk) // 2), h), # [h[size(h) - 1]] * (size(fk) // 2)) # faster alternative to calculate h_add [h[np.size(h) - 1]] * (np.size(fk) // 2))) #hd = hd[size(fk) // 2:(size(hd) - size(fk) // 2)] # correct start and end values of time derivative fs / 2 / pi) #omega_add = np.append(np.append([omega[0]] * (size(fk) // 2), omega), # [omega[size(omega) - 1]] * (size(fk) // 2)) # faster alternative to calculate omega_add [omega[np.size(omega) - 1]] * (np.size(fk) // 2))) #domega = domega[size(fk) // 2:(size(domega) - size(fk) // 2)] # correct start and end values of time derivative else: omega = np.zeros(size(x[0]), dtype='float64') f = np.real(x[0]) h = np.imag(x[0]) #f_add = np.append(np.append([f[0]] * (size(fk) // 2), f), # [f[size(f) - 1]] * (size(fk) // 2)) # faster alternative to calculate f_add f_add = np.hstack(([f[0]] * (np.size(fk) // 2), f, \ [f[np.size(f) - 1]] * (np.size(fk) // 2))) fd = signal.lfilter(fk, 1, f_add) #fd = fd[size(fk) // 2:(size(fd) - size(fk) // 2)] # correct start and end values of time derivative fd = fd[size(fk) - 1:size(fd)] #h_add = np.append(np.append([h[0]] * (size(fk) // 2), h), # [h[size(h) - 1]] * (size(fk) // 2)) # faster alternative to calculate h_add h_add = np.hstack(([h[0]] * (np.size(fk) // 2), h, \ [h[np.size(h) - 1]] * (np.size(fk) // 2))) hd = signal.lfilter(fk, 1, h_add) #hd = hd[size(fk) // 2:(size(hd) - size(fk) // 2)] # correct start and end values of time derivative hd = hd[size(fk) - 1:size(hd)] omega = abs(((f * hd - fd * h) / (f * f + h * h)) * fs / 2 / pi) return omega
""" Instantaneous bandwidth of a signal.
Computes the instantaneous bandwidth of the given data which can be windowed or not. The instantaneous bandwidth is determined by the time derivative of the envelope normalized by the envelope of the input data.
:type data: :class:`~numpy.ndarray` :param data: Data to determine instantaneous bandwidth of. :param fs: Sampling frequency. :param fk: Filter coefficients for computing time derivative. :return: **sigma[, dsigma]** - Instantaneous bandwidth of input data, Time derivative of instantaneous bandwidth (windowed only). """ #A_win_add = np.append(np.append([row[0]] * (size(fk) // 2), row), # [row[size(row) - 1]] * (size(fk) // 2)) # faster alternative to calculate A_win_add [row[np.size(row) - 1]] * (np.size(fk) // 2))) #t = t[size(fk) // 2:(size(t) - size(fk) // 2)] # correct start and end values #sigma_add = np.append(np.append([sigma[0]] * (size(fk) // 2), sigma), # [sigma[size(sigma) - 1]] * (size(fk) // 2)) # faster alternative to calculate sigma_add [sigma[np.size(sigma) - 1]] * (np.size(fk) // 2))) #dsigma = dsigma[size(fk) // 2:(size(dsigma) - size(fk) // 2)] # correct start and end values else: sigma = np.zeros(size(x[0]), dtype='float64') #A_win_add = np.append(np.append([x[1][0]] * (size(fk) // 2), x[1]), # [x[1][size(x[1]) - 1]] * (size(fk) // 2)) # faster alternative to calculate A_win_add A_win_add = np.hstack(([row[0]] * (np.size(fk) // 2), row, \ [row[np.size(row) - 1]] * (np.size(fk) // 2))) t = signal.lfilter(fk, 1, A_win_add) #t = t[size(fk) // 2:(size(t) - size(fk) // 2)] # correct start and end values t = t[size(fk) - 1:size(t)] sigma = abs((t * fs) / (x[1] * 2 * pi)) return sigma |