【发布时间】:2016-12-07 17:32:56
【问题描述】:
我正在尝试使用 Math.Net,特别是 FFT 部分。我正在尝试从纯正弦波中提取频域信息。代码如下:
private void Form1_Load(object sender, EventArgs e)
{
//Set up the wave and derive some useful info
Double WaveFreq = 500;
Double WavePeriod = 1 / WaveFreq;
Double SampleFreq = 20000;
Double SampleTime = (1 / SampleFreq);
//Generate the wave using the above parameters
var points = Generate.Sinusoidal(100000, SampleFreq, WaveFreq, 1);
//Array to hold our complex numbers
var data = new Complex[points.Length];
//Set up the series to display our raw wave
Series WaveSeries = new Series("Waveform");
WaveSeries.ChartType = SeriesChartType.Line;
//Creat the series for displaying the FFT
Series FFTSeries = new Series("FFT Test");
FFTSeries.ChartType = SeriesChartType.Column;
//Populate both the wave series and the data array
for (int i = 0; i < points.Length; i++)
{
Double x = SampleTime * i;
WaveSeries.Points.AddXY(x, points[i]);
data[i] = new Complex(x, points[i]);
}
//Create the window to evaluate (using a window 5 times wider than the wavelength of the lowest ferequency being measured)
int WindowWidth = (int)Math.Round((1 / WaveFreq) / (1 / SampleFreq) * 5 + 0.5f);
var HannWindow = Window.HannPeriodic(WindowWidth);
var window = new Complex[WindowWidth];
for(int i = 0; i < WindowWidth; i++)
{
var y = data[i].Imaginary * HannWindow[i];
window[i] = new Complex(data[i].Real, y);
}
//Perform the FFT
Fourier.Forward(window);
//Add the calculated FFT to our FFTSeries
foreach(Complex sample in window)
{
FFTSeries.Points.AddXY(sample.Phase, sample.Magnitude);
}
chart2.Series.Add(WaveSeries);
chart2.ChartAreas[0].AxisX.Minimum = 0;
chart2.ChartAreas[0].AxisX.Maximum = .01;
chart2.ChartAreas[0].AxisY.Minimum = -2;
chart2.ChartAreas[0].AxisY.Maximum = 2;
chart1.Series.Add(FFTSeries);
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 1000;
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 5;
}
如您所见,我正在生成频率为 500Hz 的正弦波,以 20kHz 采样并生成 10k 个样本。
FFT 完全没有显示(除了 0Hz 附近的 1.8 峰值)!我怀疑这可能是窗口错误,但对于我的生活,我看不到它是什么。
【问题讨论】:
-
当我尝试复制这个时,我找不到 Window.HannPeriodic 函数。它在 MathNet 文档中,但我只能在切换到仅使用 Window.Hann 时进行编译。我错过了什么吗?
-
@KelsonBall 在 3.14.0-beta3 版本中。