Triangle function (derived from Sawtooth)

Here is a generalization of the sawtooth() function, that creates a triangle wave. The optional asymmetry argument, v, makes the triangle segments asymmetrical to the right or left. If v=1, the function is the same as a sawtooth, and if v=0 the function is the oppositely directed sawtooth. If the optional argument is omitted, the default v=0.5, and a symmetrical trangle is created. Values of v outside the range [0,1] are pinned to the nearest limit. See the sawtooth() help file for general usage details.

function tri(x, [v])
	variable x, v
	if (ParamIsDefault(v))
		v = 0.5
	endif
	if (v>=1)
		return sawtooth(x)
	elseif (v<=0)
		return 1 - sawtooth(x)
	else
		return sawtooth(x) * ((v - sawtooth(x))>=0) / v  +  sawtooth(-x) * ((1-v) - (sawtooth(-x)) >=0) / (1-v)
	endif
end

Attached is a graph with v=0.8
Nice snippet! The following is obvious if you look up the sawtooth doc, but who has time? The assignment returns a symmetric triangle wave of specified period, given a scaling x.


Variable period=1                                      // your favorite number goes here
TriangleWave = tri(2*pi*x/period)
Here's another way to generate a triangle waveform of symmetric duty cycle (v=0.5). Note that its amplitude and phase, as defined are different from tri, above. I found this, in all places, quote in Apple's Dictionary :)


Function tri1(t)		// triangle wave, period=1, amplitude=1, mean=0
	variable t
	variable temp = floor((4*t+1)/2)
	return (4*t - 2*temp) * (-1)^temp
End



John Bechhoefer
Department of Physics
Simon Fraser University
Burnaby, BC, Canada
Simple way to generate a triangle wave:

Make/O ddd=2*asin(sin(2*pi*x/20))/pi

... just add amplitude and period to taste :)

A.G.

Forum

Support

Gallery

Igor Pro 10

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More