Mathematics Related Functions¶
noize.mathfun.dsp¶
Script with functions useful in filtering / digital signal processing

noize.mathfun.dsp.
apply_gain_fft
(fft_vals, gain)[source]¶ Reduces noise by applying gain values to the stft / fft array of the target signal
 Parameters
fft_vals (ndarray(complex)) – Matrix containing complex values (i.e. stft values) of target signal
gain (ndarray(real)) – Matrix containing calculated attenuation values to apply to ‘fft_vals’
 Returns
enhanced_fft – Matrix with attenuated noise in target (stft) values
 Return type
ndarray(complex)

noize.mathfun.dsp.
apply_window
(samples, window)[source]¶ Applies predefined window to a section of samples
The length of the samples must be the same length as the window.
 Parameters
samples (ndarray) – series of samples with the length of input window
window (ndarray) – window to be applied to the signal
 Returns
samples_win – series with tapered sides according to the window provided
 Return type
ndarray
Examples
>>> import numpy as np >>> input_signal = np.array([ 0. , 0.36371897, 0.302721, ... 0.1117662 , 0.3957433 ]) >>> window_hamming = np.array([0.08, 0.54, 1. , 0.54, 0.08]) >>> apply_window(input_signal, window_hamming) array([ 0. , 0.19640824, 0.302721 , 0.06035375, 0.03165946]) >>> window_hann = np.array([0. , 0.5, 1. , 0.5, 0. ]) >>> apply_window(input_signal, window_hann) array([ 0. , 0.18185948, 0.302721 , 0.0558831 , 0. ])

noize.mathfun.dsp.
calc_average_power
(matrix, num_iters)[source]¶ Divides matrix values by the number of times power values were added.
This function assumes the power values of nnumber of series were calculated and added. It divides the values in the input matrix by n, i.e. ‘num_iters’.
 Parameters
matrix (ndarray) – a collection of floats or ints representing the sum of power values across several series sets
num_iters (int) – an integer denoting the number of times power values were added to the input matrix
 Returns
matrix – the averaged input matrix
 Return type
ndarray
Examples
>>> matrix = np.array([[6,6,6],[3,3,3],[1,1,1]]) >>> ave_matrix = calc_average_power(matrix, 3) >>> ave_matrix array([[2. , 2. , 2. ], [1. , 1. , 1. ], [0.33333333, 0.33333333, 0.33333333]])

noize.mathfun.dsp.
calc_fft
(signal_section, norm=False)[source]¶ Calculates the fast Fourier transform of a 1D time series
The length of the signal_section determines the number of frequency bins analyzed. Therefore, if there are higher frequencies in the signal, the length of the signal_section should be long enough to accommodate those frequencies.
The frequency bins with energy levels at around zero denote frequencies not prevelant in the signal;the frequency bins with prevalent energy levels relate to frequencies as well as their amplitudes that are in the signal.

noize.mathfun.dsp.
calc_frame_length
(dur_frame_millisec, sampling_rate)[source]¶ Calculates the number of samples necessary for each frame
 Parameters
 Returns
frame_length – the number of samples necessary to fill a frame
 Return type
Examples
>>> calc_frame_length(dur_frame_millisec=20, sampling_rate=1000) 20 >>> calc_frame_length(dur_frame_millisec=20, sampling_rate=48000) 960 >>> calc_frame_length(dur_frame_millisec=25.5, sampling_rate=22500) 573

noize.mathfun.dsp.
calc_gain
(prior_snr)[source]¶ Calculates the gain (i.e. attenuation) values to reduce noise.
 Parameters
prior_snr (ndarray) – The prior signaltonoise ratio estimation
 Returns
gain – An array of attenuation values to be applied to the signal (stft) array at the current frame.
 Return type
ndarray
References
C Loizou, P. (2013). Speech Enhancement: Theory and Practice.
Scalart, P. and Filho, J. (1996). Speech enhancement based on a priori signal to noise estimation. Proc. IEEE Int. Conf. Acoust., Speech, Signal Processing, 629632.

noize.mathfun.dsp.
calc_ifft
(signal_section, norm=False)[source]¶ Calculates the inverse fft of a series of fft values
The real values of the ifft can be used to be saved as an audiofile

noize.mathfun.dsp.
calc_linear_impulse
(noise_frame_len, num_freq_bins)[source]¶ Calc. the post filter coefficients to be applied to gain values.

noize.mathfun.dsp.
calc_noise_frame_len
(SNR_decision, threshold, scale)[source]¶ Calc. window length for calculating moving average.
Note: lower SNRs require larger window.

noize.mathfun.dsp.
calc_num_overlap_samples
(samples_per_frame, percent_overlap)[source]¶ Calculate the number of samples that constitute the overlap of frames
 Parameters
 Returns
num_overlap_samples – the number of samples in the overlap
 Return type
Examples
>>> calc_num_overlap_samples(samples_per_frame=100,percent_overlap=0.10) 10 >>> calc_num_overlap_samples(samples_per_frame=100,percent_overlap=10) 10 >>> calc_num_overlap_samples(samples_per_frame=960,percent_overlap=0.5) 480 >>> calc_num_overlap_samples(samples_per_frame=960,percent_overlap=75) 720

noize.mathfun.dsp.
calc_num_subframes
(tot_samples, frame_length, overlap_samples)[source]¶ Assigns total frames needed to process entire noise or target series
This function calculates the number of full frames that can be created given the total number of samples, the number of samples in each frame, and the number of overlapping samples.
 Parameters
 Returns
 Return type
Examples
>>> calc_num_subframes(30,10,5) 5 >>> calc_num_subframes(30,20,5) 3

noize.mathfun.dsp.
calc_posteri_prime
(posteri_snr)[source]¶ Calculates the posteri prime
 Parameters
posteri_snr (ndarray) – The signaltonoise ratio of the noisey signal, frame by frame.
 Returns
posteri_prime – The primed posteri_snr, calculated according to the reference paper.
 Return type
ndarray
References
Scalart, P. and Filho, J. (1996). Speech enhancement based on a priori signal to noise estimation. Proc. IEEE Int. Conf. Acoust., Speech, Signal Processing, 629632.

noize.mathfun.dsp.
calc_posteri_snr
(target_power_spec, noise_power_spec)[source]¶ Calculates and updates signal to noise ratio of current frame
 Parameters
target_power_spec (ndarray) – matrix of shape with power values of target signal
noise_power_spec (ndarray) – matrix of shape with power values of noise signal
 Returns
posteri_snr – matrix containing the signal to noise ratio
 Return type
ndarray
Examples
>>> sig_power = np.array([6,6,6,6]) >>> noise_power = np.array([2,2,2,2]) >>> calc_posteri_snr(sig_power, noise_power) array([3., 3., 3., 3.])

noize.mathfun.dsp.
calc_power
(fft_vals)[source]¶ Calculates the power of fft values
 Parameters
fft_vals (ndarray (complex or floats)) – the fft values of a windowed section of a series
 Returns
power_spec – the squared absolute value of the input fft values
 Return type
ndarray
Example
>>> import numpy as np >>> matrix = np.array([[1,1,1],[2j,2j,2j],[3,3,3]], ... dtype=np.complex_) >>> calc_power(matrix) array([[0.33333333, 0.33333333, 0.33333333], [1.33333333, 1.33333333, 1.33333333], [3. , 3. , 3. ]])

noize.mathfun.dsp.
calc_power_ratio
(original_powerspec, noisereduced_powerspec)[source]¶ Calc. the ratio of original vs noise reduced power spectrum.

noize.mathfun.dsp.
calc_prior_snr
(snr, snr_prime, smooth_factor=0.98, first_iter=None, gain=None)[source]¶ Estimates the signaltonoise ratio of the previous frame
Depending on the first_iter argument, the prior snr is calculated according to different algorithms. If first_iter is None, prior snr is calculated according to Scalart and Filho (1996); if first_iter is True or False, snr prior is calculated according to Loizou (2013).
 Parameters
snr (ndarray) – The soundtonoise ratio of target vs noise power/energy levels.
snr_prime (ndarray) – The prime of the snr (see Scalart & Filho (1996))
smooth_factor (float) – The value applied to smooth the signal. (default 0.98)
first_iter (None, True, False) – If None, snr prior values are estimated the same, no matter if it is the first iteration or not (Scalart & Filho (1996)) If True, snr prior values are estimated without gain (Loizou 2013) If False, snr prior values are enstimed with gain (Loizou 2013) (default None)
gain (None, ndarray) – If None, gain will not be used. If gain, it is a previously calculated value from the previous frame. (default None)
 Returns
prior_snr – Estimation of signaltonoise ratio of the previous frame of target signal.
 Return type
ndarray
References
C Loizou, P. (2013). Speech Enhancement: Theory and Practice.
Scalart, P. and Filho, J. (1996). Speech enhancement based on a priori signal to noise estimation. Proc. IEEE Int. Conf. Acoust., Speech, Signal Processing, 629632.

noize.mathfun.dsp.
collect_features
(samples, feature_type='mfcc', sr=48000, window_size_ms=20, window_shift_ms=10, num_filters=40, num_mfcc=40, window_function=None)[source]¶ Collects fbank and mfcc features.

noize.mathfun.dsp.
control_volume
(samples, max_limit)[source]¶ Keeps max volume of samples to within a specified range.
 Parameters
samples (ndarray) – series of audio samples
max_limit (float) – maximum boundary of the maximum value of the audio samples
 Returns
 Return type
samples with volume adjusted (if need be)
Examples
>>> import numpy as np >>> #low volume example: increase volume to desired window >>> x = np.array([0.03, 0.04, 0.05, 0.02]) >>> x = control_volume(x, max_limit=0.25) >>> x array([0.13888889, 0.25 , 0.25 , 0.13888889]) >>> #high volume example: decrease volume to desired window >>> y = np.array([0.3, 0.4, 0.5, 0.2]) >>> y = control_volume(y, max_limit=0.15) >>> y array([0.08333333, 0.15 , 0.15 , 0.08333333])

noize.mathfun.dsp.
create_window
(window_type, frame_length)[source]¶ Creates window according to set window type and frame length
the Hamming window tapers edges to around 0.08 while the Hann window tapers edges to 0.0. Both are commonly used in noise filtering.
 Parameters
window_type (str) – type of window to be applied (default ‘hamming’)
 Returns
window – a window fitted to the class attribute ‘frame_length’
 Return type
ndarray
Examples
>>> #create Hamming window >>> hamm_win = create_window('hamming', frame_length=5) >>> hamm_win array([0.08, 0.54, 1. , 0.54, 0.08]) >>> #create Hann window >>> hann_win = create_window('hann',frame_length=5) >>> hann_win array([0. , 0.5, 1. , 0.5, 0. ])

noize.mathfun.dsp.
load_signal
(wav, sampling_rate=48000, dur_sec=None)[source]¶ Loads wavfile, resamples if necessary, and normalizes signal.

noize.mathfun.dsp.
postfilter
(original_powerspec, noisereduced_powerspec, gain, threshold=0.4, scale=10)[source]¶ Apply filter that reduces musical noise resulting from other filter.
If it is estimated that speech (or target signal) is present, reduced filtering is applied.
References
T. Esch and P. Vary, “Efficient musical noise suppression for speech enhancement system,” Proceedings of IEEE International Conference on Acoustics, Speech and Signal Processing, Taipei, 2009.
noize.mathfun.augmentdata¶
Audio data is augmented to result in more resiliant models and filters.

noize.mathfun.augmentdata.
spread_volumes
(samples, vol_list=[0.1, 0.3, 0.5])[source]¶ Returns samples with a range of volumes.
 Parameters
samples (ndarray) – Series belonging to acoustic signal.
vol_list (list) – List of floats or ints representing the volumes the samples are to be oriented towards. (default [0.1,0.3,0.5])
 Returns
volrange_dict – Tuple of volrange_dict values containing samples at various vols.
 Return type
noize.mathfun.matrixfun¶
The matrixfun module offers insight into the matrix manipulation necessary in both digital signal processing and machine learning

noize.mathfun.matrixfun.
add_tensor
(matrix)[source]¶ Adds tensor / dimension to input ndarray.
Keras requires an extra dimension at some layers, which represents the ‘tensor’ encapsulating the data.
Further clarification taking the example below. The input matrix has shape (2,3,4). Think of it as 2 different events, each having 3 sets of measurements, with each of those having 4 features. So, let’s measure differences between 2 cities at 3 different times of day. Let’s take measurements at 08:00, 14:00, and 19:00 in… Magic City and Neverever Town. We’ll measure.. 1) tempurature, 2) wind speed 3) light level 4) noise level.
How I best understand it, putting our measurements into a matrix with an added dimension/tensor, this highlights the separate measurements, telling the algorithm: yes, these are 4 features from the same city, BUT they occur at different times. Or it’s just how Keras set up the code :P
 Parameters
matrix (numpy.ndarray) – The matrix holds the numerical data to add a dimension to.
 Returns
matrix – The matrix with an additional dimension.
 Return type
numpy.ndarray
Examples
>>> import numpy as np >>> matrix = np.arange(24).reshape((2,3,4)) >>> matrix.shape (2, 3, 4) >>> matrix array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) >>> matrix_2 = add_tensor(matrix) >>> matrix_2.shape (2, 3, 4, 1) >>> matrix_2 array([[[[ 0], [ 1], [ 2], [ 3]], [[ 4], [ 5], [ 6], [ 7]], [[ 8], [ 9], [10], [11]]], [[[12], [13], [14], [15]], [[16], [17], [18], [19]], [[20], [21], [22], [23]]]])

noize.mathfun.matrixfun.
create_empty_matrix
(shape, complex_vals=False)[source]¶ Allows creation of a matrix filled with real or complex zeros.
In digital signal processing, complex numbers are common; it is important to note that if complex_vals=False and complex values are inserted into the matrix, the imaginary part will be removed.
 Parameters
 Returns
matrix – a matrix filled with real or complex zeros
 Return type
ndarray
Examples
>>> matrix = create_empty_matrix((3,4)) >>> matrix array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) >>> matrix_complex = create_empty_matrix((3,4),complex_vals=True) >>> matrix_complex array([[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]]) >>> vector = create_empty_matrix(5,) >>> vector array([0., 0., 0., 0., 0.])

noize.mathfun.matrixfun.
separate_dependent_var
(matrix)[source]¶ Separates matrix into features and labels.
Assumes the last column of the last dimension of the matrix constitutes the dependent variable (labels), and all other columns the indpendent variables (features). Additionally, it is assumed that for each block of data, only one label is needed; therefore, just the first label is taken for each block.
 Parameters
matrix (numpy.ndarray) – The matrix holds the numerical data to separate
 Returns
X (numpy.ndarray) – A matrix holding the (assumed) independent variables
y (numpy.ndarray, numpy.int64, numpy.float64) – A vector holding the labels assigned to the independent variables. If only one value in array, just the value inside is returned
Examples
>>> import numpy as np >>> #vector >>> separate_dependent_var(np.array([1,2,3,4])) (array([1, 2, 3]), 4) >>> #simple matrix >>> matrix = np.arange(4).reshape(2,2) >>> matrix array([[0, 1], [2, 3]]) >>> X, y = separate_dependent_var(matrix) >>> X array([[0], [2]]) >>> y 1 >>> #more complex matrix >>> matrix = np.arange(20).reshape((2,2,5)) >>> matrix array([[[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9]], [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]]) >>> X, y = separate_dependent_var(matrix) >>> X array([[[ 0, 1, 2, 3], [ 5, 6, 7, 8]], [[10, 11, 12, 13], [15, 16, 17, 18]]]) >>> y array([ 4, 14])