Filters and their Features

noize.filterfun.filters

The filters module covers functions related to the filtering out of noise of a target signal, whether that be the collection of power spectrum values to calculate the average power spectrum of each audio class or to measure signal-to-noise ratio of a signal and ultimately the the actual filtering process.

class noize.filterfun.filters.FilterSettings(frame_duration_ms=20, percent_overlap=0.5, sampling_rate=48000, window_type='hamming')[source]

Bases: object

Basic settings for filter related classes to inherit from.

frame_dur

Time in milliseconds of each audio frame window. (default 20)

Type

int, float

sr

Desired sampling rate of audio; audio will be resampled to match if audio has other sampling rate. (default 48000)

Type

int

frame_length

Number of audio samples in each frame: frame_dur multiplied with sampling_rate, divided by 1000. (default 960)

Type

int

percent_overlap

Percentage of overlap between frames.

Type

float

overlap_length

Number of overlapping audio samples between subsequent frames: frame_length multiplied by percent_overlap, floored. (default 480)

Type

int

window_type

Type of window applied to audio frames: hann vs hamming (default ‘hamming’)

Type

str

num_fft_bins

The number of frequency bins used when calculating the fft. Currently the frame_length is used to set num_fft_bins.

Type

int

get_window()[source]

Returns window acc. to attributes window_type and frame_length

class noize.filterfun.filters.WelchMethod(len_noise_sec=1)[source]

Bases: noize.filterfun.filters.FilterSettings

Applies Welch’s method according to filter class attributes.

len_noise_sec

The amount of time in seconds to use from signal to apply the Welch’s method. (default 1)

Type

int, float

target_subframes

The number of total subsections within the total number of samples belonging to the target signal (i.e. wavfile being filtered). Until target_subframes is calculated, it is set to None. Note: if the target signal contains time sensitive information, e.g. speech, it is not advised to apply Welch’s method as the time senstive data would be lost. (default None)

Type

int, None

noise_subframes

The number of total subsections within the total number of samples belonging to the noise signal. Until noise_subframes is calculated, it is set to None. (default None)

Type

int, None

coll_pow_average(wave_list, scale=None, augment_data=False)[source]

Performs Welch’s method on (noise) signals in wave_list.

Parameters
  • wave_list (list) – List of wavfiles belonging to noise class. The Welch’s method will be applied to entire noise class.

  • scale (float, int, optional) – A value to increase or decrease the noise values. This will result in a stronger or weaker filtering.

  • augment_data (bool) – If True, the sound data will be augmented. Currently, this uses three versions of each wavefile, at three different energy levels. This is to increase representation of noise that is quiet, mid, and loud.

Returns

noise_powspec – The average power spectrum of the entire wave_list

Return type

ndarray

get_power(samples, matrix2store_power)[source]

Calculates and adds the power of the noise signal.

Parameters
  • samples (ndarray) – The samples from the noise signal.

  • matrix2store_power (ndarray) – Where the power values will be added to. Note: this is to be later averaged to complete Welch’s method.

Returns

matrix2store_powermatrix2store_power with the current frame of noise power added.

Return type

ndarray

set_num_subframes(len_samples, noise=True)[source]

Calculate and set num subframes required to process all samples.

Parameters
  • len_samples (int) – Number of total samples in signal.

  • noise (bool) – If True, the class attribute noise_subframes will be set; if False, the class attribute target_subframes will be set.

Returns

Return type

None

class noize.filterfun.filters.WienerFilter(smooth_factor=0.98, first_iter=None, max_vol=0.4)[source]

Bases: noize.filterfun.filters.FilterSettings

Interactive class to explore Wiener filter settings on audio signals.

These class methods implement research based algorithms with low computational cost, aimed for noise reduction via mobile phone.

beta

Value applied in Wiener filter that smooths the application of ‘gain’; default set according to previous research. (default 0.98)

Type

float

first_iter

Keeps track if first_iter is relevant in filtering. If True, filtering has just started, and calculations made for filtering cannot use information from previous frames; if False, calculations for filtering use information from previous frames; if None, no difference is applied when processing the 1st vs subsequent frames. (default None)

Type

bool, optional

target_subframes

The number of total subsections within the total number of samples belonging to the target signal (i.e. wavfile being filtered). Until target_subframes is calculated, it is set to None. (default None)

Type

int, None

noise_subframes

The number of total subsections within the total number of samples belonging to the noise signal. If noise power spectrum is used, this doesn’t need to be calculated. Until noise_subframes is calculated, it is set to None. (default None)

Type

int, None

gain

Once calculated, the attenuation values to be applied to the fft for noise reduction. Until calculated, None. (default None)

Type

ndarray, None

max_vol

The maximum volume allowed for the filtered signal. (default 0.4)

Type

float, int

check_volume(samples)[source]

ensures volume of filtered signal is within the bounds of the original

get_samples(wavfile, dur_sec=None)[source]

Load signal and save original volume

Parameters
  • wavfile (str) – Path and name of wavfile to be loaded

  • dur_sec (int, float optional) – Max length of time in seconds (default None)

Returns

samples – Array containing signal amplitude values in time domain

Return type

ndarray

load_power_vals(path_npy)[source]

Loads and checks shape compatibility of averaged power values

Parameters

path_npy (str, pathlib.PosixPath) – Path to .npy file containing power information.

Returns

power_values – The power values as long as they have the shape (self.num_fft_bins, 1)

Return type

ndarray

save_filtered_signal(output_file, samples, overwrite=False)[source]
set_num_subframes(len_samples, is_noise=False)[source]

Sets the number of target or noise subframes available for processing

Parameters
  • len_samples (int) – The total number of samples in a given signal

  • is_noise (bool) – If False, subframe number saved under self.target_subframes, otherwise self.noise_subframes (default False)

Returns

Return type

None

set_volume(samples, max_vol=0.4, min_vol=0.15)[source]

Records and limits the maximum amplitude of original samples.

This enables the output wave to be within a range of volume that does not go below or too far above the orignal maximum amplitude of the signal.

Parameters
  • samples (ndarray) – The original samples of a signal (1 dimensional), of any length

  • max_vol (float) – The maximum volume level. If a signal has values higher than this number, the signal is curtailed to remain at and below this number.

  • min_vol (float) – The minimum volume level. If a signal has only values lower than this number, the signal is amplified to be at this number and below.

Returns

Return type

None

noize.filterfun.filters.calc_audioclass_powerspecs(path_class, dur_ms=1000, augment_data=False)[source]

Uses class’s path settings to set up Welch’s method for audio classes.

The settings applied for average power spectrum collection are also saved in a .csv file.

Parameters
  • path_class (class) – Class with attributes for necessary paths to load relevant wavfiles and save average power spectrum values.

  • dur_ms (int, float) – Time in milliseconds for the Welch’s method / average power spectrum calculation to be applied for each wavfile. (default 1000)

  • augment_data (bool) – Whether or not the sound data should be augmented. If True, the sound data will be processed three times: with low energy, mid energy, and high energy. (default False)

Returns

Return type

None

noize.filterfun.filters.coll_beg_audioclass_samps(path_class, feature_class, num_each_audioclass=1, dur_ms=1000)[source]

Saves dur_ms of num_each_audioclass wavfiles of each audio class.

This is an option for using noise data that comes from an audio class but is not an average of the entire class. It is raw sample data from one or more random noise wavfiles from each class.

Parameters
  • path_class (class) – Class with attributes for necessary paths to load relevant wavfiles and save sample values.

  • feature_class (class) – Class with attributes for sampling rate used in feature extraction and/or filtering. This is useful to maintain consistency in sampling rate throughout the modules.

  • num_each_audioclass (int) – The number of random wavfiles from each audio class chosen for raw sample collection. (default 1)

  • dur_ms (int, float) – Time in milliseconds of raw sample data to be saved. (default 1000)

Returns

Return type

None

noize.filterfun.filters.get_average_power(class_waves_dict, encodelabel_dict, powspec_dir, duration_sec=1, augment_data=False)[source]

Collects ave. power spectrum from audio classes; saves in .npy files

Parameters
  • class_waves_dict (dict) – Dictionary containing audio class labels and the wavfile paths of the files belonging to each audio class.

  • encodelabel_dict (dict) – Dictionary with keys matching the audio class labels and the values matching the integer each audio class is encoded as.

  • powspec_dir (str, pathlib.PosixPath) – Path to where average power spectrum files will be stored.

  • duration_sec (int, float) – The length in seconds to be processed when calculating the average power spectrum of each wavfile. (default 1)

  • augmentdata (bool) – If True, the samples will be augmented in their energy levels so that the sounds are represented at quiet, mid, and loud levels. If False, no augmentation will take place.

Returns

Return type

None

noize.filterfun.filters.get_save_begsamps(wavlist, audioclass_int, powspec_dir, samplerate=48000, dur_ms=1000)[source]

Saves the beginning raw samples from the wavfiles in wavlist.

Parameters
  • wavlist (list) – List containing paths of relevant wavfiles

  • audioclass_int (int) – The integer the audio class is encoded as.

  • powspec_dir (str, pathlib.PosixPath) – Path to where data relevant for audio class power spectrum data are to be saved.

  • samplerate (int) – The sampling rate of wavfiles. This is needed to calculate num samples necessary to get dur_ms of sound. (default 48000)

  • dur_ms (int, float) – Time in milleseconds of the wavefiles to collect and save.

noize.filterfun.applyfilter

noize.filterfun.applyfilter.filtersignal(output_filename, wavfile, noise_file=None, scale=1, apply_postfilter=False, duration_ms=1000, max_vol=0.4)[source]

Apply Wiener filter to signal using noise. Saves at output_filename.

Parameters
  • output_filename (str) – path and name the filtered signal is to be saved

  • wavfile (str) – the filename to the signal for filtering; if None, a signal will be generated (default None)

  • noise_file (str optional) – path to either noise wavfile or .npy file containing average power spectrum values or noise samples. If None, the beginning of the wavfile will be used for noise data. (default None)

  • scale (int or float) – The scale at which the filter should be applied. (default 1) Note: scale cannot be set to 0.

  • apply_postfilter (bool) – Whether or not the post filter should be applied. The post filter reduces musical noise (i.e. distortion) in the signal as a byproduct of filtering.

  • duration_ms (int or float) – The amount of time in milliseconds to use from noise to apply the Welch’s method to. In other words, how much of the noise to use when approximating the average noise power spectrum.

  • max_vol (int or float) – The maximum volume level of the filtered signal.

Returns

Return type

None