Skip to content

Commit 69c93a3

Browse files
author
Benjamin Moody
committed
plot_signal: accept non-smooth signal data as input.
When plotting signals, in addition to allowing the signal argument to be a one-dimensional or two-dimensional array, allow it to be a list of one-dimensional arrays (so that each channel can have a different length.) This means that the array of X values (t) must be calculated separately for each channel, as both the sampling frequency and the number of samples may vary. (Here, we don't require that the lengths of the signals, in seconds, must all be the same, although they always will be when plotting a WFDB record.) The sig_len parameter makes no sense if the channels have different lengths; this parameter is now unused, and marked as deprecated.
1 parent 317c427 commit 69c93a3

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

wfdb/plot/plot.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,14 @@ def plot_signal(signal, sig_len, n_sig, fs, time_units, sig_style, axes,
364364
365365
Parameters
366366
----------
367-
signal : ndarray
368-
Tranformed expanded signal into uniform signal.
367+
signal : 1d or 2d numpy array or list
368+
The uniformly sampled signal or signals to be plotted. If signal
369+
is a one-dimensional array, it is assumed to represent a single
370+
channel. If it is a two-dimensional array, axes 0 and 1 must
371+
represent time and channel number respectively. Otherwise it must
372+
be a list of one-dimensional arrays (one for each channel).
369373
sig_len : int
370-
The signal length (per channel) of the dat file.
374+
The signal length (per channel) of the dat file. Deprecated.
371375
n_sig : int
372376
The number of signals contained in the dat file.
373377
fs : float
@@ -392,6 +396,8 @@ def plot_signal(signal, sig_len, n_sig, fs, time_units, sig_style, axes,
392396
N/A
393397
394398
"""
399+
# Convert signal to a list if needed
400+
signal = _expand_channels(signal)
395401
if n_sig == 0:
396402
return
397403

@@ -402,27 +408,24 @@ def plot_signal(signal, sig_len, n_sig, fs, time_units, sig_style, axes,
402408
# Convert sampling_freq to a list if needed
403409
sampling_freq = _get_sampling_freq(sampling_freq, n_sig, fs)
404410

405-
if any(f != sampling_freq[0] for f in sampling_freq):
406-
raise NotImplementedError(
407-
'multiple sampling frequencies are not supported')
408-
409-
# Figure out time indices
410-
if time_units == 'samples':
411-
t = np.linspace(0, sig_len-1, sig_len)
412-
else:
413-
downsample_factor = {
414-
'seconds': sampling_freq[0],
415-
'minutes': sampling_freq[0] * 60,
416-
'hours': sampling_freq[0] * 3600
417-
}
418-
t = np.linspace(0, sig_len-1, sig_len) / downsample_factor[time_units]
419-
420411
# Plot the signals
421-
if signal.ndim == 1:
422-
axes[0].plot(t, signal, sig_style[0], zorder=3)
423-
else:
424-
for ch in range(n_sig):
425-
axes[ch].plot(t, signal[:,ch], sig_style[ch], zorder=3)
412+
for ch in range(n_sig):
413+
ch_len = len(signal[ch])
414+
ch_freq = sampling_freq[ch]
415+
416+
# Figure out time indices
417+
if time_units == 'samples':
418+
t = np.linspace(0, ch_len-1, ch_len)
419+
else:
420+
downsample_factor = {
421+
'seconds': ch_freq,
422+
'minutes': ch_freq * 60,
423+
'hours': ch_freq * 3600
424+
}
425+
t = np.linspace(0, ch_len-1, ch_len)
426+
t /= downsample_factor[time_units]
427+
428+
axes[ch].plot(t, signal[ch], sig_style[ch], zorder=3)
426429

427430

428431
def plot_annotation(ann_samp, n_annot, ann_sym, signal, n_sig, fs, time_units,

0 commit comments

Comments
 (0)