API

Fluent Interface API

The Signal Class inherits from numpy.ndarray via the BaseSignal class:

Inheritance diagram of audiotools.Signal

As a consequence, numpy.ndarray methods such as x.min(), x.max(), x.sum(), x.var() and others can also be used on auditools.Signal objects. For more informations check the numpy docs.

class audiotools.Signal(n_channels, duration, fs, dtype=<class 'float'>)
abs()

Absolute value

Calculates the absolute value or modulus of all values of the signal

add(x)

In-place summation

This function allowes for in-place summation.

Parameters

x (scalar or ndarray) – The value or array to add to the signal

Returns

Return type

Returns itself

Examples

>>> sig = audiotools.Signal(1, 1, 48000).add_tone(500).add(2)
>>> print(sig.mean())
2.0
add_cos_modulator(frequency, m, start_phase=0)

Multiply a cosinus amplitude modulator to the signal

Multiplies a cosinus amplitude modulator following the equation:

\[1 + m \cos{2 \pi f_m t \phi_{0}}\]

where \(m\) is the modulation depth, \(f_m\) is the modualtion frequency and \(t\) is the time. \(\phi_0\) is the start phase

Parameters
  • frequency (float) – The frequency of the cosine modulator.

  • m (float, optional) – The modulation index. (Default = 1)

  • start_phase (float) – The starting phase of the cosine in radiant.

Returns

Returns itself

Return type

Signal

add_fade_window(rise_time, type='cos', **kwargs)

Add a fade in/out window to the signal

This function multiplies a fade window with a given rise time onto the signal. for mor information about the indiviual window functions refer to the implementations:

Parameters
  • rise_time (float) – The rise time in seconds.

  • type ('cos', 'gauss', 'cos2') – The type of the window. (default = ‘cos’)

Returns

Return itself

Return type

Signal

add_noise(ntype='white', variance=1, seed=None)

Add uncorrelated noise to the signal

add gaussian noise with a defined variance and different spectral shapes. The noise is generated in the frequency domain using the gaussian pseudorandom generator numpy.random.randn. The real and imaginary part of each frequency component is set using the psudorandom generator. Each frequency bin is then weighted dependent on the spectral shape. The resulting spektrum is then transformed into the time domain using numpy.fft.ifft

Weighting functions:

  • white: \(w(f) = 1\)

  • pink: \(w(f) = \frac{1}{\sqrt{f}}\)

  • brown: \(w(f) = \frac{1}{f}\)

Parameters
  • ntype ({'white', 'pink', 'brown'}) – spectral shape of the noise

  • variance (scalar, optional) – The Variance of the noise

  • seed (int or 1-d array_like, optional) – Seed for RandomState. Must be convertible to 32 bit unsigned integers.

Returns

Returns itself

Return type

Signal

add_tone(frequency, amplitude=1, start_phase=0)

Add a cosine to the signal

This function will add a pure tone to the current waveform. following the equation:

\[x = x + cos(2\pi f t + \phi_0)\]

where \(x\) is the waveform, \(f\) is the frequency, \(t\) is the time and \(\phi_0\) the starting phase. The first evulated timepoint is 0.

Parameters
  • frequency (scalar) – The tone frequency in Hz.

  • amplitude (scalar, optional) – The amplitude of the cosine. (default = 1)

  • start_phase (scalar, optional) – The starting phase of the cosine. (default = 0)

Returns

Returns itself

Return type

Signal

add_uncorr_noise(corr=0, variance=1, seed=None)

Add partly uncorrelated noise

This function adds partly uncorrelated noise using the N+1 generator method.

To generate N partly uncorrelated noises with a desired correlation coefficent of $rho$, the algoritm first generates N+1 noise tokens which are then orthogonalized using the Gram-Schmidt process (as implementd in numpy.linalg.qr). The N+1 th noise token is then mixed with the remaining noise tokens using the equation

\[X_{\rho,n} = X_{N+1} \sqrt{\rho} + X_n \beta \sqrt{1 - \rho}\]

where \(X_{\rho,n}\) is the nth output and noise, \(X_{n}\) the nth indipendent noise and \(X_{N=1}\) is the common noise.

for two noise tokens, this is identical to the assymetric three-generator method described in [1]_

Parameters
  • corr (int, optional) – Desired correlation of the noise tokens, (default=0)

  • variance (scalar, optional) – The desired variance of the noise, (default=1)

  • seed (int or 1-d array_like, optional) – Seed for RandomState. Must be convertible to 32 bit unsigned integers.

Returns

Returns itself

Return type

Signal

References

1

Hartmann, W. M., & Cho, Y. J. (2011). Generating partially correlated noise—a comparison of methods. The Journal of the Acoustical Society of America, 130(1), 292–301. http://dx.doi.org/10.1121/1.3596475

bandpass(f_center, bw, ftype, **kwargs)

Apply a bandpass filter

Applies a bandpass filter to the signal. The availible filters are:

  • brickwall: A ‘optimal’ brickwall filter

  • gammatone: A real valued gammatone filter

For additional filter parameters and detailed description see the respective implementations:

Parameters
  • f_center (scalar) – The banddpass center frequency in Hz

  • bw (scalar) – The filter bandwidth in Hz

  • ftype ({'brickwall', 'gammatone'}) – The filtertype

  • **kwargs – Further keyword arguments are passed to the respective filter functions

Returns

Returns itself

Return type

Signal

calc_crest_factor()

Calculate crest factor

Calculates the crest factor for the signal. The crest factor is defined as:

\[C = \frac{|x_{peak}|}{x_{rms}}\]

where \(x_{peak}\) is the maximum of the absolute value and \(x_{rms}\) is the effective value of the signal.

Returns

The crest factor

Return type

scalar

calc_dbfs()

Calculate the dBFS RMS value for the signal

\[L = 20 \log_10\left(\sqrt{2}\sigma\right)\]

where \(\sigma\) is the signals RMS.

Returns

float

Return type

The dBFS RMS value

calc_dbspl()

Calculate the sound pressure level of the signal

\[L = 20 \log_{10}\left(\frac{\sigma}{p_o}\right)\]

where \(L\) is the SPL, \(p_0=20\mu Pa\) and \(\sigma\) is the RMS of the signal.

Returns

float

Return type

The sound pressure level in dB

property ch

Direct channel indexer

Returns an indexer class which enables direct indexing and slicing of the channels indipendent of samples.

Examples

>>> sig = audiotools.Signal((2, 3), 1, 48000).add_noise()
>>> print(np.all(sig.ch[1, 2] is sig[:, 1, 2]))
True
clip(t_start, t_end=None)

Clip the signal between two points in time

removes the number of semples according to t_start and t_end. This method can not be applied to a single channel or slice.

Parameters
  • t_start (float) – Signal time at which the returned signal should start

  • t_end (flot or None (optional)) – Signal time at which the signal should stop. The full remaining signal is used if set to None. (default: None)

Returns

Returns itself

Return type

Signal

concatenate(signal)

Concatenate another signal or array

This method appends another signal to the end of the current signal.

Parameters

signal (signal or ndarray) – The signal to append

Returns

Return type

Returns itself

delay(delay, method='fft')

Delays the signal by circular shifting

Circular shift the functions foreward to create a certain time delay relative to the orginal time. E.g if shifted by an equivalent of N samples, the value at sample i will move to sample i + N.

Two methods can be used. Using the default method ‘fft’, the signal is shifted by applyint a FFT transform, and phase shifting each frequency accoring to the delay and applying an inverse transform. This is identical to using the :meth:’audiotools.FrequencyDomainSignal.time_shift’ method. When using the method ‘sample’, the signal is time delayed by circular shifting the signal by the number of samples that is closest to delay.

Parameters
  • delay (float) – The delay in secons

  • method ({'fft', 'samples'} optional) – The method used to delay the signal (default: ‘fft’)

Returns

Returns itself

Return type

Signal

See also

audio.shift_signal, audio.FreqDomainSignal.time_shift

property duration

Duration of the signal in seconds

property fs

Sampling rate of the signal in Hz

multiply(x)

In-place multiplication

This function allowes for in-place multiplication

Parameters

x (scalar or ndarray) – The value or array to muliply with the signal

Returns

Return type

Returns itself

Examples

>>> sig = audiotools.Signal(1, 1, 48000).add_tone(500).multiply(2)
>>> print(sig.max())
2.0
property n_channels

Number of channels in the signal

property n_samples

Number of samples in the signal

phase_shift(phase)

Shifts all frequency components of a signal by a constant phase.

Shift all frequency components of a given signal by a constant phase. This is identical to calling the phase_shift method of the FrequencyDomainSignal class.

Parameters

phase (scalar) – The phase in rad by which the signal is shifted.

Returns

Returns itself

Return type

Signal

rectify()

One-way rectification of the signal

Returns

Returns itself

Return type

Signal

rms()

Root mean square

Returns

float

Return type

The RMS value

set_dbfs(dbfs)

Normalize the signal to a given dBFS RMS value.

Normalizes the signal to dB Fullscale for this, the Signal is multiplied with the factor \(A\)

\[A = \frac{1}{\sqrt{2}\sigma} 10^\frac{L}{20}\]

where \(L\) is the goal Level, and \(\sigma\) is the RMS of the signal.

Parameters

dbfs (float) – The dBFS RMS value in dB

Returns

Returns itself

Return type

Signal

set_dbspl(dbspl)

Set sound pressure level in dB

Normalizes the signal to a given sound pressure level in dB relative 20e-6 Pa.

Normalizes the signal to a given sound pressure level in dB relative 20e-6 Pa. for this, the Signal is multiplied with the factor \(A\)

\[A = \frac{p_0}{\sigma} 10^{L / 20}\]

where \(L\) is the goal SPL, \(p_0=20\mu Pa\) and \(\sigma\) is the RMS of the signal.

Parameters

dbspl (float) – The sound pressure level in dB

Returns

Returns itself

Return type

Signal

property time

The time vector for the signal

to_freqdomain()

Convert to frequency domain by applying a DFT

This function returns a frequency domain representation of the signal.

As opposed to most methods, this conversion is not in-place but a new :meth:’audiotools.FrequencyDomainSignal’ object is returned

Returns

The frequency domain representation of the signal

Return type

FrequencyDomainSignal

zeropad(number=None, duration=None)

Add zeros to start and end of signal

This function adds zeros of a given number or duration to the start or end of a signal.

If number or duration is a scalar, an equal number of zeros will be appended at the front and end of the array. If a vector of two values is given, the first defines the number or duration at the beginning, the second the number or duration of zeros at the end.

Parameters
  • number (scalar or vecor of len(2), optional) – Number of zeros.

  • duration (scalar or vecor of len(2), optional) – duration of zeros in seconds.

Returns

Returns itself

Return type

Signal

class audiotools.FrequencyDomainSignal(n_channels, duration, fs, dtype=<class 'complex'>)
abs()

Absolute value

Calculates the absolute value or modulus of all values of the signal

add(x)

In-place summation

This function allowes for in-place summation.

Parameters

x (scalar or ndarray) – The value or array to add to the signal

Returns

Return type

Returns itself

Examples

>>> sig = audiotools.Signal(1, 1, 48000).add_tone(500).add(2)
>>> print(sig.mean())
2.0
property ch

Direct channel indexer

Returns an indexer class which enables direct indexing and slicing of the channels indipendent of samples.

Examples

>>> sig = audiotools.Signal((2, 3), 1, 48000).add_noise()
>>> print(np.all(sig.ch[1, 2] is sig[:, 1, 2]))
True
concatenate(signal)

Concatenate another signal or array

This method appends another signal to the end of the current signal.

Parameters

signal (signal or ndarray) – The signal to append

Returns

Return type

Returns itself

property duration

Duration of the signal in seconds

property freq

Returns the frequency axis

Returns

The frequency axis in Hz

Return type

numpy.ndarray

property fs

Sampling rate of the signal in Hz

property mag

Retruns the magnitudes of the frequency components

equivalent to audiotools.FrequencyDomainSignal.abs()

Returns

The magnitudes

Return type

numpy.ndarray

multiply(x)

In-place multiplication

This function allowes for in-place multiplication

Parameters

x (scalar or ndarray) – The value or array to muliply with the signal

Returns

Return type

Returns itself

Examples

>>> sig = audiotools.Signal(1, 1, 48000).add_tone(500).multiply(2)
>>> print(sig.max())
2.0
property n_channels

Number of channels in the signal

property n_samples

Number of samples in the signal

property phase

The Argument of the frequency components

Returns

The arguments in radiants

Return type

numpy.ndarray

phase_shift(phase)

Apply a phase shift

Phase shift the spectrum by multiplying all frequency components with:

\[H(\omega) = e^{-j\psi}\]

where \(j\) is the complex unit and \(\psi\) is the desired phaseshift.

Parameters

phase (scalar) – The phaseshift

Returns

  • Returns itself (FrequencyDomainSignal)

  • Also See

  • ——–

  • audiotools.Signal.phase_shift

time_shift(time)

Apply a time shift

Time shift the signal by a linear phase shift by multiplying all frequency components with:

\[H(\omega) = e^{-j\omega\Delta t}\]

where \(j\) is the complex unit, \(omega\) the circular frequency and \(\Delta t\) the timeshift.

Parameters

time (scalar) – The time by which the signal should be shifted

Returns

  • Returns itself (FrequencyDomainSignal)

  • Also See

  • ——–

  • audiotools.Signal.delay

to_timedomain()

Convert to timedomain.

Convert to timedomain by means of inverse DFT. If the complex part after DFT is small (< 222e-16), it is neglected. This method is not applied in-place but a new :meth:’audiotools.Signal’ object is returned

The timedomain representation: Signal

Function Based API

audiotools.calc_coherence(signal)

normalized complex valued coherence

This function calculates the normalized complex valued degree of coherence between two signals \(f(t)\) and \(g(t)\). It is defined as:

\[\gamma(tau) = \frac{<f_a(t)g^*_a(t-\tau)>}{\sqrt{<|f_a(t)|^2><|g_a(t)|^2>}}\]

where \(f_a(t)\) is the analytic signals of \(f(t)\) and and \(g^*_a(t)\) is the complex conjugate of the analytic signal of \(g(t)\). \(<\dots>\) symbolizes the mean over time.

Requires an input signal with the shape (N, 2). If only a one-dimensional signal is provided, the auto-coherence function where \(f(t) = g(t)\) is calculated.

The real part of the complex valued coherence equals the normalized cross-correlation.

Parameters

signal (Signal or ndarray) – The input signal

Returns

The coherence vector

Return type

Signal or ndarray

audiotools.calc_dbfs(signal)

Calculate the dBFS RMS value of a given signal.

\[L = 20 \log_{10}\left(\sqrt{2}\sigma\right)\]

where \(\sigma\) is the signals RMS.

Parameters

signal (ndarray) – The input signal

Returns

The dBFS RMS value

Return type

float

audiotools.calc_dbspl(signal)

Calculate the dB (SPL) value for a given signal.

\[L = 20 \log_{10}\left(\frac{\sigma}{p_o}\right)\]

where \(L\) is the SPL, \(p_0=20\mu Pa\) and \(\sigma\) is the RMS of the signal.

Parameters

signal (ndarray) – The input signal

Returns

The dB (SPL) value

Return type

float

audiotools.cos_amp_modulator(signal, modulator_freq, fs, mod_index=1, start_phase=0)

Cosinus amplitude modulator

Returns a cosinus amplitude modulator following the equation:

\[1 + m \cos{2 \pi f_m t \phi_{0}}\]

where \(m\) is the modulation depth, \(f_m\) is the modualtion frequency and \(t\) is the time. :math;`phi_0` is the start phase

Parameters
  • signal (ndarray) – An input array that is used to determine the length of the modulator.

  • modulator_freq (float) – The frequency of the cosine modulator.

  • fs (float) – The sample frequency of the input signal.

  • mod_index (float, optional) – The modulation index. (Default = 1)

Returns

ndarray

Return type

The modulator

audiotools.cosine_fade_window(signal, rise_time, fs)

Raised cosine fade-in and fade-out window.

This function generates a raised cosine / hann fade-in and fade out. The Window ramps are calculated as

\[\frac{1}{2} \left(1 + \cos{\left(\frac{\pi t}{t_r}\right)} \right)\]

where \(t_r\) is the rise_time

Parameters
  • signal (ndarray) – The length of the array will be used to determin the window length.

  • rise_time (scalar) – Duration of the cosine fade in and fade out in seconds. The number of samples is determined via rounding to the nearest integer value.

  • fs (scalar) – The sampling rate in Hz

Returns

ndarray

Return type

The fading window

audiotools.crest_factor(signal, axis=0)

Calculate crest factor

Calculates the crest factor of the input signal. The crest factor is defined as:

\[C = \frac{|x_{peak}|}{x_{rms}}\]

where \(x_{peak}\) is the maximum of the absolute value and \(x_{rms}\) is the effective value of the signal.

Parameters
  • signal (ndarray) – The input signal

  • axis (int) – The axis for which to calculate the crest factor (default = 0)

Returns

The crest factor

Return type

scalar

audiotools.gaussian_fade_window(signal, rise_time, fs, cutoff=- 60)

Gausiapn fade-in and fade-out window.

This function generates a window function with a gausian fade in and fade out. The gausian slope is cut at the level defined by the cutoff parameter

The window is given by:

\[w(t) = e^{\frac{-(t-t_r)^2}{2 * \sigma^2}}\]

where \(t\) is the time, \(t_r\) is the the rise time and \(\sigma\) is calculated as

\[\sigma = \sqrt{\frac{r_t^2}{2 \log{(10^{ p / 20})}}}\]

where \(p\) is the cutoff in dB

Parameters
  • signal (ndarray) – The length of the array will be used to determin the window length.

  • rise_time (scalar) – Duration of the gaussian fade in and fade out in seconds. The value is measured from the cutof level until reaching a value of 1. The number of samples is determined via rounding to the nearest integer value.

  • fs (scalar) – The sampling rate in Hz

  • cutoff (scalar, optional) – The level at which the gausian slope is cut (default = -60dB)

Returns

ndarray

Return type

The fading window

audiotools.generate_noise(duration, fs, ntype='white', n_channels=1, seed=None)

Generate Noise

Generate gaussian noise with a variance of 1 and different spectral shapes. The noise is generated in the frequency domain using the gaussian pseudorandom generator numpy.random.randn. The real and imaginary part of each frequency component is set using the psudorandom generator. Each frequency bin is then weighted dependent on the spectral shape. The resulting spektrum is then transformed into the time domain using numpy.fft.ifft

Weighting functions

  • white: \(w(f) = 1\)

  • pink: \(w(f) = \frac{1}{\sqrt{f}}\)

  • brown: \(w(f) = \frac{1}{f}\)

Parameters
  • duration (scalar) – Noise duration in seconds

  • fs (int) – Sampling frequency

  • ntype ({'white', 'pink', 'brown'}) – spectral shape of the noise

  • n_channels (int) – number of indipendant noise channels

  • seed (int or 1-d array_like, optional) – Seed for RandomState. Must be convertible to 32 bit unsigned integers.

Returns

noise vector of the shape (NxM) where N is the number of samples and M >the number of channels

Return type

ndarray

audiotools.generate_tone(frequency, duration, fs, start_phase=0)

create a cosine

This function will generate a pure tone following the equation:

\[x = x + cos(2\pi f t + \phi_0)\]

where \(x\) is the waveform, \(f\) is the frequency, :math`t` is the time and \(\phi_0\) the starting phase. The first evulated timepoint is 0.

Parameters
  • frequency (scalar) – The tone frequency in Hz.

  • duration (scalar) – The tone duration in seconds.

  • fs (scalar) – The sampling rate for the tone.

  • start_phase (scalar, optional) – The starting phase of the sine tone.

Returns

ndarray

Return type

The sine tone

audiotools.generate_uncorr_noise(duration, fs, n_channels, corr=0, seed=None)

Generate partly uncorrelated noise

This function generates partly uncorrelated noise using the N+1 generator method.

To generate N partly uncorrelated noises with a desired correlation coefficent of $rho$, the algoritm first generates N+1 noise tokens which are then orthogonalized using the Gram-Schmidt process (as implementd in numpy.linalg.qr). The N+1 th noise token is then mixed with the remaining noise tokens using the equation

\[X_{\rho,n} = X_{N+1} \sqrt{\rho} + X_n \beta \sqrt{1 - \rho}\]

where \(X_{\rho,n}\) is the nth output and noise, \(X_{n}\) the nth indipendent noise and \(X_{N=1}\) is the common noise.

for two noise tokens, this is identical to the assymetric three-generator method described in [1]_

Parameters
  • duration (scalar) – Noise duration in seconds

  • fs (int) – Sampling frequency

  • n_channels (int) – number of indipendant noise channels

  • corr (int, optional) – Desired correlation of the noise tokens, (default=0)

  • seed (int or 1-d array_like, optional) – Seed for RandomState. Must be convertible to 32 bit unsigned integers.

Returns

noise vector of the shape (NxM) where N is the number of samples and M >the number of channels

Return type

ndarray

References

1

Hartmann, W. M., & Cho, Y. J. (2011). Generating partially correlated noise—a comparison of methods. The Journal of the Acoustical Society of America, 130(1), 292–301. http://dx.doi.org/10.1121/1.3596475

audiotools.nsamples(duration, fs)

Number of samples in a signal with a given duration.

This function calculates the number of samples that will be returned when generating a signal with a certain duration and sampling rates. The number is determined by multiplying the sampling rate with the duration and rounding to the next integer.

Parameters
  • duration (scalar) – The tone duration in seconds.

  • fs (scalar) – The sampling rate for the tone.

Returns

number of samples in the signal

Return type

int

audiotools.phase2time(phase, frequency)

Pase to Time for a given frequency

\[t = \frac{\phi}{2 \pi f}\]
Parameters

phase (ndarray) – The phase values to convert

Returns

converted time values

Return type

ndarray

audiotools.phon_to_dbspl(frequency, l_phon, interpolate=False, limit=True)

Sound pressure levels from loudness level (following DIN ISO 226:2006-04)

Calulates the sound pressure level at a given frequency that is necessary to reach a specific loudness level following DIN ISO 226:2006-04

The normed values are tabulated for the following frequencies and sound pressure levels:

  1. 20phon to 90phon

  • 20 Hz, 25 Hz, 31.5 Hz, 40 Hz, 50 Hz, 63 Hz, 80 Hz, 100 Hz, 125 Hz, 160 Hz, 200 Hz, 250 Hz, 315 Hz, 400 Hz, 500 Hz, 630 Hz, 800 Hz, 1000 Hz, 1250 Hz, 1600 Hz, 2000 Hz, 2500 Hz, 3150 Hz, 4000 Hz

  1. 20phon to 80phon

  • 5000 Hz, 6300 Hz, 8000 Hz, 10000 Hz, 12500 Hz

Values for other frequencies can be interpolated (cubic spline) by setting the parameter interpolate=True. The check for correct sound pressure levels can be switched off by setting limit=False. In both cases, the results are not covered by the DIN ISO norm

Parameters
  • frequency (scalar) – The frequency in Hz. must be one of the tabulated values above if interpolate = False

  • l_phon (scalar) – loudness level that should be converted

  • interpolate (bool, optional) – Defines whether the tabulated values from the norm should be interpolated. If set to True, the tabulated values will be interpolated using a cubic spline (default = False)

  • limit (bool, optional) – Defines whether the limits of the norm should be checked (default = True)

Returns

The soundpressure level in dB SPL

Return type

scalar

audiotools.schroeder_phase(harmonics, amplitudes, phi0=0.0)

Phases for a schroeder phase harmonic complex

This function calculates the phases for a schroeder phase harmonic comlex following eq. 11 of [1]_:

\[\phi_n = \phi_l - 2\pi \sum\limits^{n-1}_{l=1}(n - l)p_l\]

\(n\) is the order of the harmonic and p_l is the relative power of the spectral component p_l.

Parameters
  • harmonics (ndarray) – A vector of harmonics for which the schroeder phases should be calculated

  • amplitudes (ndarray) – A vector of amplitudes for the given harmonics

  • phi0 (scalar) – The starting phase of the first harmonic (default = 0)

Returns

The phase values for the harmonic compontents

Return type

ndarray

References

1

Schroeder, M. (1970). Synthesis of low-peak-factor signals and binary sequences with low autocorrelation (corresp.). IEEE Transactions on Information Theory, 16(1), 85-89

audiotools.set_dbfs(signal, dbfs_val)

Full scale normalization of the signal.

Normalizes the signal to dB Fullscale for this, the Signal is multiplied with the factor \(A\)

\[A = \frac{1}{\sqrt{2}\sigma} 10^\frac{L}{20}\]

where \(L\) is the goal Level, and \(\sigma\) is the RMS of the signal.

Parameters
  • signal (ndarray) – The input signal

  • dbfs_val (float) – The db full scale value to reach

Returns

The amplitude adjusted signal

Return type

ndarray

audiotools.set_dbspl(signal, dbspl_val)

Adjust signal amplitudes to a given dbspl value.

Normalizes the signal to a given sound pressure level in dB relative 20e-6 Pa. for this, the Signal is multiplied with the factor \(A\)

\[A = \frac{p_0}{\sigma} 10^{L / 20}\]

where \(L\) is the goal SPL, \(p_0=20\mu Pa\) and \(\sigma\) is the RMS of the signal.

Parameters
  • signal (ndarray) – The input signal

  • dbspl_val (float) – The dbspl value to reach

Returns

The amplitude adjusted signal

Return type

ndarray

audiotools.shift_signal(signal, nr_samples)

Shift signal by nr_samples samples.

Shift a signal by a given number of samples. The shift happens

cyclically so that the length of the signal does not change.

Parameters
  • signal (array_like) – Input signal

  • nr_samples (int) – The number of samples that the signal should be shifted. Must be positive if mode is ‘zeros’.

Returns

  • res (ndarray) – The shifted signal

  • See Also

  • ———

  • delay_signal (A high level delaying / shifting function.)

  • fftshift_signal (Shift a signal in frequency space.)

audiotools.time2phase(time, frequency)

Time to phase for a given frequency

\[\phi = 2 \pi t f\]
Parameters

time (ndarray) – The time values to convert

Returns

converted phase values

Return type

ndarray

audiotools.zeropad(signal, number)

Add a number of zeros to both sides of a signal

This function adds a given number of zeros to the start or end of a signal.

If number is a scalar, an equal number of zeros will be appended at the front and end of the array. If a vector of two values is given, the first defines the number at the beginning, the second the number or duration of zeros at the end.

Parameters
  • signal (ndarray) – The input Signal

  • number (scalar or vecor of len(2), optional) – Number of zeros.

Returns

ndarray

Return type

The bufferd signal

Filter

audiotools.filter.brickwall(signal, fs, low_f, high_f)

Brickwall bandpass filter

Bandpass filters an input signal by setting all frequency outside of the passband [low_f, high_f] to zero.

Parameters
  • signal (ndarray) – The input signal

  • fs (scalar) – The signals sampling rate in Hz

  • low_f (scalar or None) – The lower cutoff frequency in Hz

  • high_f (scalar) – The upper cutoff frequency in Hz

Returns

Return type

The filtered signal

audiotools.filter.gammatone(signal, fs, cf, bw, order=4, attenuation_db=- 3, return_complex=True)

Apply a gammatone filter to the signal

Applys a gammatone filter following [1]_ to the input signal and returns the filtered signal.

Parameters
  • signal (ndarray) – The input signal

  • fs (int) – The sample frequency in Hz

  • cf (scalar) – The center frequency of the filter in Hz

  • bw (scalar) – The bandwidth of the filter in Hz

  • order (int) – The filter order (default = 4)

  • attenuation_db (scalar) – The attenuation at half bandwidth in dB (default = -3)

  • return_complex (bool) – Whether the complex filter output or only it’s real part is returned (default = True)

Returns

Return type

The filtered signal.

References

1

Hohmann, V., Frequency analysis and synthesis using a Gammatone filterbank, Acta Acustica, Vol 88 (2002), 43 -3442