private void ProcessSample(float sample) { var windowsize = size; _recorded.Add(sample); if (_recorded.Count == windowsize) { #region add double[] dftIn = new double[size]; double[] dftInIm = new double[size]; DataPoint[] DftIn = new DataPoint[size]; DataPoint[] DFTResult = new DataPoint[size]; DataPoint[] FFTResult = new DataPoint[size]; double[] data = new double[size]; for (int i = 0; i < size; i++) { dftInIm[i] = 0.0; } var window = MathNet.Numerics.Window.Hamming(windowsize); _recorded = _recorded.Select((v, i) => (double)v * window[i]).ToList(); dftIn = _recorded.Take(size).ToArray(); // FFT FFT(dftIn, dftInIm, out reFFT, out imFFT, (int)Math.Log(size, 2)); // 波形显示 for (int i = 0; i < size / 2; i++) { if (i > 0) { float a = ((float)fs / size); float x = (float)i * a; double y = Math.Sqrt(reFFT[i] * reFFT[i] + imFFT[i] * imFFT[i]); FFTResult[i] = new DataPoint(x, y); } } line1.ItemsSource = FFTResult.Take((FFTResult.Count() / 20)); #endregion _recorded.Clear(); } }
private void ProcessSample(float sample) { var windowsize = size; _recorded.Add(sample); if (_recorded.Count == windowsize) { #region add // DFT用データ double[] dftIn = new double[size]; double[] dftInIm = new double[size]; DataPoint[] DftIn = new DataPoint[size]; DataPoint[] DFTResult = new DataPoint[size]; DataPoint[] FFTResult = new DataPoint[size]; // 窓関数後データ double[] data = new double[size]; // 波形生成 for (int i = 0; i < size; i++) { dftInIm[i] = 0.0; } var window = MathNet.Numerics.Window.Hamming(windowsize); _recorded = _recorded.Select((v, i) => (double)v * window[i]).ToList(); dftIn = _recorded.Take(size).ToArray(); // DFT //DFT(size, dftIn, out re, out im); // FFT FFT(dftIn, dftInIm, out reFFT, out imFFT, (int)Math.Log(size, 2)); // DFT波形表示 for (int i = 0; i < size / 2; i++) { if (i > 0) { //FFTの横軸の周波数の単位、分解能についてはここを見る //URL http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1350561093 //FFTの分解能(ひと間隔)はfs/sizeでももとまる。また、横軸の最大値は、サンプリング周波数の半分(サンプリング定理)となる。 //DFTResult[i] = new DataPoint((double)i, Math.Sqrt(re[i] * re[i] + im[i] * im[i])); float a = ((float)fs / size); float x = (float)i * a; double y = Math.Sqrt(reFFT[i] * reFFT[i] + imFFT[i] * imFFT[i]); FFTResult[i] = new DataPoint(x, y); } } //line1.ItemsSource = dftIn; line2.ItemsSource = FFTResult.Take((FFTResult.Count() / 20)); //line1.ItemsSource = DFTResult; #endregion //var points = _recorded.Select((v, index) => // new DataPoint((double)index, v) // ).ToList(); //var window = MathNet.Numerics.Window.Hamming(windowsize); //_recorded = _recorded.Select((v, i) => (double)v * window[i]).ToList(); //double[] Fourierdata = _recorded.Select(v => (v)).ToArray(); //Fourier.ForwardReal(Fourierdata, Fourierdata.Length - 2, FourierOptions.Matlab); ; //var s = windowsize * (1.0 / 8000.0); ////var point = Fourierdata.Take(Fourierdata.Count()).Select((v, index) => //// new DataPoint((double)index, v) ////).ToList(); //var point = Fourierdata.Take(Fourierdata.Count() / 2).Select((v, index) =>new DataPoint((double)index / s,v)).ToList(); //_lineSeries.Points.Clear(); //_lineSeries.Points.AddRange(points); //line1.ItemsSource = points; //line2.ItemsSource = point; _recorded.Clear(); } }