Exemplo n.º 1
0
    def get_steps(self, sigs, weights, cutoff_hz=1.0, order=2, window=4.0,
                  x_threshold=2.0, figname=None, min_step_window=2.0):
        """
        Parameters
        ----------

        sigs : list(ndarray(n), ndarray(n))
            List of signals (1D-arrays)
        """

        x = sigs[0]
        y = sigs[1]
        xf, xf_ds, xf_ds_dt, x_regress = self.conditioning(x, cutoff_hz=cutoff_hz,
                                                    order=order, window=window)
        xf_ds_regress = x_regress[:,0]
        yf, yf_ds, yf_ds_dt, y_regress = self.conditioning(y, cutoff_hz=cutoff_hz,
                                                    order=order, window=window)
        yf_ds_regress = y_regress[:,0]

        # select on the product of yf_ds_regress * xf_ds_regress,
        # both need to be steady!
        xw = weights[0]
        yw = weights[1]
        xy_ds = np.abs(xf_ds_regress*xw) + np.abs(yf_ds_regress*yw)

        xf_sel_mask, xf_sel_arg = self.select(xy_ds, x_threshold)

        step_ds_mask, steps_ds = self.steady_steps(xf_sel_mask,
                                                   step_lenght=min_step_window)
        # save steps in high-res sampling of the original signal
        steps = np.round(steps_ds * self.sps / self.freq_ds, 0).astype(np.int)
        np.savetxt(figname.replace('.png', '_steps.txt'), steps)
#        steps_ds_times = self.t_ds[steps_ds.flatten()]
#        steps = np.ndarray(steps_ds.shape) * np.nan
#        for k in range(steps.shape[0]):
#            t0 = self.t_ds[steps[k,0]]
#            t1 = self.t_ds[steps[k,1]]
#            steps[k,0] = np.abs(self.time - t0).argmin()
#            steps[k,0] = np.abs(self.time - t1).argmin()

        if figname is not None:
            print('start plotting...')
            fig, axes = plotting.subplots(nrows=3, ncols=1, figsize=(8,9),
                                          dpi=120)
            ax = axes[0,0]
            ax.set_title('original and filtered signals')
            ax.plot(self.time, x, 'r-', alpha=0.3)
            ax.plot(self.time, xf, 'r-')
            ax.grid()
            axr = ax.twinx()
            axr.plot(self.time, y, 'g-', alpha=0.3)
            axr.plot(self.time, yf, 'g-')

            ax = axes[1,0]
            ax.set_title('lin regr window: %1.02f sec' % window)
            t_mask = self.t_ds.copy()
            t_mask[~xf_sel_mask] = np.nan
            x_mask = xf_ds.copy()
            x_mask[~xf_sel_mask] = np.nan
            ax.plot(self.t_ds, xf_ds, 'r-', alpha=1.0, label='xf ds')
            ax.plot(self.t_ds, x_mask, 'k-+', alpha=0.7, label='xf select')
            ax.grid()
            axr = ax.twinx()
            axr.plot(self.t_ds, yf_ds, 'g-', alpha=0.8, label='yx ds')
            y_mask = yf_ds.copy()
            y_mask[~xf_sel_mask] = np.nan
            axr.plot(self.t_ds, y_mask, 'k-+', alpha=0.7, label='yf select')
            xmin = axr.get_ylim()[0]
            xmax = axr.get_ylim()[1]
            collection = region.span_where(self.t_ds, ymin=xmin, ymax=xmax,
                                           where=xf_sel_mask, facecolor='grey',
                                           alpha=0.4)
            axr.add_collection(collection)
            leg = plotting.one_legend(ax, axr, loc='best')
            leg.get_frame().set_alpha(0.5)

            ax = axes[2,0]
            rpl = (x_threshold, min_step_window)
            ax.set_title('threshold: %1.02f, min step window: %1.2f sec' % rpl)
            ax.plot(self.t_ds, np.abs(xf_ds_regress), 'r-',
                    label='xf lin regress', alpha=0.9)
            ax.plot(self.t_ds, np.abs(yf_ds_regress), 'g-',
                     label='yf lin regress', alpha=0.9)
            ax.plot(self.t_ds, np.abs(xy_ds), 'k-', label='xy*w', alpha=0.7)
            ax.axhline(y=x_threshold, linewidth=1, color='k', linestyle='--',
                       aa=False)
            ax.set_ylim([0,5])
            xmin = ax.get_ylim()[0]
            xmax = ax.get_ylim()[1]
            collection = region.span_where(self.t_ds, ymin=xmin, ymax=xmax,
                                           where=step_ds_mask, facecolor='grey',
                                           alpha=0.4)
            ax.add_collection(collection)
#            axr = ax.twinx()
#            axr.plot(self.t_ds, np.abs(yf_ds_regress), 'g-',
#                     label='yf lin regress', alpha=0.9)
#            ax, axr = plotting.match_yticks(ax, axr)
#            axr.set_ylim([0,5])
#            leg = plotting.one_legend(ax, axr, loc='best')
#            leg.get_frame().set_alpha(0.5)

            ax.grid()
            leg = ax.legend(loc='best')
            leg.get_frame().set_alpha(0.5)

            fig.tight_layout()
            fig.savefig(figname)
            print(figname)

        return steps