intan.processing package

Signal processing, filtering, feature extraction, and metrics utilities for EMG data.

intan.processing._filters

Comprehensive EMG signal preprocessing module.

Includes:

  • Bandpass, lowpass, and notch filters

  • Hilbert envelope extraction

  • RMS and windowed RMS computation

  • Common average referencing (CAR)

  • Sliding windows and PCA-based dimensionality reduction

  • CNN-ECA compatible preprocessing pipeline

This module supports feature extraction pipelines for real-time classification and pre-training EMG datasets with overlapping or fixed windows.

preprocess_emg(emg_data, sample_rate)[source]

Applies filtering and extracts RMS features.

Parameters:
  • emg_data – 2D numpy array of EMG data (channels, samples).

  • sample_rate – Sampling rate of the EMG data.

Returns:

2D numpy array of RMS features (channels, windows).

Return type:

rms_features

parse_channel_ranges(channel_arg)[source]

Parses a channel range string (e.g., [1:8, 64:72]) and returns a flat list of integers.

Parameters:

channel_arg (str) – The string containing channel ranges (e.g., “[1:8, 64:72]”).

Returns:

A flat list of integers.

Return type:

list

notch_filter(data, fs=4000, f0=60.0, Q=10, axis=1)[source]

Applies a notch filter to the data to remove 60 Hz interference. Assumes data shape (n_channels, n_samples). A bandwidth of 10 Hz is recommended for 50 or 60 Hz notch filters; narrower bandwidths lead to poor time-domain properties with an extended ringing response to transient disturbances.

Parameters:
  • data (ndarray) – Input data to be filtered.

  • fs (float) – Sampling frequency of the data.

  • f0 (float) – Frequency to be removed from the data (60 Hz).

  • Q (float) – Quality factor of the notch filter.

Return type:

nn.array

Example

out = notch_filter(signal_in, 30000, 60, 10);

lowpass_filter(data, cutoff, fs, order=4, axis=1)[source]

Applies a lowpass filter to the data using a Butterworth filter.

Parameters:
  • data (ndarray) – Input data to be filtered.

  • cutoff (float) – Cutoff frequency.

  • fs (float) – Sampling frequency of the data.

  • order (int) – Order of the filter.

  • axis (int) – Axis along which to apply the filter.

Returns:

Filtered data.

Return type:

ndarray

bandpass_filter(data, lowcut=10, highcut=500, fs=4000, order=4, axis=1, verbose=False)[source]

Applies a bandpass filter to the data using a Butterworth filter.

Parameters:
  • data (ndarray) – Input data to be filtered.

  • lowcut (float) – Low cutoff frequency.

  • highcut (float) – High cutoff frequency.

  • fs (float) – Sampling frequency of the data.

  • order (int) – Order of the filter.

  • axis (int) – Axis along which to apply the filter.

  • verbose (bool) – Whether to print filter parameters.

Returns:

Filtered data.

Return type:

ndarray

filter_emg(emg_data, filter_type='bandpass', lowcut=30, highcut=500, fs=1259, order=5, verbose=False)[source]

Applies a bandpass or lowpass filter to EMG data using numpy arrays.

Parameters:
  • emg_data – Numpy array of shape (num_samples, num_channels) with EMG data.

  • filter_type – Type of filter to apply (‘bandpass’ or ‘lowpass’).

  • lowcut – Low cutoff frequency for the bandpass filter.

  • highcut – High cutoff frequency for the bandpass filter.

  • fs – Sampling rate of the EMG data.

  • order – Filter order.

  • verbose – Whether to print progress.

Returns:

Filtered data as a numpy array (same shape as input data).

process_emg_pipeline(data, lowcut=30, highcut=500, order=5, window_size=400, verbose=False)[source]

Processing steps to match the CNN-ECA methodology https://pmc.ncbi.nlm.nih.gov/articles/PMC10669079/ Input data is assumed to have shape (N_channels, N_samples)

Parameters:
  • data – 2D numpy array of EMG data (channels, samples).

  • lowcut – Low cutoff frequency for the bandpass filter.

  • highcut – High cutoff frequency for the bandpass filter.

  • order – Order of the Butterworth filter.

  • window_size – Window size for RMS calculation.

  • verbose – Whether to print progress.

Returns:

2D numpy array of processed EMG data (channels, samples).

Return type:

smoothed

sliding_window(data, window_size, step_size)[source]

Splits the data into overlapping windows.

Parameters:
  • data – 2D numpy array of shape (channels, samples).

  • window_size – Window size in number of samples.

  • step_size – Step size in number of samples.

Returns:

List of numpy arrays, each representing a window of data.

Return type:

windows

orthogonalize(W, wp, i)[source]

Orthogonalizes the weight vector wp with respect to the first i columns of W.

Parameters:
  • W – Weight matrix of shape (n_features, n_features).

  • wp – Weight vector to be orthogonalized of shape (n_features,).

  • i – Index of the column in W to orthogonalize against.

Returns:

Orthogonalized weight vector of shape (n_features,).

Return type:

wp

normalize(wp)[source]

Normalizes the weight vector wp.

Parameters:

wp – Weight vector to be normalized of shape (n_features,).

Returns:

Normalized weight vector of shape (n_features,).

Return type:

wp

rectify(emg_data)[source]

Rectifies EMG data by converting all values to their absolute values.

Parameters:

emg_data (numpy array) – List of numpy arrays or pandas DataFrame items with filtered EMG data.

Returns:

List of rectified numpy arrays (same shape as input data).

Return type:

rectified_data

window_rms(emg_data, window_size=400, verbose=False)[source]

Apply windowed RMS to each channel in the multichannel EMG data.

Parameters:
  • emg_data – Numpy array of shape (num_samples, num_channels).

  • window_size – Size of the window for RMS calculation.

  • verbose – Whether to print progress.

Returns:

Smoothed EMG data with windowed RMS applied to each channel (same shape as input).

window_rms_1D(signal, window_size)[source]

Compute windowed RMS of the signal.

Parameters:
  • signal – Input EMG signal.

  • window_size – Size of the window for RMS calculation.

Returns:

Windowed RMS signal.

calculate_rms(data, window_size, verbose=False)[source]

Calculates RMS features for each channel using non-overlapping windows.

Parameters:
  • data – 2D numpy array of EMG data (channels, samples).

  • window_size – Size of the window for RMS calculation.

  • verbose – Whether to print progress.

Returns:

2D numpy array of RMS features (channels, windows).

Return type:

rms_features

compute_rolling_rms(signal, window_size=50)[source]

Compute rolling RMS on a 1D signal.

downsample(emg_data, sampling_rate, target_fs=1000)[source]

Downsamples the EMG data to the target sampling rate.

Parameters:
  • emg_data – 2D numpy array of shape (num_channels, num_samples).

  • sampling_rate – Sampling rate of the original EMG data.

  • target_fs – Target sampling rate for downsampling.

Returns:

2D numpy array of shape (num_channels, downsampled_samples).

Return type:

downsampled_data

common_average_reference(emg_data, ignore_channels=None)[source]

Applies Common Average Referencing (CAR) to the multi-channel EMG data.

Parameters:
  • emg_data – 2D numpy array of shape (num_channels, num_samples).

  • ignore_channels – List of channels to ignore in CAR calculation (optional).

Returns:

2D numpy array after applying CAR (same shape as input).

Return type:

car_data

envelope_extraction(data, method='hilbert')[source]

Extracts the envelope of the EMG signal using the Hilbert transform.

Parameters:
  • data – 2D numpy array of EMG data (channels, samples).

  • method – Method for envelope extraction (‘hilbert’ or other).

Returns:

2D numpy array of the envelope (channels, samples).

Return type:

envelope

z_score_norm(data)[source]

Apply z-score normalization to the input data.

Parameters:

data – 2D numpy array of shape (channels, samples).

Returns:

2D numpy array of shape (channels, samples) after z-score normalization.

Return type:

normalized_data

compute_rms(emg_window, axis=-1)[source]

Compute RMS of EMG data along a given axis.

Parameters:
  • emg_window (np.ndarray) – EMG data with one or two dimensions.

  • axis (int) – Axis to compute RMS over. Default is -1 (last axis).

Returns:

RMS value(s) along the given axis.

Return type:

np.ndarray or float

compute_grid_average(emg_data, grid_spacing=8, axis=0)[source]

Computes the average of the EMG grids according to the grid spacing. For example, a spacing of 8 means that channels 1, 9, 17, etc. will be averaged together to form the first grid, and so on.

Parameters:
  • emg_data (np.ndarray) – 2D numpy array of shape (num_channels, num_samples).

  • grid_spacing (int) – Number of channels to average together.

  • axis (int) – Axis along which to compute the grid averages.

Returns:

2D numpy array of shape (num_grids, num_samples).

Return type:

grid_averages (np.ndarray)

variance(data)[source]

Computes the variance of the input data.

Parameters:

data (np.ndarray) shape (n_channels, n_samples) – Input data for which to compute the variance.

Returns:

Variance value for each channel.

Return type:

np.ndarray

mean_absolute_value(data)[source]

Computes the Mean Absolute Value (MAV) of the input data.

Parameters:

data (np.ndarray) shape (n_channels, n_samples) – Input data for which to compute the MAV.

Returns:

MAV value for each channel.

Return type:

np.ndarray

zero_crossings(ch, threshold=0.01)[source]

Computes the number of zero crossings in the input data.

Parameters:
  • ch (np.ndarray) – Input data for which to compute the zero crossings.

  • threshold (float) – Threshold for detecting significant changes.

Returns:

Number of zero crossings.

Return type:

int

integrated_emg(data)[source]

Computes the Integrated EMG (IEMG) of the input data.

Parameters:

data (np.ndarray) shape (n_channels, n_samples) – Input data for which to compute the IEMG.

Returns:

Integrated EMG value for each channel.

Return type:

np.ndarray

slope_sign_changes(ch, threshold=0.01)[source]

Computes the number of slope sign changes in the input data.

Parameters:
  • ch (np.ndarray) – Input data for which to compute the slope sign changes.

  • threshold (float) – Threshold for detecting significant changes.

Returns:

Number of slope sign changes.

Return type:

int

waveform_length(data)[source]

Computes the waveform length of the input data.

Parameters:

data (np.ndarray) shape (n_channels, n_samples) – Input data for which to compute the waveform length.

Returns:

Waveform length for each channel.

Return type:

np.ndarray

root_mean_square(data)[source]

Computes the Root Mean Square (RMS) of the input data.

Parameters:

data (np.ndarray) shape (n_channels, n_samples) – Input data for which to compute the RMS.

Returns:

RMS value for each channel.

Return type:

np.ndarray

extract_features(segment, feature_fns=None)[source]

Extracts features from a multichannel segment.

Parameters:
  • segment – np.ndarray (n_channels, n_samples)

  • feature_fns – list of strings or callables

Returns:

flattened feature vector

Return type:

1D np.ndarray

extract_features_sliding_window(data, fs, window_ms, step_ms, feature_fns=None)[source]
Parameters:
  • data (ndarray) – (n_channels, n_samples) array of EMG data

  • fs (float) – (int) sampling rate in Hz

  • window_ms (float) – window length in ms

  • step_ms (float) – step between windows in ms

Return type:

ndarray

Returns:

(n_windows, n_features)

feature_spec_from_registry(feature_registry, feature_fns=None, *, per_channel=True, layout='channel_major', channels='training_order')[source]

Build a self-describing spec for your feature vector.

If feature_fns is None, we assume ‘use all’ features in the registry, in the registry’s key order.

Return type:

Dict

Parameters:
  • feature_registry (Dict[str, Callable])

  • feature_fns (List[str | Callable] | None)

  • per_channel (bool)

  • layout (str)

  • channels (str)

intan.processing._metrics_utils

Utilities for loading gesture metrics and creating gesture-label mappings.

This module:

  • Loads EMG trial classification metadata from CSV or TXT

  • Parses gesture names into integer class mappings

  • Provides helper functions for checking or retrieving metrics files

Used in training/testing pipelines that require alignment between gesture labels and EMG signal segments.

load_metrics_data(metrics_filepath, verbose=True)[source]

Loads the metrics data from the specified file path and returns the data along with the gesture mapping.

Parameters:
  • metrics_filepath (str) – The path to the metrics data file.

  • verbose (bool) – Whether to print the loaded data and gesture mapping.

Returns:

A tuple containing the metrics data as a pandas DataFrame and the gesture mapping as a dictionary.

Return type:

tuple

get_metrics_file(metrics_filepath, verbose=False)[source]

Checks if the metrics file exists at the specified path. If it does, loads the data and returns it.

Parameters:
  • metrics_filepath (str) – The path to the metrics data file.

  • verbose (bool) – Whether to print the loaded data.

Returns:

The loaded metrics data as a pandas DataFrame.

Return type:

pd.DataFrame