### Install NxSignal via Mix.install Source: https://github.com/elixir-nx/nx_signal/blob/main/README.md Demonstrates how to dynamically install the NxSignal dependency for standalone scripts or Livebook environments. ```elixir Mix.install([ {:nx_signal, "~> 0.1"} ]) ``` -------------------------------- ### Install NxSignal Elixir Library Source: https://context7.com/elixir-nx/nx_signal/llms.txt Instructions for adding the NxSignal library to an Elixir project using Mix.deps or Mix.install. ```elixir def deps do [ {:nx_signal, "~> 0.1"} ] end ``` ```elixir Mix.install([ {:nx_signal, "~> 0.1"} ]) ``` -------------------------------- ### Find Custom Extrema using Comparator Functions Source: https://context7.com/elixir-nx/nx_signal/llms.txt Illustrates how to use a custom comparator function with argrelextrema to define specific peak detection logic, such as finding values relative to neighbors or excluding global extrema. ```elixir # Find values >= 2x their neighbors comparator = fn x, y -> Nx.greater_equal(x, Nx.multiply(y, 2)) end x = Nx.tensor([0, 1, 3, 2, 0, 1, 0, 0, 0, 2, 1]) result = NxSignal.PeakFinding.argrelextrema(x, comparator) # Find non-global local minima x = Nx.tensor([0, 1, 0, 2, 1, 3, 0, 1]) global_minimum = Nx.reduce_min(x) comparator = fn x, y -> x_not_global = Nx.not_equal(x, global_minimum) y_not_global = Nx.not_equal(y, global_minimum) both_not_global = Nx.logical_and(x_not_global, y_not_global) Nx.logical_and(Nx.less(x, y), both_not_global) end result = NxSignal.PeakFinding.argrelextrema(x, comparator) ``` -------------------------------- ### Add NxSignal Dependency to Mix Project Source: https://github.com/elixir-nx/nx_signal/blob/main/README.md Shows how to include the NxSignal library in an Elixir project using the mix.exs dependencies configuration. ```elixir def deps do [ {:nx_signal, "~> 0.1"} ] end ``` -------------------------------- ### FIR Filter Design using Window Method Source: https://context7.com/elixir-nx/nx_signal/llms.txt Designs Finite Impulse Response (FIR) filters using the window method. Supports lowpass, highpass, bandpass, and bandstop filter configurations with various window types and sampling rates. ```elixir # Lowpass filter with 5 taps, cutoff at 0.3 * Nyquist coeffs = NxSignal.Filters.firwin(5, [0.3], window: :hamming, sampling_rate: 2.0) # => Tensor of shape {5} ``` ```elixir # Highpass filter (pass_zero: false) coeffs = NxSignal.Filters.firwin( 11, [0.4], window: :hann, pass_zero: false, sampling_rate: 2.0 ) ``` ```elixir # Bandpass filter (two cutoffs, pass_zero: false) coeffs = NxSignal.Filters.firwin( 21, [0.2, 0.5], window: :blackman, pass_zero: false, sampling_rate: 2.0 ) ``` ```elixir # Bandstop filter (two cutoffs, pass_zero: true) coeffs = NxSignal.Filters.firwin( 21, [0.2, 0.5], window: {:kaiser, 8.0}, pass_zero: true, sampling_rate: 2.0 ) ``` ```elixir # With specific sampling rate in Hz coeffs = NxSignal.Filters.firwin( 15, [1000.0], # 1kHz cutoff window: :hamming, sampling_rate: 8000.0 # 8kHz sampling rate ) ``` -------------------------------- ### Windowing Functions Source: https://context7.com/elixir-nx/nx_signal/llms.txt Provides various window functions and a utility to split a signal into overlapping windows. ```APIDOC ## POST /as_windowed ### Description Splits a 1D tensor into overlapping windows (frames) for frame-based processing. ### Method POST ### Endpoint /as_windowed ### Parameters #### Query Parameters - **signal** (Nx.Tensor) - Required - The input signal tensor. - **window_length** (integer) - Required - The desired length of each window. - **stride** (integer) - Optional - The number of samples to shift between consecutive windows. Defaults to 1. - **padding** (atom or list) - Optional - Padding to apply to the signal before windowing. Can be `:reflect` or a list of tuples `{{before, after}}`. ### Request Example ```json { "signal": "Nx.tensor([0, 1, 2, 3, 4, 10, 11, 12])", "window_length": 4, "stride": 1 } ``` ### Response #### Success Response (200) - **windows** (Nx.Tensor) - A tensor where each row represents a windowed frame. #### Response Example ```json { "windows": "" } ``` ``` -------------------------------- ### FFT Frequency Bins Source: https://context7.com/elixir-nx/nx_signal/llms.txt Computes the frequency bins for a given FFT configuration. ```APIDOC ## POST /fft_frequencies ### Description Computes the frequency bins corresponding to the FFT output. ### Method POST ### Endpoint /fft_frequencies ### Parameters #### Query Parameters - **sampling_rate** (float) - Required - The sampling rate of the signal in Hz. - **fft_length** (integer) - Required - The length of the FFT. ### Request Example ```json { "sampling_rate": 16000, "fft_length": 10 } ``` ### Response #### Success Response (200) - **frequencies** (Nx.Tensor) - A tensor containing the frequency bins in Hz. #### Response Example ```json { "frequencies": "" } ``` ``` -------------------------------- ### Calculate FFT Frequency Bins in Elixir Source: https://context7.com/elixir-nx/nx_signal/llms.txt Computes the frequency bins corresponding to an FFT computation given the sampling rate and FFT length using NxSignal. ```elixir # Get frequency bins for FFT with 10 bins at 16kHz sampling rate frequencies = NxSignal.fft_frequencies(1.6e4, fft_length: 10) # => #Nx.Tensor ``` -------------------------------- ### Vectorized Tensor Operation with NxSignal Source: https://context7.com/elixir-nx/nx_signal/llms.txt Demonstrates using Nx.vectorize for batch processing with tensors and applying overlap and add operations. This is useful for efficiently processing batched data where operations can be applied independently to each batch element. ```elixir t = Nx.tensor([[[[0, 1, 2, 3], [4, 5, 6, 7]]], [[[10, 11, 12, 13], [14, 15, 16, 17]]]]) |> Nx.vectorize(x: 2, y: 1) result = NxSignal.overlap_and_add(t, overlap_length: 3) ``` -------------------------------- ### Find Relative Minima using NxSignal Source: https://context7.com/elixir-nx/nx_signal/llms.txt Shows how to detect local minima in a tensor. It returns both the indices of potential minima and the count of valid minima based on the specified neighborhood order. ```elixir x = Nx.tensor([2, 1, 2, 3, 2, 0, 1, 0]) # Find local minima %{indices: indices, valid_indices: valid_indices} = NxSignal.PeakFinding.argrelmin(x) # Slice valid indices valid = Nx.slice_along_axis(indices, 0, Nx.to_number(valid_indices), axis: 0) # With larger neighborhood %{indices: indices, valid_indices: valid_indices} = NxSignal.PeakFinding.argrelmin(x, order: 3) ``` -------------------------------- ### Create Overlapping Windows from Signal in Elixir Source: https://context7.com/elixir-nx/nx_signal/llms.txt Splits a 1D tensor into overlapping or non-overlapping windows (frames) for signal processing tasks using NxSignal's `as_windowed` function. Supports custom window lengths, strides, and various padding modes including reflection. ```elixir # Basic windowing with stride 1 signal = Nx.tensor([0, 1, 2, 3, 4, 10, 11, 12]) windows = NxSignal.as_windowed(signal, window_length: 4) # => #Nx.Tensor # With custom stride and padding windows = NxSignal.as_windowed( Nx.tensor([0, 1, 2, 3, 4, 10, 11]), window_length: 2, stride: 2, padding: [{0, 3}] ) # => #Nx.Tensor # Reflect padding for edge handling windows = NxSignal.as_windowed( Nx.iota({7}), window_length: 6, padding: :reflect, stride: 1 ) ``` -------------------------------- ### Rectangular Window Function Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates rectangular windows, which consist of all ones. This is useful for disabling windowing or when a flat frequency response is desired. Supports specifying the data type. ```elixir w = NxSignal.Windows.rectangular(5) # => #Nx.Tensor ``` ```elixir w = NxSignal.Windows.rectangular(5, type: :f32) # => #Nx.Tensor ``` -------------------------------- ### Triangular Window Function Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates triangular windows, similar to the Bartlett window but with non-zero values at the endpoints. This window provides a simple tapering effect. ```elixir w = NxSignal.Windows.triangular(3) # => #Nx.Tensor ``` -------------------------------- ### Find Relative Maxima using NxSignal Source: https://context7.com/elixir-nx/nx_signal/llms.txt Demonstrates how to identify local maxima in 1D and 2D tensors. It supports specifying the neighborhood order and the axis along which to perform the search. ```elixir # With larger neighborhood (order: 3) %{indices: indices, valid_indices: valid_indices} = NxSignal.PeakFinding.argrelmax(x, order: 3) # 2D tensor with axis selection x = Nx.tensor([[1, 2, 1, 2], [6, 2, 0, 0], [5, 3, 4, 4]]) %{indices: indices} = NxSignal.PeakFinding.argrelmax(x, axis: 1) ``` -------------------------------- ### Window Functions Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates various types of window functions used in signal processing, such as Hann, Hamming, Blackman, Kaiser, Bartlett, Triangular, and Rectangular windows. ```APIDOC ## Window Functions ### Hann Window Cosine-shaped window with good frequency resolution and moderate spectral leakage. ```elixir # Symmetric window (for filter design) w = NxSignal.Windows.hann(5, is_periodic: false) # => #Nx.Tensor # Periodic window (for spectral analysis/STFT) w = NxSignal.Windows.hann(5, is_periodic: true) # => #Nx.Tensor ``` ### Hamming Window Similar to Hann but with different coefficients for reduced first sidelobe. ```elixir w = NxSignal.Windows.hamming(5, is_periodic: true) # => #Nx.Tensor w = NxSignal.Windows.hamming(5, is_periodic: false) # => #Nx.Tensor ``` ### Blackman Window Three-term cosine window with excellent sidelobe suppression. ```elixir w = NxSignal.Windows.blackman(5, is_periodic: false) # => #Nx.Tensor w = NxSignal.Windows.blackman(6, is_periodic: true, type: {:f, 32}) # => #Nx.Tensor ``` ### Kaiser Window Parameterized window using Bessel functions. Beta controls the trade-off between main lobe width and sidelobe level. ```elixir # High beta (12.0) for narrow main lobe w = NxSignal.Windows.kaiser(4, beta: 12.0, is_periodic: true) # => #Nx.Tensor w = NxSignal.Windows.kaiser(5, beta: 12.0, is_periodic: true) # => #Nx.Tensor ``` ### Bartlett Window Triangular window with zero endpoints. ```elixir w = NxSignal.Windows.bartlett(3) # => #Nx.Tensor ``` ### Triangular Window Similar to Bartlett but with non-zero endpoints. ```elixir w = NxSignal.Windows.triangular(3) # => #Nx.Tensor ``` ### Rectangular Window No windowing (all ones), useful when you want to disable windowing. ```elixir w = NxSignal.Windows.rectangular(5) # => #Nx.Tensor w = NxSignal.Windows.rectangular(5, type: :f32) # => #Nx.Tensor ``` ``` -------------------------------- ### Compute Short-Time Fourier Transform (STFT) in Elixir Source: https://context7.com/elixir-nx/nx_signal/llms.txt Calculates the Short-Time Fourier Transform of a signal using NxSignal. It breaks the signal into frames, applies a window, and computes the DFT for each frame. Supports various parameters like overlap, FFT length, sampling rate, and scaling options for spectrum or power spectral density. ```elixir # Create a simple signal signal = Nx.iota({100}) |> Nx.sin() # Create a Hann window of length 16 window = NxSignal.Windows.hann(16) # Compute STFT with 8-sample overlap {z, times, frequencies} = NxSignal.stft( signal, window, overlap_length: 8, fft_length: 16, sampling_rate: 1000 ) # z is a complex tensor of shape [frames, frequencies] # times contains the center time of each frame in seconds # frequencies contains the frequency bins in Hz # Example with scaling options {z_scaled, _, _} = NxSignal.stft( signal, window, overlap_length: 8, fft_length: 16, sampling_rate: 1000, scaling: :spectrum # or :psd for power spectral density ) ``` -------------------------------- ### Reconstruct Signal from STFT using Inverse STFT in Elixir Source: https://context7.com/elixir-nx/nx_signal/llms.txt Reconstructs a time-domain signal from its STFT representation using the overlap-and-add method with NxSignal. Requires the same parameters and window function used for the original STFT to ensure perfect reconstruction. ```elixir # Original signal t = Nx.tensor([10, 10, 1, 0, 10, 10, 2, 20]) # Create window w = NxSignal.Windows.hann(4) # STFT parameters opts = [sampling_rate: 1, fft_length: 4] # Forward STFT {z, _time, _freqs} = NxSignal.stft(t, w, opts) # Inverse STFT to reconstruct signal result = NxSignal.istft(z, w, opts) # Convert back to original type reconstructed = Nx.as_type(result, Nx.type(t)) # => #Nx.Tensor # Note: First sample may differ due to Hann window edge effects ``` -------------------------------- ### Hamming Window Function Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates Hamming windows, which are similar to Hann windows but use different coefficients to reduce the first sidelobe level. Supports both periodic and symmetric configurations. ```elixir w = NxSignal.Windows.hamming(5, is_periodic: true) # => #Nx.Tensor ``` ```elixir w = NxSignal.Windows.hamming(5, is_periodic: false) # => #Nx.Tensor ``` -------------------------------- ### Overlap and Add Source: https://context7.com/elixir-nx/nx_signal/llms.txt Reconstructs a signal from overlapping frames using the overlap-and-add algorithm. ```APIDOC ## POST /overlap_and_add ### Description Reconstructs a signal from overlapping frames using the overlap-and-add algorithm. ### Method POST ### Endpoint /overlap_and_add ### Parameters #### Query Parameters - **frames** (Nx.Tensor) - Required - A tensor where each row represents a frame. - **overlap_length** (integer) - Required - The number of samples that overlap between consecutive frames. ### Request Example ```json { "frames": "Nx.iota({3, 4})", "overlap_length": 3 } ``` ### Response #### Success Response (200) - **reconstructed_signal** (Nx.Tensor) - The reconstructed time-domain signal. #### Response Example ```json { "reconstructed_signal": "" } ``` ``` -------------------------------- ### Kaiser Window Function Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates Kaiser windows, a flexible window function parameterized by beta, which controls the trade-off between main lobe width and sidelobe level. Supports periodic configurations and various beta values. ```elixir # High beta (12.0) for narrow main lobe w = NxSignal.Windows.kaiser(4, beta: 12.0, is_periodic: true) # => #Nx.Tensor ``` ```elixir w = NxSignal.Windows.kaiser(5, beta: 12.0, is_periodic: true) # => #Nx.Tensor ``` -------------------------------- ### Chirp Waveform Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates frequency-swept cosine signals (chirps) with support for linear, quadratic, and hyperbolic frequency sweeps. ```elixir t = Nx.linspace(0, 10, n: 100) # Linear chirp: 10Hz at t=0 to 1Hz at t=10 chirp = NxSignal.Waveforms.chirp(t, 10, 10, 1, method: :linear) # Quadratic chirp chirp = NxSignal.Waveforms.chirp(t, 10, 10, 1, method: :quadratic) # Quadratic with vertex at end chirp = NxSignal.Waveforms.chirp(t, 10, 10, 1, method: :quadratic, vertex_zero: false) # Hyperbolic chirp chirp = NxSignal.Waveforms.chirp(t, 10, 10, 1, method: :hyperbolic) # Logarithmic (exponential) chirp chirp = NxSignal.Waveforms.chirp(t, 10, 10, 1, method: :logarithmic) ``` -------------------------------- ### Short-Time Fourier Transform (STFT) Source: https://context7.com/elixir-nx/nx_signal/llms.txt Computes the Short-Time Fourier Transform of a tensor, returning the complex spectrum, time points, and frequency bins. The STFT breaks a signal into overlapping frames, applies a window function, and computes the DFT of each frame. ```APIDOC ## POST /stft ### Description Computes the Short-Time Fourier Transform (STFT) of a signal. ### Method POST ### Endpoint /stft ### Parameters #### Query Parameters - **signal** (Nx.Tensor) - Required - The input signal tensor. - **window** (Nx.Tensor) - Required - The window function to apply to each frame. - **overlap_length** (integer) - Optional - The number of samples to overlap between frames. Defaults to 0. - **fft_length** (integer) - Optional - The length of the FFT to compute for each frame. Defaults to the window length. - **sampling_rate** (float) - Optional - The sampling rate of the signal in Hz. Defaults to 1.0. - **scaling** (atom) - Optional - The type of scaling to apply to the spectrum. Can be `:spectrum` or `:psd`. Defaults to `:spectrum`. - **window_padding** (atom or list) - Optional - Padding to apply to the signal before windowing. Can be `:reflect` or a list of tuples `{{before, after}}`. ### Request Example ```json { "signal": "Nx.iota({100}) |> Nx.sin()", "window": "NxSignal.Windows.hann(16)", "overlap_length": 8, "fft_length": 16, "sampling_rate": 1000, "scaling": "spectrum" } ``` ### Response #### Success Response (200) - **z** (Nx.Tensor) - The complex spectrum tensor of shape `[frames, frequencies]`. - **times** (Nx.Tensor) - The time points for each frame in seconds. - **frequencies** (Nx.Tensor) - The frequency bins in Hz. #### Response Example ```json { "z": "", "times": "", "frequencies": "" } ``` ``` -------------------------------- ### Inverse Short-Time Fourier Transform (ISTFT) Source: https://context7.com/elixir-nx/nx_signal/llms.txt Reconstructs a time-domain signal from its STFT representation using overlap-and-add. For perfect reconstruction, use the same parameters and window as the original STFT. ```APIDOC ## POST /istft ### Description Reconstructs a time-domain signal from its STFT representation using the overlap-and-add method. ### Method POST ### Endpoint /istft ### Parameters #### Query Parameters - **z** (Nx.Tensor) - Required - The complex spectrum tensor obtained from STFT. - **window** (Nx.Tensor) - Required - The window function used in the original STFT. - **overlap_length** (integer) - Optional - The overlap length used in the original STFT. Defaults to 0. - **fft_length** (integer) - Optional - The FFT length used in the original STFT. Defaults to the window length. - **sampling_rate** (float) - Optional - The sampling rate of the signal in Hz. Defaults to 1.0. ### Request Example ```json { "z": "", "window": "NxSignal.Windows.hann(4)", "overlap_length": 8, "fft_length": 16, "sampling_rate": 1000 } ``` ### Response #### Success Response (200) - **reconstructed_signal** (Nx.Tensor) - The reconstructed time-domain signal. #### Response Example ```json { "reconstructed_signal": "" } ``` ``` -------------------------------- ### Hann Window Function Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates Hann (cosine-shaped) windows, supporting both symmetric (for filter design) and periodic (for spectral analysis) modes. The Hann window offers good frequency resolution with moderate spectral leakage. ```elixir # Symmetric window (for filter design) w = NxSignal.Windows.hann(5, is_periodic: false) # => #Nx.Tensor ``` ```elixir # Periodic window (for spectral analysis/STFT) w = NxSignal.Windows.hann(5, is_periodic: true) # => #Nx.Tensor ``` -------------------------------- ### Reconstruct Signal using Overlap-and-Add in Elixir Source: https://context7.com/elixir-nx/nx_signal/llms.txt Reconstructs a continuous signal from a set of overlapping frames using the overlap-and-add algorithm provided by NxSignal. This is often used in conjunction with STFT. ```elixir # No overlap frames = Nx.iota({3, 4}) result = NxSignal.overlap_and_add(frames, overlap_length: 0) # => #Nx.Tensor # With 3-sample overlap result = NxSignal.overlap_and_add(frames, overlap_length: 3) # => #Nx.Tensor ``` -------------------------------- ### FIR Filter Design Source: https://context7.com/elixir-nx/nx_signal/llms.txt Designs Finite Impulse Response (FIR) filters using the window method. Supports lowpass, highpass, bandpass, and bandstop filter configurations. ```APIDOC ## FIR Filter Design Designs FIR filters using the window method. Supports lowpass, highpass, bandpass, and bandstop configurations. ```elixir # Lowpass filter with 5 taps, cutoff at 0.3 * Nyquist coeffs = NxSignal.Filters.firwin(5, [0.3], window: :hamming, sampling_rate: 2.0) # => Tensor of shape {5} # Highpass filter (pass_zero: false) coeffs = NxSignal.Filters.firwin( 11, [0.4], window: :hann, pass_zero: false, sampling_rate: 2.0 ) # Bandpass filter (two cutoffs, pass_zero: false) coeffs = NxSignal.Filters.firwin( 21, [0.2, 0.5], window: :blackman, pass_zero: false, sampling_rate: 2.0 ) # Bandstop filter (two cutoffs, pass_zero: true) coeffs = NxSignal.Filters.firwin( 21, [0.2, 0.5], window: {:kaiser, 8.0}, pass_zero: true, sampling_rate: 2.0 ) # With specific sampling rate in Hz coeffs = NxSignal.Filters.firwin( 15, [1000.0], # 1kHz cutoff window: :hamming, sampling_rate: 8000.0 # 8kHz sampling rate ) ``` ``` -------------------------------- ### Blackman Window Function Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates Blackman windows, a three-term cosine window known for its excellent sidelobe suppression. This function allows specifying the number of taps, periodicity, and data type. ```elixir w = NxSignal.Windows.blackman(5, is_periodic: false) # => #Nx.Tensor ``` ```elixir w = NxSignal.Windows.blackman(6, is_periodic: true, type: {:f, 32}) # => #Nx.Tensor ``` -------------------------------- ### Bartlett Window Function Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates Bartlett windows, which are triangular windows with zero values at the endpoints. This is a simple window function often used in signal processing. ```elixir w = NxSignal.Windows.bartlett(3) # => #Nx.Tensor ``` -------------------------------- ### Convert STFT to MEL Spectrogram in Elixir Source: https://context7.com/elixir-nx/nx_signal/llms.txt Converts an STFT spectrum to the MEL scale using NxSignal, which is beneficial for audio analysis and machine learning tasks as it mimics human auditory perception. It can also generate MEL filter weights directly. ```elixir # Generate STFT fft_length = 16 sampling_rate = 8.0e3 signal = Nx.iota({100}) |> Nx.multiply(0.1) {z, _, _} = NxSignal.stft( signal, NxSignal.Windows.hann(4), overlap_length: 2, fft_length: fft_length, sampling_rate: sampling_rate, window_padding: :reflect ) # Convert to MEL spectrogram with 4 MEL bins mel_spec = NxSignal.stft_to_mel(z, sampling_rate, fft_length: fft_length, mel_bins: 4) # Returns tensor of shape [frames, mel_bins] # Generate MEL filter weights directly filters = NxSignal.mel_filters(fft_length, 5, sampling_rate) # Returns tensor of shape [mels, frequencies] ``` -------------------------------- ### Square Waveform Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates periodic square waves with a configurable duty cycle. The duty cycle can be a scalar or a time-varying tensor. ```elixir t = Nx.iota({10}) |> Nx.multiply(:math.pi() * 2 / 10) # 10% duty cycle wave = NxSignal.Waveforms.square(t, duty: 0.1) # 50% duty cycle (default) wave = NxSignal.Waveforms.square(t, duty: 0.5) # Time-varying duty cycle duty = Nx.tensor([0.1, 0, 0.3, 0, 0.5, 0, 0.7, 0, 0.9, 0]) wave = NxSignal.Waveforms.square(t, duty: duty) ``` -------------------------------- ### Polynomial Frequency Sweep Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates frequency-swept cosine signals where the frequency follows a polynomial function of time. Supports phase offsets and degree units. ```elixir t = Nx.linspace(0, 10, n: 100) # Frequency varies as f(t) = 2t^2 + 1 (quadratic sweep) sweep = NxSignal.Waveforms.polynomial_sweep(t, Nx.tensor([2, 0, 1])) # Linear sweep: f(t) = t sweep = NxSignal.Waveforms.polynomial_sweep(t, Nx.tensor([1, 0])) # With phase offset sweep = NxSignal.Waveforms.polynomial_sweep( t, Nx.tensor([1, 0]), phi: :math.pi() / 2 ) # Phase in degrees sweep = NxSignal.Waveforms.polynomial_sweep( t, Nx.tensor([1, 0]), phi: 180, phi_unit: :degrees ) ``` -------------------------------- ### Gaussian Pulse Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates Gaussian-modulated sinusoidal pulses. The output includes envelope, in-phase, and quadrature components. Supports custom bandwidth and reference levels. ```elixir t = Nx.linspace(0, 1, n: 100) # Basic Gaussian pulse at 4Hz center frequency pulse = NxSignal.Waveforms.gaussian_pulse(t, center_frequency: 4) # Returns a map with three components: # pulse.envelope - Gaussian envelope # pulse.in_phase - Real part (I component) # pulse.quadrature - Imaginary part (Q component) # With custom bandwidth (fractional bandwidth) pulse = NxSignal.Waveforms.gaussian_pulse( t, center_frequency: 4, bandwidth: 0.25, bandwidth_reference_level: -6 ) ``` -------------------------------- ### Unit Impulse Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates a discrete delta function (unit impulse) in a tensor. Supports specifying the shape, data type, and impulse location (e.g., :midpoint or specific indices). ```elixir # 1D impulse at index 0 impulse = NxSignal.Waveforms.unit_impulse({5}) # 2D impulse at midpoint impulse = NxSignal.Waveforms.unit_impulse({3, 5}, type: :s32, index: :midpoint) # Impulse at specific index impulse = NxSignal.Waveforms.unit_impulse({3, 5}, index: Nx.tensor([[2, 3]]), type: :s32) ``` -------------------------------- ### Sawtooth Waveform Generation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Generates periodic sawtooth or triangular waveforms. The 'width' parameter controls the shape, with values from 0 to 1. ```elixir # 5Hz sawtooth sampled at 500Hz for 1 second t = Nx.linspace(0, 1, n: 500) phase = Nx.multiply(2 * :math.pi() * 5, t) # Standard sawtooth (width: 1) wave = NxSignal.Waveforms.sawtooth(phase) # Triangular wave (width: 0.5) triangle = NxSignal.Waveforms.sawtooth(phase, width: 0.5) # Reverse sawtooth (width: 0) reverse_saw = NxSignal.Waveforms.sawtooth(phase, width: 0) ``` -------------------------------- ### Convolution using NxSignal Source: https://context7.com/elixir-nx/nx_signal/llms.txt Performs convolution between two tensors. Supports different modes like :same and :full. FFT-based convolution is also available for efficiency. ```elixir result = NxSignal.Convolution.convolve( Nx.tensor([1, 2, 3, 4, 5]), Nx.tensor([1, 2]), mode: :same ) result = NxSignal.Convolution.fftconvolve( Nx.tensor([1, 2, 3]), Nx.tensor([3, 4, 5]), mode: :full ) ``` -------------------------------- ### Wiener Filter Application Source: https://context7.com/elixir-nx/nx_signal/llms.txt Applies a Wiener filter for noise reduction, utilizing local statistics of the signal. It can automatically estimate noise or accept user-specified parameters like kernel size and noise power. ```elixir # Basic Wiener filter with automatic noise estimation t = Nx.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) filtered = NxSignal.Filters.wiener(t) ``` ```elixir # With specified kernel size and noise power filtered = NxSignal.Filters.wiener(t, kernel_size: {2, 2}, noise: 10) # => #Nx.Tensor ``` ```elixir # Kernel size as single integer (applied to all dimensions) filtered = NxSignal.Filters.wiener(t, kernel_size: 3, noise: nil) ``` -------------------------------- ### MEL Spectrogram Conversion Source: https://context7.com/elixir-nx/nx_signal/llms.txt Converts an STFT spectrum to MEL-scale for audio analysis and machine learning applications. MEL scale better represents human auditory perception. ```APIDOC ## POST /stft_to_mel ### Description Converts a Short-Time Fourier Transform (STFT) spectrum to the MEL scale. ### Method POST ### Endpoint /stft_to_mel ### Parameters #### Query Parameters - **z** (Nx.Tensor) - Required - The complex spectrum tensor obtained from STFT. - **sampling_rate** (float) - Required - The sampling rate of the original signal in Hz. - **fft_length** (integer) - Optional - The FFT length used in the original STFT. Defaults to the window length. - **mel_bins** (integer) - Optional - The number of MEL bins to create. Defaults to 40. ### Request Example ```json { "z": "", "sampling_rate": 8000, "fft_length": 16, "mel_bins": 4 } ``` ### Response #### Success Response (200) - **mel_spectrogram** (Nx.Tensor) - The MEL-scale spectrogram tensor of shape `[frames, mel_bins]`. #### Response Example ```json { "mel_spectrogram": "" } ``` ``` ```APIDOC ## POST /mel_filters ### Description Generates MEL filter weights for converting frequency bins to MEL scale. ### Method POST ### Endpoint /mel_filters ### Parameters #### Query Parameters - **fft_length** (integer) - Required - The FFT length used. - **mel_bins** (integer) - Required - The number of MEL bins to generate. - **sampling_rate** (float) - Required - The sampling rate of the signal in Hz. ### Request Example ```json { "fft_length": 16, "mel_bins": 5, "sampling_rate": 8000 } ``` ### Response #### Success Response (200) - **filters** (Nx.Tensor) - The MEL filter weights tensor of shape `[mel_bins, frequencies]`. #### Response Example ```json { "filters": "" } ``` ``` -------------------------------- ### Correlation using NxSignal Source: https://context7.com/elixir-nx/nx_signal/llms.txt Computes the correlation between two tensors. The function supports various modes, including :same, to control the output size. ```elixir result = NxSignal.Convolution.correlate( Nx.tensor([1, 2, 3]), Nx.tensor([3, 4, 5]) ) result = NxSignal.Convolution.correlate( Nx.tensor([1, 2, 3, 4, 5]), Nx.tensor([1, 2]), mode: :same ) ``` -------------------------------- ### Wiener Filter Source: https://context7.com/elixir-nx/nx_signal/llms.txt Applies a Wiener filter for noise reduction, utilizing local statistics of the signal. ```APIDOC ## Wiener Filter Applies a Wiener filter for noise reduction using local statistics. ```elixir # Basic Wiener filter with automatic noise estimation t = Nx.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) filtered = NxSignal.Filters.wiener(t) # With specified kernel size and noise power filtered = NxSignal.Filters.wiener(t, kernel_size: {2, 2}, noise: 10) # => #Nx.Tensor # Kernel size as single integer (applied to all dimensions) filtered = NxSignal.Filters.wiener(t, kernel_size: 3, noise: nil) ``` ``` -------------------------------- ### Tensor Convolution Source: https://context7.com/elixir-nx/nx_signal/llms.txt Computes the convolution of two tensors. Supports both direct convolution (default) and FFT-based convolution for improved performance with large inputs. Various output modes are available. ```elixir # Direct convolution (default) result = NxSignal.Convolution.convolve( Nx.tensor([1, 2, 3]), Nx.tensor([3, 4, 5]) ) # => #Nx.Tensor ``` ```elixir # FFT-based convolution (faster for large inputs) result = NxSignal.Convolution.convolve( Nx.tensor([1, 2, 3]), Nx.tensor([3, 4, 5]), method: :fft ) ``` ```elixir # Different output modes # :full - returns all N + K - 1 samples (default) ``` -------------------------------- ### Median Filter Source: https://context7.com/elixir-nx/nx_signal/llms.txt Applies a median filter to remove noise while preserving edges in both 1D and 2D signals. ```APIDOC ## Median Filter Applies a median filter to remove noise while preserving edges. ```elixir # 1D median filter signal = Nx.tensor([1.0, 2.0, 100.0, 4.0, 5.0]) # 100.0 is noise filtered = NxSignal.Filters.median(signal, kernel_shape: {3}) # 2D median filter for images image = Nx.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) filtered = NxSignal.Filters.median(image, kernel_shape: {3, 3}) ``` ``` -------------------------------- ### Median Filter Application Source: https://context7.com/elixir-nx/nx_signal/llms.txt Applies a median filter to a signal or image to remove noise while preserving important edges. The kernel shape determines the neighborhood size for the median calculation. ```elixir # 1D median filter signal = Nx.tensor([1.0, 2.0, 100.0, 4.0, 5.0]) # 100.0 is noise filtered = NxSignal.Filters.median(signal, kernel_shape: {3}) ``` ```elixir # 2D median filter for images image = Nx.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) filtered = NxSignal.Filters.median(image, kernel_shape: {3, 3}) ``` -------------------------------- ### Peak Finding - Relative Maxima Source: https://context7.com/elixir-nx/nx_signal/llms.txt Finds the indices of relative maxima in a tensor along a specified axis. Returns a map containing indices and the count of valid maxima. ```elixir x = Nx.tensor([2, 1, 2, 3, 2, 0, 1, 0]) # Find local maxima %{indices: indices, valid_indices: valid_indices} = NxSignal.PeakFinding.argrelmax(x) # Extract valid indices valid = Nx.slice_along_axis(indices, 0, Nx.to_number(valid_indices), axis: 0) ``` -------------------------------- ### Sinc Function Computation Source: https://context7.com/elixir-nx/nx_signal/llms.txt Computes the normalized sinc function, defined as sinc(t) = sin(πt) / (πt). The function handles the special case where sinc(0) is defined as 1.0. ```elixir result = NxSignal.Waveforms.sinc(Nx.tensor([0, 0.25, 1])) # => #Nx.Tensor # sinc(0) is defined as 1.0 ``` -------------------------------- ### Convolution Source: https://context7.com/elixir-nx/nx_signal/llms.txt Computes the convolution of two tensors, supporting both direct and FFT-based methods. ```APIDOC ## Convolution Computes convolution of two tensors using either direct or FFT methods. ```elixir # Direct convolution (default) result = NxSignal.Convolution.convolve( Nx.tensor([1, 2, 3]), Nx.tensor([3, 4, 5]) ) # => #Nx.Tensor # FFT-based convolution (faster for large inputs) result = NxSignal.Convolution.convolve( Nx.tensor([1, 2, 3]), Nx.tensor([3, 4, 5]), method: :fft ) # Different output modes # :full - returns all N + K - 1 samples (default) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.