christosh
christosh

Reputation: 193

Real time detection of gaussian bell in signal in C#

I have got a real time signal in c# which is stored in a List. I want to check if the signal pass from several points in order direction. In fact I want to detect real time if my signal performing a gaussian bell. What I have in my mind is to check if the signal pass from two thresholds in ascent order and in descent order. My signal takes values from 0-1 For example if the signal is passing from 0.25 with ascent order and from 0.75 again with ascent order and after that from 0.75 and 0.25 in descent order to detect my bell. How can I perform real time the above algorithm in c#. I store the signal value real time in a List<double> called parts.

double currentValue = parts[parts.Count - 1];
double previousValue = parts[parts.Count - 2];
double diffValue = parts[parts.Count - 1] - parts[parts.Count - 2];

if (currentValue > 0.25 && (currentValue - previousValue) > 0)
{ 
// the first point is passed
}
if (currentValue  < 0.750 && (currentValue - previousValue) > 0)
{
 // the second point is passed
}
// the same for the descent order.  
}

What I want is to count how many times my signal is performing the gaussian bell and to detect the time window returning the timestamps (from 0.25 point to 0.25).

Upvotes: 0

Views: 332

Answers (1)

jacoblambert
jacoblambert

Reputation: 787

An improved answer, albeit in process. It should be refactored.

    void Main()
    {
        //Assuming a temporally ordered list
        List<double> ascOnly = new List<double> { 0, 0.1, 0.2, 0.25, 0.3, 0.5, 0.7, 0.8};
        List<double> gBell = new List<double> { 0, 0.1, 0.2, 0.25, 0.3, 0.5, 0.7, 0.8, 0.81, 0.6, 0.45, 0.32, 0.24, 0.1 };

        //make sure a first point below .25 exists. find it and its index in the List. (IndexOf also gets the first value so pv1 should always be the index of v1)
        var firstValue = gBell.FirstOrDefault(item => item < 0.25);
        var positionFirstValue = gBell.IndexOf(firstValue);
        Console.WriteLine("Starting below 0.25: " + firstValue + " , Position: " + positionFirstValue);

        //find the first point at or above .25 and its index in the List.
        var ascendingBottomThreshold = gBell.FirstOrDefault(item => item >= 0.25) ;
        var positionAscendingBottomThreshold = gBell.FindIndex(item => item == ascendingBottomThreshold);

        //make sure the first threshold is crossed after the initially small threshold (positive slope)
        if (positionAscendingBottomThreshold > positionFirstValue) //fyi FindIndex returns -1 if condition is not satisfied
            Console.WriteLine("First threshold at/above 0.25: " + ascendingBottomThreshold + " , Position: " + positionAscendingBottomThreshold);

        //find the first point at or above .75 and its index in the List
        var ascendingTopThreshold = gBell.FirstOrDefault(item => item >= 0.75) ;
        var positionAscendingTopThreshold = gBell.IndexOf(ascendingTopThreshold);

        //make sure the .75 threshold is crossed after the .25 threshold (positive slope)
        if (positionAscendingTopThreshold > positionAscendingBottomThreshold) //fyi FindIndex returns -1 if condition is not satisfied
            Console.WriteLine("Second threshold at/above 0.75: " + ascendingTopThreshold + " , Position: " + positionAscendingTopThreshold);


        //do i next have to cross .75 while maintaining a positive slope? or just never dip below .25? (assumed the latter)
        //make sure the signal that passed .25 never dips below .25 before it reaches .75.
        for (int p = positionAscendingBottomThreshold + 1 ; p < positionAscendingTopThreshold ; p++)
        {   
            if (gBell[p] < 0.25)
            {
                Console.WriteLine("Fell below threshold ...does not qualify(?)");
            }
        }


        //assuming i need at least one more distinct point at or above .75; shows that signal did not just touch and go below, but actually crested.
        gBell.RemoveAt(positionAscendingTopThreshold);
        var descendingTopThreshold = gBell.FirstOrDefault(item => item >= 0.75) ;
        var positionDescendingTopThreshold = gBell.IndexOf(descendingTopThreshold);

        //make sure the .75 threshold is hit after the .75 threshold (positive slope)
        if (positionDescendingTopThreshold >= positionAscendingTopThreshold) //fyi FindIndex returns -1 if condition is not satisfied
            Console.WriteLine("Third threshold at/above 0.75: " + descendingTopThreshold + " , Position: " + positionDescendingTopThreshold);

        for (int p = positionAscendingTopThreshold ; p < positionDescendingTopThreshold ; p++) //just positionAscendingTopThreshold (not plus 1 because value removed)
        {   
            if (gBell[p] < 0.75)
            {
                Console.WriteLine("Fell below threshold ...does not qualify(?)");
            }
        }       

    //looking next for .25;
        //remove everything that got you 'up the hill'
        gBell.RemoveRange(0,positionDescendingTopThreshold);
        var descendingBottomThreshold = gBell.FirstOrDefault(item => item <= 0.25) ;
        var positionDescendingBottomThreshold = gBell.IndexOf(descendingBottomThreshold);

        //make sure the .25 threshold is hit after the second .75 threshold (downhill)
        //if (positionDescendingBottomThreshold >= positionDescendingTopThreshold) //fyi FindIndex returns -1 if condition is not satisfied
            Console.WriteLine("Fourth threshold at/below 0.25: " + descendingBottomThreshold + " , Position: " + positionDescendingBottomThreshold);

        for (int p = 1 ; p < positionDescendingBottomThreshold ; p++)
        {   
            if (gBell[p] > 0.75)
            {
                Console.WriteLine("Rose above threshold ...does not qualify(?) " + gBell[p]);
            }
        }           

    }

Upvotes: 1

Related Questions