フィルターのクリア

Adding zero points on a plot of a damped sine wave - quick question

1 回表示 (過去 30 日間)
Jesse
Jesse 2015 年 2 月 13 日
コメント済み: Geoff Hayes 2015 年 2 月 21 日
Greetings all,
This will probably be a quick one for you experts, but I have a damped sine wave of:
y1=2*sin(w*t).*exp(-lambda*t);
The user inputs w and lambda, and I have t=0:.001:1;
Now, since the sine wave crosses zero, I would like to add a circle or X where that crossing is just to highlight it. I played around with for and if statements, but not getting it, even though it's kinda trivial.
Any assistance?
Thanks!
-J

採用された回答

Geoff Hayes
Geoff Hayes 2015 年 2 月 13 日
Jesse - you could compare each element to its neighbour and see if there is a switch in sign i.e. from positive to negative or negative to positive. The easiest way to do this, without a for loop, is to use arrayfun and just check to see if each neighbour is different. Something like
idcs = arrayfun(@(k)sign(y1(k))~=sign(y1(k+1)),1:length(y1)-1);
We use sign to check the sign of the number, and if the sign between two neighbouring elements is different then
sign(y1(k))~=sign(y1(k+1)
is true (or 1). Note how we use the anonymous function
@(k)sign(y1(k))~=sign(y1(k+1))
to take as an input k which will be an index into y1. The resulting logical array, idcs, will have ones where there is a difference in sign which is the zero crossing. Use find to find those indices as
find(idcs==1)
which you can then use to draw your X or circle at the zero crossing. Try the above and see what happens!
  4 件のコメント
Jesse
Jesse 2015 年 2 月 20 日
Yes that's it. Thanks Geoff!
Geoff Hayes
Geoff Hayes 2015 年 2 月 21 日
Glad it worked out, Jesse!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by