Search code examples
pythongraphsignalssignal-processing

finding peaks in a vibration signal


I am new in python and I just graduated and my thesis was about vibratory analysis, so when I started learning python. I wanted to make an an app that reads the signal and gives spesific informations about the graph such as peaks, here is what I have for now

    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()

I am removing the first three lines because the first line contains the name of each column and the next two lines have some transient (I have more than 1600 values)

My code gives the following result

RESULTS

I want it to mark those peaks and give me their value on the y axes

and thank you


Solution

  • You could start with scipy.signal.find_peaks. In the documentation you will find how to do an example like this.

    enter image description here

    The orange crosses are the points selected with find_peaks, you have several parameters to tune, and it will probably be more productive than if you try to implement from scratch. After that if your can do a better job what is done by that function you can contribute to the library with your implementation.