def get_br_spectrogram(br,win_): fs=4 w_size = win_ * fs w_shift = 1*fs #consistant in overlap rfft_spect_h = ama.strfft_spectrogram(br, 4, w_size, w_shift, win_function = 'hamming' ) return rfft_spect_h
def get_psd_ama(x_,fs): #computing signal fft N = len(x_) # sample spacing w_size = 20 * fs w_shift= 20*fs rfft_spect_h = ama.strfft_spectrogram(x_, 4, 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'] psd=(rfft_spect_h['power_spectrogram'][0,:,0]) psd=psd/np.sum(psd) freq=rfft_spect_h['freq_axis'] return freq, psd
fs, x = wavfile.read('./example_data/p234_004.wav') x_name = ['speech'] x = x / 32768 # 1s segment to analyze x = x[int(fs * 1.6):int(fs * 3.6)] #%% 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)
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()
f_step = psd_rfft_r['freq_delta'] power_psd_rfft_x_rw = f_step * sum(psd_rfft_r['PSD'])[0] plt.figure() 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']
xo = ama.irfft_psd(xi_psd) 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_psd_data(xi_psd) plt.title('PSD of x(t)') 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)