MATLAB: Identify ‘Presses’ From Continuous Data

continuous datagroupingpressesthreshold

Hi everyone,
I'm using a force transducer to measure finger presses. This device gives me continuous data across 10 channels (one column for each finger) and I want to identify where any presses reach a threshold.
Currently, I index using my threshold and receive a logical matrix of 1s and 0s, where 1 is above threshold. How can I identify these as presses, where a channel may reach threshold once, descend to below threshold, and then exceed the threshold again, which would count as 2 presses? Currently participants are only required to make one press per finger, and I'd like to punish them on any trials where they make any additional presses.
Thanks in advance,
Rhys

Best Answer

Let's say you call your continuous data from one channel x (you can generalize this later to a matrix over the 10 channels).
Lets find all the points that are over the specified threshold and call these isPress
isPress = x>threshold
Now you want to find all the instances where it goes from not being pressed to being pressed. You can do this using diff
transition = diff(isPress) % will be 1 when goes to press level and -1 when it goes back to normal
You can count how many positive transitions, which is the number of times the key is pressed using
numPress = sum(transition==1)
I assume that when you say "I'd like to punish them on any trials where they make any additional presses". That this only means taking away points, or something like that. I certainly wouldn't want to help with any experiment that actually involved physical or any other form of actual "punishment"
Related Question