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);

 Respuesta aceptada

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 comentarios

nastia korzoun
nastia korzoun el 9 de Nov. de 2020
Editada: nastia korzoun el 9 de Nov. de 2020
It indeed solves the assignement problem, yet the shape is weird.
thanks anyway!
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;
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.
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
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!!!
you're welcome

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 9 de Nov. de 2020

Comentada:

el 9 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by