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

 Accepted Answer

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 Comments

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!!!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!