analyse.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy.signal import butter, filtfilt
  4. def load_csv(file_path):
  5. try:
  6. with open(file_path, 'r', encoding='utf-8') as f:
  7. cleaned_lines = []
  8. for line in f:
  9. line = line.strip()
  10. if line.endswith(','):
  11. line = line[:-1]
  12. cleaned_lines.append(line)
  13. loaded_array = np.loadtxt(cleaned_lines, delimiter=',')
  14. if loaded_array.ndim == 1:
  15. loaded_array = loaded_array.reshape(-1, 1)
  16. return loaded_array
  17. except Exception as e:
  18. print(f"Could not load file:\n{e}")
  19. return -1
  20. arr = load_csv('output\\data_fixed.csv') # Load CSV
  21. tarr = np.transpose(arr)
  22. t = np.arange(0, 1152000, 12.5)
  23. envelope = np.abs(tarr[1])
  24. cutoff = 1000e3
  25. nyquist = 0.5 * 80e6
  26. order = 5
  27. b, a = butter(order, cutoff / nyquist, btype='low')
  28. filt_envelope = filtfilt(b, a, envelope)
  29. print(np.size(tarr[0]))
  30. startcut = round(0.1 * np.size(tarr[1]))
  31. endcut = round(0.5 * np.size(tarr[1]))
  32. demodulated_voltage = np.average(tarr)
  33. print(f'Start cut: {startcut}')
  34. print(f'End cut: {endcut}')
  35. print(f'Altitude demodulated: {demodulated_voltage}')
  36. i = startcut
  37. while np.abs(filt_envelope[i]) > np.abs(demodulated_voltage) * 0.3 and i < np.size(filt_envelope):
  38. i = i + 1
  39. delay = round((np.size(tarr[1]) - i) / 0.08)
  40. print(f'Delay: {delay} nS')
  41. plt.figure(figsize=(12,8))
  42. plt.subplot(2, 1, 1)
  43. plt.plot(t, tarr[1] * (10 / 65536))
  44. plt.title('RF Signal')
  45. plt.xlabel('Time, nS')
  46. plt.ylabel('Voltage, V')
  47. plt.grid(True)
  48. plt.subplot(2, 1, 1)
  49. plt.plot(t, filt_envelope * (10 / 65536))
  50. plt.title('Demodulate Signal')
  51. plt.xlabel('Time, nS')
  52. plt.ylabel('Voltage, V')
  53. plt.grid(True)
  54. plt.show()