xmd79

Wave Generator

Feb 10th, 2023
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © peacefulLizard50262
  3.  
  4. //@version=5
  5. indicator("Wave Generator")
  6.  
  7. n = bar_index
  8.  
  9. max(source)=>
  10. var float max = na
  11. src = array.new<float>()
  12. array.push(src, source)
  13. max := math.max(nz(max[1]), array.get(src, 0))
  14.  
  15. min(source)=>
  16. var float min = na
  17. src = array.new<float>()
  18. array.push(src, source)
  19. min := math.min(nz(min[1]), array.get(src, 0))
  20.  
  21. min_max(src)=>
  22. ((src - min(src))/(max(src) - min(src)) - 0.5) * 2
  23.  
  24. f_sine_wave(_wave_height, _wave_duration, _phase_shift, _phase_shift_2) =>
  25. _pi = math.pi
  26. _w = 2 * _pi / _wave_duration
  27. _sine_wave = _wave_height * math.sin(_w * n + _phase_shift + _phase_shift_2)
  28. _sine_wave
  29.  
  30. triangle_wave(_wave_height, _wave_duration, _num_harmonics, _phase_shift) =>
  31. _pi = math.pi
  32. sine_waves = array.new<float>()
  33. for i = 1 to _num_harmonics
  34. array.push(sine_waves, f_sine_wave(_wave_height / math.pow(2 * i - 1, 2), _wave_duration / (2 * i - 1), (i - 1) * _pi, int(_phase_shift) * _pi))
  35. _triangle_wave = array.avg(sine_waves)
  36. min_max(_triangle_wave)
  37.  
  38. saw_wave(_wave_height, _wave_duration, _num_harmonics, _phase_shift_2) =>
  39. _pi = math.pi
  40. sine_waves = array.new<float>()
  41. for i = 1 to _num_harmonics
  42. array.push(sine_waves, f_sine_wave(_wave_height / (i), _wave_duration / (i), i * _phase_shift_2 * _pi, 0))
  43. _saw_wave = array.avg(sine_waves)
  44. min_max(_saw_wave)
  45.  
  46. ramp_saw_wave(_wave_height, _wave_duration, _num_harmonics, _phase_shift_2) =>
  47. _pi = math.pi
  48. sine_waves = array.new<float>()
  49. for i = 1 to _num_harmonics
  50. array.push(sine_waves, f_sine_wave(_wave_height / (i), _wave_duration / (i), i * _phase_shift_2 * _pi, 66))
  51. _ramp_saw_wave = array.sum(sine_waves)
  52. min_max(_ramp_saw_wave)
  53.  
  54. square_wave(_wave_height, _wave_duration, _num_harmonics, _phase_shift_2) =>
  55. _pi = math.pi
  56. sine_waves = array.new<float>()
  57. for i = 1 to _num_harmonics
  58. array.push(sine_waves, f_sine_wave(_wave_height / (2 * i - 1), _wave_duration / (2 * i - 1), (i * 2 - 1) * _phase_shift_2 * _pi, 0))
  59. _square_wave = array.avg(sine_waves)
  60. min_max(_square_wave)
  61.  
  62. wave_select(style, _wave_height, _wave_duration, _num_harmonics, _phase_shift_2)=>
  63. switch style
  64. "Sine" => f_sine_wave(_wave_height, _wave_duration, 0, _phase_shift_2)
  65. "Triangle" => triangle_wave(_wave_height, _wave_duration, _num_harmonics, _phase_shift_2)
  66. "Saw" => saw_wave(_wave_height, _wave_duration, _num_harmonics, _phase_shift_2)
  67. "Ramp Saw" => ramp_saw_wave(_wave_height, _wave_duration, _num_harmonics, _phase_shift_2)
  68. "Square" => square_wave(_wave_height, _wave_duration, _num_harmonics, _phase_shift_2)
  69.  
  70. fun = input.int(217, "Fundimental", 1)
  71. harmonics = input.int(1024)
  72. _phase_shift_2 = input.float(0, "Phase Rotation", step = 0.5)
  73. style = input.string("Sine", "Wave Select", ["Sine","Triangle","Saw","Ramp Saw","Square"])
  74.  
  75. wave = wave_select(style, 1, fun, harmonics, _phase_shift_2)
  76.  
  77. plot(wave)
Advertisement
Add Comment
Please, Sign In to add comment