複素信号と2波の重畳信号が生成できるようにしました。 また、2波の周波数成分が表示できるようにしました。
単一波、実信号です。
2波重畳、実信号です。
単一波、複素信号です。
2波重畳、複素信号です。
ソールです。
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 183 184 185 186 187 |
public partial class Form1 : Form { public Form1() { InitializeComponent(); } Complex[] wavedata1; double[] db; double freq1, freq2, samplerate; int magnitude, addcnt; string romtext = ""; Complex[] Xk; bool dataready, sa_dataready, cmp; DrawGraph dg; Speana sa; private void textBox2_TextChanged(object sender, EventArgs e) { magnitude = (int)(Math.Pow(2, int.Parse(textBox2.Text)) / 2); dg.mag = magnitude; } private void textBox3_TextChanged(object sender, EventArgs e) { samplerate = double.Parse(textBox3.Text) * 1000000; } private void checkBox1_CheckedChanged(object sender, EventArgs e) { cmp = checkBox1.Checked; } private void pictureBox2_Paint(object sender, PaintEventArgs e) { sa.DrawAxis(e.Graphics); if (sa_dataready) sa.DrawSpectrum(e.Graphics, db); } private void checkBox2_CheckedChanged(object sender, EventArgs e) { if (checkBox2.Checked) { textBox5.Enabled = true; label9.Enabled = true; label10.Enabled = true; } else { textBox5.Enabled = false; label9.Enabled = false; label10.Enabled = false; } } private void textBox4_TextChanged(object sender, EventArgs e) { freq1 = double.Parse(textBox4.Text) * 1000; //Hz } private void textBox5_TextChanged(object sender, EventArgs e) { freq2 = double.Parse(textBox5.Text) * 1000; //Hz } private void button2_Click(object sender, EventArgs e) { var fname = textBox7.Text; File.WriteAllText(@fname + ".txt", romtext); } private void textBox1_TextChanged(object sender, EventArgs e) { addcnt = (int)Math.Pow(2, int.Parse(textBox1.Text)); dg.add = addcnt; sa.add = addcnt; } private void pictureBox1_Paint(object sender, PaintEventArgs e) { dg.DrawAxis(e.Graphics); if (dataready) dg.DrawData(e.Graphics, wavedata1, cmp); } private void Form1_Load(object sender, EventArgs e) { freq1 = double.Parse(textBox4.Text) * 1000; //Hz freq2 = double.Parse(textBox5.Text) * 1000; //Hz magnitude = (int)(Math.Pow(2, int.Parse(textBox2.Text)) / 2); addcnt = (int)Math.Pow(2, int.Parse(textBox1.Text)); samplerate = double.Parse(textBox3.Text) * 1000000; cmp = checkBox1.Checked; dg = new DrawGraph(pictureBox1, addcnt, magnitude, cmp); sa = new Speana(pictureBox2, addcnt); if (checkBox2.Checked) { textBox5.Enabled = true; label9.Enabled = true; label10.Enabled = true; } else { textBox5.Enabled = false; label9.Enabled = false; label10.Enabled = false; } dataready = false; sa_dataready = false; } private void button1_Click(object sender, EventArgs e) { wavedata1 = new Complex[addcnt]; dg.mag = magnitude; if (!checkBox1.Checked & !checkBox2.Checked) //実数、単独 { for(int i = 0; i < addcnt; i++) wavedata1[i] = magnitude * Math.Cos(i * 2.0 * Math.PI * freq1 / samplerate); } else if (!checkBox1.Checked & checkBox2.Checked) //実数、重畳 { for (int i = 0; i < addcnt; i++) { wavedata1[i] = magnitude * (Math.Cos(i * 2.0 * Math.PI * freq1 / samplerate) + Math.Cos(i * 2.0 * Math.PI * freq2 / samplerate))/2; } } else if(checkBox1.Checked & !checkBox2.Checked) //複素数、単独 { for(int i = 0; i < addcnt; i++) { wavedata1[i] = magnitude * Math.Cos(i * 2.0 * Math.PI * freq1 / samplerate) + Complex.ImaginaryOne * magnitude * Math.Sin(i * 2.0 * Math.PI * freq1 / samplerate); } } else if(checkBox1.Checked & checkBox2.Checked) //複素数、重畳 { for(int i = 0; i < addcnt; i++) { wavedata1[i] = magnitude * ((Math.Cos(i * 2.0 * Math.PI * freq1 / samplerate) + Math.Cos(i * 2.0 * Math.PI * freq2 / samplerate)) + Complex.ImaginaryOne * (Math.Sin(i * 2.0 * Math.PI * freq1 / samplerate) + Math.Sin(i * 2.0 * Math.PI * freq2 / samplerate)))/2; } } romtext = ""; for (int i = 0; i < addcnt; i++) { var mm = int.Parse(textBox2.Text); var buf1 = Convert.ToString((int)wavedata1[i].Real, 2).PadLeft(mm, '0'); buf1 = buf1.Substring(buf1.Length - mm, mm); if (checkBox1.Checked) { var buf2 = Convert.ToString((int)wavedata1[i].Imaginary, 2).PadLeft(mm, '0'); buf2 = buf2.Substring(buf2.Length - mm, mm); buf1 = buf1 + buf2; } romtext = romtext + buf1 + "\r\n"; } textBox6.Text = romtext; dataready = true; pictureBox1.Refresh(); db = new double[wavedata1.Length]; Xk = new Complex[wavedata1.Length]; Xk = DftExecute(wavedata1); for (int i = 0; i < wavedata1.Length; i++) db[i] = Math.Log10(Xk[i].Magnitude + 1e-9) * 20; sa_dataready = true; pictureBox2.Refresh(); } Complex[] DftExecute(Complex[] xn) { Complex[] Xk = new Complex[xn.Length]; for (int i = 0; i < xn.Length; i++) { Xk[i] = 0; for (int j = 0; j < xn.Length; j++) { Xk[i] = Xk[i] + xn[j] * Complex.Exp(-2 * Math.PI * j * i * Complex.ImaginaryOne / xn.Length); } } return Xk; } } |
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 |
class DrawGraph { private PictureBox picturebox_; private float x0_; //x軸原点 private float y0_; //y軸原点 private float y1_; //y軸1に対するピクセル値 private float width_; private bool comp_; private float tick; private int _add, _mag; private Font myfont_ = new Font("MS UI Gothic", 10); private SolidBrush mysolidbrush_ = new SolidBrush(Color.Black); private const float x_offset = 10.0F; private const float y_offset = 10.0F; //コンストラクタ public DrawGraph(PictureBox myPictureBox, int add, int mag, bool comp) { picturebox_ = myPictureBox; x0_ = picturebox_.Left; y0_ = picturebox_.ClientSize.Height - 1; y1_ = y0_ /(float)(1.2* mag * 2); width_ = picturebox_.ClientSize.Width - 1; tick = width_ / add; _add = add; _mag = mag; comp_ = comp; } public int add { get { return _add; } set { _add = value; tick = width_ / _add; } } public int mag { get { return _mag; } set { _mag = value; y1_ = y0_ / (float)(1.2 * _mag * 2); } } public bool comp { get { return comp_; } set { comp_ = value; } } public void DrawAxis(Graphics gr) { DrawVerticalLines(gr, Pens.Black); DrawHolizontalLines(gr, Pens.Black); } private void DrawHolizontalLines(Graphics gr, Pen mypen) { float yn = y0_ / 10; for (int i = 0; i <= y0_; i += 1) gr.DrawLine(mypen, 0, y0_ - yn * i, width_, y0_ - yn * i); } private void DrawVerticalLines(Graphics gr, Pen mypen) { float xn = width_ / 10; for (int i = 0; i <= (int)width_; i += 1) gr.DrawLine(mypen, xn * i, 0, xn * i, y0_); } public void DrawData(Graphics gr, Complex[] gdata, bool comp) { Pen mypen1 = new Pen(Color.Red); Pen mypen2 = new Pen(Color.Green); PointF oldPoint1 = new PointF(0, (float)(y0_ - y1_ * gdata[0].Real - y0_/2)); PointF oldPoint2 = new PointF(0, (float)(y0_ - y1_ * gdata[0].Imaginary - y0_ / 2)); for (int i = 1; i < gdata.Length - 1; i++) { PointF newPoint1 = new PointF(i * tick, (float)(y0_ - y1_ * gdata[i].Real - y0_/2)); gr.DrawLine(mypen1, oldPoint1, newPoint1); oldPoint1 = newPoint1; if(comp) { PointF newPoint2 = new PointF(i * tick, (float)(y0_ - y1_ * gdata[i].Imaginary - y0_ / 2)); gr.DrawLine(mypen2, oldPoint2, newPoint2); oldPoint2 = newPoint2; } } } } |
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 |
class Speana { private PictureBox picturebox_; private float x0_; //x軸原点 private float y0_; //y軸原点 private float width_; private const float x_offset = 10.0F; private const float y_offset = 10.0F; int add_; public Speana(PictureBox myPitureBox, int add) { picturebox_ = myPitureBox; add_ = add; x0_ = picturebox_.Left; y0_ = picturebox_.ClientSize.Height - 1; width_ = picturebox_.ClientSize.Width - 1; } public int add { get { return add_; } set { add_ = value; } } public void DrawAxis(Graphics gr) { float step = width_ / 16; for (int i = 0; i <= 16; i++) gr.DrawLine(Pens.Black, step * i, 0, step * i, y0_); step = y0_ / 10; for (int i = 0; i <= 10; i++) gr.DrawLine(Pens.Black, 0, y0_ - step * i, width_, y0_ - step * i); step = step/2; for (int i = 0; i <= 20; i++) gr.DrawLine(Pens.Black, width_/2, y0_ - step * i, width_/2 + 3, y0_ - step * i); } public void DrawSpectrum(Graphics gr, double[] data) { float xtick = width_ / add_; PointF oldPoint = new PointF(0, y0_- (float)(data[0])); PointF newPoint = new PointF(); for (int i = 1; i < data.Length; i++) { newPoint.X = i * xtick; newPoint.Y = y0_-(float)data[i]; gr.DrawLine(Pens.Red, oldPoint, newPoint); oldPoint = newPoint; } } } |