@audio/spectral-spread 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +5 -0
  2. package/package.json +30 -0
  3. package/spread.js +9 -0
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @audio/spectral-spread
2
+
3
+ > Spectral spread — variance around the spectral centroid
4
+
5
+ Planned — not implemented yet. See the umbrella README for status and sources.
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@audio/spectral-spread",
3
+ "version": "1.0.0",
4
+ "description": "Spectral spread — magnitude-weighted standard deviation around the centroid (Hz). Peeters 2004 §6.1.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "spread.js",
8
+ "exports": {
9
+ ".": "./spread.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "spread.js"
14
+ ],
15
+ "keywords": [
16
+ "audio",
17
+ "dsp",
18
+ "spectral",
19
+ "features",
20
+ "spread"
21
+ ],
22
+ "license": "MIT",
23
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
24
+ "engines": {
25
+ "node": ">=18"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ }
30
+ }
package/spread.js ADDED
@@ -0,0 +1,9 @@
1
+ // Spectral spread — magnitude-weighted standard deviation around the centroid (Hz). Peeters 2004 §6.1.
2
+ export default function spread (mag, { fs = 44100, n = 2 * (mag.length - 1) } = {}) {
3
+ let num = 0, den = 0
4
+ for (let k = 0; k < mag.length; k++) { num += (k * fs / n) * mag[k]; den += mag[k] }
5
+ if (den <= 0) return 0
6
+ let c = num / den, v = 0
7
+ for (let k = 0; k < mag.length; k++) { let d = k * fs / n - c; v += d * d * mag[k] }
8
+ return Math.sqrt(v / den)
9
+ }