lazy_analysis
Module¶
Audio analysis and block processing module
Summary of module contents:
Name | Description |
---|---|
window | This is a StrategyDict instance object called window .
Strategies stored: 7. |
wsymm | This is a StrategyDict instance object called wsymm . Strategies
stored: 7. |
acorr | Calculate the autocorrelation of a given 1-D block sequence. |
lag_matrix | Finds the lag matrix for a given 1-D block sequence. |
dft | Complex non-optimized Discrete Fourier Transform |
zcross | Zero-crossing stream. |
envelope | This is a StrategyDict instance object called envelope .
Strategies stored: 3. |
maverage | This is a StrategyDict instance object called maverage .
Strategies stored: 3. |
clip | Clips the signal up to both a lower and a higher limit. |
unwrap | Parametrized signal unwrapping. |
amdf | Average Magnitude Difference Function non-linear filter for a given size and a fixed lag. |
overlap_add | This is a StrategyDict instance object called overlap_add .
Strategies stored: 2. |
stft | This is a StrategyDict instance object called stft . Strategies
stored: 3. |
-
acorr
(blk, max_lag=None)[source]¶ Calculate the autocorrelation of a given 1-D block sequence.
Parameters: - blk – An iterable with well-defined length. Don’t use this function with Stream objects!
- max_lag – The size of the result, the lags you’d need. Defaults to
len(blk) - 1
, since any lag beyond would result in zero.
Returns: A list with lags from 0 up to max_lag, where its
i
-th element has the autocorrelation for a lag equals toi
. Be careful with negative lags! You should use abs(lag) indexes when working with them.- Examples:
>>> seq = [1, 2, 3, 4, 3, 4, 2] >>> acorr(seq) # Default max_lag is len(seq) - 1 [59, 52, 42, 30, 17, 8, 2] >>> acorr(seq, 9) # Zeros at the end [59, 52, 42, 30, 17, 8, 2, 0, 0, 0] >>> len(acorr(seq, 3)) # Resulting length is max_lag + 1 4 >>> acorr(seq, 3) [59, 52, 42, 30]
-
lag_matrix
(blk, max_lag=None)[source]¶ Finds the lag matrix for a given 1-D block sequence.
Parameters: - blk – An iterable with well-defined length. Don’t use this function with Stream objects!
- max_lag – The size of the result, the lags you’d need. Defaults to
len(blk) - 1
, the maximum lag that doesn’t create fully zeroed matrices.
Returns: The covariance matrix as a list of lists. Each cell (i, j) contains the sum of
blk[n - i] * blk[n - j]
elements for all n that allows such without padding the given block.
-
dft
(blk, freqs, normalize=True)[source]¶ Complex non-optimized Discrete Fourier Transform
Finds the DFT for values in a given frequency list, in order, over the data block seen as periodic.
Parameters: - blk – An iterable with well-defined length. Don’t use this function with Stream objects!
- freqs – List of frequencies to find the DFT, in rad/sample. FFT implementations like numpy.fft.ftt finds the coefficients for N frequencies equally spaced as
line(N, 0, 2 * pi, finish=False)
for N frequencies. - normalize – If True (default), the coefficient sums are divided by
len(blk)
, and the coefficient for the DC level (frequency equals to zero) is the mean of the block. If False, that coefficient would be the sum of the data in the block.
Returns: A list of DFT values for each frequency, in the same order that they appear in the freqs input.
Note
This isn’t a FFT implementation, and performs \(O(M . N)\) float pointing operations, with \(M\) and \(N\) equals to the length of the inputs. This function can find the DFT for any specific frequency, with no need for zero padding or finding all frequencies in a linearly spaced band grid with N frequency bins at once.
-
zcross
(seq, hysteresis=0, first_sign=0)[source]¶ Zero-crossing stream.
Parameters: - seq – Any iterable to be used as input for the zero crossing analysis
- hysteresis – Crossing exactly zero might happen many times too fast due to high frequency oscilations near zero. To avoid this, you can make two threshold limits for the zero crossing detection:
hysteresis
and-hysteresis
. Defaults to zero (0), which means no hysteresis and only one threshold. - first_sign – Optional argument with the sign memory from past. Gets the sig from any signed number. Defaults to zero (0), which means “any”, and the first sign will be the first one found in data.
Returns: A Stream instance that outputs 1 for each crossing detected, 0 otherwise.
-
clip
(sig, low=-1.0, high=1.0)[source]¶ Clips the signal up to both a lower and a higher limit.
Parameters: - sig – The signal to be clipped, be it a Stream instance, a list or any iterable.
- [low – ...
- high] – Lower and higher clipping limit, “saturating” the input to them. Defaults to -1.0 and 1.0, respectively. These can be None when needed one-sided clipping. When both limits are set to None, the output will be a Stream that yields exactly the
sig
input data.
Returns: Clipped signal as a Stream instance.
-
unwrap
(sig, max_delta=3.141592653589793, step=6.283185307179586)[source]¶ Parametrized signal unwrapping.
Parameters: - sig – An iterable seen as an input signal.
- max_delta – Maximum value of \(\Delta = sig_i - sig_{i-1}\) to keep output without another minimizing step change. Defaults to \(\pi\).
- step – The change in order to minimize the delta is an integer multiple of this value. Defaults to \(2 . \pi\).
Returns: The signal unwrapped as a Stream, minimizing the step difference when any adjacency step in the input signal is higher than
max_delta
by summing/subtractingstep
.
-
amdf
(lag, size)[source]¶ Average Magnitude Difference Function non-linear filter for a given size and a fixed lag.
Parameters: - lag – Time lag, in samples. See
freq2lag
if needs conversion from frequency values. - size – Moving average size.
Returns: A callable that accepts two parameters: a signal
sig
and the starting memory elementzero
that behaves like theLinearFilter.__call__
arguments. The output from that callable is a Stream instance, and has no decimation applied.See also
freq2lag
- Frequency (in rad/sample) to lag (in samples) converter.
- lag – Time lag, in samples. See
lazy_analysis.window
StrategyDict¶
This is a StrategyDict instance object called
window
. Strategies stored: 7.
Strategy window.bartlett. Docstring starts with:
Bartlett (triangular starting with zero) windowing/apodization function (periodic).
Strategy window.blackman. Docstring starts with:
Blackman windowing/apodization function (periodic).
Strategy window.cos. Docstring starts with:
Cosine to the power of alpha windowing/apodization function (periodic).
Strategy window.hamming. Docstring starts with:
Hamming windowing/apodization function (periodic).
Strategy window.hann (Default).
An alias for it is window.hanning
.
Docstring starts with:
Hann windowing/apodization function (periodic).
Strategy window.rect.
Aliases available are window.dirichlet
, window.rectangular
.
Docstring starts with:
Dirichlet/rectangular windowing/apodization function.
Strategy window.triangular.
An alias for it is window.triangle
.
Docstring starts with:
Triangular (with no zero end-point) windowing/apodization function (periodic).
Note
This docstring is self-generated, see the StrategyDict class and the strategies docs for more details.
-
hann
(size)¶ Hann windowing/apodization function (periodic).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[\frac{1}{2} \left[ 1 - \cos \left( \frac{2 \pi n}{size} \right) \right]\]This window model was taken from:
Harris, F. J. "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform". Proceedings of the IEEE, vol. 66, no. 1, January 1978.
Parameters: size – Window size in samples. Returns: List with the window samples. Note
Be careful as this isn’t a “symmetric” window implementation by default, you should append the first sample at the end to get a
size + 1
symmetric window. The “periodic” window implementation returned by this function is designed for using directly with DFT/STFT. See the F. J. Harris paper for more information on these.By default, Numpy, Scipy signal subpackage, GNU Octave and MatLab uses the symmetric approach for the window functions, with [1.0] as the result when the size is 1 (which means the window is actually empty). Here the implementation differ expecting that these functions will be mainly used in a DFT/STFT process.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the symmetric strategywsymm.hann
with:window.hann.symm
;window.symm.hann
(window.symm is wsymm);wsymm.hann.symm
(unneeded.symm
);wsymm.symm.hann
(pleonastically, as wsymm.symm is wsymm).
See also
wsymm
- StrategyDict instance with symmetric windowing/apodization functions.
wsymm.hann
- Hann windowing/apodization function (symmetric).
stft
- Short Time Fourier Transform block processor / phase vocoder wrapper.
overlap_add
- Overlap-add algorithm for an interables of blocks.
-
hamming
(size)¶ Hamming windowing/apodization function (periodic).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[0.54 - 0.46 \cos \left( \frac{2 \pi n}{size} \right)\]This window model was taken from:
Harris, F. J. "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform". Proceedings of the IEEE, vol. 66, no. 1, January 1978.
Parameters: size – Window size in samples. Returns: List with the window samples. Note
Be careful as this isn’t a “symmetric” window implementation by default, you should append the first sample at the end to get a
size + 1
symmetric window. The “periodic” window implementation returned by this function is designed for using directly with DFT/STFT. See the F. J. Harris paper for more information on these.By default, Numpy, Scipy signal subpackage, GNU Octave and MatLab uses the symmetric approach for the window functions, with [1.0] as the result when the size is 1 (which means the window is actually empty). Here the implementation differ expecting that these functions will be mainly used in a DFT/STFT process.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the symmetric strategywsymm.hamming
with:window.hamming.symm
;window.symm.hamming
(window.symm is wsymm);wsymm.hamming.symm
(unneeded.symm
);wsymm.symm.hamming
(pleonastically, as wsymm.symm is wsymm).
See also
wsymm
- StrategyDict instance with symmetric windowing/apodization functions.
wsymm.hamming
- Hamming windowing/apodization function (symmetric).
stft
- Short Time Fourier Transform block processor / phase vocoder wrapper.
overlap_add
- Overlap-add algorithm for an interables of blocks.
-
rect
(size)¶ Dirichlet/rectangular windowing/apodization function.
This window model was taken from:
Harris, F. J. "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform". Proceedings of the IEEE, vol. 66, no. 1, January 1978.
Parameters: size – Window size in samples. Returns: List with the window samples. All values are ones (1.0). Note
As this strategy is both “symmetric” and “periodic”,
window.rect
andwsymm.rect
are the very same function/strategy.Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). However, in this case, they’re the same, i.e.,window.rect
iswsymm.rect
.See also
ones
- Lazy
1.0
stream generator.
-
bartlett
(size)¶ Bartlett (triangular starting with zero) windowing/apodization function (periodic).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[1 - \frac{2}{size} \left| \frac{n - size}{2} \right|\]Parameters: size – Window size in samples. Returns: List with the window samples. Note
Be careful as this isn’t a “symmetric” window implementation by default, you should append the first sample at the end to get a
size + 1
symmetric window. The “periodic” window implementation returned by this function is designed for using directly with DFT/STFT. See the F. J. Harris paper for more information on these.By default, Numpy, Scipy signal subpackage, GNU Octave and MatLab uses the symmetric approach for the window functions, with [1.0] as the result when the size is 1 (which means the window is actually empty). Here the implementation differ expecting that these functions will be mainly used in a DFT/STFT process.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the symmetric strategywsymm.bartlett
with:window.bartlett.symm
;window.symm.bartlett
(window.symm is wsymm);wsymm.bartlett.symm
(unneeded.symm
);wsymm.symm.bartlett
(pleonastically, as wsymm.symm is wsymm).
See also
wsymm
- StrategyDict instance with symmetric windowing/apodization functions.
wsymm.bartlett
- Bartlett (triangular starting with zero) windowing/apodization function (symmetric).
window.triangular
- Triangular with no zero end-point (periodic).
wsymm.triangular
- Triangular with no zero end-point (symmetric).
stft
- Short Time Fourier Transform block processor / phase vocoder wrapper.
overlap_add
- Overlap-add algorithm for an interables of blocks.
-
triangular
(size)¶ Triangular (with no zero end-point) windowing/apodization function (periodic).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[1 - \frac{2}{size + 2} \left| \frac{n - size}{2} \right|\]Parameters: size – Window size in samples. Returns: List with the window samples. Note
Be careful as this isn’t a “symmetric” window implementation by default, you should append the first sample at the end to get a
size + 1
symmetric window. The “periodic” window implementation returned by this function is designed for using directly with DFT/STFT. See the F. J. Harris paper for more information on these.By default, Numpy, Scipy signal subpackage, GNU Octave and MatLab uses the symmetric approach for the window functions, with [1.0] as the result when the size is 1 (which means the window is actually empty). Here the implementation differ expecting that these functions will be mainly used in a DFT/STFT process.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the symmetric strategywsymm.triangular
with:window.triangular.symm
;window.symm.triangular
(window.symm is wsymm);wsymm.triangular.symm
(unneeded.symm
);wsymm.symm.triangular
(pleonastically, as wsymm.symm is wsymm).
See also
wsymm
- StrategyDict instance with symmetric windowing/apodization functions.
wsymm.triangular
- Triangular (with no zero end-point) windowing/apodization function (symmetric).
window.bartlett
- Triangular starting with zero (periodic).
wsymm.bartlett
- Triangular starting with zero (symmetric).
stft
- Short Time Fourier Transform block processor / phase vocoder wrapper.
overlap_add
- Overlap-add algorithm for an interables of blocks.
-
cos
(size, alpha=1)¶ Cosine to the power of alpha windowing/apodization function (periodic).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[\left[ \sin \left( \frac{\pi n}{size} \right) \right]^{\alpha}\]This window model was taken from:
Harris, F. J. "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform". Proceedings of the IEEE, vol. 66, no. 1, January 1978.
Parameters: - size – Window size in samples.
- alpha – Power value. Defaults to 1.
Returns: List with the window samples.
Note
Be careful as this isn’t a “symmetric” window implementation by default, you should append the first sample at the end to get a
size + 1
symmetric window. The “periodic” window implementation returned by this function is designed for using directly with DFT/STFT. See the F. J. Harris paper for more information on these.By default, Numpy, Scipy signal subpackage, GNU Octave and MatLab uses the symmetric approach for the window functions, with [1.0] as the result when the size is 1 (which means the window is actually empty). Here the implementation differ expecting that these functions will be mainly used in a DFT/STFT process.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the symmetric strategywsymm.cos
with:window.cos.symm
;window.symm.cos
(window.symm is wsymm);wsymm.cos.symm
(unneeded.symm
);wsymm.symm.cos
(pleonastically, as wsymm.symm is wsymm).
See also
wsymm
- StrategyDict instance with symmetric windowing/apodization functions.
wsymm.cos
- Cosine to the power of alpha windowing/apodization function (symmetric).
stft
- Short Time Fourier Transform block processor / phase vocoder wrapper.
overlap_add
- Overlap-add algorithm for an interables of blocks.
-
blackman
(size, alpha=0.16)¶ Blackman windowing/apodization function (periodic).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[\frac{1 - \alpha}{2} - \frac{1}{2} \cos \left( \frac{2 \pi n}{size} \right) + \frac{\alpha}{2} \cos \left( \frac{4 \pi n}{size} \right)\]This window model was taken from:
Harris, F. J. "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform". Proceedings of the IEEE, vol. 66, no. 1, January 1978.
Parameters: - size – Window size in samples.
- alpha – Blackman window alpha value. Defaults to 0.16. Use
2.0 * 1430 / 18608
for the ‘exact Blackman’ window.
Returns: List with the window samples.
Note
Be careful as this isn’t a “symmetric” window implementation by default, you should append the first sample at the end to get a
size + 1
symmetric window. The “periodic” window implementation returned by this function is designed for using directly with DFT/STFT. See the F. J. Harris paper for more information on these.By default, Numpy, Scipy signal subpackage, GNU Octave and MatLab uses the symmetric approach for the window functions, with [1.0] as the result when the size is 1 (which means the window is actually empty). Here the implementation differ expecting that these functions will be mainly used in a DFT/STFT process.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the symmetric strategywsymm.blackman
with:window.blackman.symm
;window.symm.blackman
(window.symm is wsymm);wsymm.blackman.symm
(unneeded.symm
);wsymm.symm.blackman
(pleonastically, as wsymm.symm is wsymm).
See also
wsymm
- StrategyDict instance with symmetric windowing/apodization functions.
wsymm.blackman
- Blackman windowing/apodization function (symmetric).
stft
- Short Time Fourier Transform block processor / phase vocoder wrapper.
overlap_add
- Overlap-add algorithm for an interables of blocks.
lazy_analysis.wsymm
StrategyDict¶
This is a StrategyDict instance object called
wsymm
. Strategies stored: 7.
Strategy wsymm.bartlett. Docstring starts with:
Bartlett (triangular starting with zero) windowing/apodization function (symmetric).
Strategy wsymm.blackman. Docstring starts with:
Blackman windowing/apodization function (symmetric).
Strategy wsymm.cos. Docstring starts with:
Cosine to the power of alpha windowing/apodization function (symmetric).
Strategy wsymm.hamming. Docstring starts with:
Hamming windowing/apodization function (symmetric).
Strategy wsymm.hann (Default).
An alias for it is wsymm.hanning
.
Docstring starts with:
Hann windowing/apodization function (symmetric).
Strategy wsymm.rect. Docstring starts with:
Dirichlet/rectangular windowing/apodization function.
Strategy wsymm.triangular.
An alias for it is wsymm.triangle
.
Docstring starts with:
Triangular (with no zero end-point) windowing/apodization function (symmetric).
Note
This docstring is self-generated, see the StrategyDict class and the strategies docs for more details.
-
hann
(size)¶ Hann windowing/apodization function (symmetric).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[\frac{1}{2} \left[ 1 - \cos \left( \frac{2 \pi n}{size - 1} \right) \right]\]This window model was taken from:
Harris, F. J. "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform". Proceedings of the IEEE, vol. 66, no. 1, January 1978.
Parameters: size – Window size in samples. Returns: List with the window samples. Warning
Don’t use this strategy for FFT/DFT/STFT windowing! You should use the periodic approach for that. See the F. J. Harris paper for more information.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the periodic strategywindow.hann
with:wsymm.hann.periodic
;wsymm.periodic.hann
(wsymm.periodic is window);window.hann.periodic
(unneeded.periodic
);window.periodic.hann
(pleonastically, as window.periodic is window).
See also
window
- StrategyDict instance with periodic windowing/apodization functions.
window.hann
- Hann windowing/apodization function (periodic).
-
hamming
(size)¶ Hamming windowing/apodization function (symmetric).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[0.54 - 0.46 \cos \left( \frac{2 \pi n}{size - 1} \right)\]This window model was taken from:
Harris, F. J. "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform". Proceedings of the IEEE, vol. 66, no. 1, January 1978.
Parameters: size – Window size in samples. Returns: List with the window samples. Warning
Don’t use this strategy for FFT/DFT/STFT windowing! You should use the periodic approach for that. See the F. J. Harris paper for more information.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the periodic strategywindow.hamming
with:wsymm.hamming.periodic
;wsymm.periodic.hamming
(wsymm.periodic is window);window.hamming.periodic
(unneeded.periodic
);window.periodic.hamming
(pleonastically, as window.periodic is window).
See also
window
- StrategyDict instance with periodic windowing/apodization functions.
window.hamming
- Hamming windowing/apodization function (periodic).
-
blackman
(size, alpha=0.16)¶ Blackman windowing/apodization function (symmetric).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[\frac{1 - \alpha}{2} - \frac{1}{2} \cos \left( \frac{2 \pi n}{size - 1} \right) + \frac{\alpha}{2} \cos \left( \frac{4 \pi n}{size - 1} \right)\]This window model was taken from:
Harris, F. J. "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform". Proceedings of the IEEE, vol. 66, no. 1, January 1978.
Parameters: - size – Window size in samples.
- alpha – Blackman window alpha value. Defaults to 0.16. Use
2.0 * 1430 / 18608
for the ‘exact Blackman’ window.
Returns: List with the window samples.
Warning
Don’t use this strategy for FFT/DFT/STFT windowing! You should use the periodic approach for that. See the F. J. Harris paper for more information.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the periodic strategywindow.blackman
with:wsymm.blackman.periodic
;wsymm.periodic.blackman
(wsymm.periodic is window);window.blackman.periodic
(unneeded.periodic
);window.periodic.blackman
(pleonastically, as window.periodic is window).
See also
window
- StrategyDict instance with periodic windowing/apodization functions.
window.blackman
- Blackman windowing/apodization function (periodic).
-
bartlett
(size)¶ Bartlett (triangular starting with zero) windowing/apodization function (symmetric).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[1 - \frac{2}{size - 1} \left| \frac{n - size - 1}{2} \right|\]Parameters: size – Window size in samples. Returns: List with the window samples. Warning
Don’t use this strategy for FFT/DFT/STFT windowing! You should use the periodic approach for that. See the F. J. Harris paper for more information.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the periodic strategywindow.bartlett
with:wsymm.bartlett.periodic
;wsymm.periodic.bartlett
(wsymm.periodic is window);window.bartlett.periodic
(unneeded.periodic
);window.periodic.bartlett
(pleonastically, as window.periodic is window).
See also
window
- StrategyDict instance with periodic windowing/apodization functions.
window.bartlett
- Bartlett (triangular starting with zero) windowing/apodization function (periodic).
window.triangular
- Triangular with no zero end-point (periodic).
wsymm.triangular
- Triangular with no zero end-point (symmetric).
-
triangular
(size)¶ Triangular (with no zero end-point) windowing/apodization function (symmetric).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[1 - \frac{2}{size + 1} \left| \frac{n - size - 1}{2} \right|\]Parameters: size – Window size in samples. Returns: List with the window samples. Warning
Don’t use this strategy for FFT/DFT/STFT windowing! You should use the periodic approach for that. See the F. J. Harris paper for more information.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the periodic strategywindow.triangular
with:wsymm.triangular.periodic
;wsymm.periodic.triangular
(wsymm.periodic is window);window.triangular.periodic
(unneeded.periodic
);window.periodic.triangular
(pleonastically, as window.periodic is window).
See also
window
- StrategyDict instance with periodic windowing/apodization functions.
window.triangular
- Triangular (with no zero end-point) windowing/apodization function (periodic).
window.bartlett
- Triangular starting with zero (periodic).
wsymm.bartlett
- Triangular starting with zero (symmetric).
-
cos
(size, alpha=1)¶ Cosine to the power of alpha windowing/apodization function (symmetric).
For this model, the resulting \(n\)-th sample (where \(n = 0, 1, \cdots, size - 1\)) is:
\[\left[ \sin \left( \frac{\pi n}{size - 1} \right) \right]^{\alpha}\]This window model was taken from:
Harris, F. J. "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform". Proceedings of the IEEE, vol. 66, no. 1, January 1978.
Parameters: - size – Window size in samples.
- alpha – Power value. Defaults to 1.
Returns: List with the window samples.
Warning
Don’t use this strategy for FFT/DFT/STFT windowing! You should use the periodic approach for that. See the F. J. Harris paper for more information.
Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). You can get the periodic strategywindow.cos
with:wsymm.cos.periodic
;wsymm.periodic.cos
(wsymm.periodic is window);window.cos.periodic
(unneeded.periodic
);window.periodic.cos
(pleonastically, as window.periodic is window).
See also
window
- StrategyDict instance with periodic windowing/apodization functions.
window.cos
- Cosine to the power of alpha windowing/apodization function (periodic).
-
rect
(size)¶ Dirichlet/rectangular windowing/apodization function.
This window model was taken from:
Harris, F. J. "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform". Proceedings of the IEEE, vol. 66, no. 1, January 1978.
Parameters: size – Window size in samples. Returns: List with the window samples. All values are ones (1.0). Note
As this strategy is both “symmetric” and “periodic”,
window.rect
andwsymm.rect
are the very same function/strategy.Hint
All
window
andwsymm
strategies have both aperiodic
andsymm
attribute with the respective strategy. The StrategyDict instances themselves also have these attributes (with the respective StrategyDict instance). However, in this case, they’re the same, i.e.,window.rect
iswsymm.rect
.See also
ones
- Lazy
1.0
stream generator.
lazy_analysis.envelope
StrategyDict¶
This is a StrategyDict instance object called
envelope
. Strategies stored: 3.
Strategy envelope.abs. Docstring starts with:
Envelope non-linear filter.
Strategy envelope.rms (Default). Docstring starts with:
Envelope non-linear filter.
Strategy envelope.squared. Docstring starts with:
Squared envelope non-linear filter.
Note
This docstring is self-generated, see the StrategyDict class and the strategies docs for more details.
-
rms
(sig, cutoff=0.006135923151542565)¶ Envelope non-linear filter.
This strategy finds a RMS by passing the squared data through a low pass filter and taking its square root afterwards.
Parameters: - sig – The signal to be filtered.
- cutoff – Lowpass filter cutoff frequency, in rad/sample. Defaults to
pi/512
.
Returns: A Stream instance with the envelope, without any decimation.
See also
maverage
- Moving average linear filter.
-
abs
(sig, cutoff=0.006135923151542565)¶ Envelope non-linear filter.
This strategy make an ideal half wave rectification (get the absolute value of each signal) and pass the resulting data through a low pass filter.
Parameters: - sig – The signal to be filtered.
- cutoff – Lowpass filter cutoff frequency, in rad/sample. Defaults to
pi/512
.
Returns: A Stream instance with the envelope, without any decimation.
See also
maverage
- Moving average linear filter.
-
squared
(sig, cutoff=0.006135923151542565)¶ Squared envelope non-linear filter.
This strategy squares the input, and apply a low pass filter afterwards.
Parameters: - sig – The signal to be filtered.
- cutoff – Lowpass filter cutoff frequency, in rad/sample. Defaults to
pi/512
.
Returns: A Stream instance with the envelope, without any decimation.
See also
maverage
- Moving average linear filter.
lazy_analysis.maverage
StrategyDict¶
This is a StrategyDict instance object called
maverage
. Strategies stored: 3.
Strategy maverage.deque (Default). Docstring starts with:
Moving average
Strategy maverage.fir. Docstring starts with:
Moving average
Strategy maverage.recursive.
An alias for it is maverage.feedback
.
Docstring starts with:
Moving average
Note
This docstring is self-generated, see the StrategyDict class and the strategies docs for more details.
-
fir
(size)¶ Moving average
Linear filter implementation as a FIR ZFilter.
Parameters: size – Data block window size. Should be an integer. Returns: A ZFilter instance with the FIR filter. See also
envelope
- Signal envelope (time domain) strategies.
-
deque
(size)¶ Moving average
This is the only strategy that uses a
collections.deque
object instead of a ZFilter instance. Fast, but without extra capabilites such as a frequency response plotting method.Parameters: size – Data block window size. Should be an integer. Returns: A callable that accepts two parameters: a signal sig
and the starting memory elementzero
that behaves like theLinearFilter.__call__
arguments. The output from that callable is a Stream instance, and has no decimation applied.See also
envelope
- Signal envelope (time domain) strategies.
lazy_analysis.overlap_add
StrategyDict¶
This is a StrategyDict instance object called
overlap_add
. Strategies stored: 2.
Strategy overlap_add.list. Docstring starts with:
Overlap-add algorithm using lists instead of Numpy arrays. The behavior is the same to theoverlap_add.numpy
strategy, besides the data types.
Strategy overlap_add.numpy (Default). Docstring starts with:
Overlap-add algorithm using Numpy arrays.
Note
This docstring is self-generated, see the StrategyDict class and the strategies docs for more details.
-
list
(blk_sig, size=None, hop=None, wnd=None, normalize=True)¶ Overlap-add algorithm using lists instead of Numpy arrays. The behavior is the same to the
overlap_add.numpy
strategy, besides the data types.
-
numpy
(blk_sig, size=None, hop=None, wnd=None, normalize=True)¶ Overlap-add algorithm using Numpy arrays.
Parameters: - blk_sig – An iterable of blocks (sequences), such as the
Stream.blocks
result. - size – Block size for each
blk_sig
element, in samples. - hop – Number of samples for two adjacent blocks (defaults to the size).
- wnd – Windowing function to be applied to each block or any iterable with exactly
size
elements. IfNone
(default), applies a rectangular window. - normalize – Flag whether the window should be normalized so that the process could happen in the [-1; 1] range, dividing the window by its hop gain. Default is
True
.
Returns: A Stream instance with the blocks overlapped and added.
See also
Stream.blocks
- Splits the Stream instance into blocks with given size and hop.
blocks
- Same to Stream.blocks but for without using the Stream class.
chain
- Lazily joins all iterables given as parameters.
chain.from_iterable
- Same to
chain(*data)
, but thedata
evaluation is lazy. window
- Window/apodization/tapering functions for a given size as a StrategyDict.
Note
Each block has the window function applied to it and the result is the sum of the blocks without any edge-case special treatment for the first and last few blocks.
- blk_sig – An iterable of blocks (sequences), such as the
lazy_analysis.stft
StrategyDict¶
This is a StrategyDict instance object called
stft
. Strategies stored: 3.
Strategy stft.cfft.
An alias for it is stft.complex
.
Docstring starts with:
Short Time Fourier Transform for complex data.
Strategy stft.cfftr.
An alias for it is stft.complex_real
.
Docstring starts with:
Short Time Fourier Transform for real data keeping the full FFT block.
Strategy stft.rfft (Default).
Aliases available are stft.base
, stft.real
.
Docstring starts with:
Short Time Fourier Transform block processor / phase vocoder wrapper.
Note
This docstring is self-generated, see the StrategyDict class and the strategies docs for more details.
-
cfft
(func=None, **kwparams)¶ Short Time Fourier Transform for complex data.
Same to the default STFT strategy, but with new defaults. This is the same to:
stft.base(transform=numpy.fft.fft, inverse_transform=numpy.fft.ifft)
See
stft.base
docs for more.
-
rfft
(func=None, **kwparams)¶ Short Time Fourier Transform block processor / phase vocoder wrapper.
This function can be used in many ways:
- Directly as a signal processor builder, wrapping a spectrum block/grain processor function;
- Directly as a decorator to a block processor;
- Called without the
func
parameter for a partial evalution style changing the defaults.
See the examples below for more information about these use cases.
The resulting function performs a full block-by-block analysis/synthesis phase vocoder keeping this sequence of actions:
- Blockenize the signal with the given
size
andhop
; - Lazily apply the given
wnd
window to each block; - Perform the 5 actions calling their functions in order:
before
: Pre-processing;transform
: A transform like the FFT;func
: the positional parameter with the single block processor;inverse_transform
: inverse FFT;after
: Post-processing.
- Overlap-add with the
ola
overlap-add strategy. The givenola
would deal with its own window application and normalization.
Any parameter from steps 3 and 4 can be set to
None
to skip it from the full process, without changing the other [sub]steps. The parameters defaults are based on the Numpy FFT subpackage.Parameters: - func – The block/grain processor function that receives a transformed block in the frequency domain (the
transform
output) and should return the processed data (it will be the firstinverse_transform
input). This parameter shouldn’t appear when this function is used as a decorator. - size – Block size for the STFT process, in samples.
- hop – Duration in samples between two blocks. Defaults to the
size
value. - transform – Function that receives the windowed block (in time domain) and the
size
as two positional inputs and should return the block (in frequency domain). Defaults tonumpy.fft.rfft
, which outputs a Numpy 1D array with length equals tosize // 2 + 1
. - inverse_transform – Function that receives the processed block (in frequency domain) and the
size
as two positional inputs and should return the block (in time domain). Defaults tonumpy.fft.irfft
. - wnd – Window function to be called as
wnd(size)
or window iterable with length equals tosize
. The windowing/apodization values are used before taking the FFT of each block. Defaults to None, which means no window should be applied (same behavior of a rectangular window). - before – Function to be applied just before taking the transform, after the windowing. Defaults to the
numpy.fft.ifftshift
, which, together with theafter
default, puts the time reference at thesize // 2
index of the block, centralizing it for the FFT (e.g. blocks[0, 1, 0]
and[0, 0, 1, 0]
would have zero phase). To disable this realignment, just change bothbefore=None
andafter=None
keywords. - after – Function to be applied just after the inverse transform, before calling the overlap-add (as well as before its windowing, if any). Defaults to the
numpy.fft.fftshift
function, which undo the changes done by the defaultbefore
pre-processing for block phase alignment. To avoid the default time-domain realignment, set bothbefore=None
andafter=None
keywords. - ola – Overlap-add strategy. Uses the
overlap_add
default strategy when not given. The strategy should allow at least size and hop keyword arguments, besides a first positional argument for the iterable with blocks. Ifola=None
, the result from using the STFT processor will be theStream
of blocks that would be the overlap-add input. - ola_* – Extra keyword parameters for the overlap-add strategy, if any. The extra
ola_
prefix is removed when calling it. See the overlap-add strategy docs for more information about the valid parameters.
Returns: A function with the same parameters above, besides
func
, which is replaced by the signal input (if func was given). The parameters used when building the function should be seen as defaults that can be changed when calling the resulting function with the respective keyword arguments.- Examples:
Let’s process something:
>>> my_signal = Stream(.1, .3, -.1, -.3, .5, .4, .3)
Wrapping directly the processor function:
>>> processor_w = stft(abs, size=64) >>> sig = my_signal.copy() # Any iterable >>> processor_w(sig) <audiolazy.lazy_stream.Stream object at 0x...> >>> peek200_w = _.peek(200) # Needs Numpy >>> type(peek200_w[0]).__name__ # Result is a signal (numpy.float64 data) 'float64'
Keyword parameters in a partial evaluation style (can be reassigned):
>>> stft64 = stft(size=64) # Same to ``stft`` but with other defaults >>> processor_p = stft64(abs) >>> sig = my_signal.copy() # Any iterable >>> processor_p(sig) <audiolazy.lazy_stream.Stream object at 0x...> >>> _.peek(200) == peek200_w # This should do the same thing True
As a decorator, this time with other windowing configuration:
>>> stft64hann = stft64(wnd=window.hann, ola_wnd=window.hann) >>> @stft64hann # stft(...) can also be used as an anonymous decorator ... def processor_d(blk): ... return abs(blk) >>> processor_d(sig) # This leads to a different result <audiolazy.lazy_stream.Stream object at 0x...> >>> _.peek(200) == peek200_w False
You can also use other iterables as input, and keep the parameters to be passed afterwards, as well as change transform calculation:
>>> stft_no_zero_phase = stft(before=None, after=None) >>> stft_no_wnd = stft_no_zero_phase(ola=overlap_add.list, ola_wnd=None, ... ola_normalize=False) >>> on_blocks = stft_no_wnd(transform=None, inverse_transform=None) >>> processor_a = on_blocks(reversed, hop=4) # Reverse >>> processor_a([1, 2, 3, 4, 5], size=4, hop=2) <audiolazy.lazy_stream.Stream object at 0x...> >>> list(_) # From blocks [1, 2, 3, 4] and [3, 4, 5, 0.0] [4.0, 3.0, 2.0, 6, 4, 3] >>> processor_a([1, 2, 3, 4, 5], size=4) # Default hop instead <audiolazy.lazy_stream.Stream object at 0x...> >>> list(_) # No overlap, blocks [1, 2, 3, 4] and [5, 0.0, 0.0, 0.0] [4, 3, 2, 1, 0.0, 0.0, 0.0, 5] >>> processor_a([1, 2, 3, 4, 5]) # Size was never given Traceback (most recent call last): ... TypeError: Missing 'size' argument
For analysis only, one can set
ola=None
:>>> from numpy.fft import ifftshift # [1, 2, 3, 4, 5] -> [3, 4, 5, 1, 2] >>> analyzer = stft(ifftshift, ola=None, size=8, hop=2) >>> sig = Stream(1, 0, -1, 0) # A pi/2 rad/sample cosine signal >>> result = analyzer(sig) >>> result <audiolazy.lazy_stream.Stream object at 0x...>
Let’s see the result contents. That processing “rotates” the frequencies, converting the original
[0, 0, 4, 0, 0]
real FFT block to a[4, 0, 0, 0, 0]
block, which means the block cosine was moved to a DC-only signal keeping original energy/integral:>>> result.take() array([ 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]) >>> result.take() # From [0, 0, -4, 0, 0] to [-4, 0, 0, 0, 0] array([-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5])
Note
Parameters should be passed as keyword arguments. The only exception is
func
for this function andsig
for the returned function, which are always the first positional argument, ald also the one that shouldn’t appear when using this function as a decorator.Hint
- When using Numpy FFT, one can keep data in place and return the changed input block to save time;
- Actually, there’s nothing in this function that imposes FFT or Numpy besides the default values. One can still use this even for other transforms that have nothing to do with the Fourier Transform.
See also
overlap_add
- Overlap-add algorithm for an iterable (e.g. a Stream instance) of blocks (sequences such as lists or Numpy arrays). It’s also a StrategyDict.
window
- Window/apodization/tapering functions for a given size as a StrategyDict.
-
cfftr
(func=None, **kwparams)¶ Short Time Fourier Transform for real data keeping the full FFT block.
Same to the default STFT strategy, but with new defaults. This is the same to:
stft.base(transform=numpy.fft.fft, inverse_transform=lambda *args: numpy.fft.ifft(*args).real)
See
stft.base
docs for more.