Measure time difference peaks and stores it

조회 수: 8 (최근 30일)
mpz
mpz 2022년 8월 17일
댓글: Star Strider 2022년 8월 18일
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

채택된 답변

Star Strider
Star Strider 2022년 8월 17일
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)
time_diff = 4×1
0.9091 0.9091 0.9091 0.9091
figure
plot(t, y)
ylim([0 1.1])
The times are the same here because this funciton is periodic.
The pulsewidth function may also be helpful.
.
  댓글 수: 2
mpz
mpz 2022년 8월 18일
편집: mpz 2022년 8월 18일
@Star Strider I attached the data. Column 1 is the time (x-axis) and column is the output(y-axis). Based on my plot I only have 5 peaks but I got more time. Is there a way to only output the time diffference for peaks. Could you please take a look at my code below
clear all;clc;close all
% load('seperatedata.mat')
% data1 = sep{3,1}; % extracts for specific test
ycut = data1(:,3) > 7.5 & data1(:,3) < 9; % Determine what parts of the data is within the acceptable range
figure(5001)
plot(data1(:,1),ycut) % plots logic data
figure(5002)
plot(data1(:,1),data1(:,3))
figure(5003)
yyaxis right
plot(data1(:,1),ycut)
yyaxis left
plot(data1(:,1),data1(:,3))
% method 1 testing
% c=1;
% t=data1(:,1);
% while any(ycut>=1)
% f1=find(ycut>=1,1);
% ycut(1:f1-1)=[];
% f2=find(ycut<=0,1);
% ycut(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
% method 2 testing
t=data1(:,1);
p1 = islocalmax(ycut, 'FlatSelection','first');
p2 = islocalmax(ycut, 'FlatSelection','last');
numidx = sort([find(p1) find(p2)]);
time_diff = diff(t(numidx),[],2)
Here is the output I got.
time_diff =
369.5200
299.9560
0.6200
236.0960
2.6040
308.2640
470.4560
0.7440
Here is approximately what I expected for the time difference based on my data1.mat
369.2
299.5
236.4
307.5
471
Star Strider
Star Strider 2022년 8월 18일
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'))
LD = struct with fields:
data1: [56060×3 double]
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)
Results = 323×3 table
StartTime EndTime time_diff _________ _______ _________ 11462 11463 1.736 11473 11475 1.736 11499 11513 13.64 12938 16656 3718.3 16657 16661 3.472 16661 16674 12.524 16674 16676 1.364 16676 16677 0.868 16678 16678 0.248 16678 16680 1.24 16680 16681 0.62 16681 16681 0.248 16682 16683 0.62 16685 16685 0 16685 16687 2.356 16688 16688 0
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.
.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

David Hill
David Hill 2022년 8월 17일
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
  댓글 수: 2
mpz
mpz 2022년 8월 17일
@David Hill if you don't mind could you add short descriptions of what each part of the code does so I can understand and possibly modify for my needs
mpz
mpz 2022년 8월 17일
I copy and pasted your code but the time, t, gave me continous time result. Is it possible to make it only output the time difference when there is a peak of 1 only and ignores the others when the line is at zero?

댓글을 달려면 로그인하십시오.


mpz
mpz 2022년 8월 18일
Yes that data is noisy that's why I used a boolean to only pick the ones that met my threshold. Thanks

카테고리

Help CenterFile Exchange에서 Signal Generation and Preprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by