# Finding number of pulses in a data set?

2 views (last 30 days)
Joey Parker on 6 May 2020
Commented: Michael Soskind on 6 May 2020
I have a bit_stream.mat data file where it contains a single row of 1's and 0's in a random alternating sequence (1x664 double in workspace). How do I go about finding the number of pulses in that data set?

Michael Soskind on 6 May 2020
Hi Joey,
Looks like findpeaks should be your friend here.
plot(bit_stream)
[ones,loc_ones,w,p] = findpeaks(bit_stream);
numel(ones) % number of peaks with respect to
[pulses,loc_pulses] = findpeaks(bit_stream, 'MinPeakWidth', 2);
numel(pulses) % number of peaks with a value of 1 2 or more subsequent times
Best,
Michael

Show 1 older comment
Michael Soskind on 6 May 2020
Oh sure, those simply count the number of elements in an array see more about numel here
I like it a bit more than length or size, although they all provide similar functionality.
The two answers are for slightly different cases. The first is for the number of pulses of any length, and then the second case gives an example if you have a requirement for a particular pulse length.
Joey Parker on 6 May 2020
Thank you that makes perfect sense. I have an additionally question if it involves the same function but for finding the pulse period of that data set?
Michael Soskind on 6 May 2020
So it depends, usually, mathematically that would come from the fft of the signal. Hoewever, given that the pulses do not have a particular period, as they are pretty random in the example data you provided, the sidebands you would want are quite small. However, you could still use the following code:
% Using FFT to find the most prevalent period
figure(); % defining a figure to plot the fft in
plot(fftshift(abs(fft(bit_stream)))); % pefrforming an fft of the data (shifting to have 'zero mean', but this )
% from the figure above, looks to be a period of 4, as there is a shift of four between each of the peaks
% More trivial method of taking the average of the distance between the different elements
figure(); % defining a figure to plot the differences in
plot(diff(loc_ones)); hold on; % plotting the differences of loc_ones using the diff function
plot(1:numel(loc_ones), repmat(mean(diff(loc_ones)),1,numel(loc_ones))); % plotting the average, calculated as mean(diff(loc_ones))
The second set of code plots that the average of the difference between the different functions is in fact close to 4, confirming the effectiveness of the fft. However, taking the average of the differences might be easier.
The mean function and diff function are all you really need, just thought it might be of interest to see the similarities of two different methods.