Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use tripuls(T,w)

Status
Not open for further replies.

Guest
I need to create a triangular pulse with width of 5 samples and amplitude of one at t=1,10, 15. Please show an example.

Thanks.
 
Replies continue below

Recommended for you

The simple way to generate a triangle pulse is to use the triang command in the signal processing toolbox. Also, below is a code that performs the same function as triang, with a different (hopefully clearer) algorithm.

Good Luck
J. Vorwald


function test

% Initalize time and y vector (y set to zero)

dt = 1;
t = 0:dt:20;
y = t*0;

% Get a the triangle shape using the width of the base

% triSize = 6;
triSize = 5;
triShape=triang(triSize);

% insert the base at times 1.0, 10.0, and 15.0

y = insertShape(t, y, triShape, 1.0);
y = insertShape(t, y, triShape, 10.0);
y = insertShape(t, y, triShape, 15.0);
plot(t,y);

% Set the maximum y axis to one
a = axis;
a(4) = 1;
axis(a);


function yout = insertShape(t, y, triShape, tstart);

% Insert the shape (triShape) at time tstart

triSize = size(triShape,2);
istart = find( t == tstart );
ichange = [1:triSize] + istart -1;
yout = y;
yout( ichange ) = triShape;


function triangle = triang(nPoints)

%TRIANG Triangular window.
% triangle = TRIANG(nPoints) returns the N-point triangular window.

% For a triangle, the ramp up and ramp down are the symmetric
% For a triangle
% a) The number of points going up/down is (nPoints-1)/2
% b) The peak ramp value is (nRamp - 1) / nRamp;
% For a triangle with an odd number of points
% c) The triangle reaches the maximum value at the mid point, (Npoints-1)/2 + 1

% use a flag to indicate even/odd (0 - even, 1 - odd)

flagEvenOdd = rem(nPoints,2);

nRamp = (nPoints - flagEvenOdd) / 2;

ramp = [0:nRamp-1] / nRamp;
rampUp = ramp;
rampDown = rampUp(nRamp:-1:1);

if (flagEvenOdd == 1)
rampUp = [rampUp 1];
end

triangle = [ rampUp rampDown ];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor