将EEG时序信号转换为格拉姆角场图像(Python)

摘要:import numpy as np # linear algebraimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)import osfor dirname, _,

import numpy as np # linear algebraimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)import osfor dirname, _, filenames in os.walk('/kaggle/input'):for filename in filenames:print(os.path.join(dirname, filename))pip install pytsimport numpy as npimport osfrom pyts.image import GramianAngularField, RecurrencePlotimport matplotlib.pyplot as plt# Function to read signals from files and segment themdef read_and_segment_signals(directory, segment_size):segmented_signals = for filename in os.listdir(directory):file_path = os.path.join(directory, filename)if os.path.isfile(file_path):# Load the entire signalsignal = np.loadtxt(file_path, delimiter=',', usecols=[0])# Split the signal into segments of the specified sizefor start_idx in range(0, len(signal) - segment_size + 1, segment_size):segment = signal[start_idx:start_idx + segment_size]segmented_signals.append(segment)return segmented_signals# Function to apply GASF transformation to each segmentdef apply_gasf_to_segments(segments):gasf = GramianAngularField(image_size=segments[0].shape[0])gasf_images = gasf.fit_transform(segments)return gasf_imagesdef apply_rp_to_segments(segments):# Initialize the Recurrence Plot transformerrp = RecurrencePlot# Transform each segmentrp_images = rp.fit_transform(segments)return rp_imagessegment_size = 256focal_directory = '/kaggle/input/bern-eeg-dataset/Data_F_50' non_focal_directory = '/kaggle/input/bern-eeg-dataset/Data_N_50' # Read and segment the signalsfocal_segments = read_and_segment_signals(focal_directory, segment_size)non_focal_segments = read_and_segment_signals(non_focal_directory, segment_size)# Apply GASF transformationfocal_gasf_images = apply_gasf_to_segments(focal_segments)non_focal_gasf_images = apply_gasf_to_segments(non_focal_segments)def visualize_gasf_images(focal_image, non_focal_image):"""Visualizes one focal and one non-focal GASF image side by side.Parameters:- focal_image: A 2D NumPy array representing the focal GASF image.- non_focal_image: A 2D NumPy array representing the non-focal GASF image."""# Set up the figure and axesfig, axs = plt.subplots(1, 2, figsize=(10, 5))# Plot the focal GASF imageaxs[0].imshow(focal_image, cmap='jet', aspect='auto')axs[0].set_title('Focal GASF Image')# Plot the non-focal GASF imageaxs[1].imshow(non_focal_image, cmap='jet', aspect='auto')axs[1].set_title('Non-Focal GASF Image')# Display the plotplt.tight_layoutplt.showvisualize_gasf_images(focal_gasf_images[5], non_focal_gasf_images[1])

知乎学术咨询:

https://www.zhihu.com/consult/people/792359672131756032?isMe=1

担任《Mechanical System and Signal Processing》《中国电机工程学报》等期刊审稿专家,擅长领域:信号滤波/降噪,机器学习/深度学习,时间序列预分析/预测,设备故障诊断/缺陷检测/异常检测。

分割线分割线分割线

基于小波分析的时间序列降噪(Python,ipynb文件)

完整代码:

时间序列的最大重叠离散小波分解(Python)

完整代码:

基于连续小波变换的信号滤波方法(Python,ipynb文件)

不同小波族的优缺点

来源:小萱科技观

相关推荐