def plot_average_psd(data, window_length, offset, fig=False): if fig: plt.figure() data_ep = np.squeeze(epoch_data(data, window_length, offset)) psd = ama.rfft_psd(data_ep.T, fs) m = np.mean(psd['PSD'], axis=-1) mpsd = psd mpsd['PSD'] = np.reshape(m, [-1, 1]) ama.plot_psd_data(mpsd, f_range=np.array([0, 60]))
def get_psd(self, data): sample_psd = ama.rfft_psd(data, self.headset_freq) return sample_psd['freq_axis'], sample_psd['PSD']
def plot_psd(data): psd = ama.rfft_psd(data, fs) ama.plot_psd_data(psd, f_range=np.array([0, 60]))
# The Spectograms are scaled to represent the power of the full signal wavelet_spect_segmented = n_segments * wavelet_spect_segmented wavelet_spectrogram_power_b = [] wavelet_modulation_spectrogram_power_b = [] # From each Segment of the Spectrogram, compute the Modulation Spectrogram for i_segment in range(0, n_segments): wavelet_spectrogram_power_b.append(wavelet_spect_segmented[:,:,i_segment]) # Square Root is obtained to work with the Instantaneous Amplitude x_tmp_wavelet = np.sqrt(np.squeeze(wavelet_spect_segmented[:,:, i_segment])) # PSD of the Spectrogram Segment mod_psd_wavelet = ama.rfft_psd(x_tmp_wavelet, fs) # Place results in corresponding index wavelet_modulation_spectrogram_power_b.append(mod_psd_wavelet['PSD'] / mod_psd_wavelet['freq_delta']) toc = time.time() - tic print(str(toc) + ' seconds') # Create dictionaries for Method B, for sake of plotting wavelet_spectrogram_data_b = deepcopy(wavelet_spectrogram_data_a) wavelet_modulation_spectrogram_data_b = deepcopy(wavelet_modulation_spectrogram_data_a) for i_segment in range(0, n_segments): wavelet_spectrogram_data_b[i_segment]['power_spectrogram'] = wavelet_spectrogram_power_b[i_segment][:,:,np.newaxis] wavelet_modulation_spectrogram_data_b[i_segment]['power_modulation_spectrogram'] = np.transpose(wavelet_modulation_spectrogram_power_b[i_segment])[:,:,np.newaxis]
energy_x = T * sum(x**2) duration = T * n # Power of the signal power_x = energy_x / duration # A simpler way is power_x_2 = (1 / n) * sum(x**2) # Power in Frequency domain # Power using FFT X = np.fft.fft(x) power_X = float( (1 / n**2) * sum(np.abs(X)**2)) # Power using its PSD from rFFT psd_rfft_r = ama.rfft_psd(x, fs, win_funct = 'boxcar') 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
ipls] = pls_t_win # _ _ * _ _ if age_limits[0] <= age <= age_limits[1]: # appends pulse peaks from one file to ALL files list pls_peaks_all.append(pls_peaks) ppg_signals_all.append(ppg_signals) age_all.append(age) # %% analysis # concatenate all PPG signals and Pulse peaks pls_peaks_all_cat = np.concatenate(pls_peaks_all, axis=1) ppg_signals_all_cat = np.concatenate(ppg_signals_all, axis=1) # PSD pls_peaks_psd = ama.rfft_psd(pls_peaks_all_cat, fs, 1024) ppg_signals_psd = ama.rfft_psd(ppg_signals_all_cat, fs, 1024) # average across instances pls_peaks_psd['PSD'] = np.mean(pls_peaks_psd['PSD'], axis=1)[:, None] ppg_signals_psd['PSD'] = np.mean(ppg_signals_psd['PSD'], axis=1)[:, None] # compute transfer function n_fft = 2048 pls_peaks_fft = np.fft.fft(pls_peaks_all_cat, n_fft, axis=0) ppg_signals_fft = np.fft.fft(ppg_signals_all_cat, n_fft, axis=0) # adjust for even and odd number of elements if n_fft % 2 != 0: # odd case n_freqs = int((n_fft + 1) / 2) else:
#%% time <--> frequency #%% FFT and IFFT of a real-valued signal xi_rfft = ama.rfft(xi) xo = ama.irfft(xi_rfft, n) plt.figure() fi = plt.subplot(2, 1, 1) ama.plot_signal(xi, fs, 'Original x(t)') fo = plt.subplot(2, 1, 2, sharex=fi, sharey=fi) ama.plot_signal(xo, fs, 'Recovered x(t)') r = np.corrcoef(np.squeeze(xi), np.squeeze(xo)) print('Correlation: ' + str(r[0, 1]) + '\r\n') #%% PSD data obtained with rFFT, and its inverse xi_psd = ama.rfft_psd(xi, fs) 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