#%% STFT-based # Parameters win_size_sec = 0.04 # window length for the STFFT (seconds) win_shft_sec = 0.01 # shift between consecutive windows (seconds) plt.figure() stft_spectrogram = ama.strfft_spectrogram( x, fs, win_size=round(win_size_sec * fs), win_shift=round(win_shft_sec * fs), channel_names=x_name) plt.subplot2grid((4, 5), (1, 0), rowspan=1, colspan=5) ama.plot_spectrogram_data(stft_spectrogram) plt.subplot2grid((4, 5), (0, 0), rowspan=1, colspan=5) ama.plot_signal(x, fs, x_name[0]) plt.colorbar() stft_modulation_spectrogram = ama.strfft_modulation_spectrogram( x, fs, win_size=round(win_size_sec * fs), win_shift=round(win_shft_sec * fs), channel_names=x_name) plt.subplot2grid((4, 5), (2, 1), rowspan=2, colspan=3) ama.plot_modulation_spectrogram_data(stft_modulation_spectrogram, 0, modf_range=np.array([0, 20]),
br = sio.loadmat( '/media/shruti/Data/crd_biovad_data/br_fin_biovad/br_sub_' + i + '.mat') print(br.keys()) s1 = br['br_fin'] s1 = s1.squeeze() print(s1.shape) br = s1 br = (br - np.mean(br)) / np.std(br) fs = 4 #sampling frequency w_size = 15 * fs # window size in seconds w_shift = 1 * w_size # window overlap #compute short time fourier transform rfft_spect_h = ama.strfft_spectrogram(br, fs, w_size, w_shift, win_function='hamming') power_spect_h = sum( sum(rfft_spect_h['power_spectrogram'] ))[0] * rfft_spect_h['freq_delta'] * rfft_spect_h['time_delta'] #plotting the spectrogram ama.plot_spectrogram_data(rfft_spect_h) plt.savefig('/media/shruti/Data/breathingwithouttalking_plot/' + 'br_sub_' + i + '.png') plt.clf()
ama.plot_psd_data(psd_rfft_r) # Power using its PSD from rFFT psd_rfft_b = ama.rfft_psd(x, fs, win_funct = 'blackmanharris') f_step = psd_rfft_r['freq_delta'] power_psd_rfft_x_bh = f_step * sum(psd_rfft_r['PSD'])[0] plt.figure() ama.plot_psd_data(psd_rfft_b) # Power from STFFT Spectrogram (Hamming window) w_size = 1 * fs w_shift = 0.5 * w_size rfft_spect_h = ama.strfft_spectrogram(x, fs, w_size, w_shift, win_funct = 'hamming' ) power_spect_h = sum(sum(rfft_spect_h['power_spectrogram']))[0] * rfft_spect_h['freq_delta'] * rfft_spect_h['time_delta'] plt.figure() ama.plot_spectrogram_data(rfft_spect_h) # Power from STFFT Spectrogram (Rectangular window) w_size = 1 * fs w_shift = 0.5 * w_size rfft_spect_r = ama.strfft_spectrogram(x, fs, w_size, w_shift, win_funct = 'boxcar') power_spect_r = sum(sum(rfft_spect_r['power_spectrogram']))[0] * rfft_spect_r['freq_delta'] * rfft_spect_r['time_delta'] plt.figure() ama.plot_spectrogram_data(rfft_spect_r) # Power from Wavelet Spectrogram N = 6 wav_spect_6 = ama.wavelet_spectrogram(x, fs, 6) power_wav_6 = sum(sum(wav_spect_6['power_spectrogram']))[0] * wav_spect_6['freq_delta'] * wav_spect_6['time_delta'] plt.figure() ama.plot_spectrogram_data(wav_spect_6)
# Comparison # One segment is randomly chosen random_segment = np.random.randint(0, n_segments) pwr_spectrogram_wavelet_a = np.zeros(n_segments) pwr_spectrogram_wavelet_b = np.zeros(n_segments) pwr_modulation_spectrogram_wavelet_a = np.zeros(n_segments) pwr_modulation_spectrogram_wavelet_b = np.zeros(n_segments) for i_segment in range(0, n_segments): if i_segment == random_segment: plt.figure() plt.subplot(1,2,1) ama.plot_spectrogram_data(wavelet_spectrogram_data_a[i_segment], 0); plt.subplot(1,2,2) ama.plot_spectrogram_data(wavelet_spectrogram_data_b[i_segment], 0); plt.figure() plt.subplot(1,2,1) ama.plot_modulation_spectrogram_data(wavelet_modulation_spectrogram_data_a[i_segment], 0); plt.subplot(1,2,2) ama.plot_modulation_spectrogram_data(wavelet_modulation_spectrogram_data_b[i_segment], 0); pwr_spectrogram_wavelet_a[i_segment] = sum(sum(wavelet_spectrogram_data_a[i_segment]['power_spectrogram'])) * wavelet_spectrogram_data_a[0]['freq_delta'] * wavelet_spectrogram_data_a[0]['time_delta'] pwr_spectrogram_wavelet_b[i_segment] = sum(sum(wavelet_spectrogram_data_b[i_segment]['power_spectrogram'])) * wavelet_spectrogram_data_b[0]['freq_delta'] * wavelet_spectrogram_data_b[0]['time_delta'] pwr_modulation_spectrogram_wavelet_a[i_segment] = sum(sum(wavelet_modulation_spectrogram_data_a[i_segment]['power_modulation_spectrogram'])) * wavelet_modulation_spectrogram_data_a[0]['freq_delta'] * wavelet_modulation_spectrogram_data_a[0]['freq_mod_delta'] pwr_modulation_spectrogram_wavelet_b[i_segment] = sum(sum(wavelet_modulation_spectrogram_data_b[i_segment]['power_modulation_spectrogram'])) * wavelet_modulation_spectrogram_data_b[0]['freq_delta'] * wavelet_modulation_spectrogram_data_b[0]['freq_mod_delta']
r = np.corrcoef(np.squeeze(xi), np.squeeze(xo)) print('Correlation: ' + str(r[0, 1]) + '\r\n') #%% time <--> time-frequency #%% STFT Spectrogram xi_strfft = ama.strfft_spectrogram(xi, fs, round(fs * 0.1), round(fs * 0.05)) xo = ama.istrfft_spectrogram(xi_strfft)[0] plt.figure() fi = plt.subplot(4, 1, 1) ama.plot_signal(xi, fs, 'Original x(t)') fo = plt.subplot(4, 1, 4, sharex=fi, sharey=fi) ama.plot_signal(xo, fs, 'Recovered x(t)') plt.subplot(4, 1, (2, 3)) ama.plot_spectrogram_data(xi_strfft) plt.title('STFT Spectrogram of x(t)') r = np.corrcoef(np.squeeze(xi), np.squeeze(xo)) print('Correlation: ' + str(r[0, 1]) + '\r\n') #%% CWT Spectrogram xi_cwt = ama.wavelet_spectrogram(xi, fs) xo = ama.iwavelet_spectrogram(xi_cwt) plt.figure() fi = plt.subplot(4, 1, 1) ama.plot_signal(xi, fs, 'Original x(t)') fo = plt.subplot(4, 1, 4, sharex=fi, sharey=fi) ama.plot_signal(xo, fs, 'Recovered x(t)') plt.subplot(4, 1, (2, 3)) ama.plot_spectrogram_data(xi_cwt)