@audio/spectral-slope 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.
- package/README.md +5 -0
- package/package.json +30 -0
- package/slope.js +12 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@audio/spectral-slope",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Spectral slope — least-squares linear regression of magnitude over frequency. Peeters 2004 §6.6.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "slope.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./slope.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"slope.js"
|
|
14
|
+
],
|
|
15
|
+
"keywords": [
|
|
16
|
+
"audio",
|
|
17
|
+
"dsp",
|
|
18
|
+
"spectral",
|
|
19
|
+
"features",
|
|
20
|
+
"slope"
|
|
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/slope.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Spectral slope — least-squares linear regression of magnitude over frequency. Peeters 2004 §6.6.
|
|
2
|
+
export default function slope (mag, { fs = 44100, n = 2 * (mag.length - 1) } = {}) {
|
|
3
|
+
let N = mag.length
|
|
4
|
+
if (N < 2) return 0
|
|
5
|
+
let sf = 0, sm = 0, sfm = 0, sff = 0
|
|
6
|
+
for (let k = 0; k < N; k++) {
|
|
7
|
+
let f = k * fs / n
|
|
8
|
+
sf += f; sm += mag[k]; sfm += f * mag[k]; sff += f * f
|
|
9
|
+
}
|
|
10
|
+
let den = N * sff - sf * sf
|
|
11
|
+
return den !== 0 ? (N * sfm - sf * sm) / den : 0
|
|
12
|
+
}
|