Getting the detail coefficients as feature vectors

71 Views Asked by At

I am trying to get feature vectors from the coefficients I have generated. The spectra are decomposed using 8 levels and I am only choosing the coefficients of decomposed levels from 3 to 7. I have five simulated spectra and I am using 10 wavelet family types. I'd expect to get a feature_ vector of shape (5, 10, 5) but I am somewhat getting an error ValueError: setting an array element with a sequence. when I try to assign the decomposed coefficients at level 2-7.

Here is something close to my original data;

import pywt

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

x = np.linspace(0, 1, num=200)
signal1 = pd.DataFrame(np.sin(150 * np.pi * x**2))
signal2 = pd.DataFrame(np.sin(100 * np.pi * x**2 + 2))
signal3 = pd.DataFrame(np.sin(20 * np.pi * x**2 + 4))
signal4 = pd.DataFrame(np.sin(20 * np.pi * x**2 + 6))
signal5 = pd.DataFrame(np.sin(20 * np.pi * x**2 + 8))

# append the signals
signal = pd.concat([signal1, signal2, signal3, signal4, signal5], axis=0)

# split the signals
signal = np.array_split(signal, 5)

wavelet_list = ['db1', 'db4', 'sym6', 'sym8', 'coif2', 'coif3',\
             'bior2.6', 'bior6.8', 'rbio2.6', 'rbio5.5']

feature_vector = np.zeros((5, 10, 5))

for spectra_index in range(5):
    for wavelet_index, wavelet in enumerate(wavelet_list):
        coeffs = pywt.wavedec(signal[spectra_index], wavelet=wavelet, level=8)

        cA8, cD8, cD7, cD6, cD5, cD4, cD3, cD2, cD1 = coeffs

        feature_vector[spectra_index, wavelet_index, :] = coeffs[2:7]

        # plot the coefficients from level 3 to 7 for each wavelet and each spectrum
        fig, axes = plt.subplots(nrows=len(coeffs[2:7]), ncols=1, figsize=(15, 45))
        sns.set_style("whitegrid")
        for j, ax in enumerate(axes.ravel(), start=1):
            # plot the detail coefficients for level 3-7
            ax.plot(coeffs[j])
            # format the title to include the spectrum number, wavelet name, and level
            # ax.set_title('Spectrum {} detail coeffiecients at level {} for {}'.format(spectra_index+1, j, wavelet))

0

There are 0 best solutions below