1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Helpers;
using LiveCharts.Wpf;
using Misc;
namespace FLY.Thick.Base.UI.UiModule
{
/// <summary>
/// Page_FixAnalyze.xaml 的交互逻辑
/// </summary>
public partial class PgFixAnalyze : Page
{
PgFixAnalyzeVm viewModel;
public PgFixAnalyze()
{
InitializeComponent();
}
public void Init(double intervalms, List<double> datas)
{
viewModel = new PgFixAnalyzeVm();
viewModel.Init(intervalms, datas);
this.DataContext = viewModel;
}
private void UIElement_OnMouseMove(object sender, MouseEventArgs e)
{
var vm = viewModel;
var chart = (LiveCharts.Wpf.CartesianChart)sender;
//lets get where the mouse is at our chart
var mouseCoordinate = e.GetPosition(chart);
//now that we know where the mouse is, lets use
//ConverToChartValues extension
//it takes a point in pixes and scales it to our chart current scale/values
var p = chart.ConvertToChartValues(mouseCoordinate);
//in the Y section, lets use the raw value
vm.YPointer = p.Y;
vm.XPointer = p.X;
}
private void ButtonTestClick(object sender, RoutedEventArgs e)
{
viewModel.Test();
}
private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e)
{
var vm = viewModel;
var chart = (LiveCharts.Wpf.CartesianChart)sender;
//lets get where the mouse is at our chart
var mouseCoordinate = e.GetPosition(chart);
//now that we know where the mouse is, lets use
//ConverToChartValues extension
//it takes a point in pixes and scales it to our chart current scale/values
var p = chart.ConvertToChartValues(mouseCoordinate);
//in the Y section, lets use the raw value
vm.YPointer = p.Y;
vm.XPointer = p.X;
}
}
public class PgFixAnalyzeVm : INotifyPropertyChanged
{
/// <summary>
/// X轴拖拉条 最大值
/// </summary>
public double XRangeSliderMax { get; set; } = 10000;
/// <summary>
/// X轴拖拉条 最小值
/// </summary>
public double XRangeSliderMin { get; set; } = 0;
/// <summary>
/// X轴 最大值
/// </summary>
public double XMax { get; set; } = 1000;
/// <summary>
/// X轴 最小值
/// </summary>
public double XMin { get; set; }
public double LowerValue { get; set; }
public double UpperValue { get; set; }
public double YMax { get; set; } = double.NaN;
public double YMin { get; set; } = double.NaN;
public double YPointer { get; set; } = 3;
public double XPointer { get; set; } = 3;
public ChartValues<double> Values { get; private set; } = new ChartValues<double>();
public Func<double, string> XFormatter { get; private set; }
DispatcherTimer timer;
public void Test()
{
List<double> datas = new List<double>();
double intervalms = 1.28;
for (int i = 0; i < 10000; i++)
{
double sum = 0;
sum += getd(20, intervalms, i);
sum += getd(100, intervalms, i);
sum += getd(50, intervalms, i);
datas.Add(sum);
}
Init(intervalms, datas);
}
double getd(double hz, double ms, int i)
{
double cnt = 1000 / ms / hz;
return Math.Sin(Math.PI * 2 * i / cnt);
}
public PgFixAnalyzeVm()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
this.PropertyChanged += PgFixAnalyzeVm_PropertyChanged;
}
private void PgFixAnalyzeVm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(UpperValue)
|| e.PropertyName == nameof(LowerValue)) {
if (!timer.IsEnabled)
timer.Start();
}
}
private void Timer_Tick(object sender, EventArgs e)
{
timer.Stop();
if (LowerValue < UpperValue) {
XMax = UpperValue;
XMin = LowerValue;
}
}
List<double> Magnitudes = new List<double>();
public double MinFs { get; private set; } = 0.1;
public void Init(double intervalms, List<double> datas)
{
MinFs = (1000 / intervalms) / datas.Count();
XFormatter = (d) =>
{
int n = (int)d;
return ((n) * MinFs).ToString("F2");
};
Values.Clear();
//
System.Numerics.Complex[] complexs = new System.Numerics.Complex[datas.Count()];
for (int i = 0; i < datas.Count(); i++)
{
complexs[i] = new System.Numerics.Complex(datas[i], 0);
}
MathNet.Numerics.IntegralTransforms.Fourier.Forward(complexs);
complexs[0] = new System.Numerics.Complex(0, 0);
Magnitudes.Clear();
Magnitudes.AddRange(complexs.Take(complexs.Count() / 2).Select(c=>c.Magnitude));
Values.AddRange(Magnitudes);
XRangeSliderMax = Values.Count();
XRangeSliderMin = 0;
UpperValue = XRangeSliderMax;
LowerValue = 0;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void ToPropertyChanged(string propertyName) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}