Skip to content

Commit d9bb1ab

Browse files
alex-buchananAlexBpre-commit-ci[bot]cclauss
authored
added convolution (#11202)
* added convolution * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed ruff issues * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixing ruff errors * addressing automated concerns * addressing automated concerns * addressing automated concerns * addressing automated concerns * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * addressing automated concerns * updating DIRECTORY.md * Apply suggestion from @cclauss --------- Co-authored-by: AlexB <acbucha@clemson.edu> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 92af64a commit d9bb1ab

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@
724724
* [Collatz Sequence](maths/collatz_sequence.py)
725725
* [Combinations](maths/combinations.py)
726726
* [Continued Fraction](maths/continued_fraction.py)
727+
* [Convolve 1D](maths/convolve_1d.py)
727728
* [Decimal Isolate](maths/decimal_isolate.py)
728729
* [Decimal To Fraction](maths/decimal_to_fraction.py)
729730
* [Derangement](maths/derangement.py)

maths/convolve_1d.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from dataclasses import dataclass, field
2+
from math import floor
3+
4+
# discrete_convolution
5+
"""
6+
* Calculate the discrete convolution of two
7+
linear discrete sets
8+
https://en.wikipedia.org/wiki/Convolution
9+
"""
10+
11+
12+
@dataclass
13+
class Signal:
14+
"""
15+
A discrete representation of a signal as a n-dimensional vector
16+
17+
>>> Signal([1.0,3.0,2.0,-1.0])
18+
Signal(signal=[1.0, 3.0, 2.0, -1.0], n=4)
19+
"""
20+
21+
signal: list[float] = field(default_factory=list)
22+
n: int = 0
23+
24+
def __post_init__(self) -> None:
25+
for i in self.signal:
26+
if not isinstance(i, (float, int)):
27+
raise TypeError("vector must be a list of numeric values.")
28+
else:
29+
self.n += 1
30+
31+
32+
@dataclass
33+
class DiscreteConvolve1D:
34+
"""
35+
1D discrete convolution between two linear signals
36+
37+
>>> s1 = Signal([1,2,3,4,5])
38+
>>> s2 = Signal([1,-1,2,-3])
39+
>>> DiscreteConvolve1D(s1,s2) # doctest: +NORMALIZE_WHITESPACE
40+
DiscreteConvolve1D(kern=Signal(signal=[1, 2, 3, 4, 5], n=5),
41+
sig=Signal(signal=[1, -1, 2, -3], n=4))
42+
"""
43+
44+
kern: Signal = field(default_factory=Signal)
45+
sig: Signal = field(default_factory=Signal)
46+
47+
@property
48+
def convolve_1d(self) -> Signal:
49+
conv = Signal()
50+
for i in range(self.sig.n):
51+
conv.signal.append(0)
52+
for j in range(self.kern.n):
53+
if (
54+
i + j - floor(self.kern.n / 2) < 0
55+
or i + j - floor(self.kern.n / 2) >= self.sig.n
56+
):
57+
sig_val = 0.0
58+
else:
59+
sig_val = float(self.sig.signal[i + j - floor(self.kern.n / 2)])
60+
conv.signal[i] += self.kern.signal[j] * sig_val
61+
conv.n += 1
62+
return conv

0 commit comments

Comments
 (0)