Tools.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. using FxpConvert.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Xml.Schema;
  11. namespace Shaker.Models.Tools
  12. {
  13. public static class Tools
  14. {
  15. public static ICalc Calc { get; } = new SIMDFxpConvert.SIMDCalc();
  16. /// <summary>
  17. /// 单边谱扩展到双边谱
  18. /// </summary>
  19. /// <param name="unilateralSpectrum"></param>
  20. /// <returns></returns>
  21. public static double[] UnilateralSpectrumToBilateralSpectrum(double[] unilateralSpectrum)
  22. {
  23. double[] result = new double[unilateralSpectrum.Length << 1];
  24. Unsafe.CopyBlock(ref Unsafe.As<double, byte>(ref result[0]), ref Unsafe.As<double, byte>(ref unilateralSpectrum[0]), (uint)(unilateralSpectrum.Length * Unsafe.SizeOf<double>()));
  25. result[unilateralSpectrum.Length] = unilateralSpectrum[^1];
  26. double[] temp = unilateralSpectrum.Reverse().ToArray();
  27. Unsafe.CopyBlock(ref Unsafe.As<double, byte>(ref result[unilateralSpectrum.Length+1]), ref Unsafe.As<double, byte>(ref temp[0]), (uint)((unilateralSpectrum.Length-1) * Unsafe.SizeOf<double>()));
  28. return result;
  29. }
  30. public static void CalcSlope(double[] x, double[] y,ref double k,ref double b)
  31. {
  32. if(x ==null || y==null || x.Length!=2||y.Length!=2)
  33. {
  34. k = 0;
  35. b = 0;
  36. return;
  37. }
  38. k = (y[1] - y[0]) / (x[1] - x[0]);
  39. b = y[0] - x[0] * k;
  40. }
  41. public static int FindFrequencyIndex(this SweepConfigModel model, double freq)
  42. {
  43. if (model.SweepItems.Count < 2) return -1;
  44. uint uintfreq = (uint)(freq * 100);
  45. for (int i = 0; i < model.SweepItems.Count - 1; i++)
  46. {
  47. if ((uint)(model.SweepItems[i].Frequency * 100) <= uintfreq && uintfreq <= (uint)(model.SweepItems[i + 1].Frequency * 100)) return i;
  48. }
  49. return model.SweepItems.Count - 1;
  50. }
  51. public static void CalcItemSpectrum(double[] offset, [In] double[] f, [In] double[] fy_array, [In] double deltaf, [In] double[] turn_freq, ref double spectrumdata)
  52. {
  53. for (int i = 0; i < offset.Length - 1; i++)
  54. {
  55. int index = (int)((turn_freq[i] - turn_freq[0]) / deltaf);
  56. uint len = (uint)((turn_freq[i + 1] - turn_freq[i]) / deltaf);
  57. if (offset[i + 1] == offset[i])
  58. {
  59. var x = Math.Pow(10, offset[i] / 10);
  60. Calc.Multiply.Multiply(ref fy_array[index], x, len, ref Unsafe.Add(ref spectrumdata, index));
  61. }
  62. else
  63. {
  64. double db1_psd = Math.Pow(10, offset[i] / 10) * fy_array[index];
  65. double db2_psd = Math.Pow(10, offset[i + 1] / 10) * fy_array[index + len];
  66. var x = Math.Log10(db2_psd / db1_psd) / (Math.Log2(turn_freq[i + 1] / turn_freq[i])) * 10;
  67. for (int j = 0; j < len; j++)
  68. {
  69. ref double result = ref Unsafe.Add(ref spectrumdata, index + j);
  70. result = Math.Pow(10, Math.Log2(f[index + j] / turn_freq[i]) * x / 10) * db1_psd;
  71. }
  72. }
  73. }
  74. ref double temp = ref Unsafe.Add(ref spectrumdata, fy_array.Length - 1);
  75. temp = Math.Pow(10, offset[^1] / 10) * fy_array[^1];
  76. }
  77. public static void CalcRefSpectrum(double[] f_array, double[] y_array, RandomValueType[] k_array, double deltaf, out double[] fy_array, out double[] f, out double[] turningfreq)
  78. {
  79. int n = f_array.Length;
  80. // 计算圆整频率值及其对应的谱值
  81. double[] round_f = new double[n];
  82. double[] round_y = new double[n];
  83. turningfreq = new double[n];
  84. for (int i = 0; i < n; i++)
  85. {
  86. round_f[i] = Math.Ceiling(f_array[i] / deltaf) * deltaf;
  87. turningfreq[i] = round_f[i];
  88. }
  89. // 若第一个点值未知时,需先计算第一个点的值
  90. if (k_array[0] == RandomValueType.Slope && k_array[1] == RandomValueType.Slope)
  91. {
  92. y_array[0] = y_array[1];
  93. }
  94. if (k_array[0] == RandomValueType.Slope && k_array[1] == RandomValueType.Value)
  95. {
  96. k_array[0] = 0;
  97. y_array[0] = y_array[1];
  98. }
  99. if (k_array[0] == RandomValueType.Slope)
  100. {
  101. double kk = y_array[1];
  102. y_array[0] = y_array[2] / (Math.Pow(10, kk / 10 * Math.Log(f_array[1] / f_array[0], 2)));
  103. y_array[1] = y_array[2];
  104. k_array[0] = 0;
  105. k_array[1] = 0;
  106. }
  107. // 计算第二点及其后点的值
  108. for (int i = 1; i < n; i++)
  109. {
  110. if (f_array[i] == 0)
  111. {
  112. double kk = y_array[i];
  113. y_array[i] = y_array[i + 1];
  114. f_array[i] = Math.Pow(2, (10 * Math.Log(y_array[i + 1] / y_array[i - 1], 10) / kk)) * f_array[i - 1];
  115. }
  116. else
  117. {
  118. if (k_array[i] == RandomValueType.Slope)
  119. {
  120. y_array[i] = Math.Pow(10, y_array[i] / 10 * Math.Log(f_array[i] / f_array[i - 1], 2)) * y_array[i - 1];
  121. k_array[i] = 0;
  122. }
  123. }
  124. }
  125. // 验证结果值
  126. double[] k_exam = new double[n - 1];
  127. for (int i = 0; i < n; i++)
  128. {
  129. if (i < n - 1)
  130. {
  131. k_exam[i] = 10 * Math.Log(y_array[i + 1] / y_array[i], 10) / Math.Log(f_array[i + 1] / f_array[i], 2);
  132. }
  133. }
  134. // 计算圆整后频率对应的谱值
  135. for (int i = 0; i < n; i++)
  136. {
  137. if (round_f[i] == f_array[i])
  138. {
  139. round_y[i] = y_array[i];
  140. }
  141. if (i < n - 1)
  142. {
  143. if (round_f[i] > f_array[i])
  144. {
  145. round_y[i] = Math.Pow(10, k_exam[i] / 10 * Math.Log(round_f[i] / f_array[i], 2)) * y_array[i];
  146. }
  147. }
  148. if (i == n - 1)
  149. {
  150. round_y[i] = Math.Pow(10, k_exam[i - 1] / 10 * Math.Log(round_f[i] / f_array[i], 2)) * y_array[i];
  151. }
  152. if (round_f[i] < f_array[i])
  153. {
  154. round_y[i] = Math.Pow(10, k_exam[i - 1] / 10 * Math.Log(f_array[i] / round_f[i], 2)) * y_array[i];
  155. }
  156. }
  157. int parts = k_exam.Length; // 将谱按斜率分成几段,parts为总的段数\
  158. f = Enumerable.Range(0, (int)((round_f[n - 1] - round_f[0]) / deltaf) + 1).Select(x => x * deltaf + round_f[0]).ToArray();
  159. fy_array = new double[f.Length];
  160. fy_array[0] = round_y[0];
  161. fy_array[^1] = round_y[^1];
  162. int part_begin = 0;
  163. for (int i = 0; i < parts; i++)
  164. {
  165. fy_array[part_begin] = round_y[i];
  166. int n_part = (int)Math.Round((round_f[i + 1] - round_f[i]) / deltaf);
  167. if (k_exam[i] == 0)
  168. {
  169. for (int k = part_begin + 1; k <= part_begin + n_part; k++)
  170. {
  171. fy_array[k] = fy_array[k - 1];
  172. }
  173. }
  174. else
  175. {
  176. if (n_part > 0)
  177. {
  178. for (int k = part_begin + 1; k <= part_begin + n_part; k++)
  179. {
  180. fy_array[k] = Math.Pow(10, k_exam[i] / 10 * Math.Log(f[k] / f[k - 1], 2)) * fy_array[k - 1];
  181. }
  182. }
  183. }
  184. part_begin += n_part;
  185. }
  186. }
  187. public static void CalcAmpt(this SweepConfigModel model, double freq,ref double value,ref double upstop,ref double upwarn,ref double downstop,ref double downwarn)
  188. {
  189. int index = model.FindFrequencyIndex(freq);
  190. if (index == -1) return;
  191. value = 0;
  192. SweepItemModel nowmodel = model.SweepItems[index];
  193. switch (nowmodel.SweepValueType)
  194. {
  195. case SweepValueType.FixedDisplacement:
  196. value = freq * nowmodel.Value * freq * 4 * Math.PI * Math.PI / 9800f;
  197. break;
  198. case SweepValueType.FixedVelocity:
  199. value = freq * 2 * Math.PI * nowmodel.Value / 9.8f;
  200. break;
  201. case SweepValueType.FixedAcceleration:
  202. value = nowmodel.Value;
  203. break;
  204. case SweepValueType.DynamicAcceleration:
  205. {
  206. if (index == model.SweepItems.Count - 1)
  207. {
  208. value = nowmodel.Value;
  209. }
  210. else
  211. {
  212. SweepItemModel nextmodel = model.SweepItems[index + 1];
  213. double k = 0;
  214. double b = 0;
  215. Tools.CalcSlope([Math.Log10(nowmodel.Frequency), Math.Log10(nextmodel.Frequency)], [Math.Log10(nowmodel.Value), Math.Log10(nextmodel.Value)], ref k, ref b);
  216. value = Math.Pow(10, k * Math.Log10(freq) + b);
  217. }
  218. }
  219. break;
  220. }
  221. upstop = Math.Pow(10, nowmodel.UpStop / 20) * value;
  222. upwarn = Math.Pow(10, nowmodel.UpWarn / 20) * value;
  223. downstop = Math.Pow(10, nowmodel.DownStop / 20) * value;
  224. downwarn = Math.Pow(10, nowmodel.DownWarn / 20) * value;
  225. }
  226. public static double CalcAmpt(this SweepConfigModel model, double freq)
  227. {
  228. int index = model.FindFrequencyIndex(freq);
  229. if (index == -1) return 0;
  230. double value = 0;
  231. SweepItemModel nowmodel = model.SweepItems[index];
  232. switch (nowmodel.SweepValueType)
  233. {
  234. case SweepValueType.FixedDisplacement:
  235. value = freq * nowmodel.Value * freq * 4 * MathF.PI*MathF.PI / 9800f;
  236. break;
  237. case SweepValueType.FixedVelocity:
  238. value = freq * 2 * MathF.PI * nowmodel.Value / 9.8f;
  239. break;
  240. case SweepValueType.FixedAcceleration:
  241. value = nowmodel.Value;
  242. break;
  243. case SweepValueType.DynamicAcceleration:
  244. {
  245. if (index == model.SweepItems.Count - 1)
  246. {
  247. value = nowmodel.Value;
  248. }
  249. else
  250. {
  251. SweepItemModel nextmodel = model.SweepItems[index + 1];
  252. double k = 0;
  253. double b = 0;
  254. Tools.CalcSlope(new double[] { Math.Log10(nowmodel.Frequency), Math.Log10(nextmodel.Frequency) }, new double[] { Math.Log10(nowmodel.Value), Math.Log10(nextmodel.Value) }, ref k, ref b);
  255. value = Math.Pow(10, k * Math.Log10(freq) + b);
  256. }
  257. }
  258. break;
  259. }
  260. return value;
  261. }
  262. public static double TimeToOCT(this SweepConfigModel model, double time)
  263. {
  264. double oct = 0;
  265. switch(model.SweepType)
  266. {
  267. case SweepType.Linear:
  268. oct = (model.EndFrequency - model.StartFrequency) / time * 60;
  269. break;
  270. case SweepType.Log:
  271. default:
  272. oct = Math.Log2(model.EndFrequency / model.StartFrequency) / time * 60;
  273. break;
  274. }
  275. return oct;
  276. }
  277. public static double OCTToTime(this SweepConfigModel model, double oct)
  278. {
  279. double time = 0;
  280. switch(model.SweepType)
  281. {
  282. case SweepType.Linear:
  283. time = (model.EndFrequency - model.StartFrequency) / oct * 60;
  284. break;
  285. case SweepType.Log:
  286. default:
  287. time = Math.Log2(model.EndFrequency / model.StartFrequency) / oct * 60;
  288. break;
  289. }
  290. return time;
  291. }
  292. public static double DisplacementToVelocity(double displacement, double freq)
  293. {
  294. return displacement * 2 * Math.PI * freq / 1000;
  295. }
  296. public static double DisplacementToAcceleration(double displacement, double freq)
  297. {
  298. return displacement * 2 * Math.PI * freq * 2*Math.PI*freq / 9800;
  299. }
  300. public static double VelocityToAcceleration(double velocity, double freq)
  301. {
  302. return velocity * 2 * Math.PI * freq / 9.8;
  303. }
  304. public static double VelocityToDisplacement(double velocity, double freq)
  305. {
  306. return velocity * 1000 / (2 * Math.PI * freq);
  307. }
  308. public static double AccelerationToDisplacement(double acceleration, double freq)
  309. {
  310. return acceleration * 9800 / (2 * Math.PI * freq * 2 * Math.PI * freq);
  311. }
  312. public static double AccelerationToVelocity(double acceleration, double freq)
  313. {
  314. return acceleration * 9.8f / (2 * Math.PI * freq );
  315. }
  316. /// <summary>
  317. /// 工程量转电压
  318. /// </summary>
  319. /// <param name="value">工程量</param>
  320. /// <param name="sensitivity">灵敏度</param>
  321. /// <returns>电压</returns>
  322. public static double QuantitiesToVoltage(double value, double sensitivity)
  323. {
  324. return value * sensitivity / 1000;
  325. }
  326. /// <summary>
  327. /// 工程量转电压
  328. /// </summary>
  329. /// <param name="value">工程量</param>
  330. /// <param name="sensitivity">灵敏度</param>
  331. /// <returns>电压</returns>
  332. public static float QuantitiesToVoltage(float value, float sensitivity)
  333. {
  334. return value * sensitivity / 1000;
  335. }
  336. /// <summary>
  337. /// 电压转工程量
  338. /// </summary>
  339. /// <param name="value">电压</param>
  340. /// <param name="sensitivity">灵敏度</param>
  341. /// <returns>工程量</returns>
  342. public static double VoltageToQuantities(double value, double sensitivity)
  343. {
  344. return value * 1000 / sensitivity;
  345. }
  346. /// <summary>
  347. /// 电压转工程量
  348. /// </summary>
  349. /// <param name="value">电压</param>
  350. /// <param name="sensitivity">灵敏度</param>
  351. /// <returns>工程量</returns>
  352. public static float VoltageToQuantities(float value, float sensitivity)
  353. {
  354. return value * 1000 / sensitivity;
  355. }
  356. /// <summary>
  357. /// 计算计数的间隔时间
  358. /// </summary>
  359. /// <param name="maxCount">最大计数</param>
  360. /// <returns></returns>
  361. public static double CalcInterval(uint maxCount)
  362. {
  363. return 1d / (maxCount << 1);
  364. }
  365. /// <summary>
  366. /// 二维数组转置
  367. /// </summary>
  368. /// <typeparam name="T"></typeparam>
  369. /// <param name="source"></param>
  370. /// <param name="destination"></param>
  371. /// <param name="rowcount"></param>
  372. /// <param name="colnumcount"></param>
  373. public static void ArrayTranspose<T>(ref T source, ref T destination, int rowcount, int colnumcount,int maxcolnumcount)
  374. {
  375. if (rowcount == 0 || colnumcount == 0) return;
  376. for (int i = 0; i < rowcount; i++)
  377. {
  378. for (int j = 0; j < colnumcount; j++)
  379. {
  380. Unsafe.Add(ref destination, i * maxcolnumcount + j) = Unsafe.Add(ref source, j * rowcount + i);
  381. }
  382. }
  383. }
  384. public static double CalcLevel(double currentlevel,double level)
  385. {
  386. return Math.Pow(10, level / 20) * currentlevel;
  387. }
  388. public static float CalcLevel(float currentlevel, float level)
  389. {
  390. return MathF.Pow(10, level / 20) * currentlevel;
  391. }
  392. /// <summary>
  393. /// 解交织
  394. /// </summary>
  395. /// <typeparam name="T"></typeparam>
  396. /// <param name="source"></param>
  397. /// <param name="destination"></param>
  398. /// <param name="rowcount"></param>
  399. /// <param name="colnumcount"></param>
  400. public static void Deinterweaving<T>(ref T source, ref T destination, int rowcount, int colnumcount,int maxcolnumcount) => ArrayTranspose(ref source, ref destination, rowcount, colnumcount,maxcolnumcount);
  401. /// <summary>
  402. /// 压缩数据包
  403. /// </summary>
  404. /// <param name="arrays"></param>
  405. /// <returns></returns>
  406. public static byte[] CompressionBytes(byte[] arrays)
  407. {
  408. if (arrays == null || arrays.Length == 0) return new byte[0];
  409. using (MemoryStream ms =new MemoryStream())
  410. {
  411. using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionLevel.SmallestSize, true))
  412. {
  413. ds.Write(arrays, 0, arrays.Length);
  414. }
  415. return ms.ToArray();
  416. }
  417. }
  418. /// <summary>
  419. /// 解压数据包
  420. /// </summary>
  421. /// <param name="arrays"></param>
  422. /// <returns></returns>
  423. public static byte[] DecompressionBytes(byte[] arrays)
  424. {
  425. if (arrays == null || arrays.Length == 0) return new byte[0];
  426. using (MemoryStream ms = new MemoryStream(arrays))
  427. {
  428. using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress,true))
  429. {
  430. using (MemoryStream memoryStream = new MemoryStream())
  431. {
  432. ds.CopyTo(memoryStream);
  433. return memoryStream.ToArray();
  434. }
  435. }
  436. }
  437. }
  438. public static byte[] CompressionWaves<T>(ref T f,uint count) where T : unmanaged
  439. {
  440. if(count==0)return new byte[0];
  441. byte[] temp = new byte[Unsafe.SizeOf<T>() * count];
  442. Unsafe.CopyBlock(ref temp[0], ref Unsafe.As<T, byte>(ref f), (uint)temp.Length);
  443. return CompressionBytes(temp);
  444. }
  445. public static T[] DecompressionWaves<T>(byte[] f) where T : unmanaged
  446. {
  447. if (f ==null || f.Length == 0) return new T[0];
  448. byte[] temp = DecompressionBytes(f);
  449. T[] result = new T[temp.Length / Unsafe.SizeOf<T>()];
  450. Unsafe.CopyBlock( ref Unsafe.As<T, byte>(ref result[0]),ref temp[0], (uint)temp.Length);
  451. return result;
  452. }
  453. public static HistogramData[] Histogram(ref float value,uint count,ref float max,ref float min,int histogramCount = 256)
  454. {
  455. if (count == 0 || histogramCount ==0) return new HistogramData[0];
  456. max = float.MaxValue;
  457. min = float.MinValue;
  458. for(int i=0;i<count;i++)
  459. {
  460. max = MathF.Max(Unsafe.Add(ref value, i), max);
  461. min = MathF.Min(Unsafe.Add(ref value, i), min);
  462. }
  463. float invert = (max - min) / histogramCount;
  464. float temomin = min;
  465. var result = Enumerable.Range(0, (int)histogramCount).Select(x =>new HistogramData(x * invert + temomin, (x + 1) * invert + temomin, (uint)0)).ToArray();
  466. for(int i=0;i<count;i++)
  467. {
  468. int index = (int)Math.Floor((Unsafe.Add(ref value, i) - min) / invert);
  469. if (index == histogramCount) index--;
  470. result[index].H++;
  471. }
  472. return result;
  473. }
  474. public static HistogramData[] Histogram(ref float value, uint count, float max, float min, int histogramCount = 256)
  475. {
  476. if (count == 0 || histogramCount == 0) return new HistogramData[0];
  477. float invert = (max - min) / histogramCount;
  478. var result = Enumerable.Range(0, (int)histogramCount).Select(x => new HistogramData(x * invert + min, (x + 1) * invert + min, (uint)0)).ToArray();
  479. for (int i = 0; i < count; i++)
  480. {
  481. int index = (int)Math.Floor((Unsafe.Add(ref value, i) - min) / invert);
  482. if (index == histogramCount) index--;
  483. result[index].H++;
  484. }
  485. return result;
  486. }
  487. /// <summary>
  488. /// 按SI单位制为数字添加后缀
  489. /// </summary>
  490. /// <param name="value">需要转换的数字</param>
  491. /// <param name="decimals">小数位数</param>
  492. /// <param name="unit">附加单位</param>
  493. /// <returns></returns>
  494. public static string ValueChangeToSI(double value,double exp = 1000, int decimals = 1, string unit = "", bool invalid = false)
  495. {
  496. string formatstring = "#0";
  497. if (!invalid)
  498. {
  499. if (decimals > 0)
  500. {
  501. formatstring += ".";
  502. for (int i = 0; i < decimals; i++) formatstring += "#";
  503. }
  504. }
  505. else
  506. {
  507. formatstring = "N" + decimals;
  508. }
  509. if (double.IsNaN(value)) return "NaN";
  510. if (value == 0)
  511. {
  512. return value.ToString(formatstring) + unit;
  513. }
  514. if (Math.Abs(value) < 0.0000000000001) return Math.Round(0f, decimals) + unit;
  515. string SI = "yzafpnμmDkMGTPEZY";
  516. double d = Math.Log(Math.Abs(value), exp);
  517. double number = Math.Round(value / Math.Pow(exp, Math.Floor(d)), decimals);
  518. d += 8;
  519. string s = SI.Substring((int)d, 1);
  520. if (s == "D") s = "";
  521. return number.ToString(formatstring) + s + unit;
  522. }
  523. public static void CalcAmpt(ref float value, uint count,float max,float min, ref float highlevel,ref float lowlevel,ref float ampt)
  524. {
  525. if (count == 0) return;
  526. if(count ==1)
  527. {
  528. highlevel = value;
  529. lowlevel = value;
  530. ampt = 0;
  531. return;
  532. }
  533. var hisdata = Histogram(ref value, count,max,min);
  534. var orderdata = hisdata.OrderBy(x => x.H).ToArray();
  535. if((orderdata[^1].H + orderdata[^2].H)/(float)count>=0.05f)
  536. {
  537. int index1 = Array.FindIndex(hisdata, x => x.H == orderdata[^1].H);
  538. int index2 = Array.FindIndex(hisdata, x => x.H == orderdata[^2].H);
  539. if(index1<index2)
  540. {
  541. lowlevel = hisdata[index1].Center;
  542. highlevel = hisdata[index2].Center;
  543. }
  544. else
  545. {
  546. lowlevel = hisdata[index1].Center;
  547. highlevel = hisdata[index1].Center;
  548. }
  549. ampt = highlevel - lowlevel;
  550. }
  551. else
  552. {
  553. highlevel = max;
  554. lowlevel = min;
  555. ampt = max - min;
  556. }
  557. }
  558. public struct HistogramData
  559. {
  560. public HistogramData()
  561. {
  562. }
  563. public HistogramData(float start, float end, uint h)
  564. {
  565. Start = start;
  566. End = end;
  567. H = h;
  568. }
  569. public float Start;
  570. public float End;
  571. public uint H;
  572. public float Center => (Start + End) / 2;
  573. }
  574. }
  575. }