module Digiproc::Convolvable::InstanceMethods
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
Public Class Methods
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
# File lib/concerns/convolvable.rb, line 72 def self.included(base) base.class_eval do include Digiproc::RequiresData end end
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.
# File lib/concerns/convolvable.rb, line 81 def initialize(strategy: Digiproc::Strategies::BFConvolutionStrategy) @convolution_strategy = strategy end
Public Instance Methods
Alias to auto_correlation
# File lib/concerns/convolvable.rb, line 135 def acorr self.cross_correlation(self.data) end
auto_correlation() => returns Array[Numeric]
Performs autocorrelation of self.data ie:
includingInstance.auto_correlation # returns array of data
# File lib/concerns/convolvable.rb, line 129 def auto_correlation self.cross_correlation(self.data) end
Alias to convolve
# File lib/concerns/convolvable.rb, line 99 def conv(incoming_data) self.convolve(incoming_data) end
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
# File lib/concerns/convolvable.rb, line 106 def convolution_strategy @convolution_strategy.nil? ? Digiproc::Strategies::BFConvolutionStrategy : @convolution_strategy end
convolve(incoming_data [Array [OR a class including Digiproc::RequiresData]]) => returns Array[Numeric]
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
# 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 [Array [OR a class extending Digiproc::RequiresData]]) => returns Array[Numeric]
see convolve
for example of using an array or a Digiproc::RequiresData
. ie:
includingInstance.cross_correlation(array_data) # returns array of data
# 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
Alias to cross_correlation
# File lib/concerns/convolvable.rb, line 121 def xcorr(incoming_data) self.cross_correlation(incoming_data) end