-
-
Notifications
You must be signed in to change notification settings - Fork 51k
added convolution #11202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
added convolution #11202
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
da29403
added convolution
ed7131c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1804366
fixed ruff issues
60b97b9
fixing merge errors
bfa893b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b2e5c95
fixing ruff errors
a137c88
Merge branch 'Convolution' of github.com:alex-buchanan/Python into Co…
a0863de
addressing automated concerns
dd1c827
addressing automated concerns
09031e8
addressing automated concerns
4108595
addressing automated concerns
c7c8e6a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8e8df9a
addressing automated concerns
ff7ed93
Merge branch 'Convolution' of github.com:alex-buchanan/Python into Co…
f5335a9
Merge branch 'master' into Convolution
cclauss ce34eb9
updating DIRECTORY.md
cclauss d69eb34
Apply suggestion from @cclauss
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
| 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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__