EncProc Module#

LENZ Encoder Signal Processing Library

This library provides a comprehensive suite of signal processing functions for high-precision LENZ encoder systems, designed to enhance the accuracy of position measurements in applications such as motor control, robotics, and industrial automation. It includes advanced algorithms for differential signal computation, harmonic analysis, noise filtering, and calibration table generation, with specialized support for motor-driven systems to suppress harmonic errors.

Key Features:

  • Differential signal computation between paired encoder signals for error analysis.

  • Advanced filtering techniques, including bidirectional moving average and custom smoothing filters.

  • Harmonic analysis using Fast Fourier Transform (FFT) to identify and quantify periodic errors.

  • Generation of difference compensation tables for systematic error correction.

  • Motor harmonic suppression for enhanced accuracy in motor-driven encoder systems.

  • Support for 24-bit encoder data with proper handling of wrap-around effects.

Notes:

  • The library assumes 24-bit encoder data (0 to 2^24 - 1) but can be adapted for other resolutions.

  • Functions are designed for static use, requiring no instantiation of LenzEncoderProcessor.

  • Calibration routines (lenz_cal, lenz_cal_motor_harm) are optimized for high-precision applications but may require tuning of parameters like coarse_red and motor_harm for specific use cases.

  • The diff_table_gen_motor_harm function includes motor harmonic suppression, which is critical for motor-driven systems but requires a valid motor_harm value.

Author:

LENZ ENCODERS, 2020-2025

class lenz_flashtool.encproc.LenzEncoderProcessor#

Bases: object

Main class for LENZ encoder signal processing operations.

Provides static methods for differential signal calculation, compensation table generation, harmonic analysis, and calibration routines for LENZ encoders.

static analyze_harmonics(diff_table)#

Analyzes frequency components of the difference table using FFT.

Parameters:

diff_table (np.array) – Difference table array.

Returns:

  • Amplitude of first harmonic

  • Angle of first harmonic

  • Full FFT result

Return type:

Tuple containing

static as_filt(data, depth)#

Applies a custom filter to smooth the data.

Parameters:
  • data (np.array) – Input data to filter.

  • depth (int) – Filter depth parameter

Returns:

Filtered data

Return type:

np.array

static calc_diff(encoder_1, encoder_2, coarse_red)#

Computes the differential signal between two encoder readings with scaling.

This function calculates the difference between two encoder signals, applies mean centering, and scales the result according to the coarse reduction factor. The output represents the relative error between the two encoders, normalized.

Parameters:
  • encoder_1 (int32) – First encoder readings array (24-bit values)

  • encoder_2 (int32) – Second encoder readings array (24-bit values)

  • coarse_red (int) – Scaling factor for coarse reduction (typically 2^11 for 24-bit encoders)

Return type:

ndarray

Returns:

Differential signal array with mean removed and scaled by coarse_red/2048

static clip_to_int8(data)#

Clips and converts data to 8-bit integers.

Parameters:

data (np.array) – Input data.

Returns:

Clipped and converted data.

Return type:

np.array

static comp_diff(encoder_in, diff_table_in, coarse_red)#

Compensates encoder data using a difference table for error correction.

This function applies a compensation table to raw encoder data to correct systematic errors, using linear interpolation between table entries.

Parameters:
  • encoder_in (ndarray) – Raw encoder input data (24-bit values as int32)

  • diff_table_in (ndarray) – Difference compensation table (int16 values)

  • coarse_red (int) – Coarse reduction factor (scaling parameter)

Returns:

Compensated encoder data (24-bit values as int32)

Return type:

np.ndarray

static compute_difftable(diff, lenz_fix, coarse_red)#

Computes a difference compensation table by averaging error values at each encoder position.

Parameters:
  • diff (ndarray) – Array of measured error values (difference between reference and raw encoder values)

  • lenz_fix (ndarray) – Array of fixed LENZ encoder positions corresponding to the error measurements.

  • coarse_red (int) – Coarse reduction factor.

Returns:

Difference compensation table where each index corresponds to an encoder

position and contains the average error at that position.

Return type:

np.ndarray

static diff_gen(enc_filt, enc_in, coarse_red)#

Generates difference signal between filtered and raw encoder data.

Parameters:
  • enc_filt (ndarray) – Filtered encoder data

  • enc_in (ndarray) – Raw encoder input data

  • coarse_red (int) – Coarse reduction factor

Returns:

Difference signal (double)

Return type:

np.ndarray

static diff_table_gen(diff_in, enc_fix, coarse_red)#

Generates a difference compensation table from error measurements.

The table is created by averaging error measurements at each position, then applying FFT-based filtering to remove noise.

Parameters:
  • diff_in (ndarray) – Array of measured errors (double)

  • enc_fix (ndarray) – Array of fixed encoder positions corresponding to errors (int32)

  • coarse_red (int) – Coarse reduction factor (scaling parameter)

Returns:

Generated difference table (int16 values)

Return type:

np.ndarray

static diff_table_gen_motor_harm(diff_in, enc_fix, coarse_red, motor_harm)#

Generates difference table with additional motor harmonic suppression.

Similar to DifTableGen but specifically removes motor-related harmonics from the compensation table.

Parameters:
  • diff_in (ndarray) – Array of measured errors (double)

  • enc_fix (ndarray) – Array of fixed encoder positions (int32)

  • coarse_red (int) – Coarse reduction factor

  • motor_harm (int) – Motor harmonic frequency to suppress

Returns:

Generated difference table with motor harmonics removed (int16)

Return type:

np.ndarray

static ext_gen(data_in)#

Extends 24-bit encoder data to 32-bit by handling overflow correctly.

This function processes raw 24-bit encoder data by maintaining proper sign extension when converting to 32-bit values, ensuring correct handling of overflow cases.

Parameters:

data_in (ndarray) – Input array of 24-bit encoder values (as int32)

Returns:

Array of extended 32-bit encoder values with proper overflow handling

Return type:

np.ndarray

static extrapolate(data, res)#

Extrapolates data to handle discontinuities or wrap-around effects.

Parameters:
  • data (np.array) – Input data to extrapolate.

  • res (int) – Bit resolution for extrapolation.

Returns:

Extrapolated data.

Return type:

np.array

static filt_enc(encoder_in, window_size)#

Applies a bidirectional moving average filter to encoder data.

The filter processes data both forwards and backwards to minimize phase distortion, then averages both results for the final output.

Parameters:
  • encoder_in (ndarray) – Raw encoder input data (int32 values)

  • window_size (int) – Size of the moving average window

Returns:

Filtered encoder data (int32 values)

Return type:

np.ndarray

static lenz_cal(encoder_in, coarse_red)#

Main calibration routine for LENZ encoder compensation.

Performs a two-stage calibration process to generate a final compensation table.

Parameters:
  • encoder_in (ndarray) – Raw encoder input data

  • coarse_red (int) – Coarse reduction factor

Returns:

Final compensation table (int8 values)

Return type:

np.ndarray

static lenz_cal_motor_harm(encoder_in, coarse_red, motor_harm)#

LENZ encoder calibration with additional motor harmonic suppression.

Similar to lenz_cal method but includes motor harmonic removal in the compensation table.

Parameters:
  • encoder_in (ndarray) – Raw encoder input data

  • coarse_red (int) – Coarse reduction factor

  • motor_harm (int) – Motor harmonic frequency to suppress

Returns:

Final compensation table with motor harmonics removed (int8)

Return type:

np.ndarray