xmd79

MathSpecialFunctionsDiscreteFourierTransform

Oct 11th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 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. // © RicardoSantos
  3.  
  4. //@version=5
  5.  
  6. //@description Method for Complex Discrete Fourier Transform (DFT).
  7. library(title='MathSpecialFunctionsDiscreteFourierTransform')
  8.  
  9. import RicardoSantos/MathComplexCore/1 as complex
  10. import RicardoSantos/MathComplexOperator/1 as complex_op
  11. import RicardoSantos/MathComplexArray/1 as complex_array
  12.  
  13. // @function Complex Discrete Fourier Transform (DFT).
  14. // @param inputs float array, pseudo complex array of paired values [(a0+b0i), ...].
  15. // @param inverse bool, invert the transformation.
  16. // @returns float array, pseudo complex array of paired values [(a0+b0i), ...].
  17. export dft (float[] _inputs, bool _inverse) => //{
  18. int _size = int(math.floor(array.size(id=_inputs) / 2))
  19. float _pi = 3.14159265359
  20. float _pi2 = _inverse ? 2.0 * _pi : -2.0 * _pi
  21. float _invs = 1.0 / _size
  22. //| @return:
  23. //| _output: Complex array
  24. float[] _output = complex_array.new(_size, complex.new(0.0, 0.0))
  25.  
  26. for _y = 0 to _size - 1 by 1
  27. for _x = 0 to _size - 1 by 1
  28. _a = _pi2 * _y * _x * _invs
  29. _ca = math.cos(_a)
  30.  
  31. _sa = -math.sin(_a)
  32. float[] _complex_input = complex_array.get(_inputs, _x)
  33. float[] _complex_output = complex_array.get(_output, _y)
  34. float _input_real = complex.get_real(_complex_input)
  35. float _input_imaginary = complex.get_imaginary(_complex_input)
  36. float _output_real = complex.get_real(_complex_output)
  37. float _output_imaginary = complex.get_imaginary(_complex_output)
  38. float _add_real = _output_real + _input_real * _ca - _input_imaginary * _sa
  39. float _add_imaginary = _output_imaginary + _input_real * _sa + _input_imaginary * _ca
  40. complex_array.set(_output, _y, complex.new(_add_real, _add_imaginary))
  41.  
  42. if not _inverse
  43. float[] _mult = complex_op.multiply(complex_array.get(_output, _y), complex.new(_invs, _invs))
  44. complex_array.set(_output, _y, _mult)
  45. _output
  46. //{ usage:
  47. var float[] inputs = complex_array.new(8, complex.new(0.0, 0.0))
  48.  
  49. if barstate.islast
  50. for _i = 0 to 8 by 1
  51. complex_array.set(inputs, _i, complex.new(close[_i], _i))
  52.  
  53. // plot(series=f_Sre(0), color=acol, linewidth=aline, style=plot.style_histogram, transp=20, histbase=0, offset=-0, show_last=1)
  54. // plot(series=f_Sim(0), color=bcol, linewidth=bline, style=plot.style_histogram, transp=20, histbase=0, offset=-0, show_last=1)
  55.  
  56. float[] S = dft(inputs, false)
  57. float[] A = dft(S, true)
  58.  
  59. var table T = table.new(position.bottom_right, 3, 9, border_width=1)
  60. table.cell(T, 0, 0, 'Original', text_color=color.silver, text_size=size.small, bgcolor=color.gray)
  61. table.cell(T, 1, 0, 'DFT', text_color=color.silver, text_size=size.small, bgcolor=color.gray)
  62. table.cell(T, 2, 0, 'Inverse', text_color=color.silver, text_size=size.small, bgcolor=color.gray)
  63.  
  64. if barstate.islast
  65. for _l = 0 to math.floor(array.size(inputs) / 2) - 1 by 1
  66. table.cell(T, 0, _l+1, str.tostring(complex_array.get(inputs, _l)), text_size=size.small, bgcolor=color.silver)
  67. table.cell(T, 1, _l+1, str.tostring(complex_array.get(S, _l)), text_size=size.small, bgcolor=color.silver)
  68. table.cell(T, 2, _l+1, str.tostring(complex_array.get(A, _l)), text_size=size.small, bgcolor=color.silver)
  69.  
  70. // interface to validate if there is values:
  71. f_Sre(_i) => array.size(S) > 0 ? complex.get_real(complex_array.get(S, _i)) : na
  72. f_Sim(_i) => array.size(S) > 0 ? complex.get_imaginary(complex_array.get(S, _i)) : na
  73.  
  74. color acol = input(#0000ff50)
  75. color bcol = input(#8080a050)
  76.  
  77. int aline = 8
  78. int bline = 3
  79. plot(series=f_Sre(1), color=acol, linewidth=aline, style=plot.style_histogram, histbase=0, offset=-1, show_last=1)
  80. plot(series=f_Sim(1), color=bcol, linewidth=bline, style=plot.style_histogram, histbase=0, offset=-1, show_last=1)
  81. plot(series=f_Sre(2), color=acol, linewidth=aline, style=plot.style_histogram, histbase=0, offset=-2, show_last=1)
  82. plot(series=f_Sim(2), color=bcol, linewidth=bline, style=plot.style_histogram, histbase=0, offset=-2, show_last=1)
  83. plot(series=f_Sre(3), color=acol, linewidth=aline, style=plot.style_histogram, histbase=0, offset=-3, show_last=1)
  84. plot(series=f_Sim(3), color=bcol, linewidth=bline, style=plot.style_histogram, histbase=0, offset=-3, show_last=1)
  85. plot(series=f_Sre(4), color=acol, linewidth=aline, style=plot.style_histogram, histbase=0, offset=-4, show_last=1)
  86. plot(series=f_Sim(4), color=bcol, linewidth=bline, style=plot.style_histogram, histbase=0, offset=-4, show_last=1)
  87. plot(series=f_Sre(5), color=acol, linewidth=aline, style=plot.style_histogram, histbase=0, offset=-5, show_last=1)
  88. plot(series=f_Sim(5), color=bcol, linewidth=bline, style=plot.style_histogram, histbase=0, offset=-5, show_last=1)
  89. plot(series=f_Sre(6), color=acol, linewidth=aline, style=plot.style_histogram, histbase=0, offset=-6, show_last=1)
  90. plot(series=f_Sim(6), color=bcol, linewidth=bline, style=plot.style_histogram, histbase=0, offset=-6, show_last=1)
  91. plot(series=f_Sre(7), color=acol, linewidth=aline, style=plot.style_histogram, histbase=0, offset=-7, show_last=1)
  92. plot(series=f_Sim(7), color=bcol, linewidth=bline, style=plot.style_histogram, histbase=0, offset=-7, show_last=1)
  93. plot(series=f_Sre(8), color=acol, linewidth=aline, style=plot.style_histogram, histbase=0, offset=-8, show_last=1)
  94. plot(series=f_Sim(8), color=bcol, linewidth=bline, style=plot.style_histogram, histbase=0, offset=-8, show_last=1)
  95. //{ remarks:
  96. // https://en.wikipedia.org/wiki/Discrete_Fourier_transform
  97. // https://www.tradingview.com/script/XXBfMnVE-Function-Discrete-Fourier-Transform/
  98. // https://www.gaussianwaves.com/2015/11/interpreting-fft-results-complex-dft-frequency-bins-and-fftshift/
  99. //}}}
Advertisement
Add Comment
Please, Sign In to add comment