- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/Mathematics/EdgeDetector.cs

    00001 ﻿//-----------------------------------------------------------------------------
    00002 // EdgeDetector.cs
    00003 // (A. Schiffler, 2009)
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace NewGamePhysics.Mathematics
    00007 {
    00008     using System;
    00009
    00013     public enum EdgeDetectionType
    00014     {
    00018         Rising,
    00019
    00023         Falling,
    00024
    00028         Both,
    00029     }
    00030
    00034     public class EdgeDetector
    00035     {
    00039         private EdgeDetectionType detectionType;
    00040
    00044         private double threshold;
    00045
    00049         private EdgeDetector()
    00050         {
    00051         }
    00052
    00061         public EdgeDetector(EdgeDetectionType detectionType, double threshold)
    00062         {
    00063             this.detectionType = detectionType;
    00064             this.threshold = threshold;
    00065         }
    00066
    00077         public double[] Calculate(double[] samples)
    00078         {
    00079             int nMax = samples.Length;
    00080             double[] result = new double[nMax];
    00081
    00082             // Make absolute and determine maximum
    00083             double maximum = Math.Abs(samples[0]);
    00084             for (int i = 0; i < nMax; i++)
    00085             {
    00086                 double value = Math.Abs(samples[i]);
    00087                 result[i] = value;
    00088                 if (value > maximum)
    00089                 {
    00090                     maximum = value;
    00091                 }
    00092             }
    00093
    00094             // Binarize
    00095             double margin = maximum * this.threshold;
    00096             for (int i = 0; i < nMax; i++)
    00097             {
    00098                 result[i] = (result[i] > margin) ? 1.0 : 0.0;
    00099             }
    00100
    00101             // Edge detect
    00102             double lastValue = 0.0;
    00103             for (int i = 0; i < nMax; i++)
    00104             {
    00105                 bool risingEdge = ((lastValue == 0.0) && (result[i] == 1.0));
    00106                 bool fallingEdge = ((lastValue == 1.0) && (result[i] == 0.0));
    00107                 lastValue = result[i];
    00108                 switch (this.detectionType)
    00109                 {
    00110                     case EdgeDetectionType.Falling:
    00111                         result[i] = (fallingEdge) ? 1.0 : 0.0;
    00112                         break;
    00113                     case EdgeDetectionType.Rising:
    00114                         result[i] = (risingEdge) ? 1.0 : 0.0;
    00115                         break;
    00116                     case EdgeDetectionType.Both:
    00117                         result[i] = (risingEdge || fallingEdge) ? 1.0 : 0.0;
    00118                         break;
    00119                 }
    00120             }
    00121
    00122             return result;
    00123         }
    00124     }
    00125 }