eval_analyse.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import customtkinter as ctk
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  6. from tkinter import filedialog
  7. # Function to load data from file
  8. def load_data():
  9. filename = filedialog.askopenfilename(title="Select CSV file", filetypes=(("CSV files", "*.csv"), ("All files", "*.*")))
  10. return pd.read_csv(filename)
  11. # Function to update data
  12. def update_data():
  13. global data
  14. data = load_data()
  15. update_plot(start_slider.get(), end_slider.get())
  16. # Function to update plot based on slider values
  17. def update_plot(start_time, end_time):
  18. filtered_data = data[(data["Timestamp"] >= start_time) & (data["Timestamp"] <= end_time)]
  19. ax1.clear()
  20. ax1.plot(filtered_data["Timestamp"], filtered_data["Value"])
  21. ax1.set_title("Time Domain")
  22. ax1.set_xlabel("Timestamp")
  23. ax1.set_ylabel("Value")
  24. # Remove DC offset
  25. data_without_dc = filtered_data["Value"] - filtered_data["Value"].mean()
  26. # Perform Fourier analysis
  27. fft_values = np.fft.fft(data_without_dc)
  28. frequencies = np.fft.fftfreq(len(filtered_data), d=1 / sampling_rate)
  29. amplitudes = np.abs(fft_values)
  30. # Consider only positive frequencies
  31. positive_frequencies = frequencies[:len(frequencies)//2]
  32. positive_amplitudes = amplitudes[:len(frequencies)//2]
  33. ax2.clear()
  34. ax2.plot(positive_frequencies, positive_amplitudes)
  35. ax2.set_title("Frequency Domain")
  36. ax2.set_xlabel("Frequency (Hz)")
  37. ax2.set_ylabel("Amplitude")
  38. canvas.draw()
  39. # Create the main CustomTkinter window
  40. root = ctk.CTk()
  41. root.title("CSV Plot with Two Sliders")
  42. # Set appearance mode (optional)
  43. ctk.set_appearance_mode("dark") # Choose between "dark", "light", or "system"
  44. # Create matplotlib figure and axes
  45. fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6))
  46. canvas = FigureCanvasTkAgg(fig, master=root)
  47. canvas.get_tk_widget().pack(side=ctk.LEFT, fill=ctk.BOTH, expand=True)
  48. # Function to initialize the plot
  49. def init_plot():
  50. global data
  51. global sampling_rate
  52. data = load_data()
  53. # Read the CSV file and extract timestamps
  54. timestamps = data["Timestamp"].tolist()
  55. # Calculate time differences
  56. time_diffs = [timestamps[i + 1] - timestamps[i] for i in range(len(timestamps) - 1)]
  57. # Calculate average time difference
  58. average_time_diff = sum(time_diffs) / len(time_diffs)
  59. # Sampling rate is the reciprocal of the average time difference
  60. sampling_rate = 1 / average_time_diff
  61. print("Sampling rate:", sampling_rate, "Hz")
  62. # Load only the last 10000 data points
  63. data = data.tail(10000)
  64. # Plot the initial data
  65. ax1.plot(data["Timestamp"], data["Value"])
  66. ax1.set_title("Time Domain")
  67. ax1.set_xlabel("Timestamp")
  68. ax1.set_ylabel("Value")
  69. # Remove DC offset
  70. data_without_dc = data["Value"] - data["Value"].mean()
  71. # Perform Fourier analysis
  72. fft_values = np.fft.fft(data_without_dc)
  73. frequencies = np.fft.fftfreq(len(data), d=1 / sampling_rate)
  74. amplitudes = np.abs(fft_values)
  75. # Consider only positive frequencies
  76. positive_frequencies = frequencies[:len(frequencies)//2]
  77. positive_amplitudes = amplitudes[:len(frequencies)//2]
  78. # Plot frequency spectrum
  79. ax2.plot(positive_frequencies, positive_amplitudes)
  80. ax2.set_title("Frequency Domain")
  81. ax2.set_xlabel("Frequency (Hz)")
  82. ax2.set_ylabel("Amplitude")
  83. # Initialize the plot
  84. init_plot()
  85. # Create sliders
  86. start_slider = ctk.CTkSlider(master=root, from_=data["Timestamp"].min(), to=data["Timestamp"].max(),
  87. command=lambda value: update_plot(value, end_slider.get()))
  88. start_slider.pack()
  89. end_slider = ctk.CTkSlider(master=root, from_=data["Timestamp"].min(), to=data["Timestamp"].max(),
  90. command=lambda value: update_plot(start_slider.get(), value))
  91. end_slider.pack()
  92. # Create a frame on the right side for displaying relevant information
  93. info_frame = ctk.CTkFrame(master=root)
  94. info_frame.pack(side=ctk.RIGHT, fill=ctk.BOTH, expand=True)
  95. # Labels for displaying information
  96. sampling_rate_label = ctk.CTkLabel(master=info_frame, text=f"Sampling Rate: {sampling_rate} Hz")
  97. sampling_rate_label.pack()
  98. max_frequency_label = ctk.CTkLabel(master=info_frame, text=f"Max Frequency: {sampling_rate / 2} Hz (Nyquist Frequency)")
  99. max_frequency_label.pack()
  100. # Button to change data file
  101. change_file_button = ctk.CTkButton(master=info_frame, text="Change File", command=update_data)
  102. change_file_button.pack()
  103. # Run the CustomTkinter event loop
  104. root.mainloop()