Guest User

Untitled

a guest
Apr 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. module Fourier
  2. extend self
  3. extend Math
  4. include Math
  5.  
  6. def to(fx)
  7. n = fx.length
  8. n.times.map do |u|
  9. real = imaginary = 0
  10. fx.each_with_index do |(f_real, f_imaginary), k|
  11. p = -2*PI*u*k/n
  12. c, s = cos(p), sin(p)
  13. real += c*f_real + s*f_imaginary
  14. imaginary += c*f_imaginary - s*f_real
  15. end
  16. [real/n, imaginary/n]
  17. end
  18. end
  19.  
  20. def from(fu, m=fu.length)
  21. n = fu.length
  22. n.times.map do |k|
  23. real = imaginary = 0
  24. fu.take(m).each_with_index do |(f_real, f_imaginary), u|
  25. p = 2*PI*u*k/n
  26. c, s = cos(p), sin(p)
  27. real += c*f_real + s*f_imaginary
  28. imaginary += c*f_imaginary - s*f_real
  29. end
  30. [real, imaginary]
  31. end
  32. end
  33. end
Add Comment
Please, Sign In to add comment