回答編集履歴
3
見直しキャンペーン中
answer
CHANGED
@@ -1,121 +1,121 @@
|
|
1
|
-
注意)はっきり言って私は計算について何一つわかっていません^^;
|
2
|
-
|
3
|
-
`windowsize`を取った後で`_recorded2.Add(sample);`としてしまっては、数が合わなくなるので当然`IndexOutOfRangeException`が出るでしょう。
|
4
|
-
|
5
|
-
おそらく`ProcessSample2`の部分は、`ProcessSample`の中でやる処理だったんだと思います(何度も言いますが計算について何一つわかりません。参考ページのコード片から推測しただけです)
|
6
|
-
~~参考ページで全文を出してくれていればいいのですが^^;~~
|
7
|
-
|
8
|
-
こうしたところ何かしら表示されましたが、本当にこれでいいのかはわかりませんし質問されても答えられません^^;
|
9
|
-
|
10
|
-
```
|
11
|
-
using System;
|
12
|
-
using System.Collections.Generic;
|
13
|
-
using System.Data;
|
14
|
-
using System.Linq;
|
15
|
-
using System.Numerics;
|
16
|
-
using System.Windows.Forms;
|
17
|
-
using MathNet.Numerics;
|
18
|
-
using MathNet.Numerics.IntegralTransforms;
|
19
|
-
using NAudio.Wave;
|
20
|
-
using OxyPlot;
|
21
|
-
using OxyPlot.Axes;
|
22
|
-
using OxyPlot.Series;
|
23
|
-
|
24
|
-
namespace Questions296462
|
25
|
-
{
|
26
|
-
public partial class Form1 : Form
|
27
|
-
{
|
28
|
-
private readonly LineSeries _lineSeries1 = new LineSeries();
|
29
|
-
private readonly LineSeries _lineSeries2 = new LineSeries();
|
30
|
-
private readonly List<float> _recorded = new List<float>();
|
31
|
-
private const int deviceNumber = 0;
|
32
|
-
|
33
|
-
public Form1() => InitializeComponent();
|
34
|
-
|
35
|
-
private void Form1_Load(object sender, EventArgs e)
|
36
|
-
{
|
37
|
-
FormBorderStyle = FormBorderStyle.FixedSingle;
|
38
|
-
|
39
|
-
var info = WaveIn.GetCapabilities(deviceNumber);
|
40
|
-
micname.Text = $"Device {deviceNumber}: {info.ProductName}, {info.Channels} channels";
|
41
|
-
|
42
|
-
InitPlot();
|
43
|
-
}
|
44
|
-
|
45
|
-
private void InitPlot()
|
46
|
-
{
|
47
|
-
var model = new PlotModel();
|
48
|
-
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, });
|
49
|
-
model.Axes.Add(new LinearAxis { Minimum = -1.0, Maximum = 1.0, Position = AxisPosition.Left, });
|
50
|
-
model.Series.Add(_lineSeries1);
|
51
|
-
plotView1.Model = model;
|
52
|
-
|
53
|
-
model = new PlotModel();
|
54
|
-
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, });
|
55
|
-
model.Axes.Add(new LinearAxis { Minimum = 0, Maximum = 5.0, Position = AxisPosition.Left, });
|
56
|
-
model.Series.Add(_lineSeries2);
|
57
|
-
plotView2.Model = model;
|
58
|
-
}
|
59
|
-
|
60
|
-
private void recording_Click(object sender, EventArgs e)
|
61
|
-
{
|
62
|
-
var waveIn = new WaveIn
|
63
|
-
{
|
64
|
-
DeviceNumber = deviceNumber,
|
65
|
-
WaveFormat = new WaveFormat(sampleRate: 8000, channels: 1),
|
66
|
-
};
|
67
|
-
waveIn.DataAvailable += WaveIn_DataAvailable;
|
68
|
-
waveIn.StartRecording();
|
69
|
-
}
|
70
|
-
|
71
|
-
private void finish_Click(object sender, EventArgs e) => Application.Exit();
|
72
|
-
|
73
|
-
private void WaveIn_DataAvailable(object sender, WaveInEventArgs e)
|
74
|
-
{
|
75
|
-
for(var index = 0; index < e.BytesRecorded; index += 2)
|
76
|
-
{
|
77
|
-
var sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]);
|
78
|
-
var sample32 = sample / 32768f;
|
79
|
-
ProcessSample(sample32);
|
80
|
-
}
|
81
|
-
}
|
82
|
-
|
83
|
-
private void ProcessSample(float sample)
|
84
|
-
{
|
85
|
-
_recorded.Add(sample);
|
86
|
-
|
87
|
-
if(_recorded.Count == 1024)
|
88
|
-
{
|
89
|
-
// plotView1
|
90
|
-
var points = _recorded.Select((v, i) => new DataPoint(i, v));
|
91
|
-
_lineSeries1.Points.Clear();
|
92
|
-
_lineSeries1.Points.AddRange(points);
|
93
|
-
plotView1.InvalidatePlot(true);
|
94
|
-
|
95
|
-
// plotView2
|
96
|
-
var windowsize = _recorded.Count;
|
97
|
-
var window = Window.Hamming(windowsize);
|
98
|
-
var complexData = _recorded.Select((v, i) => v * (float)window[i])
|
99
|
-
.Select(v => new Complex(v, 0.0)).ToArray();
|
100
|
-
Fourier.Forward(complexData, FourierOptions.Matlab);
|
101
|
-
|
102
|
-
var s = windowsize * (1.0 / 8000.0);
|
103
|
-
var point = complexData.Take(complexData.Count() / 2)
|
104
|
-
.Select((v, i) => new DataPoint(i / s, Math.Sqrt(v.Real * v.Real + v.Imaginary * v.Imaginary)))
|
105
|
-
.ToList();
|
106
|
-
|
107
|
-
_lineSeries2.Points.Clear();
|
108
|
-
_lineSeries2.Points.AddRange(point);
|
109
|
-
plotView2.InvalidatePlot(true);
|
110
|
-
|
111
|
-
// Hz
|
112
|
-
var max = point.Aggregate((r, n) => r.Y > n.Y ? r : n);
|
113
|
-
label1.Text = $"{point.IndexOf(max) / s:f0} Hz";
|
114
|
-
|
115
|
-
_recorded.Clear();
|
116
|
-
}
|
117
|
-
}
|
118
|
-
}
|
119
|
-
}
|
120
|
-
```
|
1
|
+
注意)はっきり言って私は計算について何一つわかっていません^^;
|
2
|
+
|
3
|
+
`windowsize`を取った後で`_recorded2.Add(sample);`としてしまっては、数が合わなくなるので当然`IndexOutOfRangeException`が出るでしょう。
|
4
|
+
|
5
|
+
おそらく`ProcessSample2`の部分は、`ProcessSample`の中でやる処理だったんだと思います(何度も言いますが計算について何一つわかりません。参考ページのコード片から推測しただけです)
|
6
|
+
~~参考ページで全文を出してくれていればいいのですが^^;~~
|
7
|
+
|
8
|
+
こうしたところ何かしら表示されましたが、本当にこれでいいのかはわかりませんし質問されても答えられません^^;
|
9
|
+
|
10
|
+
```cs
|
11
|
+
using System;
|
12
|
+
using System.Collections.Generic;
|
13
|
+
using System.Data;
|
14
|
+
using System.Linq;
|
15
|
+
using System.Numerics;
|
16
|
+
using System.Windows.Forms;
|
17
|
+
using MathNet.Numerics;
|
18
|
+
using MathNet.Numerics.IntegralTransforms;
|
19
|
+
using NAudio.Wave;
|
20
|
+
using OxyPlot;
|
21
|
+
using OxyPlot.Axes;
|
22
|
+
using OxyPlot.Series;
|
23
|
+
|
24
|
+
namespace Questions296462
|
25
|
+
{
|
26
|
+
public partial class Form1 : Form
|
27
|
+
{
|
28
|
+
private readonly LineSeries _lineSeries1 = new LineSeries();
|
29
|
+
private readonly LineSeries _lineSeries2 = new LineSeries();
|
30
|
+
private readonly List<float> _recorded = new List<float>();
|
31
|
+
private const int deviceNumber = 0;
|
32
|
+
|
33
|
+
public Form1() => InitializeComponent();
|
34
|
+
|
35
|
+
private void Form1_Load(object sender, EventArgs e)
|
36
|
+
{
|
37
|
+
FormBorderStyle = FormBorderStyle.FixedSingle;
|
38
|
+
|
39
|
+
var info = WaveIn.GetCapabilities(deviceNumber);
|
40
|
+
micname.Text = $"Device {deviceNumber}: {info.ProductName}, {info.Channels} channels";
|
41
|
+
|
42
|
+
InitPlot();
|
43
|
+
}
|
44
|
+
|
45
|
+
private void InitPlot()
|
46
|
+
{
|
47
|
+
var model = new PlotModel();
|
48
|
+
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, });
|
49
|
+
model.Axes.Add(new LinearAxis { Minimum = -1.0, Maximum = 1.0, Position = AxisPosition.Left, });
|
50
|
+
model.Series.Add(_lineSeries1);
|
51
|
+
plotView1.Model = model;
|
52
|
+
|
53
|
+
model = new PlotModel();
|
54
|
+
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, });
|
55
|
+
model.Axes.Add(new LinearAxis { Minimum = 0, Maximum = 5.0, Position = AxisPosition.Left, });
|
56
|
+
model.Series.Add(_lineSeries2);
|
57
|
+
plotView2.Model = model;
|
58
|
+
}
|
59
|
+
|
60
|
+
private void recording_Click(object sender, EventArgs e)
|
61
|
+
{
|
62
|
+
var waveIn = new WaveIn
|
63
|
+
{
|
64
|
+
DeviceNumber = deviceNumber,
|
65
|
+
WaveFormat = new WaveFormat(sampleRate: 8000, channels: 1),
|
66
|
+
};
|
67
|
+
waveIn.DataAvailable += WaveIn_DataAvailable;
|
68
|
+
waveIn.StartRecording();
|
69
|
+
}
|
70
|
+
|
71
|
+
private void finish_Click(object sender, EventArgs e) => Application.Exit();
|
72
|
+
|
73
|
+
private void WaveIn_DataAvailable(object sender, WaveInEventArgs e)
|
74
|
+
{
|
75
|
+
for(var index = 0; index < e.BytesRecorded; index += 2)
|
76
|
+
{
|
77
|
+
var sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]);
|
78
|
+
var sample32 = sample / 32768f;
|
79
|
+
ProcessSample(sample32);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
private void ProcessSample(float sample)
|
84
|
+
{
|
85
|
+
_recorded.Add(sample);
|
86
|
+
|
87
|
+
if(_recorded.Count == 1024)
|
88
|
+
{
|
89
|
+
// plotView1
|
90
|
+
var points = _recorded.Select((v, i) => new DataPoint(i, v));
|
91
|
+
_lineSeries1.Points.Clear();
|
92
|
+
_lineSeries1.Points.AddRange(points);
|
93
|
+
plotView1.InvalidatePlot(true);
|
94
|
+
|
95
|
+
// plotView2
|
96
|
+
var windowsize = _recorded.Count;
|
97
|
+
var window = Window.Hamming(windowsize);
|
98
|
+
var complexData = _recorded.Select((v, i) => v * (float)window[i])
|
99
|
+
.Select(v => new Complex(v, 0.0)).ToArray();
|
100
|
+
Fourier.Forward(complexData, FourierOptions.Matlab);
|
101
|
+
|
102
|
+
var s = windowsize * (1.0 / 8000.0);
|
103
|
+
var point = complexData.Take(complexData.Count() / 2)
|
104
|
+
.Select((v, i) => new DataPoint(i / s, Math.Sqrt(v.Real * v.Real + v.Imaginary * v.Imaginary)))
|
105
|
+
.ToList();
|
106
|
+
|
107
|
+
_lineSeries2.Points.Clear();
|
108
|
+
_lineSeries2.Points.AddRange(point);
|
109
|
+
plotView2.InvalidatePlot(true);
|
110
|
+
|
111
|
+
// Hz
|
112
|
+
var max = point.Aggregate((r, n) => r.Y > n.Y ? r : n);
|
113
|
+
label1.Text = $"{point.IndexOf(max) / s:f0} Hz";
|
114
|
+
|
115
|
+
_recorded.Clear();
|
116
|
+
}
|
117
|
+
}
|
118
|
+
}
|
119
|
+
}
|
120
|
+
```
|
121
121
|

|
2
296462
answer
CHANGED
@@ -21,7 +21,7 @@
|
|
21
21
|
using OxyPlot.Axes;
|
22
22
|
using OxyPlot.Series;
|
23
23
|
|
24
|
-
namespace
|
24
|
+
namespace Questions296462
|
25
25
|
{
|
26
26
|
public partial class Form1 : Form
|
27
27
|
{
|
1
Hz
answer
CHANGED
@@ -108,6 +108,7 @@
|
|
108
108
|
_lineSeries2.Points.AddRange(point);
|
109
109
|
plotView2.InvalidatePlot(true);
|
110
110
|
|
111
|
+
// Hz
|
111
112
|
var max = point.Aggregate((r, n) => r.Y > n.Y ? r : n);
|
112
113
|
label1.Text = $"{point.IndexOf(max) / s:f0} Hz";
|
113
114
|
|