def wpi(dataset, n_channels, seq_length): """ function to calculate a wavelet packet energy image # Ding.2017: Energy-Fluctuated Multiscale Feature Learning With Deep ConvNet for Intelligent Spindle Bearing Fault Diagnosis :param dataset: the raw data read from .csv or .npy file :type dataset: ndarray :param n_channels: number of channels :type n_channels: int :param seq_length: length of one data stream of a single sensor :type seq_length: int :return: the flattened images, tupel holding the image size """ level = 10 # choose an even number, paper uses 10 wavelet = 'db8' # Daubechies 8 used in paper # wavelet = 'coif3' # Daubechies 8 used in paper order = "natural" # other option is "freq" clip_energy = 1 # threshold to clip the calculated energy features (negative and positive) num_samples = dataset.shape[0] img_size = np.power(2, level // 2) size_flat = img_size * img_size pics = np.zeros([num_samples, n_channels * size_flat]) # initialize array wp_image = np.zeros([img_size, img_size]) for sample in range(num_samples): # loop over all samples for ch in range(n_channels): # loop over all channels # Construct wavelet packet tree for signal of one channel wp = WaveletPacket(dataset[sample][ch * seq_length:(ch + 1) * seq_length], wavelet, 'symmetric', maxlevel=level) nodes = wp.get_level( level, order=order ) # !required! access the tree to populate it (might be a bug of the library?) i = 0 # loop through the tree (ordered from aaa..a to ddd..d) for node in wp.get_leaf_nodes(): # use only the coefficients from node (i, p) with p = 0..(2^i-1), i.e. set all other coefficients to zero new_wp = WaveletPacket(None, wavelet, 'symmetric', level) new_wp[node.path] = wp[node.path].data # get the reconstruction coefficients (length 2^i) reconst = np.asarray(new_wp.reconstruct(update=False)) # phase shift --> arrange energy features calculated as the squared sum over the reconstruction coefficients wp_image[i % img_size, i // img_size] = np.sum(np.multiply(reconst, reconst), axis=-1) # remove node from wp tree del new_wp[node.path] i += 1 # (!) THP modification (!), clip the wpi to fixed range: especially the approximation coefficients hold a lot of energy which # scales very differently wp_image = np.clip(wp_image, -clip_energy, clip_energy) # collect all pictures, shape (samples, ch1+ch2..) pics[sample][ch * size_flat:(ch + 1) * size_flat] = np.reshape( wp_image, [1, size_flat]) return pics, [img_size, img_size]
#!/usr/bin/env python # -*- coding: utf-8 -*- from pywt import WaveletPacket wp = WaveletPacket(range(16), 'db2', maxlevel=3) print [node.path for node in wp.get_leaf_nodes(decompose=False)] print [node.path for node in wp.get_leaf_nodes(decompose=True)] coeffs = [(node.path, node.data) for node in wp.get_leaf_nodes(decompose=True)] print coeffs wp2 = WaveletPacket(None, 'db2', maxlevel=3) for path, data in coeffs: wp2[path] = data #print wp["a"] print [node.path for node in wp2.get_leaf_nodes(decompose=False)] print wp2.reconstruct()
#!/usr/bin/env python # -*- coding: utf-8 -*- from pywt import WaveletPacket wp = WaveletPacket(range(16), 'db2', maxlevel=3) print[node.path for node in wp.get_leaf_nodes(decompose=False)] print[node.path for node in wp.get_leaf_nodes(decompose=True)] coeffs = [(node.path, node.data) for node in wp.get_leaf_nodes(decompose=True)] print coeffs wp2 = WaveletPacket(None, 'db2', maxlevel=3) for path, data in coeffs: wp2[path] = data #print wp["a"] print[node.path for node in wp2.get_leaf_nodes(decompose=False)] print wp2.reconstruct()