回答編集履歴

3

見直しキャンペーン中

2023/07/23 07:46

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,241 +1,121 @@
1
1
  注意)はっきり言って私は計算について何一つわかっていません^^;
2
-
3
-
4
2
 
5
3
  `windowsize`を取った後で`_recorded2.Add(sample);`としてしまっては、数が合わなくなるので当然`IndexOutOfRangeException`が出るでしょう。
6
4
 
7
-
8
-
9
5
  おそらく`ProcessSample2`の部分は、`ProcessSample`の中でやる処理だったんだと思います(何度も言いますが計算について何一つわかりません。参考ページのコード片から推測しただけです)
10
-
11
6
  ~~参考ページで全文を出してくれていればいいのですが^^;~~
12
-
13
-
14
7
 
15
8
  こうしたところ何かしら表示されましたが、本当にこれでいいのかはわかりませんし質問されても答えられません^^;
16
9
 
17
-
18
-
19
- ```C#
10
+ ```cs
20
-
21
11
  using System;
22
-
23
12
  using System.Collections.Generic;
24
-
25
13
  using System.Data;
26
-
27
14
  using System.Linq;
28
-
29
15
  using System.Numerics;
30
-
31
16
  using System.Windows.Forms;
32
-
33
17
  using MathNet.Numerics;
34
-
35
18
  using MathNet.Numerics.IntegralTransforms;
36
-
37
19
  using NAudio.Wave;
38
-
39
20
  using OxyPlot;
40
-
41
21
  using OxyPlot.Axes;
42
-
43
22
  using OxyPlot.Series;
44
23
 
45
-
46
-
47
24
  namespace Questions296462
48
-
49
25
  {
50
-
51
26
  public partial class Form1 : Form
52
-
53
27
  {
54
-
55
28
  private readonly LineSeries _lineSeries1 = new LineSeries();
56
-
57
29
  private readonly LineSeries _lineSeries2 = new LineSeries();
58
-
59
30
  private readonly List<float> _recorded = new List<float>();
60
-
61
31
  private const int deviceNumber = 0;
62
-
63
-
64
32
 
65
33
  public Form1() => InitializeComponent();
66
34
 
67
-
68
-
69
35
  private void Form1_Load(object sender, EventArgs e)
70
-
71
36
  {
72
-
73
37
  FormBorderStyle = FormBorderStyle.FixedSingle;
74
38
 
75
-
76
-
77
39
  var info = WaveIn.GetCapabilities(deviceNumber);
78
-
79
40
  micname.Text = $"Device {deviceNumber}: {info.ProductName}, {info.Channels} channels";
80
41
 
81
-
82
-
83
42
  InitPlot();
84
-
85
43
  }
86
44
 
87
-
88
-
89
45
  private void InitPlot()
90
-
91
46
  {
92
-
93
47
  var model = new PlotModel();
94
-
95
48
  model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, });
96
-
97
49
  model.Axes.Add(new LinearAxis { Minimum = -1.0, Maximum = 1.0, Position = AxisPosition.Left, });
98
-
99
50
  model.Series.Add(_lineSeries1);
100
-
101
51
  plotView1.Model = model;
102
52
 
103
-
104
-
105
53
  model = new PlotModel();
106
-
107
54
  model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, });
108
-
109
55
  model.Axes.Add(new LinearAxis { Minimum = 0, Maximum = 5.0, Position = AxisPosition.Left, });
110
-
111
56
  model.Series.Add(_lineSeries2);
112
-
113
57
  plotView2.Model = model;
114
-
115
58
  }
116
59
 
117
-
118
-
119
60
  private void recording_Click(object sender, EventArgs e)
120
-
121
61
  {
122
-
123
62
  var waveIn = new WaveIn
124
-
125
63
  {
126
-
127
64
  DeviceNumber = deviceNumber,
128
-
129
65
  WaveFormat = new WaveFormat(sampleRate: 8000, channels: 1),
130
-
131
66
  };
132
-
133
67
  waveIn.DataAvailable += WaveIn_DataAvailable;
134
-
135
68
  waveIn.StartRecording();
136
-
137
69
  }
138
-
139
-
140
70
 
141
71
  private void finish_Click(object sender, EventArgs e) => Application.Exit();
142
72
 
143
-
144
-
145
73
  private void WaveIn_DataAvailable(object sender, WaveInEventArgs e)
146
-
147
74
  {
148
-
149
75
  for(var index = 0; index < e.BytesRecorded; index += 2)
150
-
151
76
  {
152
-
153
77
  var sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]);
154
-
155
78
  var sample32 = sample / 32768f;
156
-
157
79
  ProcessSample(sample32);
158
-
159
80
  }
160
-
161
81
  }
162
82
 
163
-
164
-
165
83
  private void ProcessSample(float sample)
166
-
167
84
  {
168
-
169
85
  _recorded.Add(sample);
170
86
 
171
-
172
-
173
87
  if(_recorded.Count == 1024)
174
-
175
88
  {
176
-
177
89
  // plotView1
178
-
179
90
  var points = _recorded.Select((v, i) => new DataPoint(i, v));
180
-
181
91
  _lineSeries1.Points.Clear();
182
-
183
92
  _lineSeries1.Points.AddRange(points);
184
-
185
93
  plotView1.InvalidatePlot(true);
186
94
 
187
-
188
-
189
95
  // plotView2
190
-
191
96
  var windowsize = _recorded.Count;
192
-
193
97
  var window = Window.Hamming(windowsize);
194
-
195
98
  var complexData = _recorded.Select((v, i) => v * (float)window[i])
196
-
197
99
  .Select(v => new Complex(v, 0.0)).ToArray();
198
-
199
100
  Fourier.Forward(complexData, FourierOptions.Matlab);
200
101
 
201
-
202
-
203
102
  var s = windowsize * (1.0 / 8000.0);
204
-
205
103
  var point = complexData.Take(complexData.Count() / 2)
206
-
207
104
  .Select((v, i) => new DataPoint(i / s, Math.Sqrt(v.Real * v.Real + v.Imaginary * v.Imaginary)))
208
-
209
105
  .ToList();
210
106
 
211
-
212
-
213
107
  _lineSeries2.Points.Clear();
214
-
215
108
  _lineSeries2.Points.AddRange(point);
216
-
217
109
  plotView2.InvalidatePlot(true);
218
110
 
219
-
220
-
221
111
  // Hz
222
-
223
112
  var max = point.Aggregate((r, n) => r.Y > n.Y ? r : n);
224
-
225
113
  label1.Text = $"{point.IndexOf(max) / s:f0} Hz";
226
114
 
227
-
228
-
229
115
  _recorded.Clear();
230
-
231
116
  }
232
-
233
117
  }
234
-
235
118
  }
236
-
237
119
  }
238
-
239
120
  ```
240
-
241
121
  ![アプリ画像](ab77743a6d1c54a8809f066e06c378af.png)

2

296462

2020/10/08 08:45

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
 
46
46
 
47
- namespace Questions42564
47
+ namespace Questions296462
48
48
 
49
49
  {
50
50
 

1

Hz

2020/10/08 08:45

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -218,6 +218,8 @@
218
218
 
219
219
 
220
220
 
221
+ // Hz
222
+
221
223
  var max = point.Aggregate((r, n) => r.Y > n.Y ? r : n);
222
224
 
223
225
  label1.Text = $"{point.IndexOf(max) / s:f0} Hz";