123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Schema;
- namespace Shaker.Models.Tools
- {
- public static class Tools
- {
- public static void CalcSlope(double[] x, double[] y,ref double k,ref double b)
- {
- if(x ==null || y==null || x.Length!=2||y.Length!=2)
- {
- k = 0;
- b = 0;
- return;
- }
- k = (y[1] - y[0]) / (x[1] - x[0]);
- b = y[0] - x[0] * k;
- }
- public static int FindFrequencyIndex(this SweepConfigModel model, double freq)
- {
- if (model.SweepItems.Count < 2) return -1;
- uint uintfreq = (uint)(freq * 100);
- for (int i = 0; i < model.SweepItems.Count - 1; i++)
- {
- if ((uint)(model.SweepItems[i].Frequency * 100) <= uintfreq && uintfreq <= (uint)(model.SweepItems[i + 1].Frequency * 100)) return i;
- }
- return model.SweepItems.Count - 1;
- }
- public static void CalcAmpt(this SweepConfigModel model, double freq,ref double value,ref double upstop,ref double upwarn,ref double downstop,ref double downwarn)
- {
- int index = model.FindFrequencyIndex(freq);
- if (index == -1) return;
- value = 0;
- SweepItemModel nowmodel = model.SweepItems[index];
- switch (nowmodel.SweepValueType)
- {
- case SweepValueType.FixedDisplacement:
- value = freq * nowmodel.Value * freq * 4 * Math.PI * Math.PI / 9800f;
- break;
- case SweepValueType.FixedVelocity:
- value = freq * 2 * Math.PI * nowmodel.Value / 9.8f;
- break;
- case SweepValueType.FixedAcceleration:
- value = nowmodel.Value;
- break;
- case SweepValueType.DynamicAcceleration:
- {
- if (index == model.SweepItems.Count - 1)
- {
- value = nowmodel.Value;
- }
- else
- {
- SweepItemModel nextmodel = model.SweepItems[index + 1];
- double k = 0;
- double b = 0;
- Tools.CalcSlope([Math.Log10(nowmodel.Frequency), Math.Log10(nextmodel.Frequency)], [Math.Log10(nowmodel.Value), Math.Log10(nextmodel.Value)], ref k, ref b);
- value = Math.Pow(10, k * Math.Log10(freq) + b);
- }
- }
- break;
- }
- upstop = Math.Pow(10, nowmodel.UpStop / 20) * value;
- upwarn = Math.Pow(10, nowmodel.UpWarn / 20) * value;
- downstop = Math.Pow(10, nowmodel.DownStop / 20) * value;
- downwarn = Math.Pow(10, nowmodel.DownWarn / 20) * value;
- }
- public static double CalcAmpt(this SweepConfigModel model, double freq)
- {
- int index = model.FindFrequencyIndex(freq);
- if (index == -1) return 0;
- double value = 0;
- SweepItemModel nowmodel = model.SweepItems[index];
- switch (nowmodel.SweepValueType)
- {
- case SweepValueType.FixedDisplacement:
- value = freq * nowmodel.Value * freq * 4 * MathF.PI*MathF.PI / 9800f;
- break;
- case SweepValueType.FixedVelocity:
- value = freq * 2 * MathF.PI * nowmodel.Value / 9.8f;
- break;
- case SweepValueType.FixedAcceleration:
- value = nowmodel.Value;
- break;
- case SweepValueType.DynamicAcceleration:
- {
- if (index == model.SweepItems.Count - 1)
- {
- value = nowmodel.Value;
- }
- else
- {
- SweepItemModel nextmodel = model.SweepItems[index + 1];
- double k = 0;
- double b = 0;
- 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);
- value = Math.Pow(10, k * Math.Log10(freq) + b);
- }
- }
- break;
- }
- return value;
- }
- public static double TimeToOCT(this SweepConfigModel model, double time)
- {
- double oct = 0;
- switch(model.SweepType)
- {
- case SweepType.Linear:
- oct = (model.EndFrequency - model.StartFrequency) / time * 60;
- break;
- case SweepType.Log:
- default:
- oct = Math.Log2(model.EndFrequency / model.StartFrequency) / time * 60;
- break;
- }
- return oct;
- }
- public static double OCTToTime(this SweepConfigModel model, double oct)
- {
- double time = 0;
- switch(model.SweepType)
- {
- case SweepType.Linear:
- time = (model.EndFrequency - model.StartFrequency) / oct * 60;
- break;
- case SweepType.Log:
- default:
- time = Math.Log2(model.EndFrequency / model.StartFrequency) / oct * 60;
- break;
- }
- return time;
- }
- public static double DisplacementToVelocity(double displacement, double freq)
- {
- return displacement * 2 * Math.PI * freq / 1000;
- }
- public static double DisplacementToAcceleration(double displacement, double freq)
- {
- return displacement * 2 * Math.PI * freq * 2*Math.PI*freq / 9800;
- }
- public static double VelocityToAcceleration(double velocity, double freq)
- {
- return velocity * 2 * Math.PI * freq / 9.8;
- }
- public static double VelocityToDisplacement(double velocity, double freq)
- {
- return velocity * 1000 / (2 * Math.PI * freq);
- }
- public static double AccelerationToDisplacement(double acceleration, double freq)
- {
- return acceleration * 9800 / (2 * Math.PI * freq * 2 * Math.PI * freq);
- }
- public static double AccelerationToVelocity(double acceleration, double freq)
- {
- return acceleration * 9.8f / (2 * Math.PI * freq );
- }
- /// <summary>
- /// 工程量转电压
- /// </summary>
- /// <param name="value">工程量</param>
- /// <param name="sensitivity">灵敏度</param>
- /// <returns>电压</returns>
- public static double QuantitiesToVoltage(double value, double sensitivity)
- {
- return value * sensitivity / 1000;
- }
- /// <summary>
- /// 工程量转电压
- /// </summary>
- /// <param name="value">工程量</param>
- /// <param name="sensitivity">灵敏度</param>
- /// <returns>电压</returns>
- public static float QuantitiesToVoltage(float value, float sensitivity)
- {
- return value * sensitivity / 1000;
- }
- /// <summary>
- /// 电压转工程量
- /// </summary>
- /// <param name="value">电压</param>
- /// <param name="sensitivity">灵敏度</param>
- /// <returns>工程量</returns>
- public static double VoltageToQuantities(double value, double sensitivity)
- {
- return value * 1000 / sensitivity;
- }
- /// <summary>
- /// 电压转工程量
- /// </summary>
- /// <param name="value">电压</param>
- /// <param name="sensitivity">灵敏度</param>
- /// <returns>工程量</returns>
- public static float VoltageToQuantities(float value, float sensitivity)
- {
- return value * 1000 / sensitivity;
- }
- /// <summary>
- /// 计算计数的间隔时间
- /// </summary>
- /// <param name="maxCount">最大计数</param>
- /// <returns></returns>
- public static double CalcInterval(uint maxCount)
- {
- return 1d / (maxCount << 1);
- }
- /// <summary>
- /// 二维数组转置
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source"></param>
- /// <param name="destination"></param>
- /// <param name="rowcount"></param>
- /// <param name="colnumcount"></param>
- public static void ArrayTranspose<T>(ref T source, ref T destination, int rowcount, int colnumcount,int maxcolnumcount)
- {
- if (rowcount == 0 || colnumcount == 0) return;
- for (int i = 0; i < rowcount; i++)
- {
- for (int j = 0; j < colnumcount; j++)
- {
- Unsafe.Add(ref destination, i * maxcolnumcount + j) = Unsafe.Add(ref source, j * rowcount + i);
- }
- }
- }
- public static double CalcLevel(double currentlevel,double level)
- {
- return Math.Pow(10, level / 20) * currentlevel;
- }
- public static float CalcLevel(float currentlevel, float level)
- {
- return MathF.Pow(10, level / 20) * currentlevel;
- }
- /// <summary>
- /// 解交织
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source"></param>
- /// <param name="destination"></param>
- /// <param name="rowcount"></param>
- /// <param name="colnumcount"></param>
- 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);
- public static byte[] CompressionBytes(byte[] arrays)
- {
- if (arrays == null || arrays.Length == 0) return new byte[0];
- using (MemoryStream ms =new MemoryStream())
- {
- using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionLevel.SmallestSize, true))
- {
- ds.Write(arrays, 0, arrays.Length);
- }
- return ms.ToArray();
- }
- }
- public static byte[] DecompressionBytes(byte[] arrays)
- {
- if (arrays == null || arrays.Length == 0) return new byte[0];
- using (MemoryStream ms = new MemoryStream(arrays))
- {
- using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress,true))
- {
- using (MemoryStream memoryStream = new MemoryStream())
- {
- ds.CopyTo(memoryStream);
- return memoryStream.ToArray();
- }
- }
- }
- }
- public static byte[] CompressionWaves<T>(ref T f,uint count) where T : unmanaged
- {
- if(count==0)return new byte[0];
- byte[] temp = new byte[Unsafe.SizeOf<T>() * count];
- Unsafe.CopyBlock(ref temp[0], ref Unsafe.As<T, byte>(ref f), (uint)temp.Length);
- return CompressionBytes(temp);
- }
- public static T[] DecompressionWaves<T>(byte[] f) where T : unmanaged
- {
- if (f ==null || f.Length == 0) return new T[0];
- byte[] temp = DecompressionBytes(f);
- T[] result = new T[temp.Length / Unsafe.SizeOf<T>()];
- Unsafe.CopyBlock( ref Unsafe.As<T, byte>(ref result[0]),ref temp[0], (uint)temp.Length);
- return result;
- }
- }
- }
|