Class: SPCore::DFT

Inherits:
Object
  • Object
show all
Defined in:
lib/spcore/transforms/dft.rb

Overview

Author:

  • James Tunnell

Class Method Summary collapse

Class Method Details

.forward(input) ⇒ Object

Parameters:

  • input (Array)

    array of real values, representing the time domain signal to be passed into the forward DFT.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/spcore/transforms/dft.rb', line 7

def self.forward input
  input_size = input.size
  raise ArgumentError, "input.size is not even" unless (input_size % 2 == 0)
  
  output_size = input_size
  output = Array.new(output_size)
  
  output.each_index do |k|
    sum = Complex(0.0)
    input.each_index do |n|
      a = TWO_PI * n * k / input_size
      sum += Complex(input[n] * Math::cos(a), -input[n] * Math::sin(a))
    end
    output[k] = sum
  end
  
  return output
end

.inverse(input) ⇒ Object

Parameters:

  • input (Array)

    array of complex values, representing the frequency domain signal obtained from the forward DFT.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/spcore/transforms/dft.rb', line 28

def self.inverse input
  input_size = input.size
  raise ArgumentError, "input.size is not even" unless (input_size % 2 == 0)
  
  output = Array.new(input_size)
  output_size = output.size
  
  output.each_index do |k|
    sum = Complex(0.0)
    input.each_index do |n|
      a = TWO_PI * n * k / input_size
      sum += Complex(input[n] * Math::cos(a), input[n] * Math::sin(a))
    end
    output[k] = sum / output_size
  end
  
  return output    
end