Class: Cowboy::Frequencies

Inherits:
Object
  • Object
show all
Defined in:
lib/cowboy/fft.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unshifted_array) ⇒ Frequencies

Returns a new instance of Frequencies.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cowboy/fft.rb', line 5

def initialize(unshifted_array)
  @unshifted = unshifted_array
  @dc = unshifted_array[0].real
  n = unshifted_array.size
  @freq = []
  for i in (n/2...n)
    @freq << unshifted_array[i].abs
  end
  for i in (0...n/2)
    @freq << unshifted_array[i].abs
  end
end

Instance Attribute Details

#dcObject (readonly)

Returns the value of attribute dc.



3
4
5
# File 'lib/cowboy/fft.rb', line 3

def dc
  @dc
end

#freqObject (readonly)

Returns the value of attribute freq.



3
4
5
# File 'lib/cowboy/fft.rb', line 3

def freq
  @freq
end

#unshiftedObject (readonly)

Returns the value of attribute unshifted.



3
4
5
# File 'lib/cowboy/fft.rb', line 3

def unshifted
  @unshifted
end

Instance Method Details

#buckets(sample_rate = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cowboy/fft.rb', line 18

def buckets(sample_rate=nil)
  return @buckets if @buckets
  sample_rate ||= @freq.size
  nyquist = sample_rate/2.0
  sum = 0
  a = []
  n = @freq.size
  for i in (0...n)
    a << -nyquist + sum
    sum += (2 * nyquist/n)
  end
  @buckets = a
  return a
end