# Count the number of "aggregate" peaks in a vector?

2 views (last 30 days)
Shovnik Paul on 23 Jul 2022
Commented: Star Strider on 24 Jul 2022
I have some data in a vector in matlab called voltage collected from a sound sensor. When plotted against a time vector containing every instant of time these values were recorded at, I get the figure below: As you can see, the data is "spikey" but there are some "spikes" that clearly larger and more noticesable than the surrounding - here I can count 10 of them. The sensor recorded sounds from a nearby buzzer - when the buzzer was silent, the voltage stayed at (around) the midway level (2.5V) but when it buzzed, it jumped to 5V, then to 0V and back, resulting in these larger spikes. This means that the buzzer beeped 10 times.
I want to count the number of these larger spikes which will tell me the number of times, the buzzer beeped.
1. My first thought was to set some arbitrary threshold like 5V and do a linear search of the voltage vector to check how many samples reached 5V but there were many samples within the same peak that did this - so that would be overcounting.
2. I also tried using the findpeaks functions which unfortunately also counts local maxima and returned many false positives even in the noisy regions around 2.5V where the buzzer is supposed to be silent.
Is there any good way to go about this? I am struggling to figure out what mathematical approach to take to analyse this. I have attached the .mat file containing the voltage and time vectors. Any help is appreciated. Thanks!

Star Strider on 23 Jul 2022
I am not certain what result you want.
Here, findpeaks finds 21 distinct spikes (and their locations)
time = LD.time;
voltage = LD.voltage;
[pks,locs] = findpeaks(voltage, 'MinPeakProminence',4.5);
mdl = mean(diff(locs));
figure
plot(time, voltage)
hold on
plot(time(locs), pks, '^r')
hold off
xlabel('time')
ylabel('voltage')
ylim([-1 6])
figure
histogram(diff(locs), 50)
The histogram is optional, and simply looks at how the differences in the ‘locs’ values are clustered.
The ‘time’ vector has irregular sampling intervals, so it will not be possible to do any sort of signal processing on these data as they currently exist. One way to deal with that is to use the resample funciton, however this results in strange ‘voltage’ values. However if you want to use it, one approach would be:
Fs = 1/mean(diff(time));
[voltagers,timers] = resample(voltage,time,Fs);
The approximate sampling frequency (calcualted from the mean of the differences in the ‘time’ vector) is about 83.9925 Hz (assuming the ‘time’ units are seconds).
This works in R2022a, and should procduce reliable results in R2018a.
.
##### 2 CommentsShowHide 1 older comment
Star Strider on 24 Jul 2022
As always, my pleasure!

### More Answers (1)

the cyclist on 23 Jul 2022
I am absolutely positively NOT an expert in this, but I think what might be helpful here is putting your signal through a low-pass filter. (You need the Signal Processing Toolbox for that.)
##### 1 CommentShowHide None
Shovnik Paul on 24 Jul 2022
Thanks for the reply. I actualy thought of doing something like that too in order to smoothen out the spikey regions - I used a first order filter of the form 1/(1 + T*s) from the Control System Toolbox by changing T - it wasnt of much help but I am also not an expert in signal processing - I will definitely try out the functions in the link you have provided. Thanks.

R2018a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!