How can I create an impulse wave via triangular pulse

I need to create an impulse wave, through a triangular pulse. I succeded that with the rectangular, but the triangular doesn't seem to work for me.
body of my code:
syms a;
a=0.1;
t=linspace(-0.2,0.2,1000000);
y=zeros(1,1000000);
x=abs(t);
y(x<a)=(1/a)*(1-x);
plot(t,y);
grid;
The error matlab shows me is:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in rectangular_pulse (line 133) (the file name is rectangular pulse)
y(x<a)=(1/a)*(1-x);

 採用された回答

Mathieu NOE
Mathieu NOE 2020 年 11 月 9 日

0 投票

hello
this solves the assignement problem, but I suspect the shape of the signal is not what you are waiting for
y(x<a)=(1/a)*(1-x(x<a));

6 件のコメント

nastia korzoun
nastia korzoun 2020 年 11 月 9 日
編集済み: nastia korzoun 2020 年 11 月 9 日
It indeed solves the assignement problem, yet the shape is weird.
thanks anyway!
Mathieu NOE
Mathieu NOE 2020 年 11 月 9 日
could it be this is what you wanted ?
a=1;
N = 1000000;
y = a - abs(linspace(-a,a,N));
x=(0:N-1);
plot(x,y);
grid;
nastia korzoun
nastia korzoun 2020 年 11 月 9 日
The triangle shape is perfect, it just wasn't exactly our assignment.
I just need to fighure out the y axis function, to make the total area of the triangle=1, as in an impulse function.
Mathieu NOE
Mathieu NOE 2020 年 11 月 9 日
well , with a = 1
the area is
area = 0.5*max(x)*max(y)
I got : area = 499999
NB : this is because the x axis is defined in samples not in seconds (what is the sampling frequency ?)
so you should convert the x axis is seconds, recalculate the area and divide the amplitude of the triangle by this number to get a unitary area
nastia korzoun
nastia korzoun 2020 年 11 月 9 日
yeah I started with declaring frequencies and limits, but then the proffesor explained that he wanted to keep things a lot more simple than that.
BUT
I finally figured it out! it was:
y(x<a)=(1/a)*(1-abs((1/a)*t(x<a)));
Thanks again!!!
Mathieu NOE
Mathieu NOE 2020 年 11 月 9 日
you're welcome

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by