def test_rng_seed_equal(): N = 64 x = normal(0, 1, N) seed = randint(1 << 30) # XXX This test fails with the S-number stopping criterion if more than one # thread is used. There is something in the CEEMDAN code that makes the # results sensitive to tiny discrepancies resulting from multithreaded # computation. This is issue #2 in libeemd. imfs1 = ceemdan(x, num_siftings=10, rng_seed=seed) imfs2 = ceemdan(x, num_siftings=10, rng_seed=seed) assert_allclose(imfs1, imfs2)
def my_hht(data): X = np.sum(data, axis=1) X = X / len(data[0]) plt.figure("hht_8") # X_0=hhtu.boundary_conditions(X,np.arange(len(X))) imfs = pyeemd.ceemdan( X) # EEMD处理部分,可得到imf(本证模态函数),所有参数均使用默认值即可,ceemdan是一种更优于eemd的数据处理方法, #其增添的噪声数据会自动选择当前情况下最好的噪声进行运算,但是运算时间会略微加长 # 画出ceemdan运算后所有imf的波形图 # plot_imfs(imfs,plot_splines=False) #画出hht后数据的散点图 plot_num = np.ceil((len(imfs) - 1) / 2) for i in range(len(imfs) - 1): hilbert_1 = hilbert(imfs[i]) hilbert_out.append(hilbert(imfs[i])) plt.subplot(plot_num, 2, i + 1) count = i print("hht_" + str(count)) plt.plot(hilbert_1, label="hht_" + str(count)) plt.legend(loc='best', frameon=False, bbox_to_anchor=(0.5, 0.5), ncol=3) plt.ylabel("amtitude") plt.show() return hilbert_out
def test_ones(): N = 64 x = [1] * N imfs = ceemdan(x, ensemble_size=100) for n in range(imfs.shape[0] - 1): imf = imfs[n, :] assert all(abs(imf) < 1e-9) assert_allclose(imfs[-1, :], x)
def test_extract_residual(): N = 100 t = linspace(1, 10, num=N) x = t**2 xn = x + normal(0, 0.5, N) imfs = ceemdan(xn) # the residual should be approximately equal to the signal without the # added noise, at least away from the ends residual = imfs[-1, :] print(abs(residual - x)[10:-10]) assert_allclose(residual[10:-10], x[10:-10], rtol=0.1, atol=1)
def emd_in_blocks(signal, result_filename, result_dtype=np.int16, block_length=10000, emd_type='ceemdan', num_imfs=15, ensemble_size=25, noise_strength=0.01, S_number=20, num_siftings=100): n_channels = signal.shape[0] n_timepoints = signal.shape[1] emd_shape = (n_channels, num_imfs, n_timepoints ) # channels X imfs X time points imfs = np.memmap(result_filename, dtype=result_dtype, mode='r+', shape=emd_shape) if n_timepoints < block_length: block_length = n_timepoints n_blocks = int(np.floor(n_timepoints / block_length)) extra_points = int(0.1 * block_length) cutoff = int(extra_points / 2) t0 = time.process_time() for b in np.arange(n_blocks): if b == 0: initial_offset = 0 shift_cutoff = 0 else: initial_offset = extra_points shift_cutoff = cutoff data = signal[0, b * block_length - initial_offset:(b + 1) * block_length] temp_imfs = pyeemd.ceemdan(data, num_imfs=num_imfs, ensemble_size=ensemble_size, noise_strength=noise_strength, S_number=S_number, num_siftings=num_siftings) offset = initial_offset - shift_cutoff temp_imfs = temp_imfs[:, offset:].astype(result_dtype) imfs[0, :, b * block_length - offset:(b + 1) * block_length] = temp_imfs print(time.process_time() - t0)
def emd_per_channel(channel, channel_data, result_filename, result_dtype, emd_shape, num_imfs, ensemble_size, noise_strength, S_number, num_siftings): imfs = np.memmap(result_filename, dtype=result_dtype, mode='r+', shape=emd_shape) print('Channel {} imfs started calculating'.format(str(channel))) t0 = time.process_time() channel_imfs = pyeemd.ceemdan(channel_data, num_imfs=num_imfs, ensemble_size=ensemble_size, noise_strength=noise_strength, S_number=S_number, num_siftings=num_siftings) imfs[channel, :, :] = channel_imfs del channel_imfs del imfs print('Channel {} imfs finished after {} minutes'.format( str(channel), str((time.process_time() - t0) / 60)))
def test_zeros(): x = zeros(64) imfs = ceemdan(x, ensemble_size=10) # the zero signal has zero standard deviation so no noise should be added assert all(imfs == 0)
def test_bogus1(): x = ceemdan("I am a banana")
def test_invalid_arguments6(): x = [] ceemdan(x, num_imfs="Just a few")
def test_invalid_arguments8(): x = [] ceemdan(x, num_imfs=-5)
def test_invalid_arguments3(): x = [] ceemdan(x, num_siftings=0, S_number=0)
def test_invalid_arguments4(): x = [] ceemdan(x, num_siftings=-3)
def test_invalid_arguments1(): x = [] ceemdan(x, ensemble_size=0)
def test_invalid_arguments2(): x = [] ceemdan(x, noise_strength=-2)
def test_rng_seed_nonequal(): N = 64 x = normal(0, 1, N) imfs1 = ceemdan(x, rng_seed=3141) imfs2 = ceemdan(x, rng_seed=5926) assert not allclose(imfs1, imfs2)
def test_wrong_dims(): x = zeros((2, 2)) ceemdan(x)
# (at your option) any later version. # # libeemd is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with libeemd. If not, see <http://www.gnu.org/licenses/>. from pyeemd import ceemdan from pyeemd.utils import plot_imfs from matplotlib.pyplot import plot, show, title from numpy import loadtxt # Load example ECG signal # The data is from the MIT-BIH Normal Sinus Rhythm Database, record 16265, ECG1 # More data can be downloaded from http://www.physionet.org/cgi-bin/atm/ATM ecg = loadtxt("ecg.csv", delimiter=',') # Plot the original data using Matplotlib title("Original signal") plot(ecg) # Calculate IMFs and the residual by CEEMDAN using the default parameters imfs = ceemdan(ecg, S_number=4, num_siftings=50) # Plot the results using the plot_imfs helper function from pyeemd.utils plot_imfs(imfs, plot_splines=False) show()
def test_num_imfs_just_residual(): N = 64 x = normal(0, 1, N) imfs = ceemdan(x, num_imfs=1) assert_allclose(imfs[-1, :], x)
def test_num_imfs_output_size(): N = 64 x = normal(0, 1, N) imfs = ceemdan(x, num_imfs=3) assert imfs.shape[0] == 3
def test_num_imfs(): N = 64 x = normal(0, 1, N) imfs1 = ceemdan(x, num_imfs=3, num_siftings=10, rng_seed=1234) imfs2 = ceemdan(x, num_imfs=4, num_siftings=10, rng_seed=1234) assert_allclose(imfs1[:2, :], imfs2[:2, :])
def check_completeness(): x = normal(0, 1, 64) imfs = ceemdan(x) imfsum = sum(imfs, axis=0) assert_allclose(x, imfsum)
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with libeemd. If not, see <http://www.gnu.org/licenses/>. from pyeemd import ceemdan from pyeemd.utils import plot_imfs from matplotlib.pyplot import plot, show, title import numpy as np # Decompose a delta-function signal N = 512 signal = np.zeros(N) signal[N // 2] = 1 # Plot the original data using Matplotlib title("Original signal") plot(signal) # Calculate IMFs and the residual by CEEMDAN imfs = ceemdan(signal, noise_strength=0.2, ensemble_size=500) # Plot the results using the plot_imfs helper function from pyeemd.utils plot_imfs(imfs, plot_splines=False) show() # You can compare the results with Fig. 1 in the original CEEMDAN paper at # http://dx.doi.org/10.1109/ICASSP.2011.5947265
def test_bogus2(): x = ceemdan(7)