-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_edf.py
43 lines (39 loc) · 1.41 KB
/
read_edf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pyedflib
import matplotlib.pyplot as plt
import numpy as np
def read_edf_file(file_path):
try:
f = pyedflib.EdfReader(file_path)
num_signals = f.signals_in_file
signal_labels = f.getSignalLabels()
signals = [f.readSignal(i) for i in range(num_signals)]
sample_frequency = f.getSampleFrequency(0)
f.close()
return signal_labels, signals, sample_frequency
except Exception as e:
print(f"Error reading .edf file: {e}")
return None
def plot_signals(signal_labels, signals, sample_frequency, save=False):
num_channels = len(signal_labels)
fig, axes = plt.subplots(num_channels, 1, figsize=(10, 2 * num_channels))
for i in range(num_channels):
total_duration = len(signals[i]) / sample_frequency
time_axis = np.linspace(0, total_duration, len(signals[i]))
# print("#", total_duration)
axes[i].plot(time_axis, signals[i], label=signal_labels[i])
axes[i].set_ylabel("Amplitude")
axes[i].legend()
axes[i].set_xlabel("Time (seconds)")
axes[i].set_xlim(0, total_duration)
plt.subplots_adjust(hspace=0.5)
if save:
plt.savefig('pre\plot.png')
plt.show()
def plot_by_nparray(array, title, save=False):
plt.figure(figsize=(6, 4))
plt.plot(array)
plt.title(title)
if save:
path = "pre\\" + title + ".png"
plt.savefig(path)
plt.show()