Reputation:
I have a signal composed of square pulses (+ some noise), here's a tiny part of it:
I look for an efficient and robust way to count how many pulses I have.
Here's what I've done so far:
The amplitude is a bit noisy but SNR is great do I can threshold:
data = data>1;
the length of each pulse can be noisy so I ignore it and use diff
, to obtain the derivatives (+ and -), find how many non-zero elements there are, and divide by 2 (since there are 2 derivative peaks per pulse) .
dd=diff(data);
num_of_pulses=length(find(diff(dd)))/2
Is that the best way to do that? I was told not to use diff
because it can be too noisy...
Upvotes: 2
Views: 4028
Reputation: 10708
Based on your description of the data, I think this will work.
numberOfPulses = nnz(diff(data > 1) > 0)
You can reliably find pulse samples using data > 1
, then use diff() > 0
find the transitions from no pulse to pulse, and finally nnz()
to count them.
Upvotes: 3