Rect function with Array

8 ビュー (過去 30 日間)
kale
kale 2022 年 9 月 24 日
編集済み: Matt J 2022 年 9 月 24 日
I would like to write my Rect function and I declared the following input variables:
A = 2; %amplitude
T0 = 3; %period
t = [0:Ts:T0]; %time vector base (Ts = 0.01 = time sampling)
The function I wrote is as follows:
function [y] = rect(T0, A, t)
if t < -T0/2
y = 0;
elseif ((t >= -T0/2) & (t <= T0/2))
y = amplitude;
else y = 0; %if t > T0/2
end
I think the problem is the comparison (in the if and elseif) of the vector t with the scalar T0/2, in fact by putting a breakpoint I seem to have noticed that the comparison of the if is only performed on the first element of t (i.e. 0) .. and not with all the others.
I am asking you to suggest the right syntax to use and also (possibly) tell me if I have got the reasoning behind this function wrong.
I thought of doing a for (or while) loop inside the rect function where I increment t=t+Ts, but I would like to avoid this by using the vector t already declared incremented by Ts (as I did)
Thanks!

採用された回答

Matt J
Matt J 2022 年 9 月 24 日
編集済み: Matt J 2022 年 9 月 24 日
function [y] = rect(T0, A, t)
y= ((t >= -T0/2) & (t <= T0/2)) * A;
end
  2 件のコメント
kale
kale 2022 年 9 月 24 日
Thank you.
What if I want a train of pulses (rectangular) ?
How do you modify your function ?
Matt J
Matt J 2022 年 9 月 24 日
編集済み: Matt J 2022 年 9 月 24 日
You can use the function to create several pulses and add them together.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by