Class: SPCore::Signal
- Inherits:
-
Object
- Object
- SPCore::Signal
- Includes:
- Hashmake::HashMakeable
- Defined in:
- lib/spcore/util/signal.rb
Overview
Constant Summary collapse
- ARG_SPECS =
Used to process hashed arguments in #initialize.
{ :data => arg_spec(:reqd => true, :type => Array, :validator => ->(a){ a.any? }), :sample_rate => arg_spec(:reqd => true, :type => Numeric, :validator => ->(a){ a > 0.0 }), #:fft_format => arg_spec(:reqd => false, :type => Symbol, :default => FFT_MAGNITUDE_DECIBEL, :validator => ->(a){FFT_FORMATS.include?(a)}) }
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#sample_rate ⇒ Object
readonly
Returns the value of attribute sample_rate.
Instance Method Summary collapse
-
#[](arg) ⇒ Object
Access signal data.
-
#abs ⇒ Object
Operate on copy of the Signal object with the absolute value function.
-
#abs! ⇒ Object
Operate on the signal data (in place) with the absolute value function.
-
#add(other) ⇒ Object
(also: #+)
Add value, values in array, or values in other signal to the current data values, and return a new Signal object with the results.
-
#add!(other) ⇒ Object
Add value, values in array, or values in other signal to the current data values, and update the current data with the results.
-
#append(other) ⇒ Object
Add data in array or other signal to the end of current data.
-
#append!(other) ⇒ Object
Add data in array or other signal to the end of current data.
-
#bandpass(left_cutoff, right_cutoff, order) ⇒ Object
Run a discrete bandpass filter over the signal data (using DualSincFilter).
-
#bandpass!(left_cutoff, right_cutoff, order) ⇒ Object
Run a discrete bandpass filter over the signal data (using DualSincFilter).
-
#bandstop(left_cutoff, right_cutoff, order) ⇒ Object
Run a discrete bandstop filter over the signal data (using DualSincFilter).
-
#bandstop!(left_cutoff, right_cutoff, order) ⇒ Object
Run a discrete bandstop filter over the signal data (using DualSincFilter).
-
#clone ⇒ Object
Produce a new Signal object with the same data.
-
#correlation(feature, zero_padding = 0) ⇒ Object
Apply Statistics.correlation to the signal data (as the image).
-
#count ⇒ Object
Size of the signal data.
-
#derivative ⇒ Object
Applies Calculus.derivative on the signal data, and returns a new Signal object with the result.
-
#divide(other) ⇒ Object
(also: #/)
Divide value, values in array, or values in other signal into the current data values, and return a new Signal object with the results.
-
#divide!(other) ⇒ Object
Divide value, values in array, or values in other signal into the current data values, and update the current data with the results.
-
#downsample_discrete(downsample_factor, filter_order) ⇒ Object
Decrease the sample rate of signal data by the given factor using discrete downsampling method.
-
#downsample_discrete!(downsample_factor, filter_order) ⇒ Object
Decrease the sample rate of signal data by the given factor using discrete downsampling method.
-
#duration ⇒ Object
Signal duration in seconds.
-
#energy ⇒ Object
Calculate the energy in current signal data.
-
#envelope(as_signal = true) ⇒ Object
Apply Features.envelope to the signal data, and return either a new Signal object or the raw envelope data.
-
#extrema ⇒ Object
Apply Features.extrema to the signal data.
-
#fft_full ⇒ Object
Return the full output of forward FFT on the signal data.
-
#fft_half ⇒ Object
Return the first half of the the forward FFT on the signal data.
-
#freq_magnitudes ⇒ Hash
Run FFT on signal data to find magnitude of frequency components.
-
#freq_peaks ⇒ Object
Apply FrequencyDomain.peaks to the signal and return the result.
- #frequency_domain(fft_format = FrequencyDomain::FFT_MAGNITUDE_DECIBEL) ⇒ Object
-
#fundamental(opts) ⇒ Object
Return the lowest frequency of the signal harmonic series.
-
#harmonic_series(opts = {}) ⇒ Object
Apply FrequencyDomain.harmonic_series to the signal frequency peaks and return the result.
-
#highpass(cutoff_freq, order) ⇒ Object
Run a discrete highpass filter over the signal data (using SincFilter).
-
#highpass!(cutoff_freq, order) ⇒ Object
Run a discrete highpass filter over the signal data (using SincFilter).
-
#initialize(hashed_args) ⇒ Signal
constructor
A new instance of Signal.
-
#integral ⇒ Object
Applies Calculus.integral on the signal data, and returns a new Signal object with the result.
-
#keep_frequencies(freq_range) ⇒ Object
Removes all but the given range of frequencies from the signal, using frequency domain filtering.
-
#keep_frequencies!(freq_range) ⇒ Object
Removes all but the given range of frequencies from the signal, using frequency domain filtering.
-
#lowpass(cutoff_freq, order) ⇒ Object
Run a discrete lowpass filter over the signal data (using SincFilter).
-
#lowpass!(cutoff_freq, order) ⇒ Object
Run a discrete lowpass filter over the signal data (using SincFilter).
-
#maxima ⇒ Object
Apply Features.maxima to the signal data.
-
#mean ⇒ Object
Apply Statistics.mean to the signal data.
-
#minima ⇒ Object
Apply Features.minima to the signal data.
-
#multiply(other) ⇒ Object
(also: #*)
Multiply value, values in array, or values in other signal with the current data values, and return a new Signal object with the results.
-
#multiply!(other) ⇒ Object
Multiply value, values in array, or values in other signal with the current data values, and update the current data with the results.
-
#negative_minima ⇒ Object
Apply Features.negative_minima to the signal data.
-
#normalize(level = 1.0) ⇒ Object
reduce all samples to the given level.
-
#normalize!(level = 1.0) ⇒ Object
reduce all samples to the given level.
-
#outer_extrema ⇒ Object
Apply Features.outer_extrema to the signal data.
-
#plot_1d ⇒ Object
Plot the signal data against sample numbers.
-
#plot_2d ⇒ Object
Plot the signal data against time.
-
#positive_maxima ⇒ Object
Apply Features.positive_maxima to the signal data.
-
#prepend(other) ⇒ Object
Add data in array or other signal to the beginning of current data.
-
#prepend!(other) ⇒ Object
Add data in array or other signal to the beginning of current data.
-
#remove_frequencies(freq_range) ⇒ Object
Removes the given range of frequencies from the signal, using frequency domain filtering.
-
#remove_frequencies!(freq_range) ⇒ Object
Removes all but the given range of frequencies from the signal, using frequency domain filtering.
-
#resample_discrete(upsample_factor, downsample_factor, filter_order) ⇒ Object
Change the sample rate of signal data by the given up/down factors, using discrete upsampling and downsampling methods.
-
#resample_discrete!(upsample_factor, downsample_factor, filter_order) ⇒ Object
Change the sample rate of signal data by the given up/down factors, using discrete upsampling and downsampling methods.
-
#resample_hybrid(upsample_factor, downsample_factor, filter_order) ⇒ Object
Change the sample rate of signal data by the given up/down factors, using polynomial upsampling and discrete downsampling.
-
#resample_hybrid!(upsample_factor, downsample_factor, filter_order) ⇒ Object
Change the sample rate of signal data by the given up/down factors, using polynomial upsampling and discrete downsampling.
-
#rms ⇒ Object
Calculate signal RMS (root-mean square), also known as quadratic mean, a statistical measure of the magnitude.
-
#size ⇒ Object
Size of the signal data.
-
#std_dev ⇒ Object
Apply Statistics.std_dev to the signal data.
-
#subset(range) ⇒ Object
Produce a new Signal object with a subset of the current signal data.
-
#subtract(other) ⇒ Object
(also: #-)
Subtract value, values in array, or values in other signal from the current data values, and return a new Signal object with the results.
-
#subtract!(other) ⇒ Object
Subtract value, values in array, or values in other signal from the current data values, and update the current data with the results.
-
#upsample_discrete(upsample_factor, filter_order) ⇒ Object
Increase the sample rate of signal data by the given factor using discrete upsampling method.
-
#upsample_discrete!(upsample_factor, filter_order) ⇒ Object
Increase the sample rate of signal data by the given factor using discrete upsampling method.
-
#upsample_polynomial(upsample_factor) ⇒ Object
Increase the sample rate of signal data by the given factor using polynomial interpolation.
-
#upsample_polynomial!(upsample_factor) ⇒ Object
Increase the sample rate of signal data by the given factor using polynomial interpolation.
Constructor Details
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
16 17 18 |
# File 'lib/spcore/util/signal.rb', line 16 def data @data end |
#sample_rate ⇒ Object (readonly)
Returns the value of attribute sample_rate.
16 17 18 |
# File 'lib/spcore/util/signal.rb', line 16 def sample_rate @sample_rate end |
Instance Method Details
#[](arg) ⇒ Object
Access signal data.
57 58 59 |
# File 'lib/spcore/util/signal.rb', line 57 def [](arg) @data[arg] end |
#abs ⇒ Object
Operate on copy of the Signal object with the absolute value function.
353 354 355 |
# File 'lib/spcore/util/signal.rb', line 353 def abs self.clone.abs! end |
#abs! ⇒ Object
Operate on the signal data (in place) with the absolute value function.
347 348 349 350 |
# File 'lib/spcore/util/signal.rb', line 347 def abs! @data = @data.map {|x| x.abs } return self end |
#add(other) ⇒ Object Also known as: +
Add value, values in array, or values in other signal to the current data values, and return a new Signal object with the results.
439 440 441 |
# File 'lib/spcore/util/signal.rb', line 439 def add(other) clone.add! other end |
#add!(other) ⇒ Object
Add value, values in array, or values in other signal to the current data values, and update the current data with the results.
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/spcore/util/signal.rb', line 415 def add!(other) if other.is_a?(Numeric) @data.each_index do |i| @data[i] += other end elsif other.is_a?(Signal) raise ArgumentError, "other.data.size #{other.size} is not equal to data.size #{@data.size}" if other.data.size != @data.size @data.each_index do |i| @data[i] += other.data[i] end elsif other.is_a?(Array) raise ArgumentError, "other.size #{other.size} is not equal to data.size #{@data.size}" if other.size != @data.size @data.each_index do |i| @data[i] += other[i] end else raise ArgumentError, "other is not a Numeric, Signal, or Array" end return self end |
#append(other) ⇒ Object
Add data in array or other signal to the end of current data.
408 409 410 |
# File 'lib/spcore/util/signal.rb', line 408 def append other clone.append! other end |
#append!(other) ⇒ Object
Add data in array or other signal to the end of current data.
398 399 400 401 402 403 404 405 |
# File 'lib/spcore/util/signal.rb', line 398 def append! other if other.is_a?(Array) @data = @data.concat other elsif other.is_a?(Signal) @data = @data.concat other.data end return self end |
#bandpass(left_cutoff, right_cutoff, order) ⇒ Object
Run a discrete bandpass filter over the signal data (using DualSincFilter). Return result in a new Signal object.
123 124 125 |
# File 'lib/spcore/util/signal.rb', line 123 def bandpass left_cutoff, right_cutoff, order self.clone.bandpass! left_cutoff, right_cutoff, order end |
#bandpass!(left_cutoff, right_cutoff, order) ⇒ Object
Run a discrete bandpass filter over the signal data (using DualSincFilter). Modifies current object.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/spcore/util/signal.rb', line 110 def bandpass! left_cutoff, right_cutoff, order filter = DualSincFilter.new( :sample_rate => @sample_rate, :order => order, :left_cutoff_freq => left_cutoff, :right_cutoff_freq => right_cutoff ) @data = filter.bandpass(@data) return self end |
#bandstop(left_cutoff, right_cutoff, order) ⇒ Object
Run a discrete bandstop filter over the signal data (using DualSincFilter). Return result in a new Signal object.
142 143 144 |
# File 'lib/spcore/util/signal.rb', line 142 def bandstop left_cutoff, right_cutoff, order self.clone.bandstop! left_cutoff, right_cutoff, order end |
#bandstop!(left_cutoff, right_cutoff, order) ⇒ Object
Run a discrete bandstop filter over the signal data (using DualSincFilter). Modifies current object.
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/spcore/util/signal.rb', line 129 def bandstop! left_cutoff, right_cutoff, order filter = DualSincFilter.new( :sample_rate => @sample_rate, :order => order, :left_cutoff_freq => left_cutoff, :right_cutoff_freq => right_cutoff ) @data = filter.bandstop(@data) return self end |
#clone ⇒ Object
Produce a new Signal object with the same data.
27 28 29 30 31 |
# File 'lib/spcore/util/signal.rb', line 27 def clone new = Signal.new(:data => @data.clone, :sample_rate => @sample_rate) new.instance_variable_set(:@frequency_domain, @frequency_domain) return new end |
#correlation(feature, zero_padding = 0) ⇒ Object
Apply Statistics.correlation to the signal data (as the image).
312 313 314 |
# File 'lib/spcore/util/signal.rb', line 312 def correlation feature, zero_padding = 0 Statistics.correlation @data, feature, zero_padding end |
#count ⇒ Object
Size of the signal data.
47 48 49 |
# File 'lib/spcore/util/signal.rb', line 47 def count @data.size end |
#derivative ⇒ Object
Applies Calculus.derivative on the signal data, and returns a new Signal object with the result.
547 548 549 |
# File 'lib/spcore/util/signal.rb', line 547 def derivative return Signal.new(:sample_rate => @sample_rate, :data => Calculus.derivative(@data)) end |
#divide(other) ⇒ Object Also known as: /
Divide value, values in array, or values in other signal into the current data values, and return a new Signal object with the results.
536 537 538 |
# File 'lib/spcore/util/signal.rb', line 536 def divide(other) clone.divide! other end |
#divide!(other) ⇒ Object
Divide value, values in array, or values in other signal into the current data values, and update the current data with the results.
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/spcore/util/signal.rb', line 511 def divide!(other) if other.is_a?(Numeric) @data.each_index do |i| @data[i] /= other end elsif other.is_a?(Signal) raise ArgumentError, "other.data.size #{other.size} is not equal to data.size #{@data.size}" if other.data.size != @data.size @data.each_index do |i| @data[i] /= other.data[i] end elsif other.is_a?(Array) raise ArgumentError, "other.size #{other.size} is not equal to data.size #{@data.size}" if other.size != @data.size @data.each_index do |i| @data[i] /= other[i] end else raise ArgumentError, "other is not a Numeric, Signal, or Array" end return self end |
#downsample_discrete(downsample_factor, filter_order) ⇒ Object
Decrease the sample rate of signal data by the given factor using discrete downsampling method. Return result in a new Signal object.
178 179 180 |
# File 'lib/spcore/util/signal.rb', line 178 def downsample_discrete downsample_factor, filter_order return self.clone.downsample_discrete!(downsample_factor, filter_order) end |
#downsample_discrete!(downsample_factor, filter_order) ⇒ Object
Decrease the sample rate of signal data by the given factor using discrete downsampling method. Modifies current object.
168 169 170 171 172 |
# File 'lib/spcore/util/signal.rb', line 168 def downsample_discrete! downsample_factor, filter_order @data = DiscreteResampling.downsample @data, @sample_rate, downsample_factor, filter_order @sample_rate /= downsample_factor return self end |
#duration ⇒ Object
Signal duration in seconds.
52 53 54 |
# File 'lib/spcore/util/signal.rb', line 52 def duration return @data.size.to_f / @sample_rate end |
#energy ⇒ Object
Calculate the energy in current signal data.
291 292 293 |
# File 'lib/spcore/util/signal.rb', line 291 def energy return @data.inject(0.0){|sum,x| sum + (x * x)} end |
#envelope(as_signal = true) ⇒ Object
Apply Features.envelope to the signal data, and return either a new Signal object or the raw envelope data.
372 373 374 375 376 377 378 379 380 |
# File 'lib/spcore/util/signal.rb', line 372 def envelope as_signal = true env_data = Features.envelope @data if as_signal return Signal.new(:sample_rate => @sample_rate, :data => env_data) else return env_data end end |
#extrema ⇒ Object
Apply Features.extrema to the signal data.
317 318 319 |
# File 'lib/spcore/util/signal.rb', line 317 def extrema return Features.extrema(@data) end |
#fft_full ⇒ Object
Return the full output of forward FFT on the signal data.
252 253 254 |
# File 'lib/spcore/util/signal.rb', line 252 def fft_full frequency_domain.fft_full end |
#fft_half ⇒ Object
Return the first half of the the forward FFT on the signal data.
257 258 259 |
# File 'lib/spcore/util/signal.rb', line 257 def fft_half frequency_domain.fft_half end |
#freq_magnitudes ⇒ Hash
Run FFT on signal data to find magnitude of frequency components.
263 264 265 266 267 268 269 270 271 272 |
# File 'lib/spcore/util/signal.rb', line 263 def freq_magnitudes freq_magnitudes = {} fft_half.each_index do |i| freq = frequency_domain.idx_to_freq(i) freq_magnitudes[freq] = fft_half[i] end return freq_magnitudes end |
#freq_peaks ⇒ Object
Apply FrequencyDomain.peaks to the signal and return the result.
275 276 277 |
# File 'lib/spcore/util/signal.rb', line 275 def freq_peaks return frequency_domain.peaks end |
#frequency_domain(fft_format = FrequencyDomain::FFT_MAGNITUDE_DECIBEL) ⇒ Object
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/spcore/util/signal.rb', line 240 def frequency_domain fft_format = FrequencyDomain::FFT_MAGNITUDE_DECIBEL if @frequency_domain.nil? @frequency_domain = FrequencyDomain.new( :time_data => @data, :sample_rate => @sample_rate, :fft_format => fft_format ) end return @frequency_domain end |
#fundamental(opts) ⇒ Object
Return the lowest frequency of the signal harmonic series.
286 287 288 |
# File 'lib/spcore/util/signal.rb', line 286 def fundamental opts return harmonic_series(opts).min end |
#harmonic_series(opts = {}) ⇒ Object
Apply FrequencyDomain.harmonic_series to the signal frequency peaks and return the result.
281 282 283 |
# File 'lib/spcore/util/signal.rb', line 281 def harmonic_series opts = {} return frequency_domain.harmonic_series(opts) end |
#highpass(cutoff_freq, order) ⇒ Object
Run a discrete highpass filter over the signal data (using SincFilter). Return result in a new Signal object.
104 105 106 |
# File 'lib/spcore/util/signal.rb', line 104 def highpass cutoff_freq, order self.clone.highpass! cutoff_freq, order end |
#highpass!(cutoff_freq, order) ⇒ Object
Run a discrete highpass filter over the signal data (using SincFilter). Modifies current object.
96 97 98 99 100 |
# File 'lib/spcore/util/signal.rb', line 96 def highpass! cutoff_freq, order filter = SincFilter.new(:sample_rate => @sample_rate, :order => order, :cutoff_freq => cutoff_freq) @data = filter.highpass(@data) return self end |
#integral ⇒ Object
Applies Calculus.integral on the signal data, and returns a new Signal object with the result.
553 554 555 |
# File 'lib/spcore/util/signal.rb', line 553 def integral return Signal.new(:sample_rate => @sample_rate, :data => Calculus.integral(@data)) end |
#keep_frequencies(freq_range) ⇒ Object
Removes all but the given range of frequencies from the signal, using frequency domain filtering. Modifes a clone of the current object, returning the clone.
579 580 581 |
# File 'lib/spcore/util/signal.rb', line 579 def keep_frequencies freq_range return self.clone.keep_frequencies!(freq_range) end |
#keep_frequencies!(freq_range) ⇒ Object
Removes all but the given range of frequencies from the signal, using frequency domain filtering. Modifes and returns the current object.
572 573 574 |
# File 'lib/spcore/util/signal.rb', line 572 def keep_frequencies! freq_range modify_freq_content freq_range, :keep end |
#lowpass(cutoff_freq, order) ⇒ Object
Run a discrete lowpass filter over the signal data (using SincFilter). Return result in a new Signal object.
90 91 92 |
# File 'lib/spcore/util/signal.rb', line 90 def lowpass cutoff_freq, order self.clone.lowpass! cutoff_freq, order end |
#lowpass!(cutoff_freq, order) ⇒ Object
Run a discrete lowpass filter over the signal data (using SincFilter). Modifies current object.
82 83 84 85 86 |
# File 'lib/spcore/util/signal.rb', line 82 def lowpass! cutoff_freq, order filter = SincFilter.new(:sample_rate => @sample_rate, :order => order, :cutoff_freq => cutoff_freq) @data = filter.lowpass(@data) return self end |
#maxima ⇒ Object
Apply Features.maxima to the signal data.
337 338 339 |
# File 'lib/spcore/util/signal.rb', line 337 def maxima return Features.maxima(@data) end |
#mean ⇒ Object
Apply Statistics.mean to the signal data.
302 303 304 |
# File 'lib/spcore/util/signal.rb', line 302 def mean Statistics.mean @data end |
#minima ⇒ Object
Apply Features.minima to the signal data.
327 328 329 |
# File 'lib/spcore/util/signal.rb', line 327 def minima return Features.minima(@data) end |
#multiply(other) ⇒ Object Also known as: *
Multiply value, values in array, or values in other signal with the current data values, and return a new Signal object with the results.
503 504 505 |
# File 'lib/spcore/util/signal.rb', line 503 def multiply(other) clone.multiply! other end |
#multiply!(other) ⇒ Object
Multiply value, values in array, or values in other signal with the current data values, and update the current data with the results.
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/spcore/util/signal.rb', line 478 def multiply!(other) if other.is_a?(Numeric) @data.each_index do |i| @data[i] *= other end elsif other.is_a?(Signal) raise ArgumentError, "other.data.size #{other.size} is not equal to data.size #{@data.size}" if other.data.size != @data.size @data.each_index do |i| @data[i] *= other.data[i] end elsif other.is_a?(Array) raise ArgumentError, "other.size #{other.size} is not equal to data.size #{@data.size}" if other.size != @data.size @data.each_index do |i| @data[i] *= other[i] end else raise ArgumentError, "other is not a Numeric, Signal, or Array" end return self end |
#negative_minima ⇒ Object
Apply Features.negative_minima to the signal data.
332 333 334 |
# File 'lib/spcore/util/signal.rb', line 332 def negative_minima return Features.negative_minima(@data) end |
#normalize(level = 1.0) ⇒ Object
reduce all samples to the given level
363 364 365 |
# File 'lib/spcore/util/signal.rb', line 363 def normalize level = 1.0 self.clone.normalize! level end |
#normalize!(level = 1.0) ⇒ Object
reduce all samples to the given level
358 359 360 |
# File 'lib/spcore/util/signal.rb', line 358 def normalize! level = 1.0 self.divide!(@data.max / level) end |
#outer_extrema ⇒ Object
Apply Features.outer_extrema to the signal data.
322 323 324 |
# File 'lib/spcore/util/signal.rb', line 322 def outer_extrema return Features.outer_extrema(@data) end |
#plot_1d ⇒ Object
Plot the signal data against sample numbers.
62 63 64 65 |
# File 'lib/spcore/util/signal.rb', line 62 def plot_1d plotter = Plotter.new(:title => "Signal: values vs. sample number", :xtitle => "sample number", :ytitle => "sample value") plotter.plot_1d "signal data" => @data end |
#plot_2d ⇒ Object
Plot the signal data against time.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/spcore/util/signal.rb', line 68 def plot_2d plotter = Plotter.new(:title => "Signal: values vs. time", :xtitle => "time (sec)", :ytitle => "sample value") data_vs_time = {} sp = 1.0 / @sample_rate @data.each_index do |i| data_vs_time[i * sp] = @data[i] end plotter.plot_2d "signal data" => data_vs_time end |
#positive_maxima ⇒ Object
Apply Features.positive_maxima to the signal data.
342 343 344 |
# File 'lib/spcore/util/signal.rb', line 342 def positive_maxima return Features.positive_maxima(@data) end |
#prepend(other) ⇒ Object
Add data in array or other signal to the beginning of current data.
393 394 395 |
# File 'lib/spcore/util/signal.rb', line 393 def prepend other clone.prepend! other end |
#prepend!(other) ⇒ Object
Add data in array or other signal to the beginning of current data.
383 384 385 386 387 388 389 390 |
# File 'lib/spcore/util/signal.rb', line 383 def prepend! other if other.is_a?(Array) @data = other.concat @data elsif other.is_a?(Signal) @data = other.data.concat @data end return self end |
#remove_frequencies(freq_range) ⇒ Object
Removes the given range of frequencies from the signal, using frequency domain filtering. Modifes a clone of the current object, returning the clone.
566 567 568 |
# File 'lib/spcore/util/signal.rb', line 566 def remove_frequencies freq_range return self.clone.remove_frequencies!(freq_range) end |
#remove_frequencies!(freq_range) ⇒ Object
Removes all but the given range of frequencies from the signal, using frequency domain filtering. Modifes and returns the current object.
559 560 561 |
# File 'lib/spcore/util/signal.rb', line 559 def remove_frequencies! freq_range modify_freq_content freq_range, :remove end |
#resample_discrete(upsample_factor, downsample_factor, filter_order) ⇒ Object
Change the sample rate of signal data by the given up/down factors, using discrete upsampling and downsampling methods. Return result in a new Signal object.
199 200 201 |
# File 'lib/spcore/util/signal.rb', line 199 def resample_discrete upsample_factor, downsample_factor, filter_order return self.clone.resample_discrete!(upsample_factor, downsample_factor, filter_order) end |
#resample_discrete!(upsample_factor, downsample_factor, filter_order) ⇒ Object
Change the sample rate of signal data by the given up/down factors, using discrete upsampling and downsampling methods. Modifies current object.
187 188 189 190 191 192 |
# File 'lib/spcore/util/signal.rb', line 187 def resample_discrete! upsample_factor, downsample_factor, filter_order @data = DiscreteResampling.resample @data, @sample_rate, upsample_factor, downsample_factor, filter_order @sample_rate *= upsample_factor @sample_rate /= downsample_factor return self end |
#resample_hybrid(upsample_factor, downsample_factor, filter_order) ⇒ Object
Change the sample rate of signal data by the given up/down factors, using polynomial upsampling and discrete downsampling. Return result as a new Signal object.
236 237 238 |
# File 'lib/spcore/util/signal.rb', line 236 def resample_hybrid upsample_factor, downsample_factor, filter_order return self.clone.resample_hybrid!(upsample_factor, downsample_factor, filter_order) end |
#resample_hybrid!(upsample_factor, downsample_factor, filter_order) ⇒ Object
Change the sample rate of signal data by the given up/down factors, using polynomial upsampling and discrete downsampling. Modifies current Signal object.
224 225 226 227 228 229 |
# File 'lib/spcore/util/signal.rb', line 224 def resample_hybrid! upsample_factor, downsample_factor, filter_order @data = HybridResampling.resample @data, @sample_rate, upsample_factor, downsample_factor, filter_order @sample_rate *= upsample_factor @sample_rate /= downsample_factor return self end |
#rms ⇒ Object
Calculate signal RMS (root-mean square), also known as quadratic mean, a statistical measure of the magnitude.
297 298 299 |
# File 'lib/spcore/util/signal.rb', line 297 def rms Math.sqrt(energy / size) end |
#size ⇒ Object
Size of the signal data.
42 43 44 |
# File 'lib/spcore/util/signal.rb', line 42 def size @data.size end |
#std_dev ⇒ Object
Apply Statistics.std_dev to the signal data.
307 308 309 |
# File 'lib/spcore/util/signal.rb', line 307 def std_dev Statistics.std_dev @data end |
#subset(range) ⇒ Object
Produce a new Signal object with a subset of the current signal data.
35 36 37 38 39 |
# File 'lib/spcore/util/signal.rb', line 35 def subset range new = Signal.new(:data => @data[range], :sample_rate => @sample_rate) new.instance_variable_set(:@frequency_domain, @frequency_domain) return new end |
#subtract(other) ⇒ Object Also known as: -
Subtract value, values in array, or values in other signal from the current data values, and return a new Signal object with the results.
470 471 472 |
# File 'lib/spcore/util/signal.rb', line 470 def subtract(other) clone.subtract! other end |
#subtract!(other) ⇒ Object
Subtract value, values in array, or values in other signal from the current data values, and update the current data with the results.
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/spcore/util/signal.rb', line 446 def subtract!(other) if other.is_a?(Numeric) @data.each_index do |i| @data[i] -= other end elsif other.is_a?(Signal) raise ArgumentError, "other.data.size #{other.size} is not equal to data.size #{@data.size}" if other.data.size != @data.size @data.each_index do |i| @data[i] -= other.data[i] end elsif other.is_a?(Array) raise ArgumentError, "other.size #{other.size} is not equal to data.size #{@data.size}" if other.size != @data.size @data.each_index do |i| @data[i] -= other[i] end else raise ArgumentError, "other is not a Numeric, Signal, or Array" end return self end |
#upsample_discrete(upsample_factor, filter_order) ⇒ Object
Increase the sample rate of signal data by the given factor using discrete upsampling method. Return result in a new Signal object.
160 161 162 |
# File 'lib/spcore/util/signal.rb', line 160 def upsample_discrete upsample_factor, filter_order return self.clone.upsample_discrete!(upsample_factor, filter_order) end |
#upsample_discrete!(upsample_factor, filter_order) ⇒ Object
Increase the sample rate of signal data by the given factor using discrete upsampling method. Modifies current object.
150 151 152 153 154 |
# File 'lib/spcore/util/signal.rb', line 150 def upsample_discrete! upsample_factor, filter_order @data = DiscreteResampling.upsample @data, @sample_rate, upsample_factor, filter_order @sample_rate *= upsample_factor return self end |
#upsample_polynomial(upsample_factor) ⇒ Object
Increase the sample rate of signal data by the given factor using polynomial interpolation. Returns result as a new Signal object.
215 216 217 |
# File 'lib/spcore/util/signal.rb', line 215 def upsample_polynomial upsample_factor return self.clone.upsample_polynomial!(upsample_factor) end |
#upsample_polynomial!(upsample_factor) ⇒ Object
Increase the sample rate of signal data by the given factor using polynomial interpolation. Modifies current Signal object.
206 207 208 209 210 |
# File 'lib/spcore/util/signal.rb', line 206 def upsample_polynomial! upsample_factor @data = PolynomialResampling.upsample @data, upsample_factor @sample_rate *= upsample_factor return self end |