Module: Digiproc::Convolvable::InstanceMethods

Included in:
DigitalSignal, FFT, Probability::GaussianDistribution
Defined in:
lib/concerns/convolvable.rb

Overview

This module contains instance methods for classes which have properties of ‘data` which are arrays and can undergo convolution, correlation, cross-correlation, and autocorrelation (arrays must then be of Numerics). As such, if a class `includes` Convolvable::InstanceMethods, it is also including `Digiproc::RequiresData` which ensures that the class has a `data` property

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

When included in a class, it automatically has that class include Digiproc::RequiresData, because methods in this module require that there be a property called ‘data` which is an Array



72
73
74
75
76
# File 'lib/concerns/convolvable.rb', line 72

def self.included(base)
    base.class_eval do 
        include Digiproc::RequiresData
    end
end

Instance Method Details

#acorrObject

Alias to auto_correlation



135
136
137
# File 'lib/concerns/convolvable.rb', line 135

def acorr
    self.cross_correlation(self.data)
end

#auto_correlationObject

auto_correlation() => returns Array Performs autocorrelation of self.data ie: includingInstance.auto_correlation # returns array of data



129
130
131
# File 'lib/concerns/convolvable.rb', line 129

def auto_correlation
    self.cross_correlation(self.data)
end

#conv(incoming_data) ⇒ Object

Alias to convolve



99
100
101
# File 'lib/concerns/convolvable.rb', line 99

def conv(incoming_data)
    self.convolve(incoming_data)
end

#convolution_strategyObject

Used internally to ensure that if the class which includes this module is not ‘Initializable`, then a Convolution strategy will still be set In this case, use Digiproc::BFConvolutionStrategy



106
107
108
# File 'lib/concerns/convolvable.rb', line 106

def convolution_strategy
    @convolution_strategy.nil? ? Digiproc::Strategies::BFConvolutionStrategy : @convolution_strategy
end

#convolve(incoming_data) ⇒ Object

convolve(incoming_data [Array [OR a class including Digiproc::RequiresData]]) => returns Array uses the ‘strategy` class to convolve the included class’ data property with the array provided as an argument ie if class DataHolder includes Convolavable::InstanceMethods, and you have two instances, d1 and d2, you can say: d1.convolve(d2) Or, if you have a 1D array of numerics in variable ‘my_data_arr` capable of convolving with d1.data, you can say: d1.convolve(my_data_arr) # returns array of data



92
93
94
95
# File 'lib/concerns/convolvable.rb', line 92

def convolve(incoming_data)
    incoming_data = incoming_data.is_a?(Array) ? incoming_data : incoming_data.data
    self.convolution_strategy.conv(self.data, incoming_data)
end

#cross_correlation(incoming_data) ⇒ Object

cross_correlation(incoming_data [Array [OR a class extending Digiproc::RequiresData]]) => returns Array see #convolve for example of using an array or a Digiproc::RequiresData. ie: includingInstance.cross_correlation(array_data) # returns array of data



114
115
116
117
# File 'lib/concerns/convolvable.rb', line 114

def cross_correlation(incoming_data)
    incoming_data = incoming_data.is_a?(Array) ? incoming_data : incoming_data.data
    self.convolution_strategy.conv(self.data, incoming_data.reverse)
end

#initialize(strategy: Digiproc::Strategies::BFConvolutionStrategy) ⇒ Object

Optionally initializable with a ‘ConvolutionStrategy` (see `Digiproc::Initializable`) This snows up as new, but when using this code, :initialize will be called. This pattern is utilized in the Digiproc::DigitalSignal class, and can be seen in its initializer.



81
82
83
# File 'lib/concerns/convolvable.rb', line 81

def initialize(strategy: Digiproc::Strategies::BFConvolutionStrategy)
    @convolution_strategy = strategy
end

#xcorr(incoming_data) ⇒ Object

Alias to cross_correlation



121
122
123
# File 'lib/concerns/convolvable.rb', line 121

def xcorr(incoming_data)
    self.cross_correlation(incoming_data)
end