Skip to content

Commit 14bf4aa

Browse files
♻️ refactor(convolution): Extract generic implementation.
1 parent ceaf29b commit 14bf4aa

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

src/_convolution.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {_window} from '@iterable-iterator/window';
2+
import {_chain} from '@iterable-iterator/chain';
3+
import {nrepeat} from '@iterable-iterator/repeat';
4+
import {map, starmap} from '@iterable-iterator/map';
5+
import {_zip2} from '@iterable-iterator/zip';
6+
import {reduce} from '@iterable-iterator/reduce';
7+
8+
/**
9+
* _convolution.
10+
*
11+
* @param {Array} rKernel
12+
* @param {Iterable} signal
13+
* @return {IterableIterator}
14+
*/
15+
const _convolution = (rKernel, signal, {add, mul, zero}) => {
16+
const n = rKernel.length;
17+
return map(
18+
(slidingWindow) => reduce(add, starmap(mul, _zip2(rKernel, slidingWindow))),
19+
_window(n, _chain([nrepeat(zero, n - 1), signal, nrepeat(zero, n - 1)])),
20+
);
21+
};
22+
23+
export default _convolution;

src/convolution.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
import {_window} from '@iterable-iterator/window';
2-
import {_chain} from '@iterable-iterator/chain';
3-
import {nrepeat} from '@iterable-iterator/repeat';
41
import {list} from '@iterable-iterator/list';
52
import {reversed} from '@iterable-iterator/reversed';
6-
import {map, starmap} from '@iterable-iterator/map';
7-
import {mul} from '@functional-abstraction/operator';
8-
import {_zip2} from '@iterable-iterator/zip';
9-
import {sum} from '@iterable-iterator/reduce';
3+
import {add, mul} from '@functional-abstraction/operator';
104

11-
const convolution = (kernel, signal) => {
12-
const rKernel = list(reversed(kernel));
13-
const n = rKernel.length;
14-
return map(
15-
(slidingWindow) => sum(starmap(mul, _zip2(rKernel, slidingWindow))),
16-
_window(n, _chain([nrepeat(0, n - 1), signal, nrepeat(0, n - 1)])),
17-
);
18-
};
5+
import _convolution from './_convolution.js';
6+
7+
/**
8+
* Convolution.
9+
*
10+
* @param {Iterable} kernel
11+
* @param {Iterable} signal
12+
* @return {IterableIterator}
13+
*/
14+
const convolution = (kernel, signal) =>
15+
_convolution(list(reversed(kernel)), signal, {add, mul, zero: 0});
1916

2017
export default convolution;

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export {default as _convolution} from './_convolution.js';
12
export {default as convolution} from './convolution.js';

0 commit comments

Comments
 (0)