Class: Digiproc::BandpassFilter

Inherits:
DigitalFilter show all
Defined in:
lib/filters/bandpass_filter.rb

Overview

Creates a Bandpass Filter via the Windowing method.

Constant Summary

Constants inherited from DigitalFilter

DigitalFilter::PI

Instance Attribute Summary collapse

Attributes inherited from DigitalFilter

#fft, #size, #weights, #window

Instance Method Summary collapse

Methods inherited from DigitalFilter

#calculate_ideal, #set_fft_size, #to_ds

Constructor Details

#initialize(size:, window: Digiproc::RectangularWindow, wo: nil, bw: nil, wcl: nil, wch: nil, correct: true) ⇒ BandpassFilter

Inputs

size
Integer

number of datapoints window should be

window
Digiproc::WindowStrategy

desired window strategy

wo
Float

center frequency in radians

bw
Float

bandwidth in radians

wcl
Float

lower cutoff frequency in radians

wch
Float

higher cutoff frequency in radians

correct
Boolean

perform frequency corrections to make frequency points more accurate. Defaults to true

Must have either ‘wo` and `bw` or `wcl` and `wch`

Digiproc::BandpassFilter.new(size: 1000, wo: Math::PI / 4, bw: Math::PI / 10)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/filters/bandpass_filter.rb', line 20

def initialize(size:, window: Digiproc::RectangularWindow, wo: nil, bw: nil, wcl: nil , wch: nil, correct: true )

    super(size: size, window: window)

    if !!wo && !!bw
        bw += @window.transition_width * 2 * PI if correct
        wcl = wo - bw / 2.0
        wch = wo + bw / 2.0
    else
        raise ArgumentError.new("You must provide either bandwidth and center freq or frequency bands") if wcl.nil? or wch.nil? 
        wcl -= @window.transition_width * PI if correct
        wch += @window.transition_width * PI if correct
        bw = wch - wcl
        wo = (wch + wcl) / 2.0
    end
    @equation = ->(n){ 
        n == 0 ?  bw / PI : ((Math.sin(bw * n / 2.0)) / (PI * n)) * (2.0 * Math.cos(n * wo))
    }
    ideal_filter = calculate_ideal
    @weights = self.window.values.times ideal_filter
    @fft = Digiproc::FFT.new(time_data: self.weights)
    @fft.calculate
end

Instance Attribute Details

#equationObject

Returns the value of attribute equation.



4
5
6
# File 'lib/filters/bandpass_filter.rb', line 4

def equation
  @equation
end