views:

142

answers:

2

Hi,

I'm trying to control a parameter with AE expressions and I need a triangle wave. I've got this so far:

freq = 20;
amplitude = 8; 

m = amplitude;
i = time*freq;

m - (i % (2*m) - m);

Unfortunately this gives a saw wave (see below) and my math's a bit rusty, any takers?

Thanks!

PJ

http://i25.photobucket.com/albums/c73/pjburnhill/Screenshot2010-04-09at153815.png

+1  A: 

Seems to have already been answered

http://stackoverflow.com/questions/1073606/is-there-a-one-line-function-that-generates-a-triangle-wave

Peter Bailey
Yeah I saw that but After Effects doesn't seem to recognise abs function, maybe it's not implemented in AE?
PJ Palomaki
What about `Math.abs()`? If not, you can always write your own (if AE lets you define functions)
Peter Bailey
Correct! Math.abs() did the trick, silly me.Thanks Peter!
PJ Palomaki
+1  A: 

An easy way to get a triangle wave from here would be to subtract m/2 and take the absolute value. Alternatively, use f = m - abs(i % (2*m) - m).

John Feminella
See above, AE doesn't seem to allow abs... Am I missing out something here?
PJ Palomaki
Use `Math.abs()`. It's in the Javascript pulldown, I think.
John Feminella
Thanks John! That's it, got it working.Much appreciated.
PJ Palomaki
Just a (hopefully) final question, how can I get the triangle wave to give me negative values as well? So oscillating between i.e. 16 and -16?The line is currently like this: x = m - Math.abs(i % (2*m) - m);
PJ Palomaki
What's it oscillating between right now?
John Feminella
Whis this code it oscillates between 0 and 8:freq = 20;nd amplitude = 8; m = amplitude;i = time*freq;m - Math.abs(i % (2*m) - m);How can I get it to go to -8?Thanks!
PJ Palomaki
Make it `2 * Math.abs(...) - m`.
John Feminella
Hmm... still doesn't give negative values.. now oscillates between 0 and 16. Code: `2 * Math.abs(i % (2*m) - m);`
PJ Palomaki
You didn't write what I said. Keep the part in the `abs` the same as in post, but make it `2 * Math.abs(...) - m`.
John Feminella
Ahhh... my bad! So finally this code works:`2 * Math.abs(i % (2*m) - m) - m;`Thanks John, very much appreciated!
PJ Palomaki
Erhm... and how would I get the wave to start from negative value instead of a positive?
PJ Palomaki
Shift `i` accordingly (e.g. by making `i +/- [something]`).
John Feminella
Thanks again John!
PJ Palomaki