{"id":19267013,"url":"https://github.com/mljs/convolution","last_synced_at":"2025-04-21T19:32:29.416Z","repository":{"id":21271603,"uuid":"92033211","full_name":"mljs/convolution","owner":"mljs","description":"Convolution using the FFT or direct algorithm.","archived":false,"fork":false,"pushed_at":"2023-05-12T09:12:45.000Z","size":151,"stargazers_count":8,"open_issues_count":4,"forks_count":1,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-12T18:18:03.237Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mljs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-22T08:59:18.000Z","updated_at":"2022-11-29T12:23:54.000Z","dependencies_parsed_at":"2024-06-18T19:36:16.000Z","dependency_job_id":"cae30d74-f6db-43c4-86f3-4445e87bb055","html_url":"https://github.com/mljs/convolution","commit_stats":{"total_commits":34,"total_committers":4,"mean_commits":8.5,"dds":0.4411764705882353,"last_synced_commit":"3b4b0fcd2e708ce9585b34fd710f5e7960abf99a"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fconvolution","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fconvolution/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fconvolution/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fconvolution/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mljs","download_url":"https://codeload.github.com/mljs/convolution/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250120069,"owners_count":21378131,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T20:09:22.243Z","updated_at":"2025-04-21T19:32:29.409Z","avatar_url":"https://github.com/mljs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# convolution\n\n[![NPM version][npm-image]][npm-url]\n[![npm download][download-image]][download-url]\n\nConvolution using the FFT or direct algorithm.\n\n## Installation\n\n```console\nnpm install ml-convolution\n```\n\n## Usage\n\n### One execution\n\n```js\nimport { directConvolution, fftConvolution } from 'ml-convolution';\n\nconst input = [0, 1, 2, 3];\nconst kernel = [-1, 1, -1];\n\nconst outputDirect = directConvolution(input, kernel); // [-1, -1, -2, 1]\nconst outputFFT = fftConvolution(input, kernel); // [-1, -1, -2, 1]\n```\n\nThe functions both take an optional third argument to determine the way borders\nare processed. The default value, `CONSTANT`, will consider that the values out\nof the bounds are all 0. If it is set to `CUT`, borders will be ignored and the\nresult will be smaller than te input by `kernel.length - 1`:\n\n```js\nconst outputDirect = directConvolution(input, kernel, 'CUT'); // [-1, -2]\n```\n\n### Optimized, multiple executions\n\nIf you need to execute the convolution many times with the same kernel and input\nlength, you should consider instead to use the class-based API:\n\n```js\nimport { DirectConvolution, FFTConvolution } from 'ml-convolution';\n\n// const input = [0, 255, 255, 255, 255, 0, 0, 0];\nconst kernel = [0.1, 0.2, 0.3];\n\n// First parameter is the size of the inputs and allows to pre-allocate an array with the correct size\nconst direct = new DirectConvolution(8, kernel, 'CUT');\n\n// The convolve function mutates the same array at each execution\ndirect.convolve([0, 255, 255, 255, 255, 0, 0, 0]); // [ 127.5, 153, 153, 76.5, 25.5, 0 ]\ndirect.convolve([255, 0, 0, 255, 255, 255, 0, 0]); // [ 25.5, 76.5, 127.5, 153, 76.5, 25.5 ]\n\nconst fft = new FFTConvolution(8, kernel, 'CONSTANT');\nfft.convolve([0, 255, 255, 255, 255, 0, 0, 0]); // [ 76.5, 127.5, 153, 153, 76.5, 25.5, 0, 0 ]\nfft.convolve([255, 0, 0, 255, 255, 255, 0, 0]); // [ 51, 25.5, 76.5, 127.5, 153, 76.5, 25.5, 0 ]\n```\n\n### Benchmark\n\nWith small kernels, direct convolution is usually faster:  \nCurrent results suggest that from a kernel size around 64, FFT convolution should be used.\n\n| Data x Kernel | fft [ops/s] | direct [ops/s] |\n| ------------- | ----------- | -------------- |\n| 128 x 5       | 97889       | 569110         |\n| 128 x 11      | 99403       | 280271         |\n| 128 x 17      | 97686       | 181608         |\n| 128 x 33      | 94633       | 93847          |\n| 128 x 65      | 96585       | 49320          |\n| 128 x 129     | 97189       | 25346          |\n| 128 x 513     | 21771       | 6469           |\n| 512 x 5       | 20712       | 144025         |\n| 512 x 11      | 21134       | 73189          |\n| 512 x 17      | 21201       | 44320          |\n| 512 x 33      | 21037       | 23591          |\n| 512 x 65      | 21398       | 12405          |\n| 512 x 129     | 21514       | 6358           |\n| 512 x 513     | 21494       | 1618           |\n| 2048 x 5      | 4746        | 36360          |\n| 2048 x 11     | 4740        | 18422          |\n| 2048 x 17     | 4735        | 11248          |\n| 2048 x 33     | 4689        | 5927           |\n| 2048 x 65     | 4740        | 3100           |\n| 2048 x 129    | 4741        | 1591           |\n| 2048 x 513    | 4753        | 405            |\n| 4096 x 5      | 2068        | 18201          |\n| 4096 x 11     | 2062        | 9241           |\n| 4096 x 17     | 2071        | 5629           |\n| 4096 x 33     | 2069        | 2976           |\n| 4096 x 65     | 2079        | 1551           |\n| 4096 x 129    | 2074        | 797            |\n| 4096 x 513    | 2079        | 203            |\n| 16384 x 5     | 370         | 4036           |\n| 16384 x 11    | 371         | 2295           |\n| 16384 x 17    | 377         | 1390           |\n| 16384 x 33    | 374         | 748            |\n| 16384 x 65    | 370         | 389            |\n| 16384 x 129   | 375         | 199            |\n| 16384 x 513   | 376         | 51             |\n| 65536 x 5     | 70          | 991            |\n| 65536 x 11    | 70          | 541            |\n| 65536 x 17    | 70          | 351            |\n| 65536 x 33    | 69          | 186            |\n| 65536 x 65    | 71          | 97             |\n| 65536 x 129   | 71          | 50             |\n| 65536 x 513   | 70          | 13             |\n| 262144 x 5    | 10          | 247            |\n| 262144 x 11   | 10          | 135            |\n| 262144 x 17   | 10          | 88             |\n| 262144 x 33   | 10          | 47             |\n| 262144 x 65   | 10          | 24             |\n| 262144 x 129  | 10          | 12             |\n| 262144 x 513  | 10          | 3              |\n| 1048576 x 5   | 2           | 60             |\n| 1048576 x 11  | 2           | 32             |\n| 1048576 x 17  | 2           | 22             |\n| 1048576 x 33  | 2           | 12             |\n| 1048576 x 65  | 2           | 6              |\n| 1048576 x 129 | 2           | 3              |\n| 1048576 x 513 | 2           | 1              |\n\n## License\n\n[MIT](./LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/ml-convolution.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/ml-convolution\n[download-image]: https://img.shields.io/npm/dm/ml-convolution.svg?style=flat-square\n[download-url]: https://npmjs.org/package/ml-convolution\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmljs%2Fconvolution","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmljs%2Fconvolution","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmljs%2Fconvolution/lists"}