예제 #1
0
class Plot(object):
    """
    Plot Examples
    =============

    See examples/plotting.py for many more examples.


    >>> from sympy import symbols, Plot
    >>> x,y,z = symbols('xyz')

    >>> Plot(x*y**3-y*x**3)

    >>> p = Plot()
    >>> p[1] = x*y
    >>> p[1].color = z, (0.4,0.4,0.9), (0.9,0.4,0.4)

    >>> p = Plot()
    >>> p[1] =  x**2+y**2
    >>> p[2] = -x**2-y**2


    Variable Intervals
    ==================

    The basic format is [var, min, max, steps], but the
    syntax is flexible and arguments left out are taken
    from the defaults for the current coordinate mode:

    >>> Plot(x**2) # implies [x,-5,5,100]
    >>> Plot(x**2, [], []) # [x,-1,1,40], [y,-1,1,40]
    >>> Plot(x**2-y**2, [100], [100]) # [x,-1,1,100], [y,-1,1,100]
    >>> Plot(x**2, [x,-13,13,100])
    >>> Plot(x**2, [-13,13]) # [x,-13,13,100]
    >>> Plot(x**2, [x,-13,13]) # [x,-13,13,100]
    >>> Plot(1*x, [], [x], mode='cylindrical')
    ... # [unbound_theta,0,2*Pi,40], [x,-1,1,20]


    Coordinate Modes
    ================

    Plot supports several curvilinear coordinate modes, and
    they independent for each plotted function. You can specify
    a coordinate mode explicitly with the 'mode' named argument,
    but it can be automatically determined for cartesian or
    parametric plots, and therefore must only be specified for
    polar, cylindrical, and spherical modes.

    Specifically, Plot(function arguments) and Plot[n] =
    (function arguments) will interpret your arguments as a
    cartesian plot if you provide one function and a parametric
    plot if you provide two or three functions. Similarly, the
    arguments will be interpreted as a curve is one variable is
    used, and a surface if two are used.

    Supported mode names by number of variables:

    1: parametric, cartesian, polar
    2: parametric, cartesian, cylindrical = polar, spherical

    >>> Plot(1, mode='spherical')


    Calculator-like Interface
    =========================

    >>> p = Plot(visible=False)
    >>> f = x**2
    >>> p[1] = f
    >>> p[2] = f.diff(x)
    >>> p[3] = f.diff(x).diff(x)
    >>> p
    [1]: x**2, 'mode=cartesian'
    [2]: 2*x, 'mode=cartesian'
    [3]: 2, 'mode=cartesian'
    >>> p.show()
    >>> p.clear()
    >>> p
    <blank plot>
    >>> p[1] =  x**2+y**2
    >>> p[1].style = 'solid'
    >>> p[2] = -x**2-y**2
    >>> p[2].style = 'wireframe'
    >>> p[1].color = z, (0.4,0.4,0.9), (0.9,0.4,0.4)
    >>> p[1].style = 'both'
    >>> p[2].style = 'both'
    >>> p.close()


    Plot Window Keyboard Controls
    =============================

    Screen Rotation:
        X,Y axis      Arrow Keys, A,S,D,W, Numpad 4,6,8,2
        Z axis        Q,E, Numpad 7,9

    Model Rotation:
        Z axis        Z,C, Numpad 1,3

    Zoom:             R,F, PgUp,PgDn, Numpad +,-

    Reset Camera:     X, Numpad 5

    Camera Presets:
        XY            F1
        XZ            F2
        YZ            F3
        Perspective   F4

    Sensitivity Modifier: SHIFT

    Axes Toggle:
        Visible       F5
        Colors        F6

    Close Window:     ESCAPE

    =============================
    """
    def __init__(self, *fargs, **win_args):
        """
        Positional Arguments
        ====================

        Any given positional arguments are used to
        initialize a plot function at index 1. In
        other words...

        >>> from sympy.core import Symbol
        >>> x = Symbol('x')
        >>> p = Plot(x**2, visible=False)

        ...is equivalent to...

        >>> p = Plot(visible=False)
        >>> p[1] = x**2

        Note that in earlier versions of the plotting
        module, you were able to specify multiple
        functions in the initializer. This functionality
        has been dropped in favor of better automatic
        plot plot_mode detection.


        Named Arguments
        ===============

        axes
            An option string of the form
            "key1=value1; key2 = value2" which
            can use the following options:

            style = ordinate
                none OR frame OR box OR ordinate

            stride = 0.25
                val OR (val_x, val_y, val_z)

            overlay = True (draw on top of plot)
                True OR False

            colored = False (False uses Black,
                             True uses colors
                             R,G,B = X,Y,Z)
                True OR False

            label_axes = False (display axis names
                                at endpoints)
                True OR False

        visible = True (show immediately
            True OR False


        The following named arguments are passed as
        arguments to window initialization:

        antialiasing = True
            True OR False

        ortho = False
            True OR False

        invert_mouse_zoom = False
            True OR False

        """
        self._win_args = win_args
        self._window = None

        self._render_lock = RLock()

        self._functions = {}
        self._pobjects = []
        self._screenshot = ScreenShot(self)

        axe_options = parse_option_string(win_args.pop('axes', ''))
        self.axes = PlotAxes(**axe_options)
        self._pobjects.append(self.axes)

        self[0] = fargs
        if win_args.get('visible', True):
            self.show()

    ## Window Interfaces

    def show(self):
        """
        Creates and displays a plot window, or activates it
        (gives it focus) if it has already been created.
        """
        if self._window and not self._window.has_exit:
            self._window.activate()
        else:
            self._win_args['visible'] = True
            self.axes.reset_resources()
            self._window = PlotWindow(self, **self._win_args)

    def close(self):
        """
        Closes the plot window.
        """
        if self._window:
            self._window.close()

    def saveimage(self, outfile=None, format='', size=(600, 500)):
        """
        Saves a screen capture of the plot window to an
        image file.

        If outfile is given, it can either be a path
        or a file object. Otherwise a png image will
        be saved to the current working directory.
        If the format is omitted, it is determined from
        the filename extension.
        """
        self._screenshot.save(outfile, format, size)

    ## Function List Interfaces

    def clear(self):
        """
        Clears the function list of this plot.
        """
        self._render_lock.acquire()
        self._functions = {}
        self.adjust_all_bounds()
        self._render_lock.release()

    def __getitem__(self, i):
        """
        Returns the function at position i in the
        function list.
        """
        return self._functions[i]

    def __setitem__(self, i, args):
        """
        Parses and adds a PlotMode to the function
        list.
        """
        if not (isinstance(i, (int, Integer)) and i >= 0):
            raise ValueError("Function index must " "be an integer >= 0.")

        if isinstance(args, PlotObject):
            f = args
        else:
            if (not isinstance(args, (list, tuple))) or isinstance(
                    args, GeometryEntity):
                args = [args]
            if len(args) == 0:
                return  # no arguments given
            kwargs = dict(bounds_callback=self.adjust_all_bounds)
            f = PlotMode(*args, **kwargs)

        if f:
            self._render_lock.acquire()
            self._functions[i] = f
            self._render_lock.release()
        else:
            raise ValueError("Failed to parse '%s'." %
                             ', '.join(str(a) for a in args))

    def __delitem__(self, i):
        """
        Removes the function in the function list at
        position i.
        """
        self._render_lock.acquire()
        del self._functions[i]
        self.adjust_all_bounds()
        self._render_lock.release()

    def firstavailableindex(self):
        """
        Returns the first unused index in the function list.
        """
        i = 0
        self._render_lock.acquire()
        while i in self._functions:
            i += 1
        self._render_lock.release()
        return i

    def append(self, *args):
        """
        Parses and adds a PlotMode to the function
        list at the first available index.
        """
        self.__setitem__(self.firstavailableindex(), args)

    def __len__(self):
        """
        Returns the number of functions in the function list.
        """
        return len(self._functions)

    def __iter__(self):
        """
        Allows iteration of the function list.
        """
        return self._functions.itervalues()

    def __repr__(self):
        return str(self)

    def __str__(self):
        """
        Returns a string containing a new-line separated
        list of the functions in the function list.
        """
        s = ""
        if len(self._functions) == 0:
            s += "<blank plot>"
        else:
            self._render_lock.acquire()
            s += "\n".join([
                "%s[%i]: %s" % ("", i, str(self._functions[i]))
                for i in self._functions
            ])
            self._render_lock.release()
        return s

    def adjust_all_bounds(self):
        self._render_lock.acquire()
        self.axes.reset_bounding_box()
        for f in self._functions:
            self.axes.adjust_bounds(self._functions[f].bounds)
        self._render_lock.release()

    def wait_for_calculations(self):
        sleep(0)
        self._render_lock.acquire()
        for f in self._functions:
            a = self._functions[f]._get_calculating_verts
            b = self._functions[f]._get_calculating_cverts
            while a() or b():
                sleep(0)
        self._render_lock.release()
예제 #2
0
class ViewPSD(QObject):  #
    view_finished = pyqtSignal()
    plot_ready = pyqtSignal(list)

    def __init__(self, config, out_file_name, parent=None):
        super(ViewPSD, self).__init__(parent)
        self.config = config
        self.out_file_name = out_file_name
        self.plot_window = PlotWindow()

        self.plot_window.closed.connect(self.stop_view_psd)

        self.plot_window.show()
        self.plot_flg = False

        self.plot_ready[list].connect(self.plot)

    def __del__(self):
        self.plot_window.close()

    def update_config(self, config):
        self.config = config

    def plot(self, data):
        print("-- plot --")
        self.plot_window.fc = self.config.fc
        self.plot_window.fs = self.config.fs
        self.plot_window.sig = data
        #        if not self.plot_window.t.isActive():
        if self.plot_flg:
            self.plot_window.t.start(50)

    def plot_psd_process(self):
        self.plot_flg = True

        bat = pjoin(self.config.gnu_radio_path, "run_gr.bat")
        rx_py = pjoin(self.config.gnu_radio_path, "uhd_rx_cfile.py")
        output_path = pjoin(self.config.save_path, self.out_file_name)

        print(
            f"{bat} {rx_py} {output_path} -s -f {self.config.fc} -r {self.config.fs} -N {self.config.sample_nums}"
        )

        print("查看频谱模式")
        while self.plot_flg:
            print("=========================")
            os.spawnl(
                os.P_WAIT, bat,
                f"{bat} {rx_py} {output_path} -s -f {self.config.fc} -r {self.config.fs} -N {self.config.sample_nums}"
            )
            with open(output_path, "rb") as f:
                buff = f.read()
                tmp = np.frombuffer(buff, np.int16) / 32768

            d_i, d_q = np.reshape(tmp, (-1, 2)).T
            data = d_i + 1j * d_q

            #            if not self.plot_flg:
            #                break
            self.plot_ready.emit(list(data))

    def stop_view_psd(self):
        print("-- stop_view_psd --")
        self.plot_flg = False
        self.view_finished.emit()
예제 #3
0
파일: plot.py 프로젝트: Maihj/sympy
class PygletPlot(object):
    """
    Plot Examples
    =============

    See examples/advaned/pyglet_plotting.py for many more examples.


    >>> from sympy.plotting.pygletplot import PygletPlot as Plot
    >>> from sympy.abc import x, y, z

    >>> Plot(x*y**3-y*x**3)
    [0]: -x**3*y + x*y**3, 'mode=cartesian'

    >>> p = Plot()
    >>> p[1] = x*y
    >>> p[1].color = z, (0.4,0.4,0.9), (0.9,0.4,0.4)

    >>> p = Plot()
    >>> p[1] =  x**2+y**2
    >>> p[2] = -x**2-y**2


    Variable Intervals
    ==================

    The basic format is [var, min, max, steps], but the
    syntax is flexible and arguments left out are taken
    from the defaults for the current coordinate mode:

    >>> Plot(x**2) # implies [x,-5,5,100]
    [0]: x**2, 'mode=cartesian'
    >>> Plot(x**2, [], []) # [x,-1,1,40], [y,-1,1,40]
    [0]: x**2, 'mode=cartesian'
    >>> Plot(x**2-y**2, [100], [100]) # [x,-1,1,100], [y,-1,1,100]
    [0]: x**2 - y**2, 'mode=cartesian'
    >>> Plot(x**2, [x,-13,13,100])
    [0]: x**2, 'mode=cartesian'
    >>> Plot(x**2, [-13,13]) # [x,-13,13,100]
    [0]: x**2, 'mode=cartesian'
    >>> Plot(x**2, [x,-13,13]) # [x,-13,13,10]
    [0]: x**2, 'mode=cartesian'
    >>> Plot(1*x, [], [x], mode='cylindrical')
    ... # [unbound_theta,0,2*Pi,40], [x,-1,1,20]
    [0]: x, 'mode=cartesian'


    Coordinate Modes
    ================

    Plot supports several curvilinear coordinate modes, and
    they independent for each plotted function. You can specify
    a coordinate mode explicitly with the 'mode' named argument,
    but it can be automatically determined for Cartesian or
    parametric plots, and therefore must only be specified for
    polar, cylindrical, and spherical modes.

    Specifically, Plot(function arguments) and Plot[n] =
    (function arguments) will interpret your arguments as a
    Cartesian plot if you provide one function and a parametric
    plot if you provide two or three functions. Similarly, the
    arguments will be interpreted as a curve if one variable is
    used, and a surface if two are used.

    Supported mode names by number of variables:

    1: parametric, cartesian, polar
    2: parametric, cartesian, cylindrical = polar, spherical

    >>> Plot(1, mode='spherical') # doctest: +SKIP


    Calculator-like Interface
    =========================

    >>> p = Plot(visible=False)
    >>> f = x**2
    >>> p[1] = f
    >>> p[2] = f.diff(x)
    >>> p[3] = f.diff(x).diff(x) # doctest: +SKIP
    >>> p # doctest: +SKIP
    [1]: x**2, 'mode=cartesian'
    [2]: 2*x, 'mode=cartesian'
    [3]: 2, 'mode=cartesian'
    >>> p.show()
    >>> p.clear()
    >>> p
    <blank plot>
    >>> p[1] =  x**2+y**2
    >>> p[1].style = 'solid'
    >>> p[2] = -x**2-y**2
    >>> p[2].style = 'wireframe'
    >>> p[1].color = z, (0.4,0.4,0.9), (0.9,0.4,0.4)
    >>> p[1].style = 'both'
    >>> p[2].style = 'both'
    >>> p.close()


    Plot Window Keyboard Controls
    =============================

    Screen Rotation:
        X,Y axis      Arrow Keys, A,S,D,W, Numpad 4,6,8,2
        Z axis        Q,E, Numpad 7,9

    Model Rotation:
        Z axis        Z,C, Numpad 1,3

    Zoom:             R,F, PgUp,PgDn, Numpad +,-

    Reset Camera:     X, Numpad 5

    Camera Presets:
        XY            F1
        XZ            F2
        YZ            F3
        Perspective   F4

    Sensitivity Modifier: SHIFT

    Axes Toggle:
        Visible       F5
        Colors        F6

    Close Window:     ESCAPE

    =============================

    """
    #python 2.5 does not support class decorators so use this workaround
    _doctest_depends_on = {'modules': ('pyglet',)}

    @doctest_depends_on(modules=('pyglet',))
    def __init__(self, *fargs, **win_args):
        """
        Positional Arguments
        ====================

        Any given positional arguments are used to
        initialize a plot function at index 1. In
        other words...

        >>> from sympy.plotting.pygletplot import PygletPlot as Plot
        >>> from sympy.core import Symbol
        >>> from sympy.abc import x
        >>> p = Plot(x**2, visible=False)

        ...is equivalent to...

        >>> p = Plot(visible=False)
        >>> p[1] = x**2

        Note that in earlier versions of the plotting
        module, you were able to specify multiple
        functions in the initializer. This functionality
        has been dropped in favor of better automatic
        plot plot_mode detection.


        Named Arguments
        ===============

        axes
            An option string of the form
            "key1=value1; key2 = value2" which
            can use the following options:

            style = ordinate
                none OR frame OR box OR ordinate

            stride = 0.25
                val OR (val_x, val_y, val_z)

            overlay = True (draw on top of plot)
                True OR False

            colored = False (False uses Black,
                             True uses colors
                             R,G,B = X,Y,Z)
                True OR False

            label_axes = False (display axis names
                                at endpoints)
                True OR False

        visible = True (show immediately
            True OR False


        The following named arguments are passed as
        arguments to window initialization:

        antialiasing = True
            True OR False

        ortho = False
            True OR False

        invert_mouse_zoom = False
            True OR False

        """
        self._win_args = win_args
        self._window = None

        self._render_lock = RLock()

        self._functions = {}
        self._pobjects = []
        self._screenshot = ScreenShot(self)

        axe_options = parse_option_string(win_args.pop('axes', ''))
        self.axes = PlotAxes(**axe_options)
        self._pobjects.append(self.axes)

        self[0] = fargs
        if win_args.get('visible', True):
            self.show()

    ## Window Interfaces

    def show(self):
        """
        Creates and displays a plot window, or activates it
        (gives it focus) if it has already been created.
        """
        if self._window and not self._window.has_exit:
            self._window.activate()
        else:
            self._win_args['visible'] = True
            self.axes.reset_resources()

            if hasattr(self, '_doctest_depends_on'):
                self._win_args['runfromdoctester'] = True

            self._window = PlotWindow(self, **self._win_args)

    def close(self):
        """
        Closes the plot window.
        """
        if self._window:
            self._window.close()

    def saveimage(self, outfile=None, format='', size=(600, 500)):
        """
        Saves a screen capture of the plot window to an
        image file.

        If outfile is given, it can either be a path
        or a file object. Otherwise a png image will
        be saved to the current working directory.
        If the format is omitted, it is determined from
        the filename extension.
        """
        self._screenshot.save(outfile, format, size)

    ## Function List Interfaces

    def clear(self):
        """
        Clears the function list of this plot.
        """
        self._render_lock.acquire()
        self._functions = {}
        self.adjust_all_bounds()
        self._render_lock.release()

    def __getitem__(self, i):
        """
        Returns the function at position i in the
        function list.
        """
        return self._functions[i]

    def __setitem__(self, i, args):
        """
        Parses and adds a PlotMode to the function
        list.
        """
        if not (isinstance(i, (int, Integer)) and i >= 0):
            raise ValueError("Function index must "
                             "be an integer >= 0.")

        if isinstance(args, PlotObject):
            f = args
        else:
            if (not is_sequence(args)) or isinstance(args, GeometryEntity):
                args = [args]
            if len(args) == 0:
                return  # no arguments given
            kwargs = dict(bounds_callback=self.adjust_all_bounds)
            f = PlotMode(*args, **kwargs)

        if f:
            self._render_lock.acquire()
            self._functions[i] = f
            self._render_lock.release()
        else:
            raise ValueError("Failed to parse '%s'."
                    % ', '.join(str(a) for a in args))

    def __delitem__(self, i):
        """
        Removes the function in the function list at
        position i.
        """
        self._render_lock.acquire()
        del self._functions[i]
        self.adjust_all_bounds()
        self._render_lock.release()

    def firstavailableindex(self):
        """
        Returns the first unused index in the function list.
        """
        i = 0
        self._render_lock.acquire()
        while i in self._functions:
            i += 1
        self._render_lock.release()
        return i

    def append(self, *args):
        """
        Parses and adds a PlotMode to the function
        list at the first available index.
        """
        self.__setitem__(self.firstavailableindex(), args)

    def __len__(self):
        """
        Returns the number of functions in the function list.
        """
        return len(self._functions)

    def __iter__(self):
        """
        Allows iteration of the function list.
        """
        return self._functions.itervalues()

    def __repr__(self):
        return str(self)

    def __str__(self):
        """
        Returns a string containing a new-line separated
        list of the functions in the function list.
        """
        s = ""
        if len(self._functions) == 0:
            s += "<blank plot>"
        else:
            self._render_lock.acquire()
            s += "\n".join(["%s[%i]: %s" % ("", i, str(self._functions[i]))
                            for i in self._functions])
            self._render_lock.release()
        return s

    def adjust_all_bounds(self):
        self._render_lock.acquire()
        self.axes.reset_bounding_box()
        for f in self._functions:
            self.axes.adjust_bounds(self._functions[f].bounds)
        self._render_lock.release()

    def wait_for_calculations(self):
        sleep(0)
        self._render_lock.acquire()
        for f in self._functions:
            a = self._functions[f]._get_calculating_verts
            b = self._functions[f]._get_calculating_cverts
            while a() or b():
                sleep(0)
        self._render_lock.release()
예제 #4
0
class MainWindow(QMainWindow, Ui_MainWindow):
    config_changed = pyqtSignal()

    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("USRP B210 信号采样")
        self.setWindowIcon(QIcon("signal_app_24.png"))
        self.resize(1280, 720)

        self.plot_win = PlotWindow()

        # qss = "QWidget#MainWindow{background-color:red;}"
        # qss = "QWidget#MainWindow{border-image:url(signal_app_24.png);}"
        # self.setStyleSheet(qss)

        self.home_dir = pjoin(os.path.expanduser("~"), ".uhd_ui")
        self.config_path = pjoin(self.home_dir, "config.json")

        self.CONFIG = {}

        self.config = Config()
        self.config_keys = [
            "gnu_radio_path", "save_path", "fc", "fs", "stop_fc",
            "sample_interval", "sample_nums", "sample_times"
        ]

        self.view_spec_config = Config()
        self.view_spec_config.save_path = self.home_dir
        self.view_spec_config.sample_times = 1
        self.view_spec_config.sample_nums = 1 * 1024 * 1024

        self.sample_thread = None  # SampleThread()

        self.initial()

        self.save_path_btn.clicked.connect(self.open_folder)
        self.gnu_radio_btn.clicked.connect(self.open_gnu_folder)

        self.ok_btn.clicked.connect(self.sample_thread_start)
        self.ok_btn.setToolTip("开始采集信号")

        self.plot_btn.clicked.connect(self.plot_psd_start)
        self.plot_win.closed.connect(self.plot_psd_stop)

        self.stop_btn.setEnabled(False)
        self.stop_btn.setToolTip("停止当前信号采样")
        self.stop_btn.clicked.connect(self.stop_current_sampling)

        self.advance_groupBox.hide()
        self.advance_mode = False
        self.advance_mode_btn.setText("打开高级模式")
        self.advance_mode_btn.clicked.connect(self.set_advance_mode)

        self.gnu_radio_path_edit.textChanged[str].connect(
            self.new_gnu_radio_path)
        self.save_path_edit.textChanged[str].connect(self.new_save_path)
        self.fc_spinBox.valueChanged[int].connect(self.new_fc_val)
        self.fc_comboBox.currentIndexChanged[int].connect(self.new_fc_cbox)
        self.stop_fc_spinBox.valueChanged[int].connect(self.new_stop_fc_val)
        self.stop_fc_comboBox.currentIndexChanged[int].connect(
            self.new_stop_fc_cbox)
        self.fs_spinBox.valueChanged[int].connect(self.new_fs_val)
        self.fs_comboBox.currentIndexChanged[int].connect(self.new_fs_cbox)
        self.sample_interval_spinBox.valueChanged[int].connect(
            self.new_sample_interval_val)
        self.sample_interval_comboBox.currentIndexChanged[int].connect(
            self.new_sample_interval_cbox)
        self.sample_nums_spinBox.valueChanged[int].connect(
            self.new_n_sample_val)
        self.sample_nums_comboBox.currentIndexChanged[int].connect(
            self.new_n_sample_cbox)
        self.sample_times_spinBox.valueChanged[int].connect(
            self.new_sample_times)

        self.config_changed.connect(self.update_config)

    def resizeEvent(self, event):
        print(f"new_window_size: [{self.width()}, {self.height()}]")

    def closeEvent(self, event):
        self.plot_win.close()
        self.sample_thread.stop()
        self.sample_thread.quit()
        self.sample_thread.wait()

    def initial(self):
        def _get(s_cfg, index=0):
            s_cfg = s_cfg.split()
            if len(s_cfg) == 2:
                try:
                    index = self.config.u2ix_dict[s_cfg[1]]
                    return s_cfg[0], index
                except:
                    raise ValueError("配置出错!")
            elif len(s_cfg) == 1:
                return s_cfg[0], index
            else:
                raise ValueError("配置出错!")

        initial_config = _config()
        if not os.path.exists(self.home_dir):
            os.makedirs(self.home_dir)
        if not os.path.exists(self.config_path):
            # self.CONFIG["gnu_radio_path"] = initial_config.gnu_radio_path
            # self.CONFIG["save_path"] = initial_config.save_path
            # self.CONFIG["FC"] = initial_config.fc
            # self.CONFIG["FS"] = initial_config.fs
            # self.CONFIG["stop_FC"] = initial_config.stop_fc
            # self.CONFIG["sample_interval"] = initial_config.sample_interval
            # self.CONFIG["sample_nums"] = initial_config.sample_nums
            # self.CONFIG["sample_times"] = initial_config.sample_times
            for key in self.config_keys:
                self.CONFIG[key] = initial_config.__getattribute__(key)

            self.save_config()

            self.gnu_radio_path_edit.setText(self.CONFIG["gnu_radio_path"])
            self.save_path_edit.setText(self.CONFIG["save_path"])

            _fc, _fc_index = _get(self.CONFIG["fc"])
            self.fc_spinBox.setValue(int(_fc))
            self.fc_comboBox.setCurrentIndex(_fc_index)

            _fs, _fs_index = _get(self.CONFIG["fs"])
            self.fs_spinBox.setValue(int(_fs))
            self.fs_comboBox.setCurrentIndex(_fs_index)

            _stop_fc, _stop_fc_index = _get(self.CONFIG["stop_fc"])
            self.stop_fc_spinBox.setValue(int(_stop_fc))
            self.stop_fc_comboBox.setCurrentIndex(_stop_fc_index)

            _interval, _interval_index = _get(self.CONFIG["sample_interval"])
            self.sample_interval_spinBox.setValue(int(_interval))
            self.sample_interval_comboBox.setCurrentIndex(_interval_index)

            _n_sample, _n_index = _get(self.CONFIG["sample_nums"])
            self.sample_nums_spinBox.setValue(int(_n_sample))
            self.sample_nums_comboBox.setCurrentIndex(_n_index)

            self.sample_times_spinBox.setValue(int(
                self.CONFIG["sample_times"]))
        else:
            with open(self.config_path, "r") as f:
                self.CONFIG = json.load(f)
                self.config_changed.emit()
                for key in self.config_keys:
                    if key not in self.CONFIG:
                        self.CONFIG[key] = initial_config.__getattribute__(key)

                self.gnu_radio_path_edit.setText(self.CONFIG["gnu_radio_path"])
                self.save_path_edit.setText(self.CONFIG["save_path"])

                _fc, _fc_index = _get(self.CONFIG["fc"])
                self.fc_spinBox.setValue(int(_fc))
                self.fc_comboBox.setCurrentIndex(_fc_index)

                _fs, _fs_index = _get(self.CONFIG["fs"])
                self.fs_spinBox.setValue(int(_fs))
                self.fs_comboBox.setCurrentIndex(_fs_index)

                _stop_fc, _stop_fc_index = _get(self.CONFIG["stop_fc"])
                self.stop_fc_spinBox.setValue(int(_stop_fc))
                self.stop_fc_comboBox.setCurrentIndex(_stop_fc_index)

                _interval, _interval_index = _get(
                    self.CONFIG["sample_interval"])
                self.sample_interval_spinBox.setValue(int(_interval))
                self.sample_interval_comboBox.setCurrentIndex(_interval_index)

                _n_sample, _n_index = _get(self.CONFIG["sample_nums"])
                self.sample_nums_spinBox.setValue(int(_n_sample))
                self.sample_nums_comboBox.setCurrentIndex(_n_index)

                self.sample_times_spinBox.setValue(
                    int(self.CONFIG["sample_times"]))

        self.update_config()

    def save_config(self):
        with open(self.config_path, "w") as f:
            json.dump(self.CONFIG, f)
        self.config_changed.emit()

    def update_config(self):
        def get_value(cfg, u=1000):
            val, unit = cfg.split() if len(cfg.split()) > 1 else [
                cfg.split()[0], ""
            ]
            val = int(int(val) * u**self.config.u2ix_dict[unit])
            return val

        self.config.gnu_radio_path = self.CONFIG["gnu_radio_path"]
        self.config.save_path = self.CONFIG["save_path"]

        self.config.fc = get_value(self.CONFIG["fc"])
        self.config.fs = get_value(self.CONFIG["fs"])
        self.config.stop_fc = get_value(self.CONFIG["stop_fc"])
        self.config.sample_interval = get_value(self.CONFIG["sample_interval"])
        self.config.sample_nums = get_value(self.CONFIG["sample_nums"], 1024)
        self.config.sample_times = int(self.CONFIG["sample_times"])

        self.view_spec_config.gnu_radio_path = self.config.gnu_radio_path
        self.view_spec_config.fc = self.config.fc
        self.view_spec_config.fs = self.config.fs

        print(f"---- config changed ----\n\
    GNU Radio 安装目录:\t{self.config.gnu_radio_path} \n\
    文件保存路径:\t{self.config.save_path} \n\
    中心频率:\t\t{self.config.fc} \n\
    采样带宽:\t\t{self.config.fs} \n\
    结束频率:\t\t{self.config.stop_fc} \n\
    采样间隔:\t\t{self.config.sample_interval} \n\
    采样点数:\t\t{self.config.sample_nums} \n\
    采样次数:\t\t{self.config.sample_times} \n")

    def sample_thread_start(self):
        self.textEdit.append("---- 开始采样 ----\n")

        self.sample_thread = SampleThread()
        self.sample_thread.one_sample_end[int,
                                          str].connect(self.one_sample_end)
        self.sample_thread.all_sample_end.connect(self.sample_thread_end)

        self.sample_thread.config = self.config
        # self.sample_thread.mode = 2  # test_mode
        self.sample_thread.mode = 1  # test_mode
        self.sample_thread.start_sample = True
        self.sample_thread.start()

        self.plot_btn.setEnabled(False)
        self.plot_btn.setToolTip("正在采集信号, 无法查看当前频谱")
        self.stop_btn.setEnabled(True)

        self.ok_btn.setEnabled(False)

    def one_sample_end(self, ix, output_path):
        self.textEdit.append(
            f"第 {ix} 次采样完成,共 {self.config.sample_times} 次,文件保存路径:{output_path}"
        )

    def sample_thread_end(self):
        self.textEdit.append("---- 采样结束 ----\n")

        # self.sample_thread.terminate()
        self.sample_thread.stop()
        self.sample_thread.wait()
        self.sample_thread = None

        self.ok_btn.setEnabled(True)
        self.plot_btn.setEnabled(True)
        self.plot_btn.setToolTip("查看当前频谱")
        self.stop_btn.setEnabled(False)

    def stop_current_sampling(self):
        self.textEdit.append("---- 停止采样 ----\n")
        self.stop_btn.setEnabled(False)

        # self.sample_thread.terminate()
        self.sample_thread.stop()
        self.sample_thread.wait()
        self.sample_thread = None

        self.plot_btn.setEnabled(True)
        self.plot_btn.setToolTip("查看当前频谱")

    def plot_psd_start(self):
        self.textEdit.append("---- 查看频谱 ----\n")
        self.sample_thread = SampleThread()
        self.sample_thread.spec_data_get[list].connect(self.plot_psd)

        self.sample_thread.out_file_name = "usrp_tmp.bin"
        self.sample_thread.config = self.view_spec_config

        self.sample_thread.mode = 0
        self.sample_thread.start_sample = True

        self.sample_thread.start()

        self.ok_btn.setEnabled(False)

    @pyqtSlot(list)
    def plot_psd(self, data):
        print("-- plot spec --")
        self.plot_win.show()
        self.plot_win.fc = self.config.fc
        self.plot_win.fs = self.config.fs
        self.plot_win.sig = data
        self.plot_win.t.start(50)

    @pyqtSlot()
    def plot_psd_stop(self):
        self.textEdit.append("---- 查看频谱 ----\n")
        self.sample_thread.start_plot = False
        # self.sample_thread.terminate()
        self.sample_thread.stop()
        self.sample_thread.wait()
        self.sample_thread = None

        self.ok_btn.setEnabled(True)

    def set_advance_mode(self):
        self.advance_mode = not self.advance_mode
        if self.advance_mode:
            self.advance_groupBox.show()
            self.advance_mode_btn.setText("退出高级模式")
            self.textEdit.setText("-- 退出高级模式 --")
        else:
            self.advance_groupBox.hide()
            self.advance_mode_btn.setText("打开高级模式")
            self.textEdit.setText("-- 打开高级模式 --")

    def open_folder(self):
        dataPath = QtWidgets.QFileDialog.getExistingDirectory(
            self, "选择文件保存路径", self.CONFIG["save_path"],
            QtWidgets.QFileDialog.ShowDirsOnly)
        if dataPath == "":
            return
        else:
            self.save_path_edit.setText(dataPath)

    def open_gnu_folder(self):
        dataPath = QtWidgets.QFileDialog.getExistingDirectory(
            self, "打开GNU Radio/bin目录", self.CONFIG["gnu_radio_path"],
            QtWidgets.QFileDialog.ShowDirsOnly)
        if dataPath == "":
            return
        else:
            self.gnu_radio_path_edit.setText(dataPath)

    @pyqtSlot(str)
    def new_gnu_radio_path(self, path):
        self.textEdit.append("gnu_path_changed: " + path)
        self.CONFIG["gnu_radio_path"] = path
        self.save_config()

    @pyqtSlot(str)
    def new_save_path(self, path):
        self.textEdit.append("save_path_changed: " + path)
        self.CONFIG["save_path"] = path
        self.save_config()

    @pyqtSlot(int)
    def new_fc_val(self, val):
        unit = self.config.ix2u_dict[self.fc_comboBox.currentIndex()]
        new_fc = f"{int(val)} {unit}"
        self.textEdit.append("FC: " + new_fc + "Hz")
        self.CONFIG["fc"] = new_fc
        self.save_config()

    @pyqtSlot(int)
    def new_fc_cbox(self, ix):
        try:
            unit = self.config.ix2u_dict[ix]
        except KeyError:
            raise ValueError("设置出错")
        new_fc = f"{self.fc_spinBox.value()} {unit}"
        self.textEdit.append("FC: " + new_fc + "Hz")
        self.CONFIG["fc"] = new_fc
        self.save_config()

    @pyqtSlot(int)
    def new_stop_fc_val(self, val):
        unit = self.config.ix2u_dict[self.stop_fc_comboBox.currentIndex()]
        new_stop_fc = f"{int(val)} {unit}"
        self.textEdit.append("stop_FC: " + new_stop_fc + "Hz")
        self.CONFIG["stop_fc"] = new_stop_fc
        self.save_config()

    @pyqtSlot(int)
    def new_stop_fc_cbox(self, ix):
        try:
            unit = self.config.ix2u_dict[ix]
        except KeyError:
            raise ValueError("设置出错")
        new_stop_fc = f"{self.stop_fc_spinBox.value()} {unit}"
        self.textEdit.append("stop_FC: " + new_stop_fc + "Hz")
        self.CONFIG["stop_fc"] = new_stop_fc
        self.save_config()

    @pyqtSlot(int)
    def new_fs_val(self, val):
        unit = self.config.ix2u_dict[self.fs_comboBox.currentIndex()]
        new_fs = f"{int(val)} {unit}"
        self.textEdit.append("FS: " + new_fs + "Hz")
        self.CONFIG["fs"] = new_fs
        self.save_config()

    @pyqtSlot(int)
    def new_fs_cbox(self, ix):
        try:
            unit = self.config.ix2u_dict[ix]
        except KeyError:
            raise ValueError("设置出错")
        new_fs = f"{self.fs_spinBox.value()} {unit}"
        self.textEdit.append("FS: " + new_fs + "Hz")
        self.CONFIG["fs"] = new_fs
        self.save_config()

    @pyqtSlot(int)
    def new_sample_interval_val(self, val):
        unit = self.config.ix2u_dict[
            self.sample_interval_comboBox.currentIndex()]
        new_sample_interval = f"{int(val)} {unit}"
        self.textEdit.append("sample_interval: " + new_sample_interval + "Hz")
        self.CONFIG["sample_interval"] = new_sample_interval
        self.save_config()

    @pyqtSlot(int)
    def new_sample_interval_cbox(self, ix):
        try:
            unit = self.config.ix2u_dict[ix]
        except KeyError:
            raise ValueError("设置出错")
        new_sample_interval = f"{self.sample_interval_spinBox.value()} {unit}"
        self.textEdit.append("sample_interval: " + new_sample_interval + "Hz")
        self.CONFIG["sample_interval"] = new_sample_interval
        self.save_config()

    @pyqtSlot(int)
    def new_n_sample_val(self, val):
        unit = self.config.ix2u_dict[self.sample_nums_comboBox.currentIndex()]
        new_n_sample = f"{int(val)} {unit}"
        # new_n_sample = f"{int(val)} {_d[self.sample_nums_comboBox.currentIndex()]}"
        self.textEdit.append("sample_num: " + new_n_sample)
        self.CONFIG["sample_nums"] = new_n_sample
        self.save_config()

    @pyqtSlot(int)
    def new_n_sample_cbox(self, ix):
        try:
            unit = self.config.ix2u_dict[ix]
        except KeyError:
            raise ValueError("设置出错")
        new_n_sample = f"{self.sample_nums_spinBox.value()} {unit}"
        self.textEdit.append("sample_num: " + new_n_sample)
        self.CONFIG["sample_nums"] = new_n_sample
        self.save_config()

    @pyqtSlot(int)
    def new_sample_times(self, val):
        new_times = str(val)
        self.textEdit.append("sample_times: " + new_times)
        self.CONFIG["sample_times"] = new_times
        self.save_config()