def Inst_freq(peeled_seq, fs=4000): t = np.arange(0, 1, 1 / fs) # print(len(t)) emd = EMD() emd.emd(peeled_seq) imfs, res = emd.get_imfs_and_residue() vis = Visualisation(emd) imfs_inst_freqs = vis._calc_inst_freq(imfs, t, order=False, alpha=None) return imfs_inst_freqs
def eemd_decompose(a, t): eemd = EEMD(trials=200, noise_width=0.4)(a) eimfs, res = eemd[:-1], eemd[-1] #这的参数有问题!!!!!!!!!!!!!!!!!!!!! vis = Visualisation() vis.plot_imfs(imfs=eimfs, residue=res, t=t, include_residue=True) vis.plot_instant_freq(t, imfs=eimfs) # vis.show() return eimfs, res
def emd_decompose(a, t): emd = EMD() emd.emd(a) imfs, res = emd.get_imfs_and_residue() plt.plot(t, a) plt.title('origin sequence') vis = Visualisation(emd) vis.plot_imfs(t=t) vis.plot_instant_freq(t) vis.show() plt.show() return imfs, res
def Visualing(peeled_seq, fs=4000): t = np.arange(0, 1, 1 / fs) # print(len(t)) emd = EMD() emd.emd(peeled_seq) imfs, res = emd.get_imfs_and_residue() vis = Visualisation(emd) # Create a plot with all IMFs and residue vis.plot_imfs(imfs=imfs, residue=res, t=t, include_residue=True) # Create a plot with instantaneous frequency of all IMFs vis.plot_instant_freq(t, imfs=imfs) # Show both plots vis.show()
def hes2(args): n = 10000 t = np.arange(0, n/args.fs, 1/args.fs) S = args.singled_out[-1, 0:n] args.temp_add = 'emd_raw' show_signal(S, args) emd = EMD() emd.emd(S) imfs, res = emd.get_imfs_and_residue() # In general: # components = EEMD()(S) # imfs, res = components[:-1], components[-1] vis = Visualisation() vis.plot_imfs(imfs=imfs, residue=res, t=t, include_residue=True) vis.plot_instant_freq(t, imfs=imfs) vis.show() return 0
def get_IMFset(data): print("start ensemble decompose") imfs_set = [] ceemdan = CEEMDAN() for decon_time in range(data.shape[0]): series = np.reshape(data[decon_time], (data.shape[1], )) imfs = ceemdan(series) imf, res = imfs[:-1], imfs[-1] imfs_set.append(imfs) print("processing No.", decon_time, "series") vis = Visualisation() vis.plot_imfs(imfs=imf, residue=res, include_residue=True) vis.show() return imfs_set
imfs, res = emd.get_imfs_and_residue() fig = plt.figure(figsize=(12, 16)) ax_main = fig.add_subplot(len(imfs) + 1, 1, 1) ax_main.set_title(title) ax_main.plot(t, d) rec_b = [] for i in imfs: rec_b.append(d - i) d -= i for i, y in enumerate(rec_b): ax = fig.add_subplot(len(rec_b) + 1, 2, 3 + i * 2) ax.plot(t, y, 'r') ax.set_ylabel("A%d" % (i + 1)) for i, y in enumerate(imfs): ax = fig.add_subplot(len(imfs) + 1, 2, 4 + i * 2) ax.plot(t, y, 'g') ax.set_ylabel("D%d" % (i + 1)) # In general: #components = EEMD()(S) #imfs, res = components[:-1], components[-1] vis = Visualisation() vis.plot_imfs(imfs=imfs, residue=res, t=t, include_residue=True) vis.plot_instant_freq(f, imfs=imfs) vis.show()
plt.figure(figsize=(16, 16)) title = 'VMD' n = len(u) fig = plt.figure(figsize=(12, 16)) ax_main = fig.add_subplot(n + 1, 1, 1) ax_main.set_title(title) ax_main.plot(t, d) rec_b = [] for i in u: rec_b.append(d - i) d -= i for i, y in enumerate(rec_b): ax = fig.add_subplot(len(rec_b) + 1, 2, 3 + i * 2) ax.plot(t, y, 'r') ax.set_ylabel("A%d" % (i + 1)) for i, y in enumerate(u): ax = fig.add_subplot(len(u) + 1, 2, 4 + i * 2) ax.plot(t, y, 'g') ax.set_ylabel("D%d" % (i + 1)) from PyEMD import Visualisation vis = Visualisation() vis.plot_instant_freq(f, imfs=u) vis.show()
def __init__(self, damage): self.damage = damage self.signals = self.extract_signals() self.vis = Visualisation()
class FrequencyAnalysis: def __init__(self, damage): self.damage = damage self.signals = self.extract_signals() self.vis = Visualisation() def extract_signals(self, scale=1, points=None): signals = [] points = points or 10000 for k in self.damage.keys(): temp = [] if len(k.split('-')[-1]) < 3: data = scio.loadmat( os.path.join(DATA_ROOT_DIR, self.damage[k][0] + '.mat')) else: params = self.damage[k][0].split('-') name = '' for p in params[:-1]: name += p + '-' name += 'n' data = scio.loadmat(os.path.join(DATA_ROOT_DIR, name + '.mat')) temp.append(data['s0'][:points, 0] / scale) temp.append(data['s1'][:points, 0] / scale) temp.append(data['s2'][:points, 0] / scale) temp.append(data['s3'][:points, 0] / scale) temp.append(data['s4'][:points, 0] / scale) temp.append(data['s5'][:points, 0] / scale) signals.append(temp) return signals def FFT(self, points=None, show_results=False, save=False, figsize=(36, 25), show_range=None): points = points or len(self.signals[0][0]) t = np.arange(0, points) * 24000 / points j = 0 fft_results = [] show_range = show_range or (0, points) # for temp in self.signals: # shape = (3, len(temp) // 3) # fig, axs = plt.subplots(shape[0], shape[1], figsize=figsize) # fft_temp = [] # for i in range(len(temp)): # fft = np.power(np.abs(np.power(np.fft.fft(temp[i], points), 2)), 0.5) # fft_temp.append(fft) # axs[i // 2, i % 2].plot(t[show_range[0]: show_range[1]], fft[show_range[0]: show_range[1]]) # axs[i // 2, i % 2].set_title('s{} energy spectrum'.format(j, i)) # fig.suptitle('class {}'.format(j)) # if save: # plt.savefig(os.path.join(RESULT_ROOT, str(i) + "_class_fft_energy_spectrum.png"), dpi=600, # bbox_inches='tight') # if show_results: # plt.show() # j += 1 # fft_results.append(fft_temp) # # return fft_results for temp in self.signals: temp = np.array(temp) self.vis.plot_instant_freq(t=t, imfs=temp) if save: plt.savefig(os.path.join( RESULT_ROOT, str(i) + "_class_fft_energy_spectrum.png"), dpi=600, bbox_inches='tight') if show_results: self.vis.show() def STFT(self, show_results=False, save=False, figsize=(25, 16), show_range=(0, 1500), nperseg=128): i = 0 stft_results = [] for temp in self.signals: shape = (3, len(temp) // 3) stft_temp = [] plt.figure(figsize=figsize) for j in range(len(temp)): params = scignal.stft(temp[j], fs=24e3, nperseg=nperseg) f, t, zxx = params plt.subplot(shape[0], shape[1], j + 1) plt.pcolormesh(t, f, np.abs(zxx)) plt.colorbar() plt.title('S{} STFT Magnitude'.format(j)) plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.ylim(show_range[0], show_range[1]) stft_temp.append(params) # plt.suptitle('Class {}'.format(i)) stft_results.append(stft_temp) if save: plt.savefig(os.path.join(RESULT_ROOT, str(i) + "_class_sfft_spectrum.png"), dpi=600, bbox_inches='tight') i += 1 if show_results: plt.show() return stft_results def wavelet(self, show_results=False, save=False, w='cgau8', sampling_rate=24e3, figsize=(36, 25), points=None): """ :param figsize: :param sampling_rate: 采样频率 :param show_results: :param save: :param w: 小波函数种类 :return: """ wavelet_results = [] totalscal = len(self.signals[0][0]) points = points or totalscal t = np.arange(0, points) * 24000 / points fc = pywt.central_frequency(w) # 中心频率 cparam = 2 * fc * totalscal scales = cparam / np.arange(totalscal, 1, -1) i = 0 for temp in self.signals: shape = (3, len(temp) // 3) # fig, axs = plt.subplots(shape[0], shape[1], figsize=figsize) wavelet_temp = [] for j in range(len(temp)): [cwtmatr, frequencies] = pywt.cwt(temp[j], scales, w, 1.0 / sampling_rate) # 连续小波变换 plt.subplot(shape[0], shape[1], j + 1) plt.contourf(t, frequencies, abs(cwtmatr)) plt.ylabel(u"freq(Hz)") plt.xlabel(u"time(s)") plt.title('S{} Wavelet Magnitude'.format(j)) wavelet_temp.append([cwtmatr, frequencies]) wavelet_results.append(wavelet_temp) if save: plt.savefig(os.path.join( RESULT_ROOT, str(i) + "_class_wavelet_spectrum.png"), dpi=600, bbox_inches='tight') i += 1 if show_results: plt.show() return wavelet_results
ceemdan = CEEMDAN(trials=10) cIMFs = ceemdan(temp) b = time.time() print('CEEMDAN consumes time is %.4f' % (b - a)) transformer = FastICA(n_components=3, random_state=0, whiten=True, max_iter=1000) transform_component = transformer.fit_transform(cIMFs, ) sperated_signale = np.dot(transform_component.T, cIMFs) plt.subplot(4, 1, 1) plt.plot(sig1) for index, sig_temp in enumerate(sperated_signale): plt.subplot(4, 1, index + 2) plt.plot(sig_temp) a = time.time() eemd = EEMD(trials=5) eIMFs = eemd(temp) b = time.time() print('EEMD consumes time is %.4f' % (b - a)) # 100次 CEEMDAN 29s EEMD 14s # 20次 CEEMDAN 7s EEMD 3s # #TODO: 太慢了,要用到多线程了 vis = Visualisation()