element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Simple Automatic Threshold Generation for Embedded Systems
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: satyavrat
  • Date Created: 20 Mar 2017 7:18 AM Date Created
  • Views 1939 views
  • Likes 2 likes
  • Comments 3 comments
  • control systems
  • data analysis
  • Embedded Systems
Related
Recommended

Simple Automatic Threshold Generation for Embedded Systems

satyavrat
satyavrat
20 Mar 2017

     Very often it so happens in sensing-actuation based projects that an embedded system must take a certain action when the value of a sensed parameter is above a certain threshold. The most basic method of handling such a control loop is by hard coding a threshold or a set point. Then, for the embedded system, it becomes a binary decision. A High and a Low, even for analog inputs. However, one of the problems caused by this method is that the threshold does not change with a change in secondary environmental parameters affecting the primary sensed value.

     Take for example, a system to detect whether or not a box has been opened using a light sensor. I would hard code the threshold, and it would work well at my place because its bright throughout the day. Now suppose I take it to another location where the lighting is not as optimal. The threshold will naturally change, but my code will not be able to adapt to this change without updating the firmware, which is a real problem with a system installed in an inaccessible location. But, if the system were able to adapt to the new lighting conditions, it would be able to determine its own threshold for the same.

image image

     I've faced this problem a few times, mainly while determining my threshold based on time series data. A different set of environmental parameters call for a new threshold. What these parameters might be is different for every application, so my initial idea of calculating a complex mathematical inclusion which would be expected to include subjective terms such as location was laughable. And all for segregating my data into two parts? That would be like buying an airliner to get to my local grocery store.

     After taking a class on statistics, the intuitive value of statistical analysis for my data led me to come up with this small, rather easy algorithm for automatic generation of thresholds for time series data generated by my systems. Obviously, there are some assumptions here.

  • The two groups to be distinguished are mutually exclusive.
  • The sampling rate of the algorithm is much slower than the slope of the system.
  • The algorithm, hence will not be used for data logging or frequency analysis
  • There at least two known values for the algorithm to run.

 

The algorithm is as follows

     Required packages : Numpy, Random, Matplotlib.pyplot

 

  1. Store sensor values in an array

 

Thresholding function :

  1. Sort the array in ascending order
  2. Get sequential difference by calculating difference between consecutive elements of ascending order array.(Store these values in an array as well) Let's call this array A.
  3. Similarly, get a sequential difference array of the sequential difference array (A) calculated in step 2, let's call it B
  4. Select the second largest value in B, and multiply it by two. This is your threshold for parameter for grouping. This will ensure that your groups aren't too close together.
  5. Select the element in sequential difference array (A) with the largest value. This element corresponds to the difference between the largest value in the 'OFF' group, ie lower threshold and the smallest value in the 'ON' group, ie the higher threshold, and gives you the difference between the two groups. Let's call this M.
  6. Generate the dynamic threshold by dividing M by 2 and adding the value of the lower threshold to get your actual dynamic threshold.
  7. Plot the graph.

 

Add time series data to input array (separate function):When your system senses a new value of light intensity (or your selected conditions);

  1. Append the value of light intensity to the input array
  2. Check the new value(NV) for the following conditions:
    1. NV > Highest value in entire input array
    2. NV < Lowest value in entire input array
    3. Lower Threshold < NV < Higher threshold
  3. If any of these conditions are true, call dynamic threshold function()

 

The resultant graphs look as follows. The first one is using the priory set of input data, and the second and third ones are after appending randomly generated values to the input dataset in two separate iterations.

imageimageimage

 

     As with all algorithms, this one does have its own set of drawbacks which are primarily illustrated by the assumptions made.

  • This algorithm cannot be used for data logging and auto classification unless the algorithm selects data at a defined interval from the array of logged values.
  • If the sampling rate of the algorithm is higher than the time required for the sensor to become stable, the algorithm will not be able to distinctly group the values because consecutive difference values are going to be very similar.

 

    In the future, I plan to get rid of these drawbacks by introducing a more sophisticated and inclusive method of statistical classification.

  • Sign in to reply

Top Comments

  • DAB
    DAB over 8 years ago +2
    Setting thresholds is always an issue. I found that the simplest method is a running filter, usually eight samples, which you average. You then set the threshold at some value above the average so as the…
  • shabaz
    shabaz over 8 years ago +1
    Interesting post : ) In the analog world this is a common task, for example when trying to decode a bitstream with out-of-band noise especially a low frequency offset or DC offset. There sometimes they…
  • satyavrat
    satyavrat over 8 years ago +1
    shabaz DAB Thank you for your suggestions! I'm actually working on a project which will require a dynamic thresholding method. This was my attempt at designing an algorithm that would serve the purpose…
  • satyavrat
    satyavrat over 8 years ago

    shabazDAB

    Thank you for your suggestions! I'm actually working on a project which will require a dynamic thresholding method. This was my attempt at designing an algorithm that would serve the purpose! I'll surely look into both your suggested algorithms and see what I can learn!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 8 years ago

    Interesting post : )

    In the analog world this is a common task, for example when trying to decode a bitstream with out-of-band noise especially a low frequency offset or DC offset.

    There sometimes they use a 'slicer' which is basically comparing the input signal with an averaged (low-pass) signal derived from the input signal, e.g. just a RC filter.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 8 years ago

    Setting thresholds is always an issue.

     

    I found that the simplest method is a running filter, usually eight samples, which you average.

    You then set the threshold at some value above the average so as the noise gets worse, you detect less.

     

    Now if you really need a good dynamic filter, you use an adaptive algorithm.  Look for work by Dr. Peter Raeth.  He and I wrote a number of papers on this technique a while back.

     

    DAB

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube