Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@
* [Collatz Sequence](maths/collatz_sequence.py)
* [Combinations](maths/combinations.py)
* [Continued Fraction](maths/continued_fraction.py)
* [Convolve 1D](maths/convolve_1d.py)
* [Decimal Isolate](maths/decimal_isolate.py)
* [Decimal To Fraction](maths/decimal_to_fraction.py)
* [Dodecahedron](maths/dodecahedron.py)
Expand Down Expand Up @@ -876,6 +877,7 @@
* [Two Pointer](maths/two_pointer.py)
* [Two Sum](maths/two_sum.py)
* [Volume](maths/volume.py)
* [Weighted Average](maths/weighted_average.py)
* [Zellers Congruence](maths/zellers_congruence.py)

## [Matrix](matrix)
Expand Down
62 changes: 62 additions & 0 deletions maths/convolve_1d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from dataclasses import dataclass, field
from math import floor

# discrete_convolution
"""
* Calculate the discrete convolution of two
linear discrete sets
https://en.wikipedia.org/wiki/Convolution
"""


@dataclass
class Signal:
"""
A discrete representation of a signal as a n-dimensional vector

>>> Signal([1.0,3.0,2.0,-1.0])
Signal(signal=[1.0, 3.0, 2.0, -1.0], n=4)
"""

signal: list[float] = field(default_factory=list)
n: int = 0

def __post_init__(self) -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file maths/convolve_1d.py, please provide doctest for the function __post_init__

for i in self.signal:
if not isinstance(i, (float, int)):
raise TypeError("vector must be a list of numeric values.")
else:
self.n += 1


@dataclass
class DiscreteConvolve1D:
"""
1D discrete convolution between two linear signals

>>> s1 = Signal([1,2,3,4,5])
>>> s2 = Signal([1,-1,2,-3])
>>> DiscreteConvolve1D(s1,s2) # doctest: +NORMALIZE_WHITESPACE
DiscreteConvolve1D(kern=Signal(signal=[1, 2, 3, 4, 5], n=5),
sig=Signal(signal=[1, -1, 2, -3], n=4))
"""

kern: Signal = field(default_factory=Signal)
sig: Signal = field(default_factory=Signal)

@property
def convolve_1d(self) -> Signal:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file maths/convolve_1d.py, please provide doctest for the function convolve_1d

conv = Signal()
for i in range(self.sig.n):
conv.signal.append(0)
for j in range(self.kern.n):
if (
i + j - floor(self.kern.n / 2) < 0
or i + j - floor(self.kern.n / 2) >= self.sig.n
):
sig_val = 0.0
else:
sig_val = float(self.sig.signal[i + j - floor(self.kern.n / 2)])
conv.signal[i] += self.kern.signal[j] * sig_val
conv.n += 1
return conv
Loading