def Hilbert_Huang_Transform(X, sample_rate=500, *a, **k): ''' HHT(Hilbert Huang Transform) is a method to extract signal information on both time and frequency domain, it performs Empirical Mode Decomposition(EMD) and Hilbert transform based Signal Analysis. First raw signal will be decomposed into Intrinsic Mode Functions(IMFs) based on algorithm presented by Huang et al. in 1998. IMFs are actually components of raw signal within different frequency durations. Comparing to samiliar decomposition algrithm Wavelet Transform, EMD generates much precies result both in time domain and frenqucy domain. HSA can compute instant frenquency of IMFs, thus outputing time-freq spectrum. +---+ EMD +------+ HSA +--------------------+ | S +---------+ IMFs +-----------+ Freq-Time spectrum | +---+ +------+ +--------------------+ Notes ----- Difference between two hilbert in scipy:: scipy.signal.hilbert(x) = x + scipy.fftpack.hilbert(x) * j 1. scipy.fftpack.hilbert(x) ==> y 2. scipy.signal.hilbert(x) ==> x + yj y is hilbert tranform of x, this result is called analytic signal ''' imfs = np.array([pyhht.EMD(ch).decompose() for ch in X]) analytic_signal = scipy.signal.hilbert(imfs, axis=-1) inst_phase_wrapped = np.angle(analytic_signal) inst_phase = np.unwrap(inst_phase_wrapped, axis=-1) return np.diff(inst_phase, axis=-1) / (2 * np.pi) * sample_rate
def getIMFstats(features): result = numpy.array([]) time = numpy.linspace(0, 2, 1332) for n, row in enumerate(features): temp_result = numpy.array([]) for n2, channel in enumerate(row): decomposer = pyhht.EMD(channel) imfs = decomposer.decompose() imfs = imfs[:7, ] if imfs.shape[0] == 6: imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) if imfs.shape[0] == 5: imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) if imfs.shape[0] == 4: imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) if imfs.shape[0] == 3: imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) if imfs.shape[0] == 2: imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) if imfs.shape[0] == 1: imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) imfs = numpy.vstack((imfs, numpy.linspace(0, 0, 1332))) stats = getStatistics(imfs.reshape(1, -1, 1332)) temp_result = numpy.hstack((temp_result, stats.reshape(-1))) if n % 400 == 0: print(n) if n == 0: result = temp_result else: result = numpy.vstack((result, temp_result)) return result
def hht_transform(signal: pd.Series, sampling_rate: int = 256): """ The Hilbert-Huang transform is useful for performing time-frequency analysis of nonstationary and nonlinear data. The Hilbert-Huang procedure consists of the following steps: 1. emd decomposes the data set x into a finite number of intrinsic mode functions. 2. For each intrinsic mode function, xi, the function hht: Uses hilbert to compute the analytic signal, zi(t)=xi(t)+jH{xi(t)}, where H{xi} is the Hilbert transform of xi. Expresses zi as zi(t)=ai(t) ejθi(t), where ai(t) is the instantaneous amplitude and θi(t) is the instantaneous phase. Computes the instantaneous energy, ai(t)2, and the instantaneous frequency, ωi(t)≡dθi(t)/dt. If given a sample rate, hht converts ωi(t) to a frequency in Hz. Outputs the instantaneous energy in imfinse and the instantaneous frequency in imfinsf. When called with no output arguments, hht plots the energy of the signal as a function of time and frequency, with color proportional to amplitude. """ Ts = sampling_rate emd_signal = pyhht.EMD(signal) imfs = emd_signal.decompose() b = [] d = [] for imf in imfs: b += [np.sum(np.multiply(imf, imf))] th = np.angle(hilbert(imf)) d += [np.diff(th) / Ts / (2 * math.pi)] b = np.array(b) v = np.argsort(b) b_max = np.max(b) b = [(1 - x) / b_max for x in b] N = len(signal) c = np.linspace(0, (N - 2) * Ts, N - 1) for k in v[0:1]: plt.plot(c, d[k], color=[b[k], b[k], b[k]], markersize=3) plt.show() pass
def _transform_signal(self, signal, window_length): decomposer = pyhht.EMD(signal, n_imfs=self.max_imfs, maxiter=self.max_iter) imfs = decomposer.decompose() if self.mode == "set_max": imfs = imfs[:-1] elif self.mode == "minkowski": distances = [] s = signal - imfs[-1] * int(self.subtract_residue) for imf in imfs[:-1]: distances.append(minkowski(s, imf)) idx = np.argpartition(distances, np.min([len(distances) - 1, self.n_imfs])) imfs = imfs[idx[:self.n_imfs]] imfs_zeros = np.zeros([window_length, self.n_imfs]) imfs_zeros[:, :len(imfs)] = imfs.T return imfs_zeros
def get_imf(i, data): decomposer = hht.EMD(data[:, i + 1] + data[:, 0]) imf_highfreq = decomposer.decompose()[2] return imf_highfreq
# -*- coding: utf-8 -*- """ Created on Wed Jun 5 16:01:27 2019 @author: veepoo """ from pyhht.visualization import plot_imfs import numpy as np import pyhht import math t = np.linspace(0, 100, 1000) fs1 = 4 fs2 = 10 modes = np.cos(math.radians(2 * math.pi * fs1 * t)) + np.cos( math.radians(2 * math.pi * fs2 * t)) x = modes + t decomposer = pyhht.EMD(x) imfs = decomposer.decompose() plot_imfs(x, imfs, t) #doctest: +SKIP
if len(x) == len(y): q = [0] for i in range(2, len(y)): q.append((y[i] - y[i - 1]) / (x[i] - x[i - 1])) q.append(0) return np.array(q) else: print("Error: Invalid shape. x and y must be the same length") #computing the derivative of column 1 wrt time differ = (diff(data[:, 1], data[:, 0])) pl.plot(data[:, 0], differ) pl.show() decomposer = hht.EMD(data[:, 1] + data[:, 0]) imfs = decomposer.decompose() vis.plot_imfs(data[:, 0] + data[:, 1], imfs, data[:, 0]) for blep in range(0, 27): #Determine where peaks are, change parameters for a change in what is defined as a "peak". In order, minimum height limit, vertical distance to neighbouring samples, horizontal distance to neighboring samples, how prominant a "peak" is, required width of peaks. ProtoPeaks = scipy.signal.find_peaks(data[:, blep], height=0, threshold=0, distance=1, prominence=70, width=0) Peaks = ProtoPeaks[0] print( "For the below graph, (graph number", blep, ")the peaks as defined by the paramters on line 53 of the program are at"
import matplotlib.pyplot as plt from pyhht.visualization import plot_imfs image = cv2.imread("dan.jpg") image = cv2.resize(image, (108, 108)) image2 = cv2.imread('images.jpg') image2 = cv2.resize(image2, (108, 108)) pi = 3.14 temp = image.flatten() temp2 = image2.flatten() t = np.linspace(0, 1, 34992) # modes = np.sin(2 * pi * 5 * t) + np.sin(2 * pi * 10 * t) # x = modes + t temp = temp + t temp2 = temp2 + t hilbert = pyhht.EMD(temp) imfs = hilbert.decompose() hilbert2 = pyhht.EMD(temp2) imfs2 = hilbert.decompose() print(imfs.shape, imfs2.shape) plot_imfs(temp, imfs, t) plot_imfs(temp, imfs2, t)