昨日の続き。
「C#によるデジタル信号処理プログラミング」という著書にあるFFTで実行した結果。
NAudio.DSPにあるFFTよりノイズが少ないみたい。
こっちを使うことにする。 ちなみに肝心のFFT処理の部分は、上記本に記載されているコードそのものなのでここでは公開しない。
その他のソース
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 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using NAudio.Wave; using System.Numerics; namespace RealFFT { public partial class Form1 : Form { public Form1() { InitializeComponent(); } WaveIn wavein; FftReal myFFT; double[] capdata0, capdata1, wHm, dBdata; bool toggle,dataready; int pb_height, pb_width; float xtick, ytick, offset, fstep; private void Form1_Load(object sender, EventArgs e) { wavein = new WaveIn(); myFFT = new FftReal(8192); wavein.DataAvailable += wavein_DataAvailable; wavein.BufferMilliseconds = 180; //180ms wavein.WaveFormat = new WaveFormat(45511, 2); //data siza 45511 * 180ms = 8191サンプル wavein.StartRecording(); wHm = getHammingWnd(); //Hamming窓生成 toggle = true; dataready = false; pb_height = pictureBox1.Height - 1; pb_width = pictureBox1.Width - 1; xtick = pb_width / (float)(Math.Log10(22756) - Math.Log10(50)); //左端 50HZ、右端 22756Hz ytick = pb_height / 10f; //相対値 offset = (float)Math.Log10(50); fstep = 45511f / 8192f; capdata0 = new double[8192]; capdata1 = new double[8192]; dBdata = new double[4096]; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { dataready = false; wavein?.StopRecording(); wavein?.Dispose(); wavein = null; } private void wavein_DataAvailable(object sender, WaveInEventArgs e) { short[] datas = new short[8192]; double[] datad = new double[8192]; double[] fftdata = new double[8192]; Complex[] spc = new Complex[4096]; for(int i = 0; i < e.BytesRecorded; i += 4) { datas[i / 4] = (short)((e.Buffer[i + 1] << 8) | e.Buffer[i + 0]); datad[i / 4] = datas[i / 4] * wHm[i / 4]; } if(toggle) { datad.CopyTo(capdata0, 0); Array.Copy(capdata0, 4096, fftdata, 0, 4096); Array.Copy(capdata1, 0, fftdata, 4096, 4096); spc = myFFT.ExecuteFft(fftdata); for (int i = 0; i < spc.Length; i++) { dBdata[i] = (float)(10 * Math.Log(spc[i].Real * spc[i].Real + spc[i].Imaginary * spc[i].Imaginary)); } dataready = true; pictureBox1.Invalidate(); } else { datad.CopyTo(capdata1, 0); Array.Copy(capdata0, 0, fftdata, 4096, 4096); Array.Copy(capdata1, 4096, fftdata, 0, 4096); spc = myFFT.ExecuteFft(fftdata); for (int i = 0; i < spc.Length; i++) { dBdata[i] = (float)(10 * Math.Log(spc[i].Real * spc[i].Real + spc[i].Imaginary * spc[i].Imaginary)); } dataready = true; pictureBox1.Invalidate(); } spc = myFFT.ExecuteFft(datad); for (int i = 0; i < spc.Length; i++) { dBdata[i] = (float)(10 * Math.Log(spc[i].Real * spc[i].Real + spc[i].Imaginary * spc[i].Imaginary)); } pictureBox1.Invalidate(); toggle = !toggle; } //Hamming窓生成 private double[] getHammingWnd() { double[] res = new double[8192]; for (int i = 0; i < 8192; i++) res[i] = 0.54 - 0.46 * Math.Cos(2.0 * Math.PI * i / 8192); return res; } private void pictureBox1_Paint(object sender, PaintEventArgs e) { DrawAxis(e.Graphics); if (dataready) { DrawSpectrum(e.Graphics, dBdata); } } private void panel1_Paint(object sender, PaintEventArgs e) { SolidBrush br = new SolidBrush(Color.Black); Font ft = new Font("MS UI Gothic", 9); int y_offset = 25; float x_offset = 15f; for (int i = 1; i < 5; i++) { string st = (i * 200).ToString(); PointF pt = new PointF(xtick * (float)(Math.Log10(200 * i) - offset) + x_offset, panel1.Height - y_offset); e.Graphics.DrawString(st, ft, br, pt); } for (int i = 1; i < 3; i++) { string st = (i * 2).ToString() + "k"; PointF pt = new PointF(xtick * (float)(Math.Log10(2000 * i) - offset) + x_offset, panel1.Height - y_offset); e.Graphics.DrawString(st, ft, br, pt); } for (int i = 1; i < 3; i++) { string st = (i * 10).ToString() + "k"; PointF pt = new PointF(xtick * (float)(Math.Log10(10000 * i) - offset) + x_offset, panel1.Height - y_offset); e.Graphics.DrawString(st, ft, br, pt); } } public void DrawAxis(Graphics gr) { gr.DrawLine(Pens.Black, 0, pb_height, 0, 0); gr.DrawLine(Pens.Black, pb_width, pb_height, pb_width, 0); //100Hz~1000Hz 100Hzステップ for (int i = 1; i < 11; i++) gr.DrawLine(Pens.Black, xtick * (float)(Math.Log10(100 * i) - offset), pb_height, xtick * (float)(Math.Log10(100 * i) - offset), 0); //2000Hz~22000Hz 1000Hzステップ for (int i = 2; i < 22; i = i + 2) gr.DrawLine(Pens.Black, xtick * (float)(Math.Log10(1000 * i) - offset), pb_height, xtick * (float)(Math.Log10(1000 * i) - offset), 0); //縦軸 for (int i = 0; i <= 10; i++) gr.DrawLine(Pens.Black, 0, ytick * i, pb_width, ytick * i); } private void DrawSpectrum(Graphics gr, double[] data) { PointF oldPoint = new PointF(); PointF newPoint = new PointF(); oldPoint.X = (float)(Math.Log10(fstep) - offset); oldPoint.Y = (float)(pb_height - data[1]); for (int i = 2; i < 4096; i++) { newPoint.X = xtick * (float)(Math.Log10(fstep * i) - offset); newPoint.Y = (float)(pb_height - data[i]); gr.DrawLine(Pens.Red, oldPoint, newPoint); oldPoint = newPoint; } } } } |
やり方は、NAudioのFFTを使ったものとほぼ同じ。 サンプルレート45511、FFTデータ数8192、ハミング窓、重複処理。
WaveInの処理は、winmmを使うより圧倒的に便利なのでNAudioを使う。