Skip to content

Commit ceaf29b

Browse files
🚧 progress(convolution): First draft.
1 parent 0a587dc commit ceaf29b

File tree

7 files changed

+10246
-11
lines changed

7 files changed

+10246
-11
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
Iterable convolution for JavaScript.
55
See [docs](https://iterable-iterator.github.io/convolution/index.html).
66

7-
> :building_construction: Caveat emptor! This is work in progress. Code may be
8-
> working. Documentation may be present. Coherence may be. Maybe.
9-
107
> :warning: Depending on your environment, the code may require
118
> `regeneratorRuntime` to be defined, for instance by importing
129
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
1310
11+
```js
12+
import {convolution} from '@iterable-iterator/convolution';
13+
14+
const moving_average = [1/4, 1/4, 1/4, 1/4];
15+
convolution(moving_average, signal); // ...
16+
17+
const first_derivative = [1, -1];
18+
convolution(first_derivative, signal); // ...
19+
20+
const second_derivative = [1, -2, 1];
21+
convolution(second_derivative, signal); // ...
22+
```
23+
1424
[![License](https://img.shields.io/github/license/iterable-iterator/convolution.svg)](https://raw.githubusercontent.com/iterable-iterator/convolution/main/LICENSE)
1525
[![Version](https://img.shields.io/npm/v/@iterable-iterator/convolution.svg)](https://www.npmjs.org/package/@iterable-iterator/convolution)
1626
[![Tests](https://img.shields.io/github/workflow/status/iterable-iterator/convolution/ci:test?event=push&label=tests)](https://github.com/iterable-iterator/convolution/actions/workflows/ci:test.yml?query=branch:main)

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,24 @@
5959
"release": "np --message ':hatching_chick: release: Bumping to v%s.'",
6060
"test": "ava"
6161
},
62-
"dependencies": {},
62+
"dependencies": {
63+
"@functional-abstraction/operator": "^1.0.4",
64+
"@iterable-iterator/chain": "^1.0.0",
65+
"@iterable-iterator/list": "^0.0.2",
66+
"@iterable-iterator/map": "^0.1.0",
67+
"@iterable-iterator/reduce": "^0.0.1",
68+
"@iterable-iterator/repeat": "^0.0.1",
69+
"@iterable-iterator/reversed": "^0.0.2",
70+
"@iterable-iterator/window": "^0.0.3",
71+
"@iterable-iterator/zip": "^0.0.2"
72+
},
6373
"devDependencies": {
6474
"@babel/core": "7.14.0",
6575
"@babel/preset-env": "7.14.0",
6676
"@babel/register": "7.13.16",
6777
"@commitlint/cli": "12.1.1",
78+
"@iterable-iterator/count": "^0.0.1",
79+
"@iterable-iterator/slice": "^0.0.1",
6880
"@js-library/commitlint-config": "0.0.4",
6981
"ava": "3.15.0",
7082
"babel-plugin-transform-remove-console": "6.9.4",

src/convolution.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {_window} from '@iterable-iterator/window';
2+
import {_chain} from '@iterable-iterator/chain';
3+
import {nrepeat} from '@iterable-iterator/repeat';
4+
import {list} from '@iterable-iterator/list';
5+
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';
10+
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+
};
19+
20+
export default convolution;

src/index.js

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

test/src/api.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/src/convolution.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import test from 'ava';
2+
3+
import {list} from '@iterable-iterator/list';
4+
import {count} from '@iterable-iterator/count';
5+
import {take} from '@iterable-iterator/slice';
6+
import {convolution} from '../../src/index.js';
7+
8+
test('https://betterexplained.com/articles/intuitive-convolution/#Part_1_Hospital_Analogy single-day treatment', (t) => {
9+
const signal = [1, 2, 3, 4, 5];
10+
const kernel = [3];
11+
const result = list(convolution(kernel, signal));
12+
const expected = [3, 6, 9, 12, 15];
13+
t.deepEqual(result, expected);
14+
});
15+
16+
test('https://betterexplained.com/articles/intuitive-convolution/#Part_1_Hospital_Analogy multi-day treatment', (t) => {
17+
const signal = [1, 2, 3, 4, 5];
18+
const kernel = [3, 2, 1];
19+
const result = list(convolution(kernel, signal));
20+
const expected = [3, 8, 14, 20, 26, 14, 5];
21+
t.deepEqual(result, expected);
22+
});
23+
24+
test('derivative of unit slope', (t) => {
25+
const signal = count();
26+
const kernel = [1, -1];
27+
const result = list(take(convolution(kernel, signal), 5));
28+
const expected = [0, 1, 1, 1, 1];
29+
t.deepEqual(result, expected);
30+
});

0 commit comments

Comments
 (0)