- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/Tests/UnitTestButterworthFilter.cs

    00001 ﻿using System;
    00002 using System.Text;
    00003 using System.Collections.Generic;
    00004 using System.Linq;
    00005 using Microsoft.VisualStudio.TestTools.UnitTesting;
    00006
    00007 using NewGamePhysics.Physics;
    00008 using NewGamePhysics.Mathematics;
    00009
    00010 namespace Tests
    00011 {
    00015     [TestClass]
    00016     public class UnitTestButterworthFilter
    00017     {
    00018         public UnitTestButterworthFilter()
    00019         {
    00020         }
    00021
    00022         private TestContext testContextInstance;
    00023
    00028         public TestContext TestContext
    00029         {
    00030             get
    00031             {
    00032                 return testContextInstance;
    00033             }
    00034             set
    00035             {
    00036                 testContextInstance = value;
    00037             }
    00038         }
    00039
    00040         #region Additional test attributes
    00041         //
    00042         // You can use the following additional attributes as you write your tests:
    00043         //
    00044         // Use ClassInitialize to run code before running the first test in the class
    00045         // [ClassInitialize()]
    00046         // public static void MyClassInitialize(TestContext testContext) { }
    00047         //
    00048         // Use ClassCleanup to run code after all tests in a class have run
    00049         // [ClassCleanup()]
    00050         // public static void MyClassCleanup() { }
    00051         //
    00052         // Use TestInitialize to run code before running each test
    00053         // [TestInitialize()]
    00054         // public void MyTestInitialize() { }
    00055         //
    00056         // Use TestCleanup to run code after each test has run
    00057         // [TestCleanup()]
    00058         // public void MyTestCleanup() { }
    00059         //
    00060         #endregion
    00061
    00062         [TestMethod]
    00063         public void HighPassSineSweep()
    00064         {
    00065             int num = 256;
    00066             ButterworthFilter filter =
    00067                 new ButterworthFilter(ButterworthFilterType.HighPass, 100, 20);
    00068
    00069             double[] samples = new double[num];
    00070             double a = 0.0;
    00071             double s = Math.PI / 20;
    00072             for (int i = 0; i < num; i++)
    00073             {
    00074                 samples[i] = Math.Sin(a);
    00075                 a = a + s;
    00076                 s = s * 1.01;
    00077             }
    00078
    00079             double[] filteredSamples = filter.Calculate(samples);
    00080             for (int i = 0; i < num; i++)
    00081             {
    00082                 System.Console.WriteLine("{0} {1} {2}", i, samples[i], filteredSamples[i]);
    00083             }
    00084         }
    00085
    00086         [TestMethod]
    00087         public void HighPassSteps()
    00088         {
    00089             int num = 320;
    00090             ButterworthFilter filter =
    00091                 new ButterworthFilter(ButterworthFilterType.HighPass, 100, 10);
    00092
    00093             double[] samples = new double[num];
    00094             double v = 0.0;
    00095             double vWant = 0.0;
    00096             Random rand = new Random();
    00097             for (int i = 0; i < num; i++)
    00098             {
    00099                 if ((i % 32) == 0)
    00100                 {
    00101                     vWant = rand.NextDouble();
    00102                 }
    00103
    00104                 v = 0.10 * v + 0.9 * vWant + 0.05*rand.NextDouble();
    00105
    00106                 samples[i] = v;
    00107             }
    00108
    00109             double[] filteredSamples = filter.Calculate(samples);
    00110             for (int i = 0; i < num; i++)
    00111             {
    00112                 System.Console.WriteLine("{0} {1} {2}", i, samples[i], filteredSamples[i]);
    00113             }
    00114         }
    00115
    00116         [TestMethod]
    00117         public void LowPassSineSweep()
    00118         {
    00119             int num = 256;
    00120             ButterworthFilter filter =
    00121                 new ButterworthFilter(ButterworthFilterType.LowPass, 100, 20);
    00122
    00123             double[] samples = new double[num];
    00124             double a = 0.0;
    00125             double s = Math.PI / 20;
    00126             for (int i = 0; i < num; i++)
    00127             {
    00128                 samples[i] = Math.Sin(a);
    00129                 a = a + s;
    00130                 s = s * 1.01;
    00131             }
    00132
    00133             double[] filteredSamples = filter.Calculate(samples);
    00134             for (int i = 0; i < num; i++)
    00135             {
    00136                 System.Console.WriteLine("{0} {1} {2}", i, samples[i], filteredSamples[i]);
    00137             }
    00138         }
    00139
    00140         [TestMethod]
    00141         public void LowPassSteps()
    00142         {
    00143             int num = 320;
    00144             ButterworthFilter filter =
    00145                 new ButterworthFilter(ButterworthFilterType.LowPass, 100, 10);
    00146
    00147             double[] samples = new double[num];
    00148             double v = 0.0;
    00149             double vWant = 0.0;
    00150             Random rand = new Random();
    00151             for (int i = 0; i < num; i++)
    00152             {
    00153                 if ((i % 32) == 0)
    00154                 {
    00155                     vWant = rand.NextDouble();
    00156                 }
    00157
    00158                 v = 0.10 * v + 0.9 * vWant + 0.05 * rand.NextDouble();
    00159
    00160                 samples[i] = v;
    00161             }
    00162
    00163             double[] filteredSamples = filter.Calculate(samples);
    00164             for (int i = 0; i < num; i++)
    00165             {
    00166                 System.Console.WriteLine("{0} {1} {2}", i, samples[i], filteredSamples[i]);
    00167             }
    00168         }
    00169     }
    00170 }