0

I need to generate a signal from the next formula:

enter image description here

I am using next code for that purpose:

function genArray<TOut>(size: number, generator: (i: number) => TOut): TOut[] {
  const res = [];
  for(let i = 0; i<size; ++i) {
    res.push(generator(i));
  }

  return res;
}

  getSignal(N = 512) {
    const b1 = 0.01;
    const b2 = 100;
    const signal = genArray(
      N,
      i => {
        const a = b1 * Math.sin(2 * Math.PI * i / N);
        let b = 0;
        for (let j = 50; j <= 70; ++j) {
          b += Math.pow(-1, Math.random()) * b2 * Math.sin(2 * Math.PI * j * i / N);
        }
        console.log(a, b);
        return a + b;
      }
    );

    return signal;
  }

That line: Math.pow(-1, Math.random()) returns NaN, because: enter image description here

Is it possible to handle such case in JS?

PS. Sorry, if it is obvious, I don't know how can I google that.

InSync
  • 4,851
  • 4
  • 8
  • 30
Nikita
  • 1,019
  • 2
  • 15
  • 39
  • I suggest looking into the npm package "complex". That would allow you to do all the computations with complex numbers. You could write your own solution, but that seems a little difficult. – Pointy Mar 26 '23 at 13:33
  • @InSync I think the OP has already figured out that the code is producing `NaN`. – Pointy Mar 26 '23 at 13:34
  • 1
    do you want things to be in complex or do you want your random number to become an integer – cmgchess Mar 26 '23 at 13:36
  • 1
    What exactly is that formula? What does the exponent mean? – Pointy Mar 26 '23 at 13:41
  • @Pointy That formula for generating a noisy signal - x(i), where i belongs to range from 0 to N. Then I am drawing result on the chart. So the `getSignal` returns array of values of the noisy signal in particular ticks. – Nikita Mar 26 '23 at 13:44
  • https://openbase.com/js/mathjs/alternatives – mplungjan Mar 26 '23 at 13:45
  • @cmgchess I need a number, so I can draw point on the chart, so that number is x(i) – Nikita Mar 26 '23 at 13:46
  • I don't know if there's a ready made function that will do it but z^(x + iy) = (z^x)(cos(y ln(z)) + i sin(y ln(z))). – Simon Goater Mar 26 '23 at 13:54
  • Well if you're raising a negative number to a fractional power, the built-in Math function `.pow()` is going to behave as you noted. That's why I suggested converting the function to use complex numbers and that library I mentioned. – Pointy Mar 26 '23 at 14:28
  • There is no real need to import a whole library for complex maths as `Math.pow(-1, Math.random())` is the only term that will be complex. And this is easily solved by conventional means: the result will be a complex number with an absolute value of `1` and an arbitrary angle in the complex number plane (from `0` to `2π`). So, in order to get your "point" in the complex number plane you simply calculate your two `sin`-based terms and apply an arbitrary phase angle to the second one, making it contribute to the real and imaginary parts of the total result according to that angle. – Carsten Massmann Mar 26 '23 at 14:53
  • @CarstenMassmann thank you, it is really make sense! jfyi, today that task was updated and `Math.pow(-1, Math.random())` has been changed for that formula to `Math.random() > 0.5 ? 1 : 0` =)) – Nikita Mar 27 '23 at 13:18

0 Answers0