sigpyproc.core.kernels#

Numba kernel functions for the core of sigpyproc.

This module contains kernel functions for the core of sigpyproc.

Functions

add_online_moments

circular_pad_goodsize

compute_online_moments

Compute central moments in one pass through the data.

compute_online_moments_basic

Compute central moments in one pass through the data.

convolve_templates

Convolve the data with the templates in the template bank.

dedisperse

dedisperse_par

detrend_1d

Detrend a 1D array using a linear fit.

detrend_2d

Detrend a 2D array using a linear fit.

disperse_block

Roll the 2D array along the second axis.

dmt_block

dmt_block_valid

downsample_1d_mean

Downsample a 1D array by averaging over bins.

downsample_1d_mean_par

Downsample a 1D array by averaging over bins.

downsample_2d_mean_flat

Downsample a flattened 2D array by averaging over bins in both dimensions.

downsample_2d_mean_par

Downsample a flattened 2D array by averaging over bins in both dimensions.

extract_bpass

extract_bpass_par

extract_tim

extract_tim_par

fftconvolve

Convolve two 1D arrays using FFT in mode "full".

fold

form_interp_mspec

form_mspec

fs_running_median

invert_freq

invert_freq_par

mask_channels

mask_channels_par

nb_fft

nb_fft_good_size

Get the good size for FFT.

nb_ifft

nb_irfft

nb_rfft

Compute the 1D FFT of a real array.

nb_roll

Roll array elements along a given axis.

normalize_template

Normalize the template to have zero mean and unit power.

pack1_8_big

pack1_8_little

pack1_8_vect

pack2_8_big

pack2_8_little

pack4_8_big

pack4_8_little

remove_zerodm

Remove zero DM from an input array.

remove_zerodm_par

Remove zero DM from an input array.

resample_tim

roll_block

Roll each row of a 2D array along columns by per-row shifts.

roll_block_valid

Roll each row of a 2D array by per-row shifts, keeping only valid columns.

simulate_ism

Convolve the input signal with the ISM effects.

subband

sum_harmonics

unpack1_8_big

unpack1_8_little

unpack2_8_big

unpack2_8_little

unpack4_8_big

unpack4_8_little

update_moments

update_moments_basic

sigpyproc.core.kernels.add_online_moments(a, b, c)#
sigpyproc.core.kernels.circular_pad_goodsize(arr)#
sigpyproc.core.kernels.compute_online_moments(array, moments, startflag=0)#

Compute central moments in one pass through the data.

sigpyproc.core.kernels.compute_online_moments_basic(array, moments, startflag=0)#

Compute central moments in one pass through the data.

sigpyproc.core.kernels.convolve_templates(data, temp_bank, ref_bin)#

Convolve the data with the templates in the template bank.

Parameters:
datanp.ndarray

Input data array.

temp_banklist[np.ndarray]

List of template arrays.

ref_binlist[int]

List of reference bin indices.

Returns:
np.ndarray

Convolved array.

Notes

The reference bin is aligned to the index 0 and the template is time-reversed (to perform convolution rather than correlation). The template is then normalised to zero mean and unity power.

sigpyproc.core.kernels.dedisperse(in_arr, out_arr, delays, nchans, nsamps, out_start_index)#
sigpyproc.core.kernels.dedisperse_par(in_arr, out_arr, delays, nchans, nsamps, out_start_index)#
sigpyproc.core.kernels.detrend_1d(arr)#

Detrend a 1D array using a linear fit.

Similar to scipiy.signal.detrend. Currently for 1d arrays only.

Parameters:
arrnp.ndarray

Input 1D array.

Returns:
np.ndarray

Detrended array.

Raises:
ValueError

If the input array is empty.

sigpyproc.core.kernels.detrend_2d(arr)#

Detrend a 2D array using a linear fit.

Parameters:
arrnp.ndarray

Input 2D array.

Returns:
np.ndarray

Detrended array.

sigpyproc.core.kernels.disperse_block(arr, shifts, nsamps_out=1, tfactor=1)#

Roll the 2D array along the second axis.

Parameters:
arrnp.ndarray

Input 2D array.

shiftsnp.ndarray

Array of shifts for each row.

nsamps_outint, optional

Desired minimum number of samples in the output, by default 1.

tfactorint, optional

Time factor to decimate the output, by default 1.

Returns:
np.ndarray

Dispersed 2D array.

sigpyproc.core.kernels.dmt_block(arr, dm_delays)#
sigpyproc.core.kernels.dmt_block_valid(arr, dm_delays)#
sigpyproc.core.kernels.downsample_1d_mean(array, factor)#

Downsample a 1D array by averaging over bins.

Parameters:
arrayndarray

Input 1D array to be downsampled.

factorint

Downsampling factor. Must be a positive integer.

Returns:
ndarray

Downsampled array

Notes

Uses float64 accumulator to avoid overflow.

sigpyproc.core.kernels.downsample_1d_mean_par(array, factor)#

Downsample a 1D array by averaging over bins.

Parameters:
arrayndarray

Input 1D array to be downsampled.

factorint

Downsampling factor. Must be a positive integer.

Returns:
ndarray

Downsampled array

Notes

Uses float64 accumulator to avoid overflow.

sigpyproc.core.kernels.downsample_2d_mean_flat(array, factor1, factor2, dim1, dim2)#

Downsample a flattened 2D array by averaging over bins in both dimensions.

Parameters:
arraynp.ndarray

Input flattened 2D array to be downsampled.

factor1int

Downsampling factor for the first dimension. Must be a positive integer.

factor2int

Downsampling factor for the second dimension. Must be a positive integer.

dim1int

Number of bins in the first dimension.

dim2int

Number of bins in the second dimension.

Returns:
np.ndarray

Downsampled flattened 2D array

Notes

dim2 must ve the fastest varying dimension.

sigpyproc.core.kernels.downsample_2d_mean_par(array, factor1, factor2, dim1, dim2)#

Downsample a flattened 2D array by averaging over bins in both dimensions.

Parameters:
arraynp.ndarray

Input flattened 2D array to be downsampled.

factor1int

Downsampling factor for the first dimension. Must be a positive integer.

factor2int

Downsampling factor for the second dimension. Must be a positive integer.

dim1int

Number of bins in the first dimension.

dim2int

Number of bins in the second dimension.

Returns:
np.ndarray

Downsampled flattened 2D array

Notes

dim2 must ve the fastest varying dimension.

sigpyproc.core.kernels.extract_bpass(in_arr, out_arr, nchans, nsamps)#
sigpyproc.core.kernels.extract_bpass_par(in_arr, out_arr, nchans, nsamps)#
sigpyproc.core.kernels.extract_tim(in_arr, out_arr, nchans, nsamps, out_start_index)#
sigpyproc.core.kernels.extract_tim_par(in_arr, out_arr, nchans, nsamps, out_start_index)#
sigpyproc.core.kernels.fftconvolve(in1, in2)#

Convolve two 1D arrays using FFT in mode “full”.

Parameters:
in1np.ndarray

First input array.

in2np.ndarray

Second input array.

Returns:
np.ndarray

Convolved array in mode “full”.

Notes

Return full discrete linear convolution of in1 and in2.

sigpyproc.core.kernels.fold(inarray, fold_ar, count_ar, delays, maxdelay, tsamp, period, accel, total_nsamps, nsamps, nchans, nbins, nints, nsubs, index)#
sigpyproc.core.kernels.form_interp_mspec(fspec)#
sigpyproc.core.kernels.form_mspec(fspec)#
sigpyproc.core.kernels.fs_running_median(spec_arr, start_width, end_width, end_freq_bin)#
sigpyproc.core.kernels.invert_freq(in_arr, nchans, nsamps)#
sigpyproc.core.kernels.invert_freq_par(in_arr, nchans, nsamps)#
sigpyproc.core.kernels.mask_channels(in_arr, chan_mask, mask_value, nchans, nsamps)#
sigpyproc.core.kernels.mask_channels_par(in_arr, chan_mask, mask_value, nchans, nsamps)#
sigpyproc.core.kernels.nb_fft(arr, n=None)#
sigpyproc.core.kernels.nb_fft_good_size(n, real=False)#

Get the good size for FFT.

Parameters:
nint

Input size.

realbool, optional

If True, the input is real, by default False

Returns:
int

Good size for FFT.

sigpyproc.core.kernels.nb_ifft(arr, n=None)#
sigpyproc.core.kernels.nb_irfft(arr, n=None)#
sigpyproc.core.kernels.nb_rfft(arr, n=None)#

Compute the 1D FFT of a real array.

Parameters:
arrnp.ndarray

Input array (real).

nint | None, optional

Length of the FFT, by default None

Returns:
np.ndarray

Real part of the FFT.

sigpyproc.core.kernels.nb_roll(arr, shift, axis=None)#

Roll array elements along a given axis.

This is a Numba-compiled wrapper around numpy.roll(), implemented via rocket_fft to support the axis argument.

Parameters:
arrndarray

Input array.

shiftint | tuple[int, …]

Number of positions to shift. Positive shifts right/down, negative left/up. If a tuple, must match the length of axis.

axisint | tuple[int, …] | None, optional

Axis or axes to roll along. If None, flattens array and rolls all elements.

Returns:
np.ndarray

Rolled array with the same shape as arr.

sigpyproc.core.kernels.normalize_template(arr)#

Normalize the template to have zero mean and unit power.

Parameters:
arrnp.ndarray

Template array.

Returns:
np.ndarray

Normalized template array.

sigpyproc.core.kernels.pack1_8_big(array, packed)#
sigpyproc.core.kernels.pack1_8_little(array, packed)#
sigpyproc.core.kernels.pack1_8_vect(array, packed, *, big_endian=True)#
sigpyproc.core.kernels.pack2_8_big(array, packed)#
sigpyproc.core.kernels.pack2_8_little(array, packed)#
sigpyproc.core.kernels.pack4_8_big(array, packed)#
sigpyproc.core.kernels.pack4_8_little(array, packed)#
sigpyproc.core.kernels.remove_zerodm(in_arr, out_arr, bpass, nchans, nsamps)#

Remove zero DM from an input array.

Parameters:
in_arrnp.ndarray

Input 2D flattened array to remove zero DM from.

out_arrnp.ndarray

Output 2D flattened array with zero DM removed.

bpassnp.ndarray

Bandpass to be added back to the data.

nchansint

Number of frequency channels in the 2D data.

nsampsint

Number of samples in the 2D data.

sigpyproc.core.kernels.remove_zerodm_par(in_arr, out_arr, bpass, nchans, nsamps)#

Remove zero DM from an input array.

Parameters:
in_arrnp.ndarray

Input 2D flattened array to remove zero DM from.

out_arrnp.ndarray

Output 2D flattened array with zero DM removed.

bpassnp.ndarray

Bandpass to be added back to the data.

nchansint

Number of frequency channels in the 2D data.

nsampsint

Number of samples in the 2D data.

sigpyproc.core.kernels.resample_tim(array, accel, tsamp)#
sigpyproc.core.kernels.roll_block(arr, shifts)#

Roll each row of a 2D array along columns by per-row shifts.

Applies a circular shift to each row independently, wrapping elements around the column axis. Positive shifts move elements right, negative shifts move left.

Parameters:
arrnp.ndarray

Input 2D array of shape (nrows, ncols).

shiftsnp.ndarray

1D array of integer shifts, length equal to nrows. Can be positive or negative.

Returns:
np.ndarray

Rolled 2D array with the same shape as arr.

Raises:
ValueError

If arr is not 2D or shifts length does not match number of rows.

sigpyproc.core.kernels.roll_block_valid(arr, shifts)#

Roll each row of a 2D array by per-row shifts, keeping only valid columns.

Similar to roll_block but only keeps the valid region where no wrapping occurs. Positive shifts move elements right, negative shifts move elements left.

Parameters:
arrnp.ndarray

Input 2D array of shape (nrows, ncols).

shiftsnp.ndarray

1D array of integer shifts, length equal to nrows. Can be positive or negative.

Returns:
np.ndarray

Rolled 2D array with shape (nrows, ncols - shift_range).

Raises:
ValueError

If arr is not 2D or shifts length doesn’t match number of rows. If the shift range exceeds the number of columns.

sigpyproc.core.kernels.simulate_ism(signal, spectrum, dm_smear, tau_nus, over_sampling=1)#

Convolve the input signal with the ISM effects.

Parameters:
signalnp.ndarray

1D pulse profile template.

spectrumnp.ndarray

Spectrum of the pulse profile (length = number of frequency channels).

dm_smearnp.ndarray

DM smearing (in samples) for each frequency channel.

tau_nusnp.ndarray

Scattering timescale (in samples) for each frequency channel.

over_samplingint, optional

Oversampling factor for higher time resolution, by default 1.

Returns:
np.ndarray

Convolved 2D pulse dynamic spectrum (nchans x nsamps).

Notes

The function performs the following steps for each frequency channel: - Replicate the signal across all frequency channels. - Apply DM smearing via convolution with a boxcar function. - Apply scattering via convolution with an exponential decay function. - Normalize the scattering kernel to maintain the signal area.

sigpyproc.core.kernels.subband(inarray, outarray, delays, chan_to_sub, maxdelay, nchans, nsubs, nsamps)#
sigpyproc.core.kernels.sum_harmonics(pow_spec, nfolds)#
sigpyproc.core.kernels.unpack1_8_big(array, unpacked)#
sigpyproc.core.kernels.unpack1_8_little(array, unpacked)#
sigpyproc.core.kernels.unpack2_8_big(array, unpacked)#
sigpyproc.core.kernels.unpack2_8_little(array, unpacked)#
sigpyproc.core.kernels.unpack4_8_big(array, unpacked)#
sigpyproc.core.kernels.unpack4_8_little(array, unpacked)#
sigpyproc.core.kernels.update_moments(val, m1, m2, m3, m4, n)#
sigpyproc.core.kernels.update_moments_basic(val, m1, m2, n)#