pulse_gen_one.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import numpy as np
  2. import struct
  3. import xml.etree.ElementTree as ET
  4. from xml.dom import minidom
  5. def main():
  6. pretime = 500 * 10 ** -6
  7. uptime = 50 * 10 ** -6
  8. posttime = 60 * 10 ** -6
  9. sample_freq = 2
  10. pulse_number = 1
  11. #add = (pulse_width * pulse_number * sample_freq * (10**6) * 2)
  12. counter = 0
  13. n_pre = round(pretime * sample_freq * (10 ** 6))
  14. n_up = round(uptime * sample_freq * (10 ** 6))
  15. n_post = round(posttime * sample_freq * (10 ** 6))
  16. size_1 =(n_pre + n_post + n_up) * 2
  17. print(f'rf array size_1 = {size_1}')
  18. size_2 = np.int32(size_1) * np.int32(pulse_number)
  19. print(f'rf array size_2 = {size_2}')
  20. rf_array = np.zeros(size_2, dtype=np.int8)
  21. for _ in range(0, pulse_number, 1):
  22. for _ in range(0, n_pre, 1):
  23. rf_array[counter] = np.int8(0)
  24. rf_array[counter + 1] = np.int8(0)
  25. counter += 2
  26. for _ in range(0, n_up, 1):
  27. rf_array[counter] = np.int8(127)
  28. rf_array[counter + 1] = np.int8(0)
  29. counter += 2
  30. for _ in range(0, n_up, 1):
  31. rf_array[counter] = np.int8(0)
  32. rf_array[counter + 1] = np.int8(0)
  33. counter += 2
  34. print(rf_array)
  35. f = open(f'test3_22_08_25.bin', 'wb') # открываем файл на чтение
  36. for elem in rf_array: # берём каждую строчку из файла f
  37. f.write(struct.pack('<b',elem))
  38. f.close()
  39. root = ET.Element('root')
  40. rf = ET.SubElement(root, 'RF')
  41. sw = ET.SubElement(root, 'SW')
  42. adc = ET.SubElement(root, 'ADC')
  43. gru1 = ET.SubElement(root, 'GR')
  44. first_pulse_delay = 17 * 10 ** -6
  45. if int(sample_freq) == 2:
  46. first_pulse_delay = 17 * 10 ** -6
  47. elif int(sample_freq) == 8:
  48. first_pulse_delay = 4.12 * 10 ** -6
  49. elif int(sample_freq) == 10:
  50. first_pulse_delay = 3.26 * 10 ** -6
  51. elif (sample_freq) == 20:
  52. first_pulse_delay = 1.5 * 10 ** -6
  53. cl = ET.SubElement(root, 'CL')
  54. ET.SubElement(rf,'RF1').text = '0'
  55. ET.SubElement(sw,'SW1').text = '0'
  56. ET.SubElement(adc,'ADC1').text = '0'
  57. ET.SubElement(gru1,'GR1').text = '1'
  58. ET.SubElement(cl, 'CL1').text = str(round(first_pulse_delay / (20*(10**-9))))
  59. param_count = 1
  60. param_count += 1
  61. ET.SubElement(rf, 'RF' + str(param_count)).text = "0"
  62. ET.SubElement(sw, 'SW' + str(param_count)).text = "0"
  63. ET.SubElement(adc, 'ADC' + str(param_count)).text = "0"
  64. ET.SubElement(gru1, 'GR' + str(param_count)).text = "0"
  65. ET.SubElement(cl, 'CL' + str(param_count)).text = str(round(pretime / (20*(10**-9))))
  66. param_count += 1
  67. ET.SubElement(rf, 'RF' + str(param_count)).text = "0"
  68. ET.SubElement(sw, 'SW' + str(param_count)).text = "0"
  69. ET.SubElement(adc, 'ADC' + str(param_count)).text = "0"
  70. ET.SubElement(gru1, 'GR' + str(param_count)).text = "0"
  71. ET.SubElement(cl, 'CL' + str(param_count)).text = str(round(uptime / (20*(10**-9))))
  72. param_count += 1
  73. ET.SubElement(rf, 'RF' + str(param_count)).text = "0"
  74. ET.SubElement(sw, 'SW' + str(param_count)).text = "0"
  75. ET.SubElement(adc, 'ADC' + str(param_count)).text = "0"
  76. ET.SubElement(gru1, 'GR' + str(param_count)).text = "0"
  77. ET.SubElement(cl, 'CL' + str(param_count)).text = str(round(posttime / (20*(10**-9))))
  78. ET.SubElement(root, 'ParamCount').text = str(param_count)
  79. xml_str = ET.tostring(root, encoding='utf-8', method='xml')
  80. parsed_str = minidom.parseString(xml_str) # Парсим строку
  81. pretty_xml_str = parsed_str.toprettyxml(indent=" ") # Добавляем отступы
  82. # Сохраняем форматированный XML в файл
  83. with open('Sync_param_test3.xml', 'w', encoding='utf-8') as f:
  84. f.write(pretty_xml_str)
  85. main()