Tools.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml.Schema;
  8. namespace Shaker.Models.Tools
  9. {
  10. public static class Tools
  11. {
  12. public static void CalcSlope(double[] x, double[] y,ref double k,ref double b)
  13. {
  14. if(x ==null || y==null || x.Length!=2||y.Length!=2)
  15. {
  16. k = 0;
  17. b = 0;
  18. return;
  19. }
  20. k = (y[1] - y[0]) / (x[1] - x[0]);
  21. b = y[0] - x[0] * k;
  22. }
  23. public static int FindFrequencyIndex(this SweepConfigModel model, double freq)
  24. {
  25. if (model.SweepItems.Count < 2) return -1;
  26. uint uintfreq = (uint)(freq * 100);
  27. for (int i = 0; i < model.SweepItems.Count - 1; i++)
  28. {
  29. if ((uint)(model.SweepItems[i].Frequency * 100) <= uintfreq && uintfreq <= (uint)(model.SweepItems[i + 1].Frequency * 100)) return i;
  30. }
  31. return model.SweepItems.Count - 1;
  32. }
  33. public static void CalcAmpt(this SweepConfigModel model, double freq,ref double value,ref double upstop,ref double upwarn,ref double downstop,ref double downwarn)
  34. {
  35. int index = model.FindFrequencyIndex(freq);
  36. if (index == -1) return;
  37. value = 0;
  38. SweepItemModel nowmodel = model.SweepItems[index];
  39. switch (nowmodel.SweepValueType)
  40. {
  41. case SweepValueType.FixedDisplacement:
  42. value = freq * nowmodel.Value * freq * 4 * Math.PI *Math.PI/ 9800f;
  43. break;
  44. case SweepValueType.FixedVelocity:
  45. value = freq * 2 * Math.PI * nowmodel.Value / 9.8f;
  46. break;
  47. case SweepValueType.FixedAcceleration:
  48. value = nowmodel.Value;
  49. break;
  50. case SweepValueType.DynamicAcceleration:
  51. {
  52. if (index == model.SweepItems.Count - 1)
  53. {
  54. value = nowmodel.Value;
  55. }
  56. else
  57. {
  58. SweepItemModel nextmodel = model.SweepItems[index + 1];
  59. double k = 0;
  60. double b = 0;
  61. 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);
  62. value = Math.Pow(10, k *Math.Log10(freq) + b);
  63. }
  64. }
  65. break;
  66. }
  67. upstop = Math.Pow(10, nowmodel.UpStop / 20) * value;
  68. upwarn = Math.Pow(10, nowmodel.UpWarn / 20) * value;
  69. downstop = Math.Pow(10, nowmodel.DownStop / 20) * value;
  70. downwarn = Math.Pow(10, nowmodel.DownWarn / 20) * value;
  71. }
  72. public static double CalcAmpt(this SweepConfigModel model, double freq)
  73. {
  74. int index = model.FindFrequencyIndex(freq);
  75. if (index == -1) return 0;
  76. double value = 0;
  77. SweepItemModel nowmodel = model.SweepItems[index];
  78. switch (nowmodel.SweepValueType)
  79. {
  80. case SweepValueType.FixedDisplacement:
  81. value = freq * nowmodel.Value * freq * 4 * MathF.PI*MathF.PI / 9800f;
  82. break;
  83. case SweepValueType.FixedVelocity:
  84. value = freq * 2 * MathF.PI * nowmodel.Value / 9.8f;
  85. break;
  86. case SweepValueType.FixedAcceleration:
  87. value = nowmodel.Value;
  88. break;
  89. case SweepValueType.DynamicAcceleration:
  90. {
  91. if (index == model.SweepItems.Count - 1)
  92. {
  93. value = nowmodel.Value;
  94. }
  95. else
  96. {
  97. SweepItemModel nextmodel = model.SweepItems[index + 1];
  98. double k = 0;
  99. double b = 0;
  100. 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);
  101. value = Math.Pow(10, k * freq + b);
  102. }
  103. }
  104. break;
  105. }
  106. return value;
  107. }
  108. public static double TimeToOCT(this SweepConfigModel model, double time)
  109. {
  110. double oct = 0;
  111. switch(model.SweepType)
  112. {
  113. case SweepType.Linear:
  114. oct = (model.EndFrequency - model.StartFrequency) / time * 60;
  115. break;
  116. case SweepType.Log:
  117. default:
  118. oct = Math.Log2(model.EndFrequency / model.StartFrequency) / time * 60;
  119. break;
  120. }
  121. return oct;
  122. }
  123. public static double OCTToTime(this SweepConfigModel model, double oct)
  124. {
  125. double time = 0;
  126. switch(model.SweepType)
  127. {
  128. case SweepType.Linear:
  129. time = (model.EndFrequency - model.StartFrequency) / oct * 60;
  130. break;
  131. case SweepType.Log:
  132. default:
  133. time = Math.Log2(model.EndFrequency / model.StartFrequency) / oct * 60;
  134. break;
  135. }
  136. return time;
  137. }
  138. public static double DisplacementToVelocity(double displacement, double freq)
  139. {
  140. return displacement * 2 * MathF.PI * freq / 1000;
  141. }
  142. public static double DisplacementToAcceleration(double displacement, double freq)
  143. {
  144. return displacement * 2 * MathF.PI * freq * 2*MathF.PI*freq / 9800;
  145. }
  146. public static double VelocityToAcceleration(double velocity, double freq)
  147. {
  148. return velocity * 2 * MathF.PI * freq / 9.8;
  149. }
  150. public static double VelocityToDisplacement(double velocity, double freq)
  151. {
  152. return velocity * 1000 / (2 * MathF.PI * freq);
  153. }
  154. public static double AccelerationToDisplacement(double acceleration, double freq)
  155. {
  156. return acceleration * 9800 / (2 * MathF.PI * freq * 2 * MathF.PI * freq);
  157. }
  158. public static double AccelerationToVelocity(double acceleration, double freq)
  159. {
  160. return acceleration * 9.8f / (2 * MathF.PI * freq );
  161. }
  162. /// <summary>
  163. /// 工程量转电压
  164. /// </summary>
  165. /// <param name="value">工程量</param>
  166. /// <param name="sensitivity">灵敏度</param>
  167. /// <returns>电压</returns>
  168. public static double QuantitiesToVoltage(double value, double sensitivity)
  169. {
  170. return value * sensitivity / 1000;
  171. }
  172. /// <summary>
  173. /// 工程量转电压
  174. /// </summary>
  175. /// <param name="value">工程量</param>
  176. /// <param name="sensitivity">灵敏度</param>
  177. /// <returns>电压</returns>
  178. public static float QuantitiesToVoltage(float value, float sensitivity)
  179. {
  180. return value * sensitivity / 1000;
  181. }
  182. /// <summary>
  183. /// 电压转工程量
  184. /// </summary>
  185. /// <param name="value">电压</param>
  186. /// <param name="sensitivity">灵敏度</param>
  187. /// <returns>工程量</returns>
  188. public static double VoltageToQuantities(double value, double sensitivity)
  189. {
  190. return value * 1000 / sensitivity;
  191. }
  192. /// <summary>
  193. /// 电压转工程量
  194. /// </summary>
  195. /// <param name="value">电压</param>
  196. /// <param name="sensitivity">灵敏度</param>
  197. /// <returns>工程量</returns>
  198. public static float VoltageToQuantities(float value, float sensitivity)
  199. {
  200. return value * 1000 / sensitivity;
  201. }
  202. /// <summary>
  203. /// 计算计数的间隔时间
  204. /// </summary>
  205. /// <param name="maxCount">最大计数</param>
  206. /// <returns></returns>
  207. public static double CalcInterval(uint maxCount)
  208. {
  209. return 1d / (maxCount << 1);
  210. }
  211. /// <summary>
  212. /// 二维数组转置
  213. /// </summary>
  214. /// <typeparam name="T"></typeparam>
  215. /// <param name="source"></param>
  216. /// <param name="destination"></param>
  217. /// <param name="rowcount"></param>
  218. /// <param name="colnumcount"></param>
  219. public static void ArrayTranspose<T>(ref T source, ref T destination, int rowcount, int colnumcount)
  220. {
  221. if (rowcount == 0 || colnumcount == 0) return;
  222. for (int i = 0; i < rowcount; i++)
  223. {
  224. for (int j = 0; j < colnumcount; j++)
  225. {
  226. Unsafe.Add(ref destination, i * colnumcount + j) = Unsafe.Add(ref source, j * rowcount + i);
  227. }
  228. }
  229. }
  230. /// <summary>
  231. /// 解交织
  232. /// </summary>
  233. /// <typeparam name="T"></typeparam>
  234. /// <param name="source"></param>
  235. /// <param name="destination"></param>
  236. /// <param name="rowcount"></param>
  237. /// <param name="colnumcount"></param>
  238. public static void Deinterweaving<T>(ref T source, ref T destination, int rowcount, int colnumcount) => ArrayTranspose(ref source, ref destination, rowcount, colnumcount);
  239. }
  240. }