RandomConfigViewModel.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 : DisplayViewModelBase<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. Identify.PropertyChanged += (_, _) => SaveIsEnabled = true;
  162. }
  163. static RandomConfigViewModel()
  164. {
  165. }
  166. [PropertyAssociation(nameof(RandomConfigModel.SpectrumItemsCount))]
  167. public uint SpectrumItemsCount => Model.SpectrumItemsCount;
  168. [PropertyAssociation(nameof(RandomConfigModel.HanningWindowCompensationCoefficient))]
  169. public double HanningWindowCompensationCoefficient { get => Model.HanningWindowCompensationCoefficient; set => SetProperty(ref Model.HanningWindowCompensationCoefficient, value); }
  170. [PropertyAssociation(nameof(RandomConfigModel.LinearAverage))]
  171. public uint LinearAverage { get => Model.LinearAverage; set => SetProperty(ref Model.LinearAverage, value); }
  172. [PropertyAssociation(nameof(RandomConfigModel.ExponentialAverage))]
  173. public uint ExponentialAverage { get => Model.ExponentialAverage; set => SetProperty(ref Model.ExponentialAverage, value); }
  174. [PropertyAssociation(nameof(RandomConfigModel.LinearAverage),nameof(RandomConfigModel.ExponentialAverage))]
  175. public uint DOF => (LinearAverage << 1) * ((ExponentialAverage << 1) - 1);
  176. [PropertyAssociation(nameof(RandomConfigModel.MaxFrequency),nameof(SpectrumLines))]
  177. public double FrequencyResolution => (double)MaxFrequency / (double)SpectrumLines;
  178. [PropertyAssociation(nameof(RandomConfigModel.RandomSampleRate))]
  179. public uint RandomSampleRate => Model.RandomSampleRate;
  180. List<OxyPlot.Series.LineSeries> lineSeries = new List<OxyPlot.Series.LineSeries>();
  181. List<DataPoint> velocitydata = new List<DataPoint>();
  182. LineSeries velocity = new LineSeries();
  183. LineSeries displacement = new LineSeries();
  184. List<DataPoint> displacementdata = new List<DataPoint>();
  185. List<OxyColor> oxyColors = new List<OxyColor>() { OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
  186. List<LineStyle> lineStyles = new List<LineStyle>() { LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
  187. List<string> properties = new List<string>() { nameof(RandomData.TargetAcceleration), nameof(RandomData.UpStopAcceleration), nameof(RandomData.DownStopAcceleration), nameof(RandomData.UpWarnAcceleration), nameof(RandomData.DownWarnAcceleration) };
  188. List<RandomData> datas = new List<RandomData>();
  189. private double maxDisplacement;
  190. private double maxVelocity;
  191. private bool canResetRMS = false;
  192. private double resetRMS;
  193. [PropertyAssociation(nameof(RandomConfigModel.MaxFrequency))]
  194. public RandomMaxFrequency MaxFrequency
  195. {
  196. get => Model.MaxFrequency;
  197. set
  198. {
  199. SetProperty(ref Model.MaxFrequency, value);
  200. OnPropertyChanged(nameof(FFTLength));
  201. }
  202. }
  203. public bool CanResetRMS { get => canResetRMS; set =>SetProperty(ref canResetRMS, value); }
  204. public double ResetRMS { get => resetRMS; set =>SetProperty(ref resetRMS , value); }
  205. [PropertyAssociation(nameof(RandomConfigModel.SynthesisType))]
  206. public AccelerationSynthesisType SynthesisType { get => Model.SynthesisType; set => SetProperty(ref Model.SynthesisType, value); }
  207. [PropertyAssociation(nameof(RandomConfigModel.MinFrequency))]
  208. public double MinFrequency { get => Model.MinFrequency; set => SetProperty(ref Model.MinFrequency, value); }
  209. [PropertyAssociation(nameof(RandomConfigModel.SpectrumLines))]
  210. public SpectrumLines SpectrumLines
  211. {
  212. get => Model.SpectrumLines;
  213. set
  214. {
  215. SetProperty(ref Model.SpectrumLines, value);
  216. OnPropertyChanged(nameof(FFTLength));
  217. }
  218. }
  219. [PropertyAssociation(nameof(RandomConfigModel.Sigma))]
  220. public double Sigma { get => Model.Sigma; set => SetProperty(ref Model.Sigma, value); }
  221. [PropertyAssociation(nameof(RandomConfigModel.RandomSampleRate),nameof(RandomConfigModel.MaxFrequency),nameof(RandomConfigModel.SpectrumLines))]
  222. public uint FFTLength => (uint)(Model.RandomSampleRate / (float)MaxFrequency * (uint)SpectrumLines);
  223. [PropertyAssociation(nameof(RandomConfigModel.RandomSampleRate), nameof(RandomConfigModel.MaxFrequency), nameof(RandomConfigModel.SpectrumLines))]
  224. public uint FFTHalfLength => FFTLength >> 1;
  225. public RandomIdentifyViewModel Identify { get; } = new RandomIdentifyViewModel();
  226. public AvaloniaList<IndexValueItemViewModel<RandomSpectrumItemViewModel>> SpectrumItems { get; }= new AvaloniaList<IndexValueItemViewModel<RandomSpectrumItemViewModel>>();
  227. public AvaloniaList<IndexValueItemViewModel<RandomPlanItemViewModel>> PlanItems { get; } = new AvaloniaList<IndexValueItemViewModel<RandomPlanItemViewModel>>();
  228. [PropertyAssociation(nameof(RandomConfigModel.StopLins))]
  229. public uint StopLins { get => Model.StopLins; set => SetProperty(ref Model.StopLins, value); }
  230. [PropertyAssociation(nameof(RandomConfigModel.WarnLines))]
  231. public uint WarnLines { get => Model.WarnLines; set => SetProperty(ref Model.WarnLines, value); }
  232. [PropertyAssociation(nameof(RandomConfigModel.StopRMS))]
  233. public double StopRMS { get => Model.StopRMS; set => SetProperty(ref Model.StopRMS, value); }
  234. public OxyPlot.PlotModel AccelerationModel { get; } = new OxyPlot.PlotModel();
  235. public PlotModel DisplacementModel { get; } = new PlotModel();
  236. public PlotModel VelocityModel { get; } = new PlotModel();
  237. public static RandomConfigViewModel Instance { get; } = new RandomConfigViewModel();
  238. [PropertyAssociation(nameof(RandomConfigModel.Sigma))]
  239. public double MaxAcceleration => RMSAcceleration * Sigma;
  240. public double MaxDisplacement { get => maxDisplacement; set =>SetProperty(ref maxDisplacement , value); }
  241. public double MaxVelocity { get => maxVelocity; set =>SetProperty(ref maxVelocity , value); }
  242. public bool AccelerationOverLimit => MaxAcceleration >= ShakerConfigViewModel.Instance.MaxAcceleration;
  243. public bool DisplacementOverLimit => MaxDisplacement > ShakerConfigViewModel.Instance.MaxDisplacement;
  244. public bool VelocityOverLimit => MaxVelocity > ShakerConfigViewModel.Instance.MaxVelocity;
  245. public double AccelerationLoad => Math.Round(MaxAcceleration / ShakerConfigViewModel.Instance.MaxAcceleration * 100f, 2);
  246. public double DisplacementLoad => Math.Round((double)MaxDisplacement / ShakerConfigViewModel.Instance.MaxDisplacement * 100f, 2);
  247. public double VelocityLoad => Math.Round((double)MaxVelocity / ShakerConfigViewModel.Instance.MaxVelocity * 100f, 2);
  248. public double RMSAcceleration { get => Model.RMSAcceleration; set =>SetProperty(ref Model.RMSAcceleration , value); }
  249. public ICommand AddCommand => new RelayCommand(Add);
  250. public override void UpDateModel(RandomConfigModel model)
  251. {
  252. SpectrumItems.Clear();
  253. PlanItems.Clear();
  254. for(int i=0;i<model.SpectrumItems.Count;i++)
  255. {
  256. SpectrumItems.Add(new IndexValueItemViewModel<RandomSpectrumItemViewModel>(i + 1, new RandomSpectrumItemViewModel(model.SpectrumItems[i])));
  257. }
  258. for(int i=0;i<model.PlanItems.Count;i++)
  259. {
  260. PlanItems.Add(new IndexValueItemViewModel<RandomPlanItemViewModel>(i + 1, new RandomPlanItemViewModel(model.PlanItems[i])));
  261. }
  262. Identify.UpDateModel(model.Identify);
  263. base.UpDateModel(model);
  264. }
  265. private void Add()
  266. {
  267. if (SpectrumItems.Count >= SpectrumItemsCount) return;
  268. if (Model.SpectrumItems.Count == 0) Model.SpectrumItems.Add(new RandomSpectrumItemModel());
  269. else Model.SpectrumItems.Add(Model.SpectrumItems[^1].CloneBase());
  270. SpectrumItems.Add(new IndexValueItemViewModel<RandomSpectrumItemViewModel>(SpectrumItems.Count + 1, new RandomSpectrumItemViewModel(Model.SpectrumItems[^1])));
  271. }
  272. public ICommand DeleteCommand => new RelayCommand(Delete);
  273. private void Delete()
  274. {
  275. if (SpectrumItems.Count == 0) return;
  276. if(Model.SpectrumItems.Count>0)
  277. {
  278. Model.SpectrumItems.RemoveAt(Model.SpectrumItems.Count - 1);
  279. }
  280. SpectrumItems.RemoveAt(SpectrumItems.Count - 1);
  281. }
  282. public ICommand ResetRMSCommand=>new RelayCommand(ResetRMSExecute);
  283. private void ResetRMSExecute()
  284. {
  285. if (!CanResetRMS) return;
  286. if (ResetRMS <= 0) return;
  287. for(int i=0;i<SpectrumItems.Count;i++)
  288. {
  289. SpectrumItems[i].Value.Value *= (ResetRMS*ResetRMS)/(RMSAcceleration * RMSAcceleration);
  290. Model.SpectrumItems[i].Value = SpectrumItems[i].Value.Value;
  291. }
  292. CanResetRMS = false;
  293. RefreshCommand?.CanExecute(null);
  294. }
  295. public ICommand AddPlanCommand => new RelayCommand(AddPlan);
  296. private void AddPlan()
  297. {
  298. if (PlanItems.Count >= 10 || Model.PlanItems.Count>=10) return;
  299. if (Model.PlanItems.Count == 0) Model.PlanItems.Add(new RandomPlanItemModel());
  300. else Model.PlanItems.Add(Model.PlanItems[^1].CloneBase());
  301. PlanItems.Add(new IndexValueItemViewModel<RandomPlanItemViewModel>(PlanItems.Count + 1, new RandomPlanItemViewModel(Model.PlanItems[^1])));
  302. }
  303. public ICommand DeletePlanCommand => new RelayCommand(DeletePlan);
  304. private void DeletePlan()
  305. {
  306. if (PlanItems.Count == 0) return;
  307. if(Model.PlanItems.Count==0)
  308. {
  309. Model.PlanItems.RemoveAt(Model.PlanItems.Count - 1);
  310. }
  311. PlanItems.RemoveAt(PlanItems.Count - 1);
  312. }
  313. public ICommand RefreshPlanCommand => new RelayCommand(RefreshPlan);
  314. private void RefreshPlan()
  315. {
  316. if (PlanItems.Count < 1) return;
  317. }
  318. public ICommand RefreshCommand => new RelayCommand(Refresh);
  319. private unsafe void Refresh()
  320. {
  321. if (SpectrumItems.Count < 3) return;
  322. var items = SpectrumItems.Select(x => x.Value.Model.CloneBase()).DistinctBy(x => x.Frequency).OrderBy(x => x.Frequency).ToList();
  323. SpectrumItems.Clear();
  324. for(int i=0;i<items.Count;i++)
  325. {
  326. SpectrumItems.Add(new IndexValueItemViewModel<RandomSpectrumItemViewModel>(i + 1, new RandomSpectrumItemViewModel(items[i])));
  327. }
  328. if (items.Count < 3) return;
  329. 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);
  330. if (fy_array.Length == 0) return;
  331. this.Model.SpectralTables.Clear();
  332. for(int i=0;i<turningfreq.Length;i++)
  333. {
  334. this.Model.SpectralTables.Add(new RandomSpectralTableModel()
  335. {
  336. TurningFrequency = turningfreq[i],
  337. TurningValue = fy_array[(int)((turningfreq[i]-SpectrumItems[0].Value.Frequency)/FrequencyResolution)],
  338. });
  339. }
  340. RMSAcceleration = Math.Sqrt(ViewModels.MainViewModel.Default.Calc.Sum.Sum(ref fy_array[0], (uint)fy_array.Length) * FrequencyResolution);
  341. ResetRMS = RMSAcceleration;
  342. double[] f2 = new double[f.Length];
  343. double[] fv = new double[f.Length];
  344. MainViewModel.Default.Calc.Multiply.Multiply(ref f[0], ref f[0], (uint)f.Length,ref f2[0]);
  345. MainViewModel.Default.Calc.Division.Division(ref fy_array[0],ref f2[0], (uint)f.Length,ref fv[0]);
  346. double sum = MainViewModel.Default.Calc.Sum.Sum(ref fv[0], (uint)fv.Length);
  347. MaxVelocity = Math.Sqrt((sum - (fv[0] + fv[^1] / 2) / 2) * FrequencyResolution) / (2 * Math.PI) * Sigma * 9.8f;
  348. MainViewModel.Default.Calc.Division.Division(ref fv[0], ref f2[0], (uint)f.Length, ref fv[0]);
  349. sum = MainViewModel.Default.Calc.Sum.Sum(ref fv[0], (uint)fv.Length);
  350. MaxDisplacement = Math.Sqrt((sum - (fv[0] + fv[^1] / 2) / 2) * FrequencyResolution) / (2 * Math.PI*2*Math.PI) * Sigma * 9800f;
  351. OnPropertyChanged(nameof(MaxAcceleration));
  352. double[,] spectrumdata = new double[5, f.Length];
  353. 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));
  354. CalcSpectrum(items, f, fy_array, FrequencyResolution, turningfreq, ref spectrumdata);
  355. datas.Clear();
  356. velocitydata.Clear();
  357. displacementdata.Clear();
  358. for (int i=0;i<f.Length;i++)
  359. {
  360. 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]));
  361. velocitydata.Add(new DataPoint(f[i], Shaker.Models.Tools.Tools.AccelerationToVelocity(spectrumdata[0, i], f[i])));
  362. displacementdata.Add(new DataPoint(f[i], Shaker.Models.Tools.Tools.AccelerationToDisplacement(spectrumdata[0, i], f[i])));
  363. }
  364. AccelerationModel.InvalidatePlot(false);
  365. VelocityModel.InvalidatePlot(false);
  366. DisplacementModel.InvalidatePlot(false);
  367. for (int i = 0; i < lineSeries.Count; i++)
  368. {
  369. lineSeries[i].ItemsSource = datas;
  370. }
  371. velocity.ItemsSource = velocitydata;
  372. displacement.ItemsSource = displacementdata;
  373. AccelerationModel.InvalidatePlot(true);
  374. VelocityModel.InvalidatePlot(true);
  375. DisplacementModel.InvalidatePlot(true);
  376. CanResetRMS = true;
  377. }
  378. private void CalcSpectrum(List<RandomSpectrumItemModel> models, [In] double[] f, [In] double[] fy_array, [In] double deltaf, [In] double[] turn_freq,ref double[,] spectrumdata)
  379. {
  380. CalcItemSpectrum(models.Select(x => x.UpStop).ToArray(), f, fy_array, deltaf, turn_freq, ref spectrumdata[1, 0]);
  381. CalcItemSpectrum(models.Select(x => x.UpWarn).ToArray(), f, fy_array, deltaf, turn_freq, ref spectrumdata[2, 0]);
  382. CalcItemSpectrum(models.Select(x => x.DownStop).ToArray(), f, fy_array, deltaf, turn_freq, ref spectrumdata[3, 0]);
  383. CalcItemSpectrum(models.Select(x => x.DownWarn).ToArray(), f, fy_array, deltaf, turn_freq, ref spectrumdata[4, 0]);
  384. }
  385. private void CalcItemSpectrum(double[] offset, [In] double[] f, [In] double[] fy_array, [In] double deltaf, [In] double[] turn_freq, ref double spectrumdata)
  386. {
  387. for (int i = 0; i < offset.Length - 1; i++)
  388. {
  389. int index = (int)((turn_freq[i] - turn_freq[0]) / deltaf);
  390. uint len = (uint)((turn_freq[i + 1] - turn_freq[i]) / deltaf);
  391. if (offset[i+1] == offset[i])
  392. {
  393. var x = Math.Pow(10, offset[i] / 10);
  394. MainViewModel.Default.Calc.Multiply.Multiply(ref fy_array[index], x, len, ref Unsafe.Add(ref spectrumdata,index));
  395. }
  396. else
  397. {
  398. double db1_psd = Math.Pow(10, offset[i] / 10) * fy_array[index];
  399. double db2_psd = Math.Pow(10, offset[i + 1] / 10) * fy_array[index+len];
  400. var x= Math.Log10(db2_psd / db1_psd) / (Math.Log2(turn_freq[i + 1] / turn_freq[i])) * 10;
  401. for(int j=0;j<len;j++)
  402. {
  403. ref double result = ref Unsafe.Add(ref spectrumdata, index + j);
  404. result = Math.Pow(10, Math.Log2(f[index + j] / turn_freq[i]) * x / 10) * db1_psd;
  405. }
  406. }
  407. }
  408. ref double temp = ref Unsafe.Add(ref spectrumdata, fy_array.Length - 1);
  409. temp = Math.Pow(10, offset[^1] / 10) * fy_array[^1];
  410. }
  411. private void CalcRefSpectrum(double[] f_array, double[] y_array, RandomValueType[] k_array, double deltaf ,out double[] fy_array,out double[] f,out double[] turningfreq)
  412. {
  413. int n=f_array.Length;
  414. // 计算圆整频率值及其对应的谱值
  415. double[] round_f = new double[n];
  416. double[] round_y = new double[n];
  417. turningfreq = new double[n];
  418. for (int i = 0; i < n; i++)
  419. {
  420. round_f[i] = Math.Ceiling(f_array[i] / deltaf) * deltaf;
  421. turningfreq[i] = round_f[i];
  422. }
  423. // 若第一个点值未知时,需先计算第一个点的值
  424. if (k_array[0] == RandomValueType.Slope && k_array[1] == RandomValueType.Slope)
  425. {
  426. y_array[0] = y_array[1];
  427. }
  428. if (k_array[0] == RandomValueType.Slope && k_array[1] == RandomValueType.Value)
  429. {
  430. k_array[0] = 0;
  431. y_array[0] = y_array[1];
  432. }
  433. if (k_array[0] == RandomValueType.Slope)
  434. {
  435. double kk = y_array[1];
  436. y_array[0] = y_array[2] / (Math.Pow(10, kk / 10 * Math.Log(f_array[1] / f_array[0], 2)));
  437. y_array[1] = y_array[2];
  438. k_array[0] = 0;
  439. k_array[1] = 0;
  440. }
  441. // 计算第二点及其后点的值
  442. for (int i = 1; i < n; i++)
  443. {
  444. if (f_array[i] == 0)
  445. {
  446. double kk = y_array[i];
  447. y_array[i] = y_array[i + 1];
  448. f_array[i] = Math.Pow(2, (10 * Math.Log(y_array[i + 1] / y_array[i - 1], 10) / kk)) * f_array[i - 1];
  449. }
  450. else
  451. {
  452. if (k_array[i] == RandomValueType.Slope)
  453. {
  454. y_array[i] = Math.Pow(10, y_array[i] / 10 * Math.Log(f_array[i] / f_array[i - 1], 2)) * y_array[i - 1];
  455. k_array[i] = 0;
  456. }
  457. }
  458. }
  459. // 验证结果值
  460. double[] k_exam = new double[n - 1];
  461. for (int i = 0; i < n; i++)
  462. {
  463. if (i < n - 1)
  464. {
  465. k_exam[i] = 10 * Math.Log(y_array[i + 1] / y_array[i], 10) / Math.Log(f_array[i + 1] / f_array[i], 2);
  466. }
  467. }
  468. // 计算圆整后频率对应的谱值
  469. for (int i = 0; i < n; i++)
  470. {
  471. if (round_f[i] == f_array[i])
  472. {
  473. round_y[i] = y_array[i];
  474. }
  475. if (i < n - 1)
  476. {
  477. if (round_f[i] > f_array[i])
  478. {
  479. round_y[i] = Math.Pow(10, k_exam[i] / 10 * Math.Log(round_f[i] / f_array[i], 2)) * y_array[i];
  480. }
  481. }
  482. if (i == n - 1)
  483. {
  484. round_y[i] = Math.Pow(10, k_exam[i - 1] / 10 * Math.Log(round_f[i] / f_array[i], 2)) * y_array[i];
  485. }
  486. if (round_f[i] < f_array[i])
  487. {
  488. round_y[i] = Math.Pow(10, k_exam[i - 1] / 10 * Math.Log(f_array[i] / round_f[i], 2)) * y_array[i];
  489. }
  490. }
  491. int parts = k_exam.Length; // 将谱按斜率分成几段,parts为总的段数\
  492. f = Enumerable.Range(0, (int)((round_f[n - 1] - round_f[0]) / deltaf) + 1).Select(x => x * deltaf + round_f[0]).ToArray();
  493. fy_array =new double[f.Length];
  494. fy_array[0] = round_y[0];
  495. fy_array[^1] = round_y[^1];
  496. int part_begin = 0;
  497. for (int i = 0; i < parts; i++)
  498. {
  499. fy_array[part_begin] = round_y[i];
  500. int n_part = (int)Math.Round((round_f[i + 1] - round_f[i]) / deltaf);
  501. if (k_exam[i] == 0)
  502. {
  503. for (int k = part_begin + 1; k <= part_begin + n_part; k++)
  504. {
  505. fy_array[k] = fy_array[k - 1];
  506. }
  507. }
  508. else
  509. {
  510. if (n_part > 0)
  511. {
  512. for (int k = part_begin + 1; k <= part_begin + n_part; k++)
  513. {
  514. fy_array[k] = Math.Pow(10, k_exam[i] / 10 * Math.Log(f[k] / f[k - 1], 2)) * fy_array[k - 1];
  515. }
  516. }
  517. }
  518. part_begin += n_part;
  519. }
  520. }
  521. protected override void Save()
  522. {
  523. base.Save();
  524. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<RandomConfigModel>()?.Publish(this, Model);
  525. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<RandomConfigModel>()?.Publish(this, Model);
  526. RandomMainPageViewModel.Instance.SetRefSpectrum(datas);
  527. }
  528. }
  529. }