Advertisement
xmd79

Harmonic Sine Waves

Feb 26th, 2023
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. //@version=4
  2. study("Harmonic Sine Waves", overlay=true)
  3.  
  4. // Define pi constant
  5. pi = 3.141592653589793
  6.  
  7. // User Input: Length of wave
  8. length = input(title="Length", type=input.integer, defval=40)
  9.  
  10. // User Input: Amplitude, Offset, and Phase Shift of Base Sine Wave
  11. amp = input(title="Base Amplitude", type=input.float, defval=1.0)
  12. offset = input(title="Base Offset", type=input.float, defval=0.0)
  13. phase_shift = input(title="Base Phase Shift", type=input.float, defval=0.0)
  14.  
  15. // Draw base sine wave
  16. base = amp * sin(2 * pi * (bar_index + phase_shift) / length) + offset
  17. plot(base, color=color.blue, linewidth=2, title="Base Sine Wave")
  18.  
  19. // Draw 3 harmonic sine waves
  20. h1 = 2 * base * sin(2 * pi * (bar_index + phase_shift) / (length / 2))
  21. h2 = base / 2 * sin(2 * pi * (bar_index + phase_shift) / (length * 2))
  22. h3 = base / 4 * sin(2 * pi * (bar_index + phase_shift) / (length * 4))
  23. plot(h1, color=color.red, linewidth=2, title="Harmonic Sine Wave 1")
  24. plot(h2, color=color.green, linewidth=2, title="Harmonic Sine Wave 2")
  25. plot(h3, color=color.orange, linewidth=2, title="Harmonic Sine Wave 3")
  26.  
  27. // Sum all sine waves
  28. sum_wave = base + h1 + h2 + h3
  29. plot(sum_wave, color=color.rgb(54, 69, 56), linewidth=2, title="Sum of All Sine Waves")
  30. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  31. // © bartekhl
  32.  
  33. //@version=5
  34. plot(close)
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement