defmodule NxSignal.WaveformsTest do
  use NxSignal.Case, async: true
  doctest NxSignal.Waveforms

  test "sawtooth scipy regression" do
    t = Nx.linspace(0, 6, n: 20)

    width_1 =
      Nx.tensor([
        -1.0,
        -0.8994811177253723,
        -0.7989621758460999,
        -0.6984432935714722,
        -0.5979243516921997,
        -0.497405469417572,
        -0.39688658714294434,
        -0.2963676452636719,
        -0.1958487629890442,
        -0.0953298807144165,
        0.005189061164855957,
        0.10570800304412842,
        0.20622682571411133,
        0.30674564838409424,
        0.40726470947265625,
        0.5077836513519287,
        0.6083024740219116,
        0.7088212966918945,
        0.809340238571167,
        0.909859299659729
      ])

    assert NxSignal.Waveforms.sawtooth(t) == width_1
    assert NxSignal.Waveforms.sawtooth(t, width: 1) == width_1

    assert NxSignal.Waveforms.sawtooth(t, width: 0.2) ==
             Nx.tensor([
               -1.0,
               -0.497405469417572,
               0.005189061164855957,
               0.5077835321426392,
               0.997405469417572,
               0.8717568516731262,
               0.7461082339286804,
               0.6204595565795898,
               0.49481093883514404,
               0.36916232109069824,
               0.24351368844509125,
               0.11786506325006485,
               -0.007783569395542145,
               -0.13343210518360138,
               -0.25908082723617554,
               -0.3847295641899109,
               -0.5103781223297119,
               -0.6360266208648682,
               -0.7616753578186035,
               -0.8873240947723389
             ])

    assert NxSignal.Waveforms.sawtooth(t, width: 0) ==
             Nx.tensor([
               1.0,
               0.8994811177253723,
               0.7989621758460999,
               0.6984432935714722,
               0.5979243516921997,
               0.497405469417572,
               0.39688655734062195,
               0.29636767506599426,
               0.1958487629890442,
               0.09532985836267471,
               -0.005189046263694763,
               -0.10570795089006424,
               -0.20622685551643372,
               -0.3067456781864166,
               -0.40726467967033386,
               -0.5077836513519287,
               -0.6083024740219116,
               -0.7088212966918945,
               -0.8093402981758118,
               -0.9098592400550842
             ])
  end
end
