|
1 | | -from typing import Any |
| 1 | +from typing import Any, Literal |
2 | 2 |
|
3 | 3 | import numpy as np |
4 | 4 | import numpy.typing as npt |
@@ -118,6 +118,60 @@ def sampling_rate_expand( |
118 | 118 | return output |
119 | 119 |
|
120 | 120 |
|
| 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 | + |
121 | 175 | def fourier_transform( |
122 | 176 | waveform: npt.ArrayLike, |
123 | 177 | time_step: float, |
|
0 commit comments