필터 지우기
필터 지우기

Cut Sinusoidal signal in a integer number of periods

조회 수: 6 (최근 30일)
youjarr
youjarr 2019년 7월 9일
댓글: Julia Gionet-Gonzales 2024년 6월 19일
Hey guys,
I have an measurement of three sinusoidal signals.
But it is enough to only work with one in this case.
The measurement doesn´t have an int number of periods and i need to cut for my further work.
To make it more clear here is an picutre:
In this case we have 4 periods.
2019-07-09_09-41-27.png
So i have to find the first negative to positive zero corssing and the last negative to positive zero crossing.
I found this code in the forum but it don´t work with my files but maby it can help u ?!
t = linspace(0,10*pi,200);
x = sin(t);
mcs = x .* circshift(x, [0 -1]);
zxix = find(mcs <= 0);
for k1 = 1:2:size(zxix,2)-1
zx(k1) = interp1(x(zxix(k1):zxix(k1)+1), t(zxix(k1):zxix(k1)+1), 0);
end
figure(1)
plot(t, x)
hold on
plot(zx, zeros(size(zx)), '+r', 'MarkerSize',10)
hold off
grid
I hope u can help me.
Thanks in advance
  댓글 수: 2
youjarr
youjarr 2019년 7월 9일
편집: youjarr 2019년 7월 9일
Now I got this:
with this:
index1 = find(y > 0, 1, 'first');
index2 = find(y > 0, 1, 'last');
yCropped = y(index1:index2);
xCropped = x(index1:index2);
But I still need to remove one half period... .
youjarr
youjarr 2019년 7월 9일
편집: youjarr 2019년 7월 9일
With this:
index1 = find(y > 0, 1, 'first');
index2 = find(y > 0, 971, 'last');
yCropped = y(index1:index2);
xCropped = x(index1:index2);
I get what i want but how can i find this 971 automtically?

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

채택된 답변

David Sanchez
David Sanchez 2019년 7월 9일
Hi,
bear in mind that the sampling of your curve might play an important role in the game.
The code below might help you out:
t = linspace(0,10*pi,200);
x = sin(t);
zero_crossings_pos = [];
for kind = 1:length(t)-1
if (x(kind) <= 0) && (x(kind+1) > 0)
zero_crossings_pos = [zero_crossings_pos kind];
end
end
figure,
plot(t,x,t,x,'o')
hold on
plot(t(zero_crossings_pos),x(zero_crossings_pos),'r*')
hold off
xlabel('x')
ylabel('sin(x)')
title('sin')
t_crop = t(zero_crossings_pos(2):zero_crossings_pos(5));
x_crop = x(zero_crossings_pos(2):zero_crossings_pos(5));
figure,
plot(t_crop,x_crop)
xlabel('x')
ylabel('sin(x)')
title('Cropped sin')
  댓글 수: 1
youjarr
youjarr 2019년 7월 9일
Thanks for your answer.
I tried the code and had to change a small thing but now it works:
t_crop = t(zero_crossings_pos(1,1):zero_crossings_pos(1,end));
x_crop = x(zero_crossings_pos(1,1):zero_crossings_pos(1,end));
Thank you very much.

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

추가 답변 (1개)

Roberto Rubino
Roberto Rubino 2022년 10월 21일
Here is a solution equivalent to the one of @David Sanchez, but faster when dealing with large vectors:
t = linspace(0,14*pi,1e6);
x = sin(t+1);
% Find the negative-to-positive crossings ==========================
Signx = sign(x);
zero_crossings_pos = find(diff(Signx)>0);
% =========================================================
t_crop = t(zero_crossings_pos(1)+1:zero_crossings_pos(end));
x_crop = x(zero_crossings_pos(1)+1:zero_crossings_pos(end));
plot(t,x)
hold on
plot(t_crop,x_crop)
legend('non-cropped sine','cropped sine')
xlabel('x')
ylabel('sin(x)')
title('Cropped sin')
  댓글 수: 1
Julia Gionet-Gonzales
Julia Gionet-Gonzales 2024년 6월 19일
Your code is still only finding the positive zero crossing. This edit will allow you to find both.
zero_crossings_pos = find(abs(diff(Signx)) == 2);

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

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by