FFT() public static method

public static FFT ( int n, bool invert, double ar, double ai ) : void
n int
invert bool
ar double
ai double
return void
Exemplo n.º 1
0
        public override void Draw(float[] data)
        {
            float dx       = (float)this.size.Width / (float)(data.Length / 4);
            float y_offset = +20.0f;

            int fftSize = data.Length / 2;
            int height  = this.size.Height / 2;

            if (this.buffer.Length != fftSize)
            {
                this.buffer = new PointF[fftSize];
                this.re     = new double[fftSize];
                this.im     = new double[fftSize];
            }

            this.graphics.Clear(this.backgroundColor);

            // L channel
            {
                for (int i = 0, j = 0; i < fftSize; i++, j += 2)
                {
                    this.re[i] = data[j];
                }

                Array.Clear(this.im, 0, fftSize);
                Utils.WindowingAsHanning(re);
                Utils.FFT(fftSize, false, re, im);

                for (int i = 0; i < fftSize; i++)
                {
                    double d = 20.0 * Math.Log10(Math.Sqrt(re[i] * re[i] + im[i] * im[i]) / fftSize) + y_offset;

                    if (d <= -height)
                    {
                        d = -height;
                    }
                    else if (d > 0.0)
                    {
                        d = 0.0;
                    }

                    this.buffer[i] = new PointF(i * dx, -(float)d);
                }

                this.graphics.DrawLines(this.p, this.buffer);
            }

            // R channel
            {
                for (int i = 0, j = 1; i < fftSize; i++, j += 2)
                {
                    this.re[i] = data[j];
                }

                Array.Clear(this.im, 0, fftSize);
                Utils.WindowingAsHanning(re);
                Utils.FFT(fftSize, false, re, im);

                for (int i = 0; i < fftSize; i++)
                {
                    double d = 20.0 * Math.Log10(Math.Sqrt(re[i] * re[i] + im[i] * im[i]) / fftSize) + y_offset;

                    if (d <= -height)
                    {
                        d = -height;
                    }
                    else if (d > 0.0)
                    {
                        d = 0.0;
                    }

                    this.buffer[i] = new PointF(i * dx, this.size.Height / 2 - (float)d);
                }

                this.graphics.DrawLines(this.p, this.buffer);
            }

            this.graphics.DrawLine(this.p, 0, height, this.size.Width, height);

            this.graphics.Flush();
        }
Exemplo n.º 2
0
        unsafe public override void Draw(float[] data)
        {
            float       dx          = (float)this.size.Width / (float)(data.Length / 4);
            const float y_offset    = +210.0f;
            const float y_amplifier = 100.0f;

            int fftSize = data.Length;

            if (fftSize != this.re.Length)
            {
                this.re       = new double[fftSize];
                this.im       = new double[fftSize];
                this.spectrum = new Bitmap(1, fftSize / 4);
            }
            else
            {
                Array.Clear(this.im, 0, fftSize);
            }

            data.CopyTo(re, 0);
            Utils.WindowingAsHanning(re);
            Utils.FFT(fftSize, false, re, im);

            for (int i = 0; i < fftSize / 4; i++)
            {
                re[i] = y_amplifier * Math.Log10(Math.Sqrt(re[i] * re[i] + im[i] * im[i]) / (fftSize * 4)) + y_offset;

                if (re[i] < -255.0)
                {
                    re[i] = -255.0;
                }
                else if (re[i] > 0.0)
                {
                    re[i] = 0.0;
                }
            }

            var bdata = this.bitmap.LockBits(new Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            {
                for (byte *i = (byte *)bdata.Scan0, j = i + 4, l = i + bdata.Width * bdata.Height * 4; j < l; i++, j++)
                {
                    *i = *j;
                }
            }
            this.bitmap.UnlockBits(bdata);

            bdata = this.spectrum.LockBits(new Rectangle(0, 0, 1, fftSize / 4), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            {
                int j = 0;
                for (byte *i = (byte *)bdata.Scan0, l = i + bdata.Height * 4; i < l; j++)
                {
                    byte b   = (byte)(255.0 + re[j]);
                    *    i++ = b;
                    *    i++ = b;
                    *    i++ = b;
                    *    i++ = 255;
                }
            }
            this.spectrum.UnlockBits(bdata);

            this.graphics.DrawImage(this.spectrum, this.size.Width - 1, this.size.Height - 1, 1, -this.size.Height);
            this.graphics.Flush();
        }