Fourier Analysis of Signals (libfmp.c2)

The FMP notebooks provide detailed textbook-like explanations of central techniques and algorithms implemented in the libfmp. The part of FMP related to this module is available at the following URL:

https://www.audiolabs-erlangen.de/resources/MIR/FMP/C2/C2.html

libfmp.c2.c2_complex.generate_figure(figsize=(2, 2), xlim=[0, 1], ylim=[0, 1])[source]

Generate figure for plotting complex numbers

Notebook: C2/C2_ComplexNumbers.ipynb

Parameters
  • figsize – Figure size (Default value = (2, 2))

  • xlim – Limits of x-axis (Default value = [0, 1])

  • ylim – Limits of y-axis (Default value = [0, 1])

libfmp.c2.c2_complex.plot_vector(c, color='k', start=0, linestyle='-')[source]

Plot arrow corresponding to difference of two complex numbers

Notebook: C2/C2_ComplexNumbers.ipynb

Parameters
  • c – Complex number

  • color – Color of arrow (Default value = ‘k’)

  • start – Complex number encoding the start position (Default value = 0)

  • linestyle – Linestyle of arrow (Default value = ‘-‘)

Returns

arrow (matplotlib.patches.FancyArrow) – Arrow

libfmp.c2.c2_digitization.decoding_mu_law(v, mu=255.0)[source]

mu-law decoding

Notebook: C2/C2S2_DigitalSignalQuantization.ipynb

Parameters
  • v (float) – Value between -1 and 1

  • mu (float) – Dencoding parameter (Default value = 255.0)

Returns

v_decode (float) – Decoded value

libfmp.c2.c2_digitization.encoding_mu_law(v, mu=255.0)[source]

mu-law encoding

Notebook: C2/C2S2_DigitalSignalQuantization.ipynb

Parameters
  • v (float) – Value between -1 and 1

  • mu (float) – Encoding parameter (Default value = 255.0)

Returns

v_encode (float) – Encoded value

libfmp.c2.c2_digitization.generate_function(Fs, dur=1)[source]

Generate example function

Notebook: C2/C2S2_DigitalSignalSampling.ipynb

Parameters
  • Fs (scalar) – Sampling rate

  • dur (float) – Duration (in seconds) of signal to be generated (Default value = 1)

Returns
  • x (np.ndarray) – Signal

  • t (np.ndarray) – Time axis (in seconds)

libfmp.c2.c2_digitization.plot_graph_quant_function(ax, quant_min=- 1.0, quant_max=1.0, quant_level=256, mu=255.0, quant='uniform')[source]

Helper function for plotting a graph of quantization function and quantization error

Notebook: C2/C2S2_DigitalSignalQuantization.ipynb

Parameters
  • ax (mpl.axes.Axes) – Axis

  • quant_min (float) – Minimum quantization level (Default value = -1.0)

  • quant_max (float) – Maximum quantization level (Default value = 1.0)

  • quant_level (int) – Number of quantization levels (Default value = 256)

  • mu (float) – Encoding parameter (Default value = 255.0)

  • quant (str) – Type of quantization (Default value = ‘uniform’)

libfmp.c2.c2_digitization.plot_mu_law(mu=255.0, figsize=(8.5, 4))[source]

Helper function for plotting a signal and its quantized version

Notebook: C2/C2S2_DigitalSignalQuantization.ipynb

Parameters
  • mu (float) – Dencoding parameter (Default value = 255.0)

  • figsize (tuple) – Figure size (Default value = (8.5, 2))

libfmp.c2.c2_digitization.plot_signal_quant(x, t, x_quant, figsize=(8, 2), xlim=None, ylim=None, title='')[source]

Helper function for plotting a signal and its quantized version

Notebook: C2/C2S2_DigitalSignalQuantization.ipynb

Parameters
  • x – Original Signal

  • t – Time

  • x_quant – Quantized signal

  • figsize – Figure size (Default value = (8, 2))

  • xlim – Limits for x-axis (Default value = None)

  • ylim – Limits for y-axis (Default value = None)

  • title – Title of figure (Default value = ‘’)

libfmp.c2.c2_digitization.quantize_nonuniform_mu(x, mu=255.0, quant_level=256)[source]

Nonuniform quantization approach using mu-encoding

Notebook: C2/C2S2_DigitalSignalQuantization.ipynb

Parameters
  • x (np.ndarray) – Original signal

  • mu (float) – Encoding parameter (Default value = 255.0)

  • quant_level (int) – Number of quantization levels (Default value = 256)

Returns

x_quant (np.ndarray) – Quantized signal

libfmp.c2.c2_digitization.quantize_uniform(x, quant_min=- 1.0, quant_max=1.0, quant_level=5)[source]

Uniform quantization approach

Notebook: C2/C2S2_DigitalSignalQuantization.ipynb

Parameters
  • x (np.ndarray) – Original signal

  • quant_min (float) – Minimum quantization level (Default value = -1.0)

  • quant_max (float) – Maximum quantization level (Default value = 1.0)

  • quant_level (int) – Number of quantization levels (Default value = 5)

Returns

x_quant (np.ndarray) – Quantized signal

libfmp.c2.c2_digitization.reconstruction_sinc(x, t, t_sinc)[source]

Reconstruction from sampled signal using sinc-functions

Notebook: C2/C2S2_DigitalSignalSampling.ipynb

Parameters
  • x (np.ndarray) – Sampled signal

  • t (np.ndarray) – Equidistant discrete time axis (in seconds) of x

  • t_sinc (np.ndarray) – Equidistant discrete time axis (in seconds) of signal to be reconstructed

Returns

x_sinc (np.ndarray) – Reconstructed signal having time axis t_sinc

libfmp.c2.c2_digitization.sampling_equidistant(x_1, t_1, Fs_2, dur=None)[source]

Equidistant sampling of interpolated signal

Notebook: C2/C2S2_DigitalSignalSampling.ipynb

Parameters
  • x_1 (np.ndarray) – Signal to be interpolated and sampled

  • t_1 (np.ndarray) – Time axis (in seconds) of x_1

  • Fs_2 (scalar) – Sampling rate used for equidistant sampling

  • dur (float) – Duration (in seconds) of sampled signal (Default value = None)

Returns
  • x (np.ndarray) – Sampled signal

  • t (np.ndarray) – Time axis (in seconds) of sampled signal

libfmp.c2.c2_fourier.dft(x)[source]

Compute the disrcete Fourier transfrom (DFT)

Notebook: C2/C2_DFT-FFT.ipynb

Parameters

x (np.ndarray) – Signal to be transformed

Returns

X (np.ndarray) – Fourier transform of x

libfmp.c2.c2_fourier.fft(x)[source]

Compute the fast Fourier transform (FFT)

Notebook: C2/C2_DFT-FFT.ipynb

Parameters

x (np.ndarray) – Signal to be transformed

Returns

X (np.ndarray) – Fourier transform of x

libfmp.c2.c2_fourier.generate_matrix_dft(N, K)[source]

Generates a DFT (discrete Fourier transfrom) matrix

Notebook: C2/C2_DFT-FFT.ipynb

Parameters
  • N (int) – Number of samples

  • K (int) – Number of frequency bins

Returns

dft (np.ndarray) – The DFT matrix

libfmp.c2.c2_fourier.generate_matrix_dft_inv(N, K)[source]

Generates an IDFT (inverse discrete Fourier transfrom) matrix

Notebook: C2/C2_STFT-Inverse.ipynb

Parameters
  • N (int) – Number of samples

  • K (int) – Number of frequency bins

Returns

dft (np.ndarray) – The IDFT matrix

libfmp.c2.c2_fourier.idft(X)[source]

Compute the inverse discrete Fourier transfrom (IDFT)

Parameters

X (np.ndarray) – Signal to be transformed

Returns

x (np.ndarray) – Inverse Fourier transform of X

libfmp.c2.c2_fourier.ifft(X)[source]

Compute the inverse fast Fourier transform (IFFT)

Parameters

X (np.ndarray) – Fourier transform of x

Returns

x (np.ndarray) – Inverse Fourier transform of X

libfmp.c2.c2_fourier.ifft_noscale(X)[source]

Compute the inverse fast Fourier transform (IFFT) without the final scaling factor of 1/N

Parameters

X (np.ndarray) – Fourier transform of x

Returns

x (np.ndarray) – Inverse Fourier transform of X

libfmp.c2.c2_fourier.istft(X, w, H, L, zero_padding=0)[source]

Compute the inverse discrete short-time Fourier transform (ISTFT)

Parameters
  • X (np.ndarray) – The discrete short-time Fourier transform

  • w (np.ndarray) – Window function

  • H (int) – Hopsize

  • L (int) – Length of time signal

  • zero_padding (bool) – Number of zeros to be padded after windowing and before the Fourier transform of a frame (Default value = 0)

Returns

x (np.ndarray) – Reconstructed time signal

libfmp.c2.c2_fourier.istft_basic(X, w, H, L)[source]

Compute the inverse of the basic discrete short-time Fourier transform (ISTFT)

Notebook: C2/C2_STFT-Inverse.ipynb

Parameters
  • X (np.ndarray) – The discrete short-time Fourier transform

  • w (np.ndarray) – Window function

  • H (int) – Hopsize

  • L (int) – Length of time signal

Returns

x (np.ndarray) – Time signal

libfmp.c2.c2_fourier.stft(x, w, H=512, zero_padding=0, only_positive_frequencies=False)[source]

Compute the discrete short-time Fourier transform (STFT)

Parameters
  • x (np.ndarray) – Signal to be transformed

  • w (np.ndarray) – Window function

  • H (int) – Hopsize (Default value = 512)

  • zero_padding (bool) – Number of zeros to be padded after windowing and before the Fourier transform of a frame (Note: The purpose of this step is to increase the frequency sampling.) (Default value = 0)

  • only_positive_frequencies (bool) – Return only positive frequency part of spectrum (non-invertible) (Default value = False)

Returns

X (np.ndarray) – The discrete short-time Fourier transform

libfmp.c2.c2_fourier.stft_basic(x, w, H=8, only_positive_frequencies=False)[source]

Compute a basic version of the discrete short-time Fourier transform (STFT)

Notebook: C2/C2_STFT-Basic.ipynb

Parameters
  • x (np.ndarray) – Signal to be transformed

  • w (np.ndarray) – Window function

  • H (int) – Hopsize (Default value = 8)

  • only_positive_frequencies (bool) – Return only positive frequency part of spectrum (non-invertible) (Default value = False)

Returns

X (np.ndarray) – The discrete short-time Fourier transform

libfmp.c2.c2_fourier.stft_convention_fmp(x, Fs, N, H, pad_mode='constant', center=True, mag=False, gamma=0)[source]

Compute the discrete short-time Fourier transform (STFT)

Notebook: C2/C2_STFT-FreqGridInterpol.ipynb

Parameters
  • x (np.ndarray) – Signal to be transformed

  • Fs (scalar) – Sampling rate

  • N (int) – Window size

  • H (int) – Hopsize

  • pad_mode (str) – Padding strategy is used in librosa (Default value = ‘constant’)

  • center (bool) – Centric view as used in librosa (Default value = True)

  • mag (bool) – Computes magnitude STFT if mag==True (Default value = False)

  • gamma (float) – Constant for logarithmic compression (only applied when mag==True) (Default value = 0)

Returns

X (np.ndarray) – Discrete (magnitude) short-time Fourier transform

libfmp.c2.c2_fourier.twiddle(N)[source]

Generate the twiddle factors used in the computation of the fast Fourier transform (FFT)

Notebook: C2/C2_DFT-FFT.ipynb

Parameters

N (int) – Number of samples

Returns

sigma (np.ndarray) – The twiddle factors

libfmp.c2.c2_fourier.twiddle_inv(N)[source]

Generate the twiddle factors used in the computation of the Inverse fast Fourier transform (IFFT)

Parameters

N (int) – Number of samples

Returns

sigma (np.ndarray) – The twiddle factors

libfmp.c2.c2_interference.generate_chirp_linear(dur, freq_start, freq_end, amp=1.0, Fs=22050)[source]

Generation chirp with linear frequency increase

Notebook: C2/C2S3_InterferenceBeating.ipynb

Parameters
  • dur (float) – Duration (seconds) of the signal

  • freq_start (float) – Start frequency of the chirp

  • freq_end (float) – End frequency of the chirp

  • amp (float) – Amplitude of chirp (Default value = 1.0)

  • Fs (scalar) – Sampling rate (Default value = 22050)

Returns
  • x (np.ndarray) – Generated chirp signal

  • t (np.ndarray) – Time axis (in seconds)

  • freq (np.ndarray) – Instant frequency (in Hz)

libfmp.c2.c2_interference.plot_interference(x1, x2, t, figsize=(8, 2), xlim=None, ylim=None, title='')[source]

Helper function for plotting two signals and its superposition

Notebook: C2/C2S3_InterferenceBeating.ipynb

Parameters
  • x1 – Signal 1

  • x2 – Signal 2

  • t – Time

  • figsize – figure size (Default value = (8, 2))

  • xlim – x limits (Default value = None)

  • ylim – y limits (Default value = None)

  • title – figure title (Default value = ‘’)

libfmp.c2.c2_interpolation.compute_f_coef_linear(N, Fs, rho=1)[source]

Refines the frequency vector by factor of rho

Notebook: C2/C2_STFT-FreqGridInterpol.ipynb

Parameters
  • N (int) – Window size

  • Fs (scalar) – Sampling rate

  • rho (int) – Factor for refinement (Default value = 1)

Returns

F_coef_new (np.ndarray) – Refined frequency vector

libfmp.c2.c2_interpolation.compute_f_coef_log(R, F_min, F_max)[source]

Adapts the frequency vector in a logarithmic fashion

Notebook: C2/C2_STFT-FreqGridInterpol.ipynb

Parameters
  • R (scalar) – Resolution (cents)

  • F_min (float) – Minimum frequency

  • F_max (float) – Maximum frequency (not included)

Returns
  • F_coef_log (np.ndarray) – Refined frequency vector with values given in Hz)

  • F_coef_cents (np.ndarray) – Refined frequency vector with values given in cents. Note: F_min serves as reference (0 cents)

libfmp.c2.c2_interpolation.interpolate_freq_stft(Y, F_coef, F_coef_new)[source]

Interpolation of STFT along frequency axis

Notebook: C2/C2_STFT-FreqGridInterpol.ipynb

Parameters
  • Y (np.ndarray) – Magnitude STFT

  • F_coef (np.ndarray) – Vector of frequency values

  • F_coef_new (np.ndarray) – Vector of new frequency values

Returns

Y_interpol (np.ndarray) – Interploated magnitude STFT