fork download
  1. import matplotlib.pyplot as plt
  2. import pandas as pd
  3. from scipy.signal import find_peaks
  4. import sys
  5.  
  6. file_path = "J125188_0508_500Hz.txt"
  7.  
  8. MIN_HEIGHT = 3.5
  9. MIN_DISTANCE = 10
  10.  
  11. try:df = pd.read_csv(file_path, header=None, names=["Time", "Voltage"])
  12. except FileNotFoundError:
  13. print("エラー")
  14. exit()
  15.  
  16. times = df["Time"].values
  17. voltages = df["Voltage"].values
  18.  
  19. peak_indices, _ = find_peaks(
  20. voltages, height=MIN_HEIGHT, distance=MIN_DISTANCE
  21. )
  22.  
  23. print("【実験2 ピーク検出結果】")
  24. print(f"ファイル名: {file_path}")
  25. print("-" * 35)
  26. print(f"{'波形番号':<6}\t{'時刻[秒]':<10}\t{'電位[V]'}")
  27. print("-" * 35)
  28.  
  29. for i, idx in enumerate(peak_indices, start=1):
  30. peak_time = times[idx]
  31. peak_voltage = voltages[idx]
  32. print(f"{i:<8}\t{peak_time:<12.2f}\t{peak_voltage:.2f}")
  33.  
  34. print("-" * 35)
  35. print(f"合計検出数: {len(peak_indices)} 回")
  36.  
  37. plt.figure(figsize=(10, 5))
  38. plt.plot(times, voltages, label="Measured Signal", color="blue")
  39. plt.plot(
  40. times[peak_indices],
  41. voltages[peak_indices],
  42. "x",
  43. label="Detected Peaks",
  44. color="red",
  45. markersize=10,
  46. )
  47.  
  48. plt.title(f"Peak Detection Result ({file_path})")
  49. plt.xlabel("Time [seconds]")
  50. plt.ylabel("Voltage [V]")
  51. plt.grid(True)
  52. plt.legend()
  53. plt.ylim(0, 5)
  54. plt.show()
Success #stdin #stdout 1.03s 97820KB
stdin
Standard input is empty
stdout
Standard output is empty