import re, numpy as np, matplotlib.pyplot as plt # Globals signal_names=[] # List of the signals we traced, grabbed from line 1 values=None # List of np arrays, one array per signal. x_axis=None # 0 .002 .004 .006... for the plotting x axis def plot_signal (signame): idx = signal_names.index(signame) plt.plot (x_axis, values[idx], label=signame) #import pdb; pdb.set_trace() def plot (filename): global signal_names, values, x_axis infile = open (filename, "r") first = True inp_ls=[]; bp_ls=[]; der2_ls=[]; der2sq_ls=[] ravg_ls=[]; thresh2_ls=[]; QRS_ls=[] for line in infile: fields = re.split (r"\s+", line.strip()) if (first): # The top line has the names of the signals we've dumped, such as # "input filtered squared". first = False signal_names = fields # For three signal names, values will be a list of 3 empty lists. # We'll then populate those lists for each line we read. values = [ [] for f in fields] continue # All lines other than the top one have a whitespace-separated list # of signals, such as "12 5 2". Three values would mean we have three # signals; append each signal to one of our three lists. numbs = [int(f) for f in fields] assert len(numbs) == len(values), "Incorrect number of numbers" for idx,n in enumerate(numbs): values[idx].append(n) # Convert the lists to arrays. Now 'values' is a list of numpy arrays. for idx in range(len(values)): values[idx] = np.array(values[idx]) n_pts = values[0].size x_axis = np.arange (n_pts,dtype=float) * .002 print ("plotting...") plot_signal ("in") plot_signal ("filt") plt.legend (loc="upper right") plt.show() plot ("run.out13")