How to find the zero crossing in x and time data sets?

조회 수: 298 (최근 30일)
vimal kumar chawda
vimal kumar chawda 2021년 6월 9일
편집: Stefan Schuberth 2022년 7월 27일
How can I find the zero crossing in the data sets?
figure()
plot(x,t)
  댓글 수: 3
vimal kumar chawda
vimal kumar chawda 2021년 6월 10일
Yes we dont have 0.000000000000 so we have range is 10^-3 so all the value fall under 10^-3 are accepted. Now how can we do it as we have two sets x& t?
Rena Berman
Rena Berman 2021년 8월 25일

(Answers Dev) Restored edit

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

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 6월 9일
편집: Scott MacKenzie 2021년 6월 10일
Here's what I put together. The variable fCross is what you are looking for.
% data from posted matlab.mat files
f = readmatrix('testdata1.txt');
t = readmatrix('testdata2.txt');
tiledlayout(3,1);
nexttile;
plot(t,f);
hold on;
axis([1 10 -1.2 1.2]);
nexttile;
fAbove = f .* (f >= 0);
fBelow = f .* (f <= 0);
area(t, fAbove, 'FaceColor', 'r');
hold on;
area(t, fBelow, 'FaceColor', 'g');
axis([1 10 -1.2 1.2]);
nexttile;
fCrossRaw = find(diff(fAbove>0));
fCross = fCrossRaw ./ length(t)*10; % as per axes
plot(fCross, zeros(1,length(fCross)), '*r');
hold on;
axis([1 10 -1.2 1.2]);
xline(fCross, 'color', [.7 .7 .7]);
yline(0, 'color', [.7 .7 .7]);
  댓글 수: 2
vimal kumar chawda
vimal kumar chawda 2021년 6월 10일
thank you sir.
Scott MacKenzie
Scott MacKenzie 2021년 6월 10일
@vimal kumar chawda You're welcome. I just updated my answer to make the 3rd plot look a bit better.

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

추가 답변 (2개)

Stefan Schuberth
Stefan Schuberth 2022년 7월 27일
편집: Stefan Schuberth 2022년 7월 27일
If you have (x,y) data and want to do it without using loops try that:
i=find(y(1:end-1).*y(2:end)<0); % index of zero crossings
m=(y(i+1)-y(i))./(x(i+1)-x(i)); % slope
x0=-y(i)./m+x(i); % x coordinates of zero crossings linear interpolated

Joel Lynch
Joel Lynch 2021년 6월 9일
편집: Joel Lynch 2021년 6월 9일
idx = find( f(2:end).*f(1:end-1)<0 )
Will return the left-hand indicies of cross-over points.
To get the exact X-values where the cross-over occurs, you would have to do some linear intepolation (inverted)
t_zero = zeros(size(idx));
for i=1:numel(idx)
j = idx(i); % Need both the index in the idx/t_zero vector, and the f/t vector
t_zero(i) = interp1( f(j:j+1), t(j:j+1), 0.0, 'linear' );
end
Note: this will fail if the cross-over happens on the last i value (i+1 would extend outside the range of the dataset)
  댓글 수: 4
vimal kumar chawda
vimal kumar chawda 2021년 6월 11일
It worked thanks.
Scott MacKenzie
Scott MacKenzie 2021년 6월 11일
편집: Scott MacKenzie 2021년 6월 29일
Yes, Joel's code gives the exact cross-over point. Bear in mind, however, that this is exact for the linearly interpolated data. The actual data are empirical, so it is not possible to know the exact cross-over point.
It probably doesn't matter much in this case, since the data appear to be gathered at a high sampling rate.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by