RandomConfigViewModel.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Utilities;
  4. using CommunityToolkit.Mvvm.Input;
  5. using OxyPlot;
  6. using OxyPlot.Series;
  7. using Shaker.Models;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Numerics;
  12. using System.Runtime.CompilerServices;
  13. using System.Runtime.InteropServices;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Input;
  17. namespace ShakerApp.ViewModels
  18. {
  19. internal class RandomConfigViewModel : ViewModelBase<Shaker.Models.RandomConfigModel>
  20. {
  21. public List<RandomData> RefSpectrum => datas;
  22. private RandomConfigViewModel()
  23. {
  24. Content = typeof(Views.RandomConfigView);
  25. #region 加速度谱曲线
  26. AccelerationModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  27. {
  28. MaximumPadding = 0,
  29. MinimumPadding = 0,
  30. Title = App.Current?.FindResource(Shaker.Models.ShakerConstant.FrequencyKey) + "",
  31. Unit = "Hz",
  32. MajorGridlineStyle = LineStyle.Solid,
  33. Position = OxyPlot.Axes.AxisPosition.Bottom,
  34. Key = "Freq"
  35. });
  36. AccelerationModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  37. {
  38. Key = "Ampt",
  39. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  40. Unit = "g",
  41. MajorGridlineStyle = LineStyle.Solid,
  42. Position = OxyPlot.Axes.AxisPosition.Left,
  43. });
  44. AccelerationModel.Title = App.Current?.FindResource(ShakerConstant.AccelerationSpectrumKey) + "";
  45. for (int i = 0; i < oxyColors.Count; i++)
  46. {
  47. LineSeries line = new LineSeries();
  48. line.Title = App.Current?.FindResource(ShakerConstant.SweepAccelerationSpectrumNames[i]) + "";
  49. line.Color = oxyColors[i];
  50. line.StrokeThickness = 1;
  51. line.DataFieldX = nameof(SweepData.Frequency);
  52. line.DataFieldY = properties[i];
  53. line.LineStyle = lineStyles[i];
  54. line.XAxisKey = "Freq";
  55. line.YAxisKey = "Ampt";
  56. lineSeries.Add(line);
  57. AccelerationModel.Series.Add(line);
  58. }
  59. AccelerationModel.Legends.Add(new OxyPlot.Legends.Legend()
  60. {
  61. ShowInvisibleSeries = true,
  62. });
  63. #endregion
  64. #region 速度
  65. VelocityModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  66. {
  67. MaximumPadding = 0,
  68. MinimumPadding = 0,
  69. Title = App.Current?.FindResource(Shaker.Models.ShakerConstant.FrequencyKey) + "",
  70. Unit = "Hz",
  71. MajorGridlineStyle = LineStyle.Solid,
  72. Position = OxyPlot.Axes.AxisPosition.Bottom,
  73. Key = "Freq"
  74. });
  75. VelocityModel.Title = App.Current?.FindResource(ShakerConstant.VelocityKey) + "";
  76. VelocityModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  77. {
  78. Key = "Ampt",
  79. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  80. Unit = "m/s",
  81. MajorGridlineStyle = LineStyle.Solid,
  82. Position = OxyPlot.Axes.AxisPosition.Left,
  83. });
  84. velocity = new LineSeries();
  85. velocity.Title = App.Current?.FindResource(ShakerConstant.VelocityKey) + "";
  86. velocity.Color = oxyColors[0];
  87. velocity.StrokeThickness = 1;
  88. velocity.DataFieldX = nameof(DataPoint.X);
  89. velocity.DataFieldY = nameof(DataPoint.Y);
  90. velocity.LineStyle = lineStyles[0];
  91. velocity.XAxisKey = "Freq";
  92. velocity.YAxisKey = "Ampt";
  93. VelocityModel.Series.Add(velocity);
  94. #endregion
  95. #region 位移
  96. DisplacementModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  97. {
  98. MaximumPadding = 0,
  99. MinimumPadding = 0,
  100. Title = App.Current?.FindResource(Shaker.Models.ShakerConstant.FrequencyKey) + "",
  101. Unit = "Hz",
  102. MajorGridlineStyle = LineStyle.Solid,
  103. Position = OxyPlot.Axes.AxisPosition.Bottom,
  104. Key = "Freq"
  105. });
  106. DisplacementModel.Title = App.Current?.FindResource(ShakerConstant.DisplacementKey) + "";
  107. DisplacementModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  108. {
  109. Key = "Ampt",
  110. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  111. Unit = "mm",
  112. MajorGridlineStyle = LineStyle.Solid,
  113. Position = OxyPlot.Axes.AxisPosition.Left,
  114. });
  115. displacement = new LineSeries();
  116. displacement.Title = App.Current?.FindResource(ShakerConstant.DisplacementKey) + "";
  117. displacement.Color = oxyColors[0];
  118. displacement.StrokeThickness = 1;
  119. displacement.DataFieldX = nameof(DataPoint.X);
  120. displacement.DataFieldY = nameof(DataPoint.Y);
  121. displacement.LineStyle = lineStyles[0];
  122. displacement.XAxisKey = "Freq";
  123. displacement.YAxisKey = "Ampt";
  124. DisplacementModel.Series.Add(displacement);
  125. #endregion
  126. GetEvent(ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
  127. {
  128. AccelerationModel.InvalidatePlot(false);
  129. AccelerationModel.Axes[0].Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "";
  130. AccelerationModel.Axes[1].Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "";
  131. AccelerationModel.Title = App.Current?.FindResource(ShakerConstant.AccelerationSpectrumKey) + "";
  132. for (int i = 0; i < lineSeries.Count; i++)
  133. {
  134. lineSeries[i].Title = App.Current?.FindResource(ShakerConstant.SweepAccelerationSpectrumNames[i]) + "";
  135. }
  136. AccelerationModel.InvalidatePlot(true);
  137. VelocityModel.InvalidatePlot(false);
  138. VelocityModel.Axes[0].Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "";
  139. VelocityModel.Axes[1].Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "";
  140. VelocityModel.Title = App.Current?.FindResource(ShakerConstant.VelocityKey) + "";
  141. velocity.Title = App.Current?.FindResource(ShakerConstant.VelocityKey) + "";
  142. VelocityModel.InvalidatePlot(true);
  143. DisplacementModel.InvalidatePlot(false);
  144. DisplacementModel.Axes[0].Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "";
  145. DisplacementModel.Axes[1].Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "";
  146. DisplacementModel.Title = App.Current?.FindResource(ShakerConstant.DisplacementKey) + "";
  147. displacement.Title = App.Current?.FindResource(ShakerConstant.DisplacementKey) + "";
  148. DisplacementModel.InvalidatePlot(true);
  149. });
  150. GetEvent<AllConfig>().Subscrip((sender, args) =>
  151. {
  152. UpDateModel(args.Data.RandomConfig);
  153. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<RandomConfigModel>()?.Subscrip((sender, args) =>
  154. {
  155. UpDateModel(args.Data);
  156. Refresh();
  157. RandomMainPageViewModel.Instance.SetRefSpectrum(datas);
  158. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<RandomConfigModel>()?.Publish(this, Model);
  159. });
  160. });
  161. }
  162. static RandomConfigViewModel()
  163. {
  164. }
  165. [PropertyAssociation(nameof(RandomConfigModel.SpectrumItemsCount))]
  166. public uint SpectrumItemsCount => Model.SpectrumItemsCount;
  167. [PropertyAssociation(nameof(RandomConfigModel.HanningWindowCompensationCoefficient))]
  168. public double HanningWindowCompensationCoefficient { get => Model.HanningWindowCompensationCoefficient; set => SetProperty(ref Model.HanningWindowCompensationCoefficient, value); }
  169. [PropertyAssociation(nameof(RandomConfigModel.LinearAverage))]
  170. public uint LinearAverage { get => Model.LinearAverage; set => SetProperty(ref Model.LinearAverage, value); }
  171. [PropertyAssociation(nameof(RandomConfigModel.ExponentialAverage))]
  172. public uint ExponentialAverage { get => Model.ExponentialAverage; set => SetProperty(ref Model.ExponentialAverage, value); }
  173. [PropertyAssociation(nameof(RandomConfigModel.LinearAverage),nameof(RandomConfigModel.ExponentialAverage))]
  174. public uint DOF => (LinearAverage << 1) * ((ExponentialAverage << 1) - 1);
  175. [PropertyAssociation(nameof(RandomConfigModel.MaxFrequency),nameof(SpectrumLines))]
  176. public double FrequencyResolution => (double)MaxFrequency / (double)SpectrumLines;
  177. [PropertyAssociation(nameof(RandomConfigModel.RandomSampleRate))]
  178. public uint RandomSampleRate => Model.RandomSampleRate;
  179. List<OxyPlot.Series.LineSeries> lineSeries = new List<OxyPlot.Series.LineSeries>();
  180. List<DataPoint> velocitydata = new List<DataPoint>();
  181. LineSeries velocity = new LineSeries();
  182. LineSeries displacement = new LineSeries();
  183. List<DataPoint> displacementdata = new List<DataPoint>();
  184. List<OxyColor> oxyColors = new List<OxyColor>() { OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
  185. List<LineStyle> lineStyles = new List<LineStyle>() { LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
  186. List<string> properties = new List<string>() { nameof(RandomData.TargetAcceleration), nameof(RandomData.UpStopAcceleration), nameof(RandomData.DownStopAcceleration), nameof(RandomData.UpWarnAcceleration), nameof(RandomData.DownWarnAcceleration) };
  187. List<RandomData> datas = new List<RandomData>();
  188. private double maxDisplacement;
  189. private double maxVelocity;
  190. private bool canResetRMS = false;
  191. private double resetRMS;
  192. [PropertyAssociation(nameof(RandomConfigModel.MaxFrequency))]
  193. public RandomMaxFrequency MaxFrequency
  194. {
  195. get => Model.MaxFrequency;
  196. set
  197. {
  198. SetProperty(ref Model.MaxFrequency, value);
  199. OnPropertyChanged(nameof(FFTLength));
  200. }
  201. }
  202. public bool CanResetRMS { get => canResetRMS; set =>SetProperty(ref canResetRMS, value); }
  203. public double ResetRMS { get => resetRMS; set =>SetProperty(ref resetRMS , value); }
  204. [PropertyAssociation(nameof(RandomConfigModel.SynthesisType))]
  205. public AccelerationSynthesisType SynthesisType { get => Model.SynthesisType; set => SetProperty(ref Model.SynthesisType, value); }
  206. [PropertyAssociation(nameof(RandomConfigModel.MinFrequency))]
  207. public double MinFrequency { get => Model.MinFrequency; set => SetProperty(ref Model.MinFrequency, value); }
  208. [PropertyAssociation(nameof(RandomConfigModel.SpectrumLines))]
  209. public SpectrumLines SpectrumLines
  210. {
  211. get => Model.SpectrumLines;
  212. set
  213. {
  214. SetProperty(ref Model.SpectrumLines, value);
  215. OnPropertyChanged(nameof(FFTLength));
  216. }
  217. }
  218. [PropertyAssociation(nameof(RandomConfigModel.Sigma))]
  219. public double Sigma { get => Model.Sigma; set => SetProperty(ref Model.Sigma, value); }
  220. [PropertyAssociation(nameof(RandomConfigModel.RandomSampleRate),nameof(RandomConfigModel.MaxFrequency),nameof(RandomConfigModel.SpectrumLines))]
  221. public uint FFTLength => (uint)(Model.RandomSampleRate / (float)MaxFrequency * (uint)SpectrumLines);
  222. [PropertyAssociation(nameof(RandomConfigModel.RandomSampleRate), nameof(RandomConfigModel.MaxFrequency), nameof(RandomConfigModel.SpectrumLines))]
  223. public uint FFTHalfLength => FFTLength >> 1;
  224. public RandomIdentifyViewModel Identify { get; } = new RandomIdentifyViewModel();
  225. public AvaloniaList<IndexValueItemViewModel<RandomSpectrumItemViewModel>> SpectrumItems { get; }= new AvaloniaList<IndexValueItemViewModel<RandomSpectrumItemViewModel>>();
  226. public AvaloniaList<IndexValueItemViewModel<RandomPlanItemViewModel>> PlanItems { get; } = new AvaloniaList<IndexValueItemViewModel<RandomPlanItemViewModel>>();
  227. [PropertyAssociation(nameof(RandomConfigModel.StopLins))]
  228. public uint StopLins { get => Model.StopLins; set => SetProperty(ref Model.StopLins, value); }
  229. [PropertyAssociation(nameof(RandomConfigModel.WarnLines))]
  230. public uint WarnLines { get => Model.WarnLines; set => SetProperty(ref Model.WarnLines, value); }
  231. [PropertyAssociation(nameof(RandomConfigModel.StopRMS))]
  232. public double StopRMS { get => Model.StopRMS; set => SetProperty(ref Model.StopRMS, value); }
  233. public OxyPlot.PlotModel AccelerationModel { get; } = new OxyPlot.PlotModel();
  234. public PlotModel DisplacementModel { get; } = new PlotModel();
  235. public PlotModel VelocityModel { get; } = new PlotModel();
  236. public static RandomConfigViewModel Instance { get; } = new RandomConfigViewModel();
  237. [PropertyAssociation(nameof(RandomConfigModel.Sigma))]
  238. public double MaxAcceleration => RMSAcceleration * Sigma;
  239. public double MaxDisplacement { get => maxDisplacement; set =>SetProperty(ref maxDisplacement , value); }
  240. public double MaxVelocity { get => maxVelocity; set =>SetProperty(ref maxVelocity , value); }
  241. public bool AccelerationOverLimit => MaxAcceleration >= ShakerConfigViewModel.Instance.MaxAcceleration;
  242. public bool DisplacementOverLimit => MaxDisplacement > ShakerConfigViewModel.Instance.MaxDisplacement;
  243. public bool VelocityOverLimit => MaxVelocity > ShakerConfigViewModel.Instance.MaxVelocity;
  244. public double AccelerationLoad => Math.Round(MaxAcceleration / ShakerConfigViewModel.Instance.MaxAcceleration * 100f, 2);
  245. public double DisplacementLoad => Math.Round((double)MaxDisplacement / ShakerConfigViewModel.Instance.MaxDisplacement * 100f, 2);
  246. public double VelocityLoad => Math.Round((double)MaxVelocity / ShakerConfigViewModel.Instance.MaxVelocity * 100f, 2);
  247. public double RMSAcceleration { get => Model.RMSAcceleration; set =>SetProperty(ref Model.RMSAcceleration , value); }
  248. public ICommand AddCommand => new RelayCommand(Add);
  249. public override void UpDateModel(RandomConfigModel model)
  250. {
  251. SpectrumItems.Clear();
  252. PlanItems.Clear();
  253. for(int i=0;i<model.SpectrumItems.Count;i++)
  254. {
  255. SpectrumItems.Add(new IndexValueItemViewModel<RandomSpectrumItemViewModel>(i + 1, new RandomSpectrumItemViewModel(model.SpectrumItems[i])));
  256. }
  257. for(int i=0;i<model.PlanItems.Count;i++)
  258. {
  259. PlanItems.Add(new IndexValueItemViewModel<RandomPlanItemViewModel>(i + 1, new RandomPlanItemViewModel(model.PlanItems[i])));
  260. }
  261. base.UpDateModel(model);
  262. }
  263. private void Add()
  264. {
  265. if (SpectrumItems.Count >= SpectrumItemsCount) return;
  266. if (Model.SpectrumItems.Count == 0) Model.SpectrumItems.Add(new RandomSpectrumItemModel());
  267. else Model.SpectrumItems.Add(Model.SpectrumItems[^1].CloneBase());
  268. SpectrumItems.Add(new IndexValueItemViewModel<RandomSpectrumItemViewModel>(SpectrumItems.Count + 1, new RandomSpectrumItemViewModel(Model.SpectrumItems[^1])));
  269. }
  270. public ICommand DeleteCommand => new RelayCommand(Delete);
  271. private void Delete()
  272. {
  273. if (SpectrumItems.Count == 0) return;
  274. if(Model.SpectrumItems.Count>0)
  275. {
  276. Model.SpectrumItems.RemoveAt(Model.SpectrumItems.Count - 1);
  277. }
  278. SpectrumItems.RemoveAt(SpectrumItems.Count - 1);
  279. }
  280. public ICommand ResetRMSCommand=>new RelayCommand(ResetRMSExecute);
  281. private void ResetRMSExecute()
  282. {
  283. if (!CanResetRMS) return;
  284. if (ResetRMS <= 0) return;
  285. for(int i=0;i<SpectrumItems.Count;i++)
  286. {
  287. SpectrumItems[i].Value.Value *= (ResetRMS*ResetRMS)/(RMSAcceleration * RMSAcceleration);
  288. Model.SpectrumItems[i].Value = SpectrumItems[i].Value.Value;
  289. }
  290. CanResetRMS = false;
  291. RefreshCommand?.CanExecute(null);
  292. }
  293. public ICommand AddPlanCommand => new RelayCommand(AddPlan);
  294. private void AddPlan()
  295. {
  296. if (PlanItems.Count >= 10 || Model.PlanItems.Count>=10) return;
  297. if (Model.PlanItems.Count == 0) Model.PlanItems.Add(new RandomPlanItemModel());
  298. else Model.PlanItems.Add(Model.PlanItems[^1].CloneBase());
  299. PlanItems.Add(new IndexValueItemViewModel<RandomPlanItemViewModel>(PlanItems.Count + 1, new RandomPlanItemViewModel(Model.PlanItems[^1])));
  300. }
  301. public ICommand DeletePlanCommand => new RelayCommand(DeletePlan);
  302. private void DeletePlan()
  303. {
  304. if (PlanItems.Count == 0) return;
  305. if(Model.PlanItems.Count==0)
  306. {
  307. Model.PlanItems.RemoveAt(Model.PlanItems.Count - 1);
  308. }
  309. PlanItems.RemoveAt(PlanItems.Count - 1);
  310. }
  311. public ICommand RefreshPlanCommand => new RelayCommand(RefreshPlan);
  312. private void RefreshPlan()
  313. {
  314. if (PlanItems.Count < 1) return;
  315. }
  316. public ICommand RefreshCommand => new RelayCommand(Refresh);
  317. private unsafe void Refresh()
  318. {
  319. if (SpectrumItems.Count < 3) return;
  320. var items = SpectrumItems.Select(x => x.Value.Model.CloneBase()).DistinctBy(x => x.Frequency).OrderBy(x => x.Frequency).ToList();
  321. SpectrumItems.Clear();
  322. for(int i=0;i<items.Count;i++)
  323. {
  324. SpectrumItems.Add(new IndexValueItemViewModel<RandomSpectrumItemViewModel>(i + 1, new RandomSpectrumItemViewModel(items[i])));
  325. }
  326. if (items.Count < 3) return;
  327. CalcRefSpectrum(items.Select(x => x.Frequency).ToArray(), items.Select(x => x.Value).ToArray(), items.Select(x => x.ValueType).ToArray(), FrequencyResolution,out var fy_array,out var f,out var turningfreq);
  328. if (fy_array.Length == 0) return;
  329. this.Model.SpectralTables.Clear();
  330. for(int i=0;i<turningfreq.Length;i++)
  331. {
  332. this.Model.SpectralTables.Add(new RandomSpectralTableModel()
  333. {
  334. TurningFrequency = turningfreq[i],
  335. TurningValue = fy_array[(int)((turningfreq[i]-SpectrumItems[0].Value.Frequency)/FrequencyResolution)],
  336. });
  337. }
  338. RMSAcceleration = Math.Sqrt(ViewModels.MainViewModel.Default.Calc.Sum.Sum(ref fy_array[0], (uint)fy_array.Length) * FrequencyResolution);
  339. ResetRMS = RMSAcceleration;
  340. double[] f2 = new double[f.Length];
  341. double[] fv = new double[f.Length];
  342. MainViewModel.Default.Calc.Multiply.Multiply(ref f[0], ref f[0], (uint)f.Length,ref f2[0]);
  343. MainViewModel.Default.Calc.Division.Division(ref fy_array[0],ref f2[0], (uint)f.Length,ref fv[0]);
  344. double sum = MainViewModel.Default.Calc.Sum.Sum(ref fv[0], (uint)fv.Length);
  345. MaxVelocity = Math.Sqrt((sum - (fv[0] + fv[^1] / 2) / 2) * FrequencyResolution) / (2 * Math.PI) * Sigma * 9.8f;
  346. MainViewModel.Default.Calc.Division.Division(ref fv[0], ref f2[0], (uint)f.Length, ref fv[0]);
  347. sum = MainViewModel.Default.Calc.Sum.Sum(ref fv[0], (uint)fv.Length);
  348. MaxDisplacement = Math.Sqrt((sum - (fv[0] + fv[^1] / 2) / 2) * FrequencyResolution) / (2 * Math.PI*2*Math.PI) * Sigma * 9800f;
  349. OnPropertyChanged(nameof(MaxAcceleration));
  350. double[,] spectrumdata = new double[5, f.Length];
  351. Unsafe.CopyBlock(ref Unsafe.As<double, byte>(ref spectrumdata[0, 0]), ref Unsafe.As<double, byte>(ref fy_array[0]), (uint)(Unsafe.SizeOf<double>() * fy_array.Length));
  352. CalcSpectrum(items, f, fy_array, FrequencyResolution, turningfreq, ref spectrumdata);
  353. datas.Clear();
  354. velocitydata.Clear();
  355. displacementdata.Clear();
  356. for (int i=0;i<f.Length;i++)
  357. {
  358. datas.Add(new RandomData(f[i],double.NaN, double.NaN, spectrumdata[0, i], spectrumdata[1, i], spectrumdata[3, i], spectrumdata[2, i], spectrumdata[4,i]));
  359. velocitydata.Add(new DataPoint(f[i], Shaker.Models.Tools.Tools.AccelerationToVelocity(spectrumdata[0, i], f[i])));
  360. displacementdata.Add(new DataPoint(f[i], Shaker.Models.Tools.Tools.AccelerationToDisplacement(spectrumdata[0, i], f[i])));
  361. }
  362. AccelerationModel.InvalidatePlot(false);
  363. VelocityModel.InvalidatePlot(false);
  364. DisplacementModel.InvalidatePlot(false);
  365. for (int i = 0; i < lineSeries.Count; i++)
  366. {
  367. lineSeries[i].ItemsSource = datas;
  368. }
  369. velocity.ItemsSource = velocitydata;
  370. displacement.ItemsSource = displacementdata;
  371. AccelerationModel.InvalidatePlot(true);
  372. VelocityModel.InvalidatePlot(true);
  373. DisplacementModel.InvalidatePlot(true);
  374. CanResetRMS = true;
  375. }
  376. private void CalcSpectrum(List<RandomSpectrumItemModel> models, [In] double[] f, [In] double[] fy_array, [In] double deltaf, [In] double[] turn_freq,ref double[,] spectrumdata)
  377. {
  378. CalcItemSpectrum(models.Select(x => x.UpStop).ToArray(), f, fy_array, deltaf, turn_freq, ref spectrumdata[1, 0]);
  379. CalcItemSpectrum(models.Select(x => x.UpWarn).ToArray(), f, fy_array, deltaf, turn_freq, ref spectrumdata[2, 0]);
  380. CalcItemSpectrum(models.Select(x => x.DownStop).ToArray(), f, fy_array, deltaf, turn_freq, ref spectrumdata[3, 0]);
  381. CalcItemSpectrum(models.Select(x => x.DownWarn).ToArray(), f, fy_array, deltaf, turn_freq, ref spectrumdata[4, 0]);
  382. }
  383. private void CalcItemSpectrum(double[] offset, [In] double[] f, [In] double[] fy_array, [In] double deltaf, [In] double[] turn_freq, ref double spectrumdata)
  384. {
  385. for (int i = 0; i < offset.Length - 1; i++)
  386. {
  387. int index = (int)((turn_freq[i] - turn_freq[0]) / deltaf);
  388. uint len = (uint)((turn_freq[i + 1] - turn_freq[i]) / deltaf);
  389. if (offset[i+1] == offset[i])
  390. {
  391. var x = Math.Pow(10, offset[i] / 10);
  392. MainViewModel.Default.Calc.Multiply.Multiply(ref fy_array[index], x, len, ref Unsafe.Add(ref spectrumdata,index));
  393. }
  394. else
  395. {
  396. double db1_psd = Math.Pow(10, offset[i] / 10) * fy_array[index];
  397. double db2_psd = Math.Pow(10, offset[i + 1] / 10) * fy_array[index+len];
  398. var x= Math.Log10(db2_psd / db1_psd) / (Math.Log2(turn_freq[i + 1] / turn_freq[i])) * 10;
  399. for(int j=0;j<len;j++)
  400. {
  401. ref double result = ref Unsafe.Add(ref spectrumdata, index + j);
  402. result = Math.Pow(10, Math.Log2(f[index + j] / turn_freq[i]) * x / 10) * db1_psd;
  403. }
  404. }
  405. }
  406. ref double temp = ref Unsafe.Add(ref spectrumdata, fy_array.Length - 1);
  407. temp = Math.Pow(10, offset[^1] / 10) * fy_array[^1];
  408. }
  409. private void CalcRefSpectrum(double[] f_array, double[] y_array, RandomValueType[] k_array, double deltaf ,out double[] fy_array,out double[] f,out double[] turningfreq)
  410. {
  411. int n=f_array.Length;
  412. // 计算圆整频率值及其对应的谱值
  413. double[] round_f = new double[n];
  414. double[] round_y = new double[n];
  415. turningfreq = new double[n];
  416. for (int i = 0; i < n; i++)
  417. {
  418. round_f[i] = Math.Ceiling(f_array[i] / deltaf) * deltaf;
  419. turningfreq[i] = round_f[i];
  420. }
  421. // 若第一个点值未知时,需先计算第一个点的值
  422. if (k_array[0] == RandomValueType.Slope && k_array[1] == RandomValueType.Slope)
  423. {
  424. y_array[0] = y_array[1];
  425. }
  426. if (k_array[0] == RandomValueType.Slope && k_array[1] == RandomValueType.Value)
  427. {
  428. k_array[0] = 0;
  429. y_array[0] = y_array[1];
  430. }
  431. if (k_array[0] == RandomValueType.Slope)
  432. {
  433. double kk = y_array[1];
  434. y_array[0] = y_array[2] / (Math.Pow(10, kk / 10 * Math.Log(f_array[1] / f_array[0], 2)));
  435. y_array[1] = y_array[2];
  436. k_array[0] = 0;
  437. k_array[1] = 0;
  438. }
  439. // 计算第二点及其后点的值
  440. for (int i = 1; i < n; i++)
  441. {
  442. if (f_array[i] == 0)
  443. {
  444. double kk = y_array[i];
  445. y_array[i] = y_array[i + 1];
  446. f_array[i] = Math.Pow(2, (10 * Math.Log(y_array[i + 1] / y_array[i - 1], 10) / kk)) * f_array[i - 1];
  447. }
  448. else
  449. {
  450. if (k_array[i] == RandomValueType.Slope)
  451. {
  452. y_array[i] = Math.Pow(10, y_array[i] / 10 * Math.Log(f_array[i] / f_array[i - 1], 2)) * y_array[i - 1];
  453. k_array[i] = 0;
  454. }
  455. }
  456. }
  457. // 验证结果值
  458. double[] k_exam = new double[n - 1];
  459. for (int i = 0; i < n; i++)
  460. {
  461. if (i < n - 1)
  462. {
  463. k_exam[i] = 10 * Math.Log(y_array[i + 1] / y_array[i], 10) / Math.Log(f_array[i + 1] / f_array[i], 2);
  464. }
  465. }
  466. // 计算圆整后频率对应的谱值
  467. for (int i = 0; i < n; i++)
  468. {
  469. if (round_f[i] == f_array[i])
  470. {
  471. round_y[i] = y_array[i];
  472. }
  473. if (i < n - 1)
  474. {
  475. if (round_f[i] > f_array[i])
  476. {
  477. round_y[i] = Math.Pow(10, k_exam[i] / 10 * Math.Log(round_f[i] / f_array[i], 2)) * y_array[i];
  478. }
  479. }
  480. if (i == n - 1)
  481. {
  482. round_y[i] = Math.Pow(10, k_exam[i - 1] / 10 * Math.Log(round_f[i] / f_array[i], 2)) * y_array[i];
  483. }
  484. if (round_f[i] < f_array[i])
  485. {
  486. round_y[i] = Math.Pow(10, k_exam[i - 1] / 10 * Math.Log(f_array[i] / round_f[i], 2)) * y_array[i];
  487. }
  488. }
  489. int parts = k_exam.Length; // 将谱按斜率分成几段,parts为总的段数\
  490. f = Enumerable.Range(0, (int)((round_f[n - 1] - round_f[0]) / deltaf) + 1).Select(x => x * deltaf + round_f[0]).ToArray();
  491. fy_array =new double[f.Length];
  492. fy_array[0] = round_y[0];
  493. fy_array[^1] = round_y[^1];
  494. int part_begin = 0;
  495. for (int i = 0; i < parts; i++)
  496. {
  497. fy_array[part_begin] = round_y[i];
  498. int n_part = (int)Math.Round((round_f[i + 1] - round_f[i]) / deltaf);
  499. if (k_exam[i] == 0)
  500. {
  501. for (int k = part_begin + 1; k <= part_begin + n_part; k++)
  502. {
  503. fy_array[k] = fy_array[k - 1];
  504. }
  505. }
  506. else
  507. {
  508. if (n_part > 0)
  509. {
  510. for (int k = part_begin + 1; k <= part_begin + n_part; k++)
  511. {
  512. fy_array[k] = Math.Pow(10, k_exam[i] / 10 * Math.Log(f[k] / f[k - 1], 2)) * fy_array[k - 1];
  513. }
  514. }
  515. }
  516. part_begin += n_part;
  517. }
  518. }
  519. protected override void Save()
  520. {
  521. base.Save();
  522. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<RandomConfigModel>()?.Publish(this, Model);
  523. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<RandomConfigModel>()?.Publish(this, Model);
  524. RandomMainPageViewModel.Instance.SetRefSpectrum(datas);
  525. }
  526. }
  527. }