Skip to content

Commit 4c1bb40

Browse files
committed
feat: implement convolve function
1 parent cc04948 commit 4c1bb40

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

site/toc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Utilities:
103103
- int_to_bits
104104
- sampling_rate_compress
105105
- sampling_rate_expand
106+
- convolve
106107
- fourier_transform
107108
- boxplus
108109
- gaussian_q

src/komm/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
from ._util.bit_operations import bits_to_int, int_to_bits
126126
from ._util.information_theory import binary_entropy, binary_entropy_inv, entropy, relative_entropy
127127
from ._util.sequences import autocorrelation, cyclic_autocorrelation
128-
from ._util.signal_processing import fourier_transform, sampling_rate_compress, sampling_rate_expand
128+
from ._util.signal_processing import convolve, fourier_transform, sampling_rate_compress, sampling_rate_expand
129129
from ._util.special_functions import boxplus, gaussian_q, gaussian_q_inv, marcum_q
130130

131131
# isort: on
@@ -241,6 +241,7 @@
241241
"binary_entropy_inv",
242242
"entropy",
243243
"relative_entropy",
244+
"convolve",
244245
"fourier_transform",
245246
"sampling_rate_compress",
246247
"sampling_rate_expand",

src/komm/_util/signal_processing.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any
1+
from typing import Any, Literal
22

33
import numpy as np
44
import numpy.typing as npt
@@ -118,6 +118,60 @@ def sampling_rate_expand(
118118
return output
119119

120120

121+
def convolve(
122+
input1: npt.ArrayLike,
123+
input2: npt.ArrayLike,
124+
axis: int = -1,
125+
mode: Literal["full", "valid", "same"] = "full",
126+
):
127+
r"""
128+
Convolves two signals. This is like [`numpy.convolve`](https://numpy.org/doc/stable/reference/generated/numpy.convolve.html), but one of the inputs may be multidimensional (the other input must still be $1$-dimensional). The convolution is applied independently to each array along a specified axis.
129+
130+
Note:
131+
This is a simple wrapper around `numpy.convolve`.
132+
133+
Parameters:
134+
input1: The first input array.
135+
136+
input2: The second input array.
137+
138+
axis: The axis along which the convolution is applied in the multidimensional input. Default is the last axis.
139+
140+
mode: Convolution mode (same as `numpy.convolve`).
141+
142+
Examples:
143+
>>> komm.convolve([1, 1], [1, 1])
144+
array([1, 2, 1])
145+
146+
>>> komm.convolve([1, 1], [[1, 1, 0], [0, 1, 1]])
147+
array([[1, 2, 1, 0],
148+
[0, 1, 2, 1]])
149+
150+
>>> komm.convolve([[1, 1, 0], [0, 1, 1]], [1, 1])
151+
array([[1, 2, 1, 0],
152+
[0, 1, 2, 1]])
153+
154+
>>> komm.convolve([1, 1], [[1, 1, 0], [0, 1, 1]], axis=0)
155+
array([[1, 1, 0],
156+
[1, 2, 1],
157+
[0, 1, 1]])
158+
159+
>>> komm.convolve([[1, 1]], [[1, 1]])
160+
Traceback (most recent call last):
161+
...
162+
ValueError: at least one of the inputs must be 1-dimensional
163+
"""
164+
input1 = np.asarray(input1)
165+
input2 = np.asarray(input2)
166+
if input1.ndim == 1:
167+
x, arr = input1, input2
168+
elif input2.ndim == 1:
169+
x, arr = input2, input1
170+
else:
171+
raise ValueError("at least one of the inputs must be 1-dimensional")
172+
return np.apply_along_axis(lambda y: np.convolve(x, y, mode), axis, arr)
173+
174+
121175
def fourier_transform(
122176
waveform: npt.ArrayLike,
123177
time_step: float,

0 commit comments

Comments
 (0)