Arithmetic Operations¶
Arithmetic operations can be applied in the time and frequency domain and
are implemented in the methods add
,
subtract
,
multiply
,
divide
,
power
and
matrix_multiplication
. For example, two
Signal
,
TimeData
, or
FrequencyData
instances can be added in the
time domain by
>>> result = pf.add((signal_1, signal_2), 'time')
and in the frequency domain by
>>> result = pf.add((signal_1, signal_2), 'freq')
Note that frequency domain operations are performed on the raw spectrum
signal.freq_raw
.
Arithmetic operations also work with more than two instances and support array likes and scalar values, e.g.,
>>> result = pf.add((signal_1, 1), 'time')
In this case the scalar 1 is broadcasted, i.e., it is is added to every
sample of signal (or every bin in case of a frequency domain operation).
The shape of arrays need to match the cshape
of the resulting audio object,
e.g.,
>>> x = np.arange(2 * 3 * 4).reshape((2, 3, 4))
>>> y = pf.Signal(np.ones((2, 3, 4, 10)), 44100)
>>> z = pf.add((x, y))
or are broadcasted, e.g.,
>>> x = np.arange(3 * 4).reshape((3, 4))
>>> y = pf.Signal(np.ones((2, 3, 4, 10)), 44100)
>>> z = pf.add((x, y))
where y
is a signal with y.cshape = (2, 3, 4)
and a length of 10
samples.
The operators +
, -
, *
, /
, **
and @
are overloaded for
convenience. Note, however, that their behavior depends on the Audio object.
Frequency domain operations are applied for
Signal
and
FrequencyData
objects, i.e,
>>> result = signal1 + signal2
is equivalent to
>>> result = pf.add((signal1, signal2), 'freq')
and time domain operations are applied for
TimeData
objects, i.e.,
>>> result = time_data_1 + time_data_2
is equivalent to
>>> result = pf.add((time_data_1, time_data_2), 'time')
In addition to the arithmetic operations, the equality operator is overloaded to allow comparisons
>>> signal_1 == signal_2
See audio classes
for a complete
documentation.
FFT Normalizations¶
The arithmetic operations are implemented in a way that only physically
meaningful operations are allowed with respect to the
FFT normalization
. These rules are motivated by
the fact that the normalizations correspond to specific types of signals (e.g.,
energy signals, pure tone signals, stochastic broadband signals). While
addition and subtraction are equivalent in the time and frequency domain,
this is not the case for multiplication and division. Nevertheless, the same
rules apply regardless of the domain for convenience:
Addition, subtraction, multiplication, and power¶
If one signal has the FFT normalization
'none'
, the results gets the normalization of the other signal.If both signals have the same FFT normalization, the results gets the same normalization.
Other combinations raise an error.
Division¶
If the denominator signal has the FFT normalization
'none'
, the result gets the normalization of the numerator signal.If both signals have the same FFT normalization, the results gets the normalization
'none'
.Other combinations raise an error.