Skip to content

Instantly share code, notes, and snippets.

@corbanbrook
Created November 16, 2009 20:55
Show Gist options
  • Save corbanbrook/236306 to your computer and use it in GitHub Desktop.
Save corbanbrook/236306 to your computer and use it in GitHub Desktop.
# is a discrete fourier transform in ruby by corban http://en.wikipedia.org/wiki/Discrete_Fourier_transform
require 'complex'
TWO_PI = 2 * Math::PI
N = ARGV.first.to_i || 256
x = Array.new(N, rand)
X = Array.new(N, 0)
X.each_index do |k|
x.each_index do |n|
X[k] += x[n] * Math::E ** -((Complex(0, TWO_PI) / N) * k * n)
end
end
zetagun:~ corban$ time ruby DFT.rb 1024
real 1m4.676s
user 1m3.956s
sys 0m0.195s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment