Borrar filtros
Borrar filtros

Number of peaks within window

19 visualizaciones (últimos 30 días)
iwantrugs
iwantrugs el 5 de Feb. de 2019
Editada: Adam Danz el 6 de Feb. de 2019
I have a 27650x1 vector which consists of a bunch of peaks and troughs.
Each element corresponds to a time of 0.04s (so first element is at 0s, 2nd is at 0.04s, 3rd is 0.08 etc).
I'm trying to make a script that finds the number of peaks in a ten second window, then divides by 10, then stores that value in a seperate vector, then the window should shift along by one second and repeat the process.
I've tried using a for loop but it doesn't work exactly:
%Window start conditions
X = 1;
X_end = (250);
Y = length(data);
for N = 1:Y
n_pks = numel(findpeaks(data(X:X_end))); %Determine number of peaks in window
bpm = (n_pks/10);
RPM(N) = bpm; %Store in seperate vector
X = X+25; %Moves start of window one second ahead
X_end = X + 250; %Keeps whole window to width of ten seconds
end
I'm sure there's many problems with the code above but the glaring one is that when it approaches the end of the length of the input data, the index parameters to find the number of peaks becomes larger than the array itself so it returns an error.
I feel like there is a way of doing this without the loop but I'm not sure how.
Any help is appreciated.
  4 comentarios
Sargondjani
Sargondjani el 5 de Feb. de 2019
yes. that way you ensure that X_end is always in your vector.
iwantrugs
iwantrugs el 5 de Feb. de 2019
I see. That helps alot. Do you think there is a way to do what I am trying to without using a for loop though?
I feel like I'm overcomplicating something relatively simple

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 5 de Feb. de 2019
Editada: Adam Danz el 6 de Feb. de 2019
Here's a solution that uses findpeaks() (<- link) and applies it to a moving window through your data. You might need to play around with the input options to findpeaks to suit your needs.
The code starts by setting the parameters you described. It then calculates the moving window and has a couple sanity checks to make sure your parameters make sense.
It then moves a window of time through your data to count the number of peaks and stores that number, divided by 10, in a vector.
The sample code runs without error but you should thoroughly test it with your data and make sure the findpeaks() function is suitable for your data.
% fake data
data = rand(27650, 1);
% Set parameters
sampleRate = 0.04; %time interval (sec)
windowSize = 10; %time (seconds), moving window size
windowIncr = 1.0; %time (seconds), window increment
n = length(data); %number of data points
start = 0; %staring point (time, sec)
% Calculate end time
stopTime = n * sampleRate - start;
% create vector of time stamps (this isn't needed but you might want it for plots, etc.)
time = start : sampleRate : stopTime;
% Window index size
windowIdxSz = windowSize / sampleRate; % !! This must be an integer!
if mod(windowIdxSz,1) ~= 0
error('Window size divided by sample rate must be an integer')
end
% initialize window index
windIdx = 1 : windowIdxSz;
% Number of indicies to shift window
windShift = windowIncr/sampleRate; % !! This must be an integer
if mod(windShift,1) ~= 0
error('Window increment divided by sample rate must be an integer')
end
% determine number of loops needed
nLoops = floor((n-start)/windowIdxSz); %floor() results in skipping leftovers outside of final window
% Loop through your data
nPeaks = nan(nLoops, 1);
for i = 1:nLoops
% find peaks within your windowed data
pks = findpeaks(data(windIdx)); % Play around with this: https://www.mathworks.com/help/signal/ref/findpeaks.html
% store the number of peaks, divided by 10
nPeaks(i) = length(pks)/10;
% shift window
windIdx = windIdx + windShift;
end

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by