在振动信号中查找峰值

2022-03-28 00:00:00 python signals signal-processing graph

问题描述

我是Python的新手,我刚刚毕业,我的论文是关于振动分析的,所以当我开始学习Python的时候。我想做一个应用程序来读取信号,并提供关于图形的具体信息,如峰值,这是我目前拥有的

    import tkinter as tk
    from tkinter import ttk
    from tkinter import filedialog as fd
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Cursor
    import numpy as np
    import os
    
    Raw_1 = []
    Raw_2 = []
    clean_1 = []
    clean_2 = []
    
    
    # create the root window
    root = tk.Tk()
    root.title("Yazid ")
    root.resizable(True, True)
    root.geometry("400x400")
    # full screen
    class FullScreenApp(object):
        def __init__(self, master, **kwargs):
            self.master = master
            pad = 3
            self._geom = "200x200+0+0"
            master.geometry(
                "{0}x{1}+0+0".format(
                    master.winfo_screenwidth() - pad, master.winfo_screenheight() - pad
                )
            )
            master.bind("<Escape>", self.toggle_geom)
    
        def toggle_geom(self, event):
            geom = self.master.winfo_geometry()
            print(geom, self._geom)
            self.master.geometry(self._geom)
            self._geom = geom
    
    
    def select_file():
        filetypes = (("text files", "*.txt"), ("All files", "*.*"))
        # get the txt file
        filename = fd.askopenfilename(
            title="select file", initialdir="/", filetypes=filetypes
        )
    
        # Get the raw list
        for line in open(filename, "r"):
            lines = [i for i in line.split("       ")]
            Raw_1.append(lines[0].replace(",", "."))
            Raw_2.append(lines[1].replace(",", "."))
        # clean means get rid of the first three lines
        for item in Raw_1[3:]:
            clean_1.append(item)
        for item in Raw_2[3:]:
            clean_2.append(item)
        # convert to float (geting the X and Y axes)
        x = [float(i) for i in clean_1]
        y = [float(i) for i in clean_2]
    
        # plotting the points
        fig = plt.figure()
        ax = fig.subplots()
        ax.plot(x, y, color="r")
        ax.grid()
    
        # naming the x axis
        plt.xlabel(Raw_2[0])
        # naming the y axis
        plt.ylabel(Raw_1[0])
    
        # title graph
        fname = os.path.splitext(filename)[0]
        name = os.path.basename(fname)
        plt.title(name)
    
        # Defining the cursor
        cursor = Cursor(ax, horizOn=True, vertOn=True, useblit=True, color="r", linewidth=1)
    
        # Creating an annotating box
        annot = ax.annotate(
            "",
            xy=(0, 0),
            xytext=(-40, 40),
            textcoords="offset points",
            bbox=dict(boxstyle="round4", fc="linen", ec="k", lw=1),
            arrowprops=dict(arrowstyle="-|>"),
        )
        annot.set_visible(False)
    
        # function to show the plot
        plt.show()
    
    # open button
    open_button = ttk.Button(root, text="Open a File", command=select_file)
    
    open_button.pack(expand=True)
    
    
    # run the application
    root.mainloop()

我要删除前三行,因为第一行包含每列的名称,后两行有一些暂态(我有1600多个值)

我的代码产生以下结果

我希望它标记这些峰值,并在y轴上给出它们的值

谢谢您


解决方案

您可以从scipy.signal.find_peaks开始。在文档中,您将了解如何执行类似于此的示例。

橙色十字是用find_peaks选择的点,您有几个参数要调优,而且它可能比您尝试从头开始实现的效率更高。在此之后,如果您可以更好地完成该函数所做的工作,则可以使用您的实现为库做出贡献。

相关文章