Tools.cs 12 KB

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