module Digiproc::FourierTransformable
Module for Classes which have a property `data` which we can take the Discrete Fourier Transform of. A class which wants to use methods outside of GenricMethods will also include Digiproc::Initializable
, and you have to use the appropriate methods in your class constructor if you want the Digiproc::FFT
class to be set up automatically You could manually set it up if your class sets up its own @fft property which is an instance of Digiproc::FFT
with the class' `data` passed in. See an example of this in action in Digiproc::DigitalSignal
initializer method.
Attributes
Public Class Methods
Include Digiproc::RequiresData
when an instance is instantiated that includes Digiproc::FourierTransformable
because methods require `data` (an array of Fourier Transformable data) to exist in the class
# File lib/concerns/fourier_transformable.rb, line 73 def self.included(base) base.class_eval do include Digiproc::RequiresData, Digiproc::Initializable end end
Initialize with (time_data: [Array(Numeric)], fft_strategy
: [optional, defaults to Digiproc::Strategies::Radix2Strategy]) Upon instantiation, a new Digiproc::FFT
class is made with the data passed in as time_data. NOTE this does not happen automatically upon instantiation of the class which includes this module. The class including this module will also include Digiproc::Initializable
, so you can initialize this module in the class' initialize method as follows:
class TestClass include Digiproc::FourierTransformable attr_accessor :data def initialize(data: ) @data = data initialize_modules(Digiproc::FourierTransformable => {time_data: data}) end end
Note that the calculation of the FFT
itself is lazy and will not be calculated unless there is an attempt to access it or calculate is called on @fft
# File lib/concerns/fourier_transformable.rb, line 100 def initialize(time_data: , fft_strategy: Digiproc::Strategies::Radix2Strategy) @fft_strategy = fft_strategy @fft = Digiproc::FFT.new(time_data: time_data.dup, strategy: fft_strategy) end
Public Instance Methods
fft(size [optional Integer]) #=> returns Digiproc::FFT instance
Will calculate the FFT
if it has not yet been calculated size is an optional parameter which allows you to delegate the size of the FFT
to be calculated.. This is useful if you want a particular resolution of the frequency domain, or if you are trying to match FFT
sizes for calculation purposes.
# File lib/concerns/fourier_transformable.rb, line 126 def fft(size = @fft.data.size) if @fft.data.size != size @fft.calculate_at_size(size) end @fft end
fft_data # => Array of angle vals (radians)
Calculate the fft if not yet calculated, and returns Arra of the angle data in radians
# File lib/concerns/fourier_transformable.rb, line 150 def fft_angle setup_fft @fft.angle end
fft_data # => Array of FFT vals (complex Numeric)
Calculates the fft if not yet calculated, and returns the calculation
# File lib/concerns/fourier_transformable.rb, line 142 def fft_data setup_fft @fft.fft end
fft_db #=> Array of decible values of the magnitude of the FFT (Float, not complex)
Ensures the fft is calculated, and then returns the dB vals
# File lib/concerns/fourier_transformable.rb, line 108 def fft_db setup_fft @fft.fft.db end
fft_imaginary # => Array of imaginary vals
Calculate the fft if not yet calculated, and retun an Array
of the imaginary values
# File lib/concerns/fourier_transformable.rb, line 166 def fft_imaginary setup_fft @fft.fft.imaginary end
fft_db #=> Array of values of the magnitude of the FFT (numeric, not complex)
Ensures the fft is calculated, and then returns the magnitude
# File lib/concerns/fourier_transformable.rb, line 115 def fft_magnitude setup_fft @fft.magnitude end
fft_real # => Array of real vals
Calculate the fft if not yet calculated, and retun an Array
of the real values
# File lib/concerns/fourier_transformable.rb, line 158 def fft_real setup_fft @fft.real end
setter for the FFT
strategy
# File lib/concerns/fourier_transformable.rb, line 135 def fft_strategy=(strategy) @fft.strategy = strategy end
Private Instance Methods
Private method which calculates the fft
# File lib/concerns/fourier_transformable.rb, line 174 def setup_fft self.fft.calculate if self.fft.fft.nil? end