Example #1
0
        private static void Slide8Render(bool drawOriginal = false, bool drawSamples = false, bool drawFiltered = false)
        {
            if (frequencyText == null) frequencyText = new Text(Text.FontSize._24pt, "0Hz @ 1000Hz", Slides.Common.TextColor);

            float f = t * 0.05f;
            if (f * 5000 > Math.PI * 1000) t = 0;
            frequencyText.String = string.Format("{0:0.0}Hz @ 1000Hz", f / Math.PI * 5000);

            float[] sine = new float[441];
            for (int i = 0; i < sine.Length; i++)
                sine[i] = 0.5f * (float)Math.Sin((i - 441 / 2) * f);

            Slides.Common.DrawPlotter(Utilities.FastMatrix4(new Vector3(72, 720 - 227 - 410, 0), new Vector3(441, 410, 1)));
            if (drawOriginal) Slides.Common.DrawPlotLeft(sine, Slides.Common.TitleColor);

            float[] samples = new float[441];
            for (int i = 0; i < sine.Length; i++)
            {
                samples[i] = sine[(int)(i / 10) * 10];
            }

            FilterTools.BiQuad lpf = new FilterTools.BiQuad(2205, 0.707, 44100, 0, FilterTools.BiQuad.Type.LPF);
            lpf.Child = (FilterTools.BiQuad)lpf.Clone();

            float[] filter = new float[441];
            lpf.Load(samples[0]);
            for (int i = 0; i < filter.Length; i++) filter[i] = (float)lpf.GetOutput(samples[i]);

            if (drawSamples) Slides.Common.DrawPlotLeft(samples, new Vector3(1, 0, 0));
            if (drawFiltered) Slides.Common.DrawPlotLeft(filter, new Vector3(0, 1, 0));

            if (drawSamples || drawFiltered)
            {
                frequencyText.ModelMatrix = Matrix4.CreateTranslation(new Vector3(70, 500, 0));
                frequencyText.Draw();
            }
        }
Example #2
0
        private static void FourierTransformExample(float transformation, bool drawSquare = false, bool drawAbs = false)
        {
            StopAudioDevice();

            if (transformation >= 9.99f) transformation = 9.99f;
            transformation /= 10;

            float t2 = (transformation > 0.5 ? 1 - transformation : transformation);
            float t3 = CubicEaseInOut(t2 * 2, 0, 0.2f, 1);
            float t4 = CubicEaseInOut(transformation, 0, 1, 1);

            // calculate a cool camera transformation
            Matrix4 rotateY = Quaternion.Slerp(Quaternion.Identity, Quaternion.FromRotationMatrix(Matrix4.CreateRotationY((float)Math.PI / 2)), t4).Matrix4;
            Matrix4 rotateX = Quaternion.Slerp(Quaternion.Identity, Quaternion.FromRotationMatrix(Matrix4.CreateRotationX((float)Math.PI / -2)), t3).Matrix4 * Matrix4.CreateTranslation(new Vector3(0, t3 * 120, 0));
            Matrix4 viewMatrix = rotateY * Matrix4.CreateTranslation(new Vector3(t4 * 100, 0, 0)) * rotateX;

            // draw the plotter frame
            Gl.Disable(EnableCap.DepthTest);

            if (transformation == 0) Slides.Common.DrawPlotter(Utilities.FastMatrix4(new Vector3(72, 720 - 227 - 410, 0), new Vector3(441, 410, 1)));
            else if (transformation >= 0.95)
            {
                Slides.Common.DrawXPlotter(Utilities.FastMatrix4(new Vector3(72, 720 - 227 - 410, 0), new Vector3(441, 410, 1)));

                if (drawAbs && fourierText == null)
                {
                    fourierText = new Text(Text.FontSize._24pt, "f  3f  5f  7f 9f  11f ....", Slides.Common.SubtitleColor);
                    fourierText.ModelMatrix = Matrix4.CreateTranslation(new Vector3(125, 265, 0));
                }
                if (drawAbs) fourierText.Draw();
            }
            else Slides.Common.DrawBox(Utilities.FastMatrix4(new Vector3(72, 720 - 227 - 410, 0), new Vector3(441, 410, 1)), Slides.Common.SubtitleColor);

            // enable stencil testing to ensure that the plots are clipped to inside the plotter
            Slides.Common.EnableStencil(Utilities.FastMatrix4(new Vector3(72, 720 - 227 - 410, 0), new Vector3(441, 410, 1)));
            Gl.StencilFunc(StencilFunction.Equal, 1, 0xFFFF);

            // create the square wave
            if (drawSquare)
            {
                float[] squareWave = new float[441];
                for (int i = 0; i < squareWave.Length; i++) squareWave[i] = ((i % 100) < 50) ? 0.5f : -0.5f;

                Slides.Common.Draw3DPlotLeft(squareWave, 0, Slides.Common.TitleColor, viewMatrix);
            }

            float[] fourier = new float[441];

            for (int i = 49; i >= 0; i--)
            {
                float[] sine = new float[441];

                for (int j = 0; j < 441; j++)
                {
                    sine[j] = 0.63662f * (float)Math.Sin(Math.PI * 2 / 100 * j * (1 + i * 2)) / (1 + i * 2);
                    if (drawAbs) sine[j] = Math.Abs(sine[j]);
                    fourier[j] += sine[j];
                }

                if (i < 13)
                    Slides.Common.Draw3DPlotLeft(sine, (i + 1) * 30, new Vector3(0.2 + i * 0.06f, 0.2 + i * 0.06f, 1), viewMatrix);
            }

            if (drawSquare) Slides.Common.Draw3DPlotLeft(fourier, 0, new Vector3(1, 0, 0), viewMatrix);
            Gl.Disable(EnableCap.StencilTest);
            Gl.Enable(EnableCap.DepthTest);
        }