# Counting blinks of a LED

I'm working on an algorithm that counts blinks of a LED. I have series of non-neagtive integers. They represent brightness of a LED in time. I give you two examples of these integer series below. Visually it's very obvious that LED blinked 7 times in example 1 and 8 times in example 2. But how can this be calculated by a computer. Any ideas? I don't even know if I'm asking this question on the right site. I'm new to this stuff but any help would be appreaciated.

Example 1: Data of Example 1 Example 2: Data of Example 2 • Are the data always this clean and discrete? – A_A Oct 11 '18 at 13:33
• A simple detection above a threshold (with some hysteresis) should work fine since your data seems noise-free. – Ben Oct 11 '18 at 13:44

You can do something like this in Python (setting data to your original signals):

import numpy as np
data = data1

max_val = np.max(data)
thresh = max_val/2
thresh_crossings = np.where(np.diff(np.signbit(np.array(data) - max_val/2)))
print(len(thresh_crossings)/2)


This code returns 7 for your first signal and 8 for the second one.

First I find the maximum value. Then I arbitrarily set a threshold (in this case, half that value), taking advantage of the fact that the signals are pretty clean and there are not any spurious peaks. Doing so leads to something like this: Then you just have to find how many times the signal crosses that threshold, and divide it by two (because it will cross it twice for each peak, once when it rises and afterwards when it falls).

If you have any additional information a priori of the frequency of the peaks, you could add some hysteresis to make the algorithm more robust (i.e. if there are small zigzags in the signal, avoid counting them as threshold crossings).

• Adding a low pass filter to the front of this will make this good approach even more robust, especially in situations like Example 2. – Dan Boschen Oct 12 '18 at 11:46