Class: Digiproc::DigitalFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/filters/digital_filter.rb

Overview

Parent class to BandpassFilter, HighpassFilter, LowpassFilter, and BandstopFilter

Constant Summary collapse

PI =
Math::PI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size:, window:) ⇒ DigitalFilter

Inputs

size
Integer

number of window datapoints

window
Digiproc::WindowStrategy


12
13
14
15
16
# File 'lib/filters/digital_filter.rb', line 12

def initialize(size: , window: )
    #TODO: allow size to be even
    @size = size.even? ? size + 1 : size
    @window = window.new(size: size)
end

Instance Attribute Details

#fftObject

Returns the value of attribute fft.



6
7
8
# File 'lib/filters/digital_filter.rb', line 6

def fft
  @fft
end

#sizeObject

Returns the value of attribute size.



6
7
8
# File 'lib/filters/digital_filter.rb', line 6

def size
  @size
end

#weightsObject

Returns the value of attribute weights.



6
7
8
# File 'lib/filters/digital_filter.rb', line 6

def weights
  @weights
end

#windowObject

Returns the value of attribute window.



6
7
8
# File 'lib/filters/digital_filter.rb', line 6

def window
  @window
end

Instance Method Details

#calculate_idealObject

Ensures size is odd, and uses @equation to make a return Array of ideal filter values. Used by the child class to multiply by the window to the return value of this method for final weights



21
22
23
24
25
26
27
28
# File 'lib/filters/digital_filter.rb', line 21

def calculate_ideal
    #TODO: allow size to be even
    @size += 1 if @size.even?
    n_vals = ((-1 * (@size - 1) / 2)..((@size - 1) / 2)).to_a
    n_vals.map do |n|
        @equation.call(n)
    end
end

#set_fft_size(size) ⇒ Object

Zero pad @weights to achieve a size of the input value. set @fft to a new Digiproc::FFT, and calculate with the new padded data. .set_fft_size(size [Integer])



35
36
37
38
39
40
41
42
# File 'lib/filters/digital_filter.rb', line 35

def set_fft_size(size)
    if size > @weights.length
        zeros = Array.new(size - @weights.length, 0)
        padded = @weights.concat(zeros)
        @fft = Digiproc::FFT.new(data: padded)
        @fft.calculate
    end
end

#to_dsObject

return a Digiproc::DigitalSignal whose values are the weights of the filter



46
47
48
# File 'lib/filters/digital_filter.rb', line 46

def to_ds
    Digiproc::DigitalSignal.new(data: self.weights)
end