Measure time difference peaks and stores it
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
mpz
el 17 de Ag. de 2022
Comentada: Star Strider
el 18 de Ag. de 2022
Hi,
Can anyone help me with a code to measure the time difference from the moment the peak is 1 till it goes back to zero and then stores it. Then it repeats again for the next peak and the next till it goes over all the peaks. So for example the graph below shows the first peak occurs at around 15197 seconds and ends at 15562 seconds, hence time difference is 365s which is then stored in a matrix. Then the code continues to the next peak and measures that time then stores it. It keeps doing that until it stores all those times.
find peak doesn't work well for my data to use widths and all that. I need some type of for and if loop. Any help appreciated
0 comentarios
Respuesta aceptada
Star Strider
el 17 de Ag. de 2022
It would help to have the data.
One approach would be to use the islocalmax function two times using the 'FlatSelection','first' with one function call and 'FlatSelection','last' with the second function call. Use the find function to get the numerical indices for each, then sort them and use reshape to create a (2xN) or (Nx2) matrix from them, if desired.
t = linspace(0, 10).'; % Code Assumes Column Vectors
y = sin(2*pi*0.5*t) >= 0;
p1 = islocalmax(y, 'FlatSelection','first');
p2 = islocalmax(y, 'FlatSelection','last');
numidx = sort([find(p1) find(p2)]);
time_diff = diff(t(numidx),[],2)
figure
plot(t, y)
ylim([0 1.1])
The times are the same here because this funciton is periodic.
.
2 comentarios
Star Strider
el 18 de Ag. de 2022
It is difficult to know how to deal with those data, since they are extremely noisy. I got the impression from the posted plot that they were limited to be between 0 and 1 and had flat tops. So I chose a threshold value of 100 (it can be any value you want) and created the data to have exactly those characteristics.
% t = linspace(0, 10).'; % Code Assumes Column Vectors
% y = sin(2*pi*0.5*t) >= 0;
LD = load(websave('data1','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1101295/data1.mat'))
t = LD.data1(:,1);
y = LD.data1(:,2);
yi = y;
thrshld = 100;
yi(yi < thrshld) = 0; % Threshold Data
yi(yi >= thrshld) = 1;
p1 = islocalmax(yi, 'FlatSelection','first');
p2 = islocalmax(yi, 'FlatSelection','last');
numidx = ([find(p1) find(p2)]);
time_diff = diff(t(numidx),[],2);
StartTime = t(p1);
EndTime = t(p2);
Results = table(StartTime,EndTime,time_diff)
figure
% plot(t, y)
plot(t, yi)
hold on
plot(t(p1), yi(p1), '>g')
plot(t(p2), yi(p2), '<r')
hold off
xlabel('Time')
ylabel('Thresholded Amplitude')
title('All Spikes')
ylim([0 1.1])
figure
% plot(t, y)
plot(t, yi)
hold on
plot(t(p1), yi(p1), '>g')
plot(t(p2), yi(p2), '<r')
hold off
% ylim([0 1.1])
axis([[1.14 1.16]*1E+4 0 1.1])
xlabel('Time')
ylabel('Thresholded Amplitude')
title('First Three Spikes')
p1idx = find(p1);
First3 = p1idx(1:3);
text(t(First3), yi(First3)/2, compose('\\Delta t = %7.3f',time_diff(1:3)), 'Horiz','left', 'Rotation',45)
At the chosen threshold level, the code detected 323 spikes. The ‘StartTime’ and ‘EndTime’ values in the table are rounded to the nearest integer. Change the format to see them with greater precision.
.
Más respuestas (2)
David Hill
el 17 de Ag. de 2022
I assume you have an t-array and a y-array.
c=1;
while any(y>=1)
f1=find(y>=1,1);
y(1:f1-1)=[];
f2=find(y<=0,1);
y(1:f2-1)=[];
deltaT(c)=t(f2)-t(f1);%provides an array of the peak time periods
t(1:f2-1)=[];
c=c+1;
end
Ver también
Categorías
Más información sobre Signal Generation and Preprocessing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!