1

Apologies in advance for a difficult-to-explain question, for which the answer might simply be "no"...

I have a value -1 >= n <= 1

I'd like to increase its amplitude, say by 2: 2*n

I'd also like to adjust its phase, say by .5: 2*n + .5

How can I do this so that when n increases past 1, it flips signs and reduces itself, for example: when n increases to 1.1, the result is actually .9.

I know this can be done using trig, but I'm having difficulty increasing its amplitude and shifting its phase - by the time I'm done doing both I get this expression:

Math.sin(n*1.57 + Math.PI/4)

And to that expression I still need to perform additional calcs/rounding to get my final values; the expression becomes complicated bloated.

I'm wondering if there's a simpler way to get the desired range/values, avoiding trig altogether? I imagine an operator similar to the modulo, except instead of "resetting" to 0, it reduces in value...

calipoop
  • 792
  • 9
  • 31
  • Could you please add a longer sequence of the result you desire, 5 to 10 elements long? – Lutz Lehmann Jan 15 '17 at 21:33
  • 1
    Do you want approximate sine/cosine values for (pi * x), such as [Bhaskara I's](https://en.wikipedia.org/wiki/Bhaskara_I's_sine_approximation_formula) or a a saw tooth function with period 4? – traktor Jan 15 '17 at 22:25
  • Thanks @Traktor53 - Bhaskara I's formula was very a interesting read/test. I think I just wanted to make sure there was no fancy operator (like %) that I'm neglecting. It seems using trig really is the simplest way to do what I want. – calipoop Jan 15 '17 at 22:55

2 Answers2

1

You can explicitly write out the formula in cases for one period, and use recursion for values outside the fundamental period.

E.g.

function notTrig(x) {
  switch (true) {
    case (x >= 0 && x < 1):
      return x
    case (x >= 1 && x < 2):
      return 2 - x
    default:
      notTrig(x - 2)
  }
}

This should give you a sawtooth signal with mean 1/2, amplitude 1/2, and period 2. You need to handle negatives as well: exercise left to asker ;-)

Edit: It occurs to me after the fact that I'm misusing the term "sawtooth wave" above. The function I am describing is continuous, and the terms I should be using is "triangle wave." That said, I am very pleased with @calipoop's answer.

Fried Brice
  • 769
  • 7
  • 20
  • 1
    Thanks for the reply. See my comment above re Traktor53 - while your solution will likely work, I was hoping to possibly learn of new secret operator or short 1-liner that can simplify my equation! In the end, I think my original code might be the best I'll do. – calipoop Jan 15 '17 at 23:01
  • Yeah, I noticed you were asking about a primitive operator. I don't think there's anything like that in Javascript or more generally in Math, aside from defining it yourself piecewise. Cheers! – Fried Brice Jan 15 '17 at 23:12
1

Turns out, a triangle wave solves my problem. It gives oscillating values similar to sine (without ease), and it avoids Math.trig and simplifies my formula. I expanded on the formula given in this SO answer as well as these wikipedia formulas.

Triangle wave with period 4 and amplitude 2

Fried Brice's answer suggesting sawtooth was on the right track - but triangle wave is better suited for me, and the 1-line formula makes my eyes feel better.

Community
  • 1
  • 1
calipoop
  • 792
  • 9
  • 31