def plot_sensors_cwt(data_seg, n_periods, wavelet, figsize=(12, 32)): f, axs = plt.subplots(10, 2, figsize=figsize, constrained_layout=True, sharex=True) periods = np.arange(1, n_periods) scales = scaleogram.periods2scales(periods) time = data_seg.reset_index()['index'].values for ax, case in zip(axs, data_seg.columns): sensor_number = case.split("_")[-1] ax[0].set_title(f'Time series: sensor:{sensor_number}') ax[0].plot(data_seg[case].values, marker='o', ls='-', ms=0.05, alpha=0.5) ax[1] = scaleogram.cws(time=time, wavelet=wavelet, scales=scales, signal=data_seg[case].values, coikw={"alpha": 0.9}, ax=ax[1]) ax[1].set_title(f'CWT: sensor:{sensor_number}: wavelet: {wavelet}') plt.show()
def generate_spectral_analysis(ecg_data: list, start: int, end: int, classif: str = 'NA', spectrum_max_hz: int = 40, fs: int = 1_000): fig, (ax0, ax1, ax2) = plt.subplots(3, 1, figsize=(8, 4), sharex=True) fig.subplots_adjust(hspace=.01) # ax0 ax0.plot(np.arange(0, len(ecg_data)), ecg_data, linewidth=1) ax0.axis('off') # ax2 f, t, Sxx = signal.spectrogram( ecg_data, fs, # window=('tukey', 0.1), # nperseg=150) window=('tukey', 0.1), nperseg=int(round(fs / 4, 0))) ax1.pcolormesh(t * fs, -f, Sxx, shading='flat') ax1.set_ylim(-spectrum_max_hz) ax1.set_ylabel('Hz (inv)') # ax1 scg.set_default_wavelet('morl') signal_length = spectrum_max_hz # range of scales to perform the transform scales = scg.periods2scales(np.arange(1, signal_length + 1)) # the scaleogram # sampling_ratio = 4 # sample_ecg = [] # for element in range(int(round(len(ecg_data)/sampling_ratio, 0))): # sample_ecg.append(np.mean(ecg_data[sampling_ratio * element: # sampling_ratio * element + # sampling_ratio])) scg.cws(ecg_data, scales=scales, figsize=(10, 2.0), coi=False, ylabel="Hz", xlabel='Frame', ax=ax2, cbar=None, title='') fig.suptitle( 'spectrum frequency from frame {} to {} - classif : {}'.format( start, end, classif), fontsize=10) st.pyplot(fig)
def wavelets(time, flux): """Using morlet wavelet from scaleogram package to determine period. Values are plotted on a 2-D contour map, and then transformed into a 1-D plot. Args: time (List): Time values from processed data file. flux (List): Flux values from processed data file. """ flux = flux / np.median(flux) - 1 flux = flux / np.std(np.diff(flux)) # Convert time to np array for scaleogram. time = np.asarray(time) # Spacing in time values for computing transform dt = time[1] - time[0] scales = scg.periods2scales(np.arange(1, len(time))) scg.set_default_wavelet('cmor2-2.0') wavelet = scg.get_default_wavelet ax2 = scg.cws(time, flux, scales=scales, coikw={ 'alpha': 0.5, 'hatch': '/' }) plt.show() # Same code in scaleogram package, used to visualize 1-D version of the data. coeff, scales_freq = scg.fastcwt(flux, scales, 'cmor2-2.0', dt) # Sum all of the x values pertaining to each period value. period_sum = [] for idx, arr in enumerate(coeff): period_sum.append(np.sum(np.abs(arr))) # Period values from the fastcwt function. transformed_time = 1. / scales_freq output.plot_graph(transformed_time, period_sum, "Period", "Sum per Period", "Wavelet Transformation - 1-D")
# Wavelet attempt wl_rise_data = rolling_rise_mean.dropna() wl_rise_time = rise_panda.time[wl_rise_data.index].to_numpy() wl_rise_data = wl_rise_data.to_numpy() wl_rise_data_norm = wl_rise_data-wl_rise_data.mean() # choose default wavelet function for the entire notebook scg.set_default_wavelet('cmor1.5-1.0') if testing == True: fig1, axs = plt.subplots(2, figsize=(20,12)); lines = axs[0].plot(wl_rise_time, wl_rise_data); axs[0].set(xlabel='time', ylabel='detrended and norm width of jet [km]') scales = scg.periods2scales(np.arange(1, 120)) scg.cws(wl_rise_time, wl_rise_data_norm, scales=scales, ax=axs[1]) scg.cws(wl_rise_time, wl_rise_data_norm, ax=axs[1], yscale='linear', cbar='horizontal'); plt.tight_layout() plt.show() wl_fall_data = rolling_fall_mean.dropna() wl_fall_time = fall_panda.time[wl_fall_data.index].to_numpy() wl_fall_time = wl_fall_time-wl_fall_time[0] wl_fall_data = wl_fall_data.to_numpy() wl_fall_data_norm = wl_fall_data-wl_fall_data.mean() # choose default wavelet function forlen() the entire notebook if testing == True: fig1, axs = plt.subplots(2, figsize=(20,12));
if st.checkbox('Show raw data'): st.subheader('Raw data') st.write(data) st.subheader(f"ECG record : {chosen_record}") st.line_chart(data) if samptovalue - sampfromvalue > 2000: st.text('No scaleogram range to long') else: # choose default wavelet function scg.set_default_wavelet('morl') signal_length = samptovalue - sampfromvalue # range of scales to perform the transform scales = scg.periods2scales(np.arange(1, signal_length + 1)) x_values_wvt_arr = range(0, len(data), 1) # the scaleogram fig = scg.cws(data, scales=scales, figsize=(10, 4.0), coi=False, ylabel="Period", xlabel="Time") #st.plotly_chart(scal) #coeff, freq = pywt.cwt(data, 500 , 'morl', 1) st.pyplot(fig.figure)