-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft.rb
37 lines (33 loc) · 903 Bytes
/
fft.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# refs http://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm
# Cooley–Tukey FFT algorithm - Wikipedia, the free encyclopedia
def fft(a)
ditfft2(a, a.size, 1)
end
def ditfft2(x, n, s)
result = []
if n == 1 then
result[0] = x[0]
else
result += ditfft2(x, n / 2, 2 * s)
result += ditfft2(x.drop(s), n / 2, 2 * s)
for k in 0..(n / 2 - 1) do
t = result[k]
result[k] = t + (Math::E ** Complex(0, -2 * Math::PI * k / n)) * result[k + n / 2]
result[k + n / 2] = t - (Math::E ** Complex(0, -2 * Math::PI * k / n)) * result[k + n / 2]
end
end
result
end
def ifft(ffta, amp)
n = ffta.size
fft(ffta.map {|i| i.conj }).map {|i| i.conj }.map {|i| i * amp / n }.map {|i| i.real.round }
end
def lpf_fft(ffta, k)
n = ffta.size
a = ffta.clone
(k + 1 .. (n / 2)).each do |i|
a[i] = 0.0
a[n - i] = 0.0
end
return a
end