LoginSignup
0
0

More than 1 year has passed since last update.

cscの作法 その187

Posted at

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

am波に、fft掛けて、グラフ化せよ。

写真

image.png

サンプルコード


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace app
{
	class form1: Form {
		public static void FFT(double[] inputRe, double[] inputIm, out double[] outputRe, out double[] outputIm, int bitSize) {
			int dataSize = 1 << bitSize;
			int[] reverseBitArray = BitScrollArray(dataSize);
			outputRe = new double[dataSize];
			outputIm = new double[dataSize];
			for (int i = 0; i < dataSize; i++)
			{
				outputRe[i] = inputRe[reverseBitArray[i]];
				outputIm[i] = inputIm[reverseBitArray[i]];
			}
			for (int stage = 1; stage <= bitSize; stage++)
			{
				int butterflyDistance = 1 << stage;
				int numType = butterflyDistance >> 1;
				int butterflySize = butterflyDistance >> 1;
				double wRe = 1.0;
				double wIm = 0.0;
				double uRe = System.Math.Cos(System.Math.PI / butterflySize);
				double uIm = -System.Math.Sin(System.Math.PI / butterflySize);
				for (int type = 0; type < numType; type++)
				{
					for (int j = type; j < dataSize; j += butterflyDistance)
					{
						int jp = j + butterflySize;
						double tempRe = outputRe[jp] * wRe - outputIm[jp] * wIm;
						double tempIm = outputRe[jp] * wIm + outputIm[jp] * wRe;
						outputRe[jp] = outputRe[j] - tempRe;
						outputIm[jp] = outputIm[j] - tempIm;
						outputRe[j] += tempRe;
						outputIm[j] += tempIm;
					}
					double tempWRe = wRe * uRe - wIm * uIm;
					double tempWIm = wRe * uIm + wIm * uRe;
					wRe = tempWRe;
					wIm = tempWIm;
				}
			}
		}
		private static int[] BitScrollArray(int arraySize) {
			int[] reBitArray = new int[arraySize];
			int arraySizeHarf = arraySize >> 1;
			reBitArray[0] = 0;
			for (int i = 1; i < arraySize; i <<= 1)
			{
				for (int j = 0; j < i; j++)
					reBitArray[j + i] = reBitArray[j] + arraySizeHarf;
				arraySizeHarf >>= 1;
			}
			return reBitArray;
		}
		private double[] po = new double[256];
		private double[] data = new double[256];
		public form1() {
			this.Text = "fft";
			this.ClientSize = new Size(400, 400);
			Button btn1 = new Button();
			btn1.Location = new Point(320, 360);
			btn1.Text = "run";
			btn1.Click += btn1_Click;
			Controls.AddRange(new Control[] {
				btn1
			});
		}
		void btn1_Click(object sender, System.EventArgs e) {
			const int size = 256;
			double[] re = new double[size];
			double[] im = new double[size];
			for (int i = 0; i < size; i++)
			{
				data[i] = (0.3 * Math.Sin(2 * 10 * i * Math.PI / 1800) + 1.0) * Math.Sin(2 * 200 * i * Math.PI / 1800);
			}
			FFT(data, im, out re, out im, 8);
			for (int i = 0; i < size; i++)
			{
				po[i] = Math.Sqrt(re[i] * re[i] + im[i] * im[i]);
			}
			this.Invalidate();
		}
		protected override void OnPaint(PaintEventArgs e) {
			Graphics g = e.Graphics;
			Pen pen0 = new Pen(Color.Red, 2);
			Pen pen1 = new Pen(Color.Black, 2);
			SolidBrush brush = new SolidBrush(Color.Yellow);
			float y2 = 250;
			for (int i = 0; i < 256; i++)
			{
				float y0 = (float)(50 + po[i] * 1.0);
				if (i < 128)
					g.DrawLine(pen0, 50 + i, 50, 50 + i, y0);
				float y1 = (float)(250 + data[i] * 100);
				g.DrawLine(pen1, 50 + i, y2, 50 + i, y1);
				y2 = y1;
			}
		}
		[STAThread]
		public static void Main() {
			Application.Run(new form1());
		}
	}
}

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0