hypnosound 1.9.0 → 1.11.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/.eslintrc CHANGED
@@ -1,12 +1,12 @@
1
- {
2
- "parserOptions": {
3
- "ecmaVersion": 2024,
4
- "sourceType": "module"
5
- },
6
- "plugins": [
7
- "prettier"
8
- ],
9
- "rules": {
10
- "prettier/prettier": "warn"
11
- }
12
- }
1
+ {
2
+ "parserOptions": {
3
+ "ecmaVersion": 2024,
4
+ "sourceType": "module"
5
+ },
6
+ "plugins": [
7
+ "prettier"
8
+ ],
9
+ "rules": {
10
+ "prettier/prettier": "warn"
11
+ }
12
+ }
@@ -0,0 +1,44 @@
1
+ name: Publish to npm
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ paths: [package.json]
7
+
8
+ jobs:
9
+ publish:
10
+ runs-on: ubuntu-latest
11
+ permissions:
12
+ contents: read
13
+ id-token: write
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+
17
+ - uses: actions/setup-node@v4
18
+ with:
19
+ node-version: 20
20
+ registry-url: https://registry.npmjs.org
21
+
22
+ - name: Update npm
23
+ run: npm install -g npm@latest
24
+
25
+ - name: Check if version changed
26
+ id: version-check
27
+ run: |
28
+ LOCAL_VERSION=$(node -p "require('./package.json').version")
29
+ NPM_VERSION=$(npm view hypnosound version 2>/dev/null || echo "0.0.0")
30
+ echo "local=$LOCAL_VERSION" >> "$GITHUB_OUTPUT"
31
+ echo "npm=$NPM_VERSION" >> "$GITHUB_OUTPUT"
32
+ if [ "$LOCAL_VERSION" != "$NPM_VERSION" ]; then
33
+ echo "changed=true" >> "$GITHUB_OUTPUT"
34
+ else
35
+ echo "changed=false" >> "$GITHUB_OUTPUT"
36
+ fi
37
+
38
+ - name: Publish
39
+ if: steps.version-check.outputs.changed == 'true'
40
+ run: npm publish --provenance --access public
41
+
42
+ - name: Skip
43
+ if: steps.version-check.outputs.changed == 'false'
44
+ run: echo "Version ${{ steps.version-check.outputs.local }} already published, skipping"
package/.nvmrc CHANGED
@@ -1 +1 @@
1
- v21.6.1
1
+ v21.6.1
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 hypnodroid
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 hypnodroid
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Proposed Improvements
2
+
3
+ ## 1. Consistent Output Normalization (0-1)
4
+
5
+ Different features currently use wildly different scales: `energy` divides by 1000 (can exceed 1.0), `spectralCrest` is multiplied by 100, `spectralFlux` and `spectralRoughness` divide by 100,000. This makes features hard to compare or feed into downstream systems (ML, visualizations). Standardize all features to the 0-1 range.
6
+
7
+ ## 2. Configurable Sample Rate
8
+
9
+ 44100 Hz is hardcoded across `bass.js`, `mids.js`, `treble.js`, and `pitchClass.js`. Browsers commonly use 48000 Hz, and pro audio uses 96000 Hz. Accept `sampleRate` as an optional parameter in all frequency-dependent features.
10
+
11
+ ## 3. Float32Array Input Support
12
+
13
+ The library assumes `Uint8Array` input from `getByteFrequencyData()`. But `getFloatFrequencyData()` returns `Float32Array` with dB values offering higher precision, and offline analysis pipelines also produce float data. Detect input type and handle both automatically.
14
+
15
+ ## 4. Configurable Frequency Bands
16
+
17
+ `bass` (0-400 Hz), `mids` (400-4000 Hz), and `treble` (4000-20000 Hz) have hardcoded ranges. Add a generic `bandEnergy(fft, lowHz, highHz, sampleRate?)` function, and make bass/mids/treble thin wrappers over it.
18
+
19
+ ## 5. RMS and dBFS
20
+
21
+ The library has `energy` (sum of squares) but no perceptual loudness measures. RMS (root-mean-square amplitude) and dBFS (decibels relative to full scale) are fundamental for audio metering and level detection. Both are cheap to compute and widely useful.
22
+
23
+ **Status: Implemented**
24
+
25
+ ## 6. Onset / Transient Detection
26
+
27
+ `spectralFlux` is already computed but the library doesn't surface onset detection - the most common real-time analysis use case (beat detection, rhythm sync). Combine spectral flux with adaptive thresholding using the existing stats infrastructure.
28
+
29
+ ## 7. Chromagram / Pitch Class Distribution
30
+
31
+ `pitchClass` returns only the dominant pitch class. For harmonic analysis, chord detection, or key detection, users need the full distribution across all 12 pitch classes. Add `chromagram(fft)` returning energy per pitch class.
32
+
33
+ ## 8. Configurable Stats History Window
34
+
35
+ `makeCalculateStats()` defaults to 500 samples. At 60 fps that's ~8 seconds; at 10 fps it's ~50 seconds. Expose `historySize` as a constructor option on `AudioProcessor`.
36
+
37
+ ## 9. Spectral Flatness (Tonality Coefficient)
38
+
39
+ Distinguishes tonal (music) from noisy (percussion, speech) content. Geometric mean / arithmetic mean of the power spectrum. Returns 0 (pure tone) to 1 (white noise).
40
+
41
+ ## 10. Zero-Crossing Rate (Time Domain)
42
+
43
+ All current features are frequency-domain. ZCR is a simple time-domain feature for distinguishing voiced/unvoiced speech and percussive vs tonal content. Accepts raw PCM samples.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hypnosound",
3
3
  "type": "module",
4
- "version": "1.9.0",
4
+ "version": "1.11.0",
5
5
  "description": "A small library for analyzing audio",
6
6
  "main": "index.js",
7
7
  "scripts": {
package/src/audio/bass.js CHANGED
@@ -1,34 +1,34 @@
1
- export default function bass(fft) {
2
- const sampleRate = 44100
3
- const totalSamples = fft.length
4
- return calculateBassPower(fft, sampleRate, totalSamples)
5
- }
6
-
7
- function calculateBassPower(fft, sampleRate, totalSamples) {
8
- const lowerBound = 0
9
- const upperBound = 400
10
- let bassEnergy = 0
11
- let maxEnergy = 0
12
-
13
- // Calculate frequency resolution
14
- const frequencyResolution = sampleRate / totalSamples
15
-
16
- for (let i = 0; i < fft.length; i++) {
17
- let frequency = i * frequencyResolution
18
- // Normalize each FFT value from 0 to 1 (assuming Uint8Array values 0-255)
19
- let magnitude = fft[i] / 255
20
- let power = magnitude * magnitude
21
-
22
- // Accumulate max energy for normalization
23
- maxEnergy += power
24
-
25
- // Isolate and accumulate bass frequencies
26
- if (frequency >= lowerBound && frequency <= upperBound) {
27
- bassEnergy += power
28
- }
29
- }
30
-
31
- // Normalize bass energy from 0 to 1
32
- let normalizedBassPower = maxEnergy > 0 ? bassEnergy / maxEnergy : 0
33
- return isNaN(normalizedBassPower) ? 0 : normalizedBassPower
34
- }
1
+ export default function bass(fft) {
2
+ const sampleRate = 44100
3
+ const totalSamples = fft.length
4
+ return calculateBassPower(fft, sampleRate, totalSamples)
5
+ }
6
+
7
+ function calculateBassPower(fft, sampleRate, totalSamples) {
8
+ const lowerBound = 0
9
+ const upperBound = 400
10
+ let bassEnergy = 0
11
+ let maxEnergy = 0
12
+
13
+ // Calculate frequency resolution
14
+ const frequencyResolution = sampleRate / totalSamples
15
+
16
+ for (let i = 0; i < fft.length; i++) {
17
+ let frequency = i * frequencyResolution
18
+ // Normalize each FFT value from 0 to 1 (assuming Uint8Array values 0-255)
19
+ let magnitude = fft[i] / 255
20
+ let power = magnitude * magnitude
21
+
22
+ // Accumulate max energy for normalization
23
+ maxEnergy += power
24
+
25
+ // Isolate and accumulate bass frequencies
26
+ if (frequency >= lowerBound && frequency <= upperBound) {
27
+ bassEnergy += power
28
+ }
29
+ }
30
+
31
+ // Normalize bass energy from 0 to 1
32
+ let normalizedBassPower = maxEnergy > 0 ? bassEnergy / maxEnergy : 0
33
+ return isNaN(normalizedBassPower) ? 0 : normalizedBassPower
34
+ }
@@ -0,0 +1,14 @@
1
+ export default function dbfs(fft) {
2
+ let sumOfSquares = 0
3
+ for (let i = 0; i < fft.length; i++) {
4
+ const normalized = fft[i] / 255
5
+ sumOfSquares += normalized * normalized
6
+ }
7
+ const rmsValue = Math.sqrt(sumOfSquares / fft.length)
8
+ if (rmsValue === 0) return 0
9
+ // 20 * log10(rms) gives dBFS, range is -Infinity to 0
10
+ // Normalize to 0-1: silence = 0, full scale = 1
11
+ // Clamp at -100 dB as practical floor
12
+ const db = 20 * Math.log10(rmsValue)
13
+ return Math.max(0, (db + 100) / 100)
14
+ }
@@ -1,19 +1,19 @@
1
- export default function energy(fft) {
2
- return calculateFFTEnergy(fft) / 1000
3
- }
4
-
5
- function calculateFFTEnergy(currentSignal) {
6
- let energy = 0
7
- const maxPossibleValue = 1 // This should be 1 if your data is normalized between 0 and 1
8
- const maxPossibleEnergy = currentSignal.length // Total samples if each sample was at maximum value
9
-
10
- for (let i = 0; i < currentSignal.length; i++) {
11
- let normalizedValue = currentSignal[i] / maxPossibleValue // Normalize each FFT value
12
- energy += normalizedValue * normalizedValue // Sum the squares of the normalized values
13
- }
14
-
15
- // Normalize the computed energy by the number of samples since each sample's max energy would be 1 if maxPossibleValue is 1
16
- energy = energy / currentSignal.length
17
-
18
- return energy
19
- }
1
+ export default function energy(fft) {
2
+ return calculateFFTEnergy(fft) / 1000
3
+ }
4
+
5
+ function calculateFFTEnergy(currentSignal) {
6
+ let energy = 0
7
+ const maxPossibleValue = 1 // This should be 1 if your data is normalized between 0 and 1
8
+ const maxPossibleEnergy = currentSignal.length // Total samples if each sample was at maximum value
9
+
10
+ for (let i = 0; i < currentSignal.length; i++) {
11
+ let normalizedValue = currentSignal[i] / maxPossibleValue // Normalize each FFT value
12
+ energy += normalizedValue * normalizedValue // Sum the squares of the normalized values
13
+ }
14
+
15
+ // Normalize the computed energy by the number of samples since each sample's max energy would be 1 if maxPossibleValue is 1
16
+ energy = energy / currentSignal.length
17
+
18
+ return energy
19
+ }
@@ -1,30 +1,34 @@
1
- export { default as bass } from "./bass.js"
2
- export { default as energy } from "./energy.js"
3
- export { default as mids } from "./mids.js"
4
- export { default as pitchClass } from "./pitchClass.js"
5
- export { default as spectralCentroid } from "./spectralCentroid.js"
6
- export { default as spectralCrest } from "./spectralCrest.js"
7
- export { default as spectralEntropy } from "./spectralEntropy.js"
8
- export { default as spectralFlux } from "./spectralFlux.js"
9
- export { default as spectralKurtosis } from "./spectralKurtosis.js"
10
- export { default as spectralRolloff } from "./spectralRolloff.js"
11
- export { default as spectralRoughness } from "./spectralRoughness.js"
12
- export { default as spectralSkew } from "./spectralSkew.js"
13
- export { default as spectralSpread } from "./spectralSpread.js"
14
- export { default as treble } from "./treble.js"
15
- export const AudioFeatures = [
16
- "bass",
17
- "energy",
18
- "mids",
19
- "pitchClass",
20
- "spectralCentroid",
21
- "spectralCrest",
22
- "spectralEntropy",
23
- "spectralFlux",
24
- "spectralKurtosis",
25
- "spectralRolloff",
26
- "spectralRoughness",
27
- "spectralSkew",
28
- "spectralSpread",
29
- "treble"
1
+ export { default as spectralSpread } from "./spectralSpread.js"
2
+ export { default as treble } from "./treble.js"
3
+ export { default as spectralSkew } from "./spectralSkew.js"
4
+ export { default as dbfs } from "./dbfs.js"
5
+ export { default as pitchClass } from "./pitchClass.js"
6
+ export { default as spectralRoughness } from "./spectralRoughness.js"
7
+ export { default as spectralKurtosis } from "./spectralKurtosis.js"
8
+ export { default as spectralCrest } from "./spectralCrest.js"
9
+ export { default as rms } from "./rms.js"
10
+ export { default as spectralCentroid } from "./spectralCentroid.js"
11
+ export { default as spectralEntropy } from "./spectralEntropy.js"
12
+ export { default as mids } from "./mids.js"
13
+ export { default as spectralFlux } from "./spectralFlux.js"
14
+ export { default as energy } from "./energy.js"
15
+ export { default as bass } from "./bass.js"
16
+ export { default as spectralRolloff } from "./spectralRolloff.js"
17
+ export const AudioFeatures = [
18
+ "spectralSpread",
19
+ "treble",
20
+ "spectralSkew",
21
+ "dbfs",
22
+ "pitchClass",
23
+ "spectralRoughness",
24
+ "spectralKurtosis",
25
+ "spectralCrest",
26
+ "rms",
27
+ "spectralCentroid",
28
+ "spectralEntropy",
29
+ "mids",
30
+ "spectralFlux",
31
+ "energy",
32
+ "bass",
33
+ "spectralRolloff"
30
34
  ]
package/src/audio/mids.js CHANGED
@@ -1,33 +1,33 @@
1
- export default function mids(fft) {
2
- const sampleRate = 44100
3
- const totalSamples = fft.length
4
- return calculateMidPower(fft, sampleRate, totalSamples)
5
- }
6
-
7
- function calculateMidPower(fft, sampleRate, totalSamples) {
8
- const lowerBound = 400 // 400 Hz
9
- const upperBound = 4000 // 4000 Hz
10
- let midEnergy = 0
11
- let maxEnergy = 0
12
-
13
- // Calculate frequency resolution
14
- const frequencyResolution = sampleRate / totalSamples
15
-
16
- for (let i = 0; i < fft.length; i++) {
17
- let frequency = i * frequencyResolution
18
- let magnitude = Math.abs(fft[i]) / totalSamples
19
- let power = magnitude * magnitude
20
-
21
- // Accumulate max energy for normalization
22
- maxEnergy += power
23
-
24
- // Isolate and accumulate mid frequencies
25
- if (frequency >= lowerBound && frequency <= upperBound) {
26
- midEnergy += power
27
- }
28
- }
29
-
30
- // Normalize mid energy from 0 to 1
31
- let normalizedMidPower = midEnergy / maxEnergy
32
- return isNaN(normalizedMidPower) ? 0 : normalizedMidPower // Scale by 10 if needed, similar to your original function
33
- }
1
+ export default function mids(fft) {
2
+ const sampleRate = 44100
3
+ const totalSamples = fft.length
4
+ return calculateMidPower(fft, sampleRate, totalSamples)
5
+ }
6
+
7
+ function calculateMidPower(fft, sampleRate, totalSamples) {
8
+ const lowerBound = 400 // 400 Hz
9
+ const upperBound = 4000 // 4000 Hz
10
+ let midEnergy = 0
11
+ let maxEnergy = 0
12
+
13
+ // Calculate frequency resolution
14
+ const frequencyResolution = sampleRate / totalSamples
15
+
16
+ for (let i = 0; i < fft.length; i++) {
17
+ let frequency = i * frequencyResolution
18
+ let magnitude = Math.abs(fft[i]) / totalSamples
19
+ let power = magnitude * magnitude
20
+
21
+ // Accumulate max energy for normalization
22
+ maxEnergy += power
23
+
24
+ // Isolate and accumulate mid frequencies
25
+ if (frequency >= lowerBound && frequency <= upperBound) {
26
+ midEnergy += power
27
+ }
28
+ }
29
+
30
+ // Normalize mid energy from 0 to 1
31
+ let normalizedMidPower = midEnergy / maxEnergy
32
+ return isNaN(normalizedMidPower) ? 0 : normalizedMidPower // Scale by 10 if needed, similar to your original function
33
+ }
@@ -1,26 +1,26 @@
1
- export default function pitchClass(fft, sampleRate = 44100) {
2
- // Constants for the FFT processing
3
- const fftSize = fft.length
4
- const freqResolution = sampleRate / fftSize
5
-
6
- // Finding the dominant frequency in the FFT data
7
- let maxIndex = 0
8
- let maxValue = 0
9
- for (let i = 1; i < fft.length; i++) {
10
- // start from 1 to skip DC offset
11
- if (fft[i] > maxValue) {
12
- maxValue = fft[i]
13
- maxIndex = i
14
- }
15
- }
16
- const dominantFreq = maxIndex * freqResolution
17
-
18
- // Convert to MIDI note then to pitchClass
19
- const midiNote = 69 + 12 * Math.log2(dominantFreq / 440)
20
- const pitchClass = Math.round(midiNote) % 12 // round to reduce minor fluctuation effects
21
-
22
- // Normalize to a 0-1 range
23
- const normalizedpitchClass = pitchClass / 12
24
-
25
- return isNaN(normalizedpitchClass) ? 0 : normalizedpitchClass
26
- }
1
+ export default function pitchClass(fft, sampleRate = 44100) {
2
+ // Constants for the FFT processing
3
+ const fftSize = fft.length
4
+ const freqResolution = sampleRate / fftSize
5
+
6
+ // Finding the dominant frequency in the FFT data
7
+ let maxIndex = 0
8
+ let maxValue = 0
9
+ for (let i = 1; i < fft.length; i++) {
10
+ // start from 1 to skip DC offset
11
+ if (fft[i] > maxValue) {
12
+ maxValue = fft[i]
13
+ maxIndex = i
14
+ }
15
+ }
16
+ const dominantFreq = maxIndex * freqResolution
17
+
18
+ // Convert to MIDI note then to pitchClass
19
+ const midiNote = 69 + 12 * Math.log2(dominantFreq / 440)
20
+ const pitchClass = Math.round(midiNote) % 12 // round to reduce minor fluctuation effects
21
+
22
+ // Normalize to a 0-1 range
23
+ const normalizedpitchClass = pitchClass / 12
24
+
25
+ return isNaN(normalizedpitchClass) ? 0 : normalizedpitchClass
26
+ }
@@ -0,0 +1,8 @@
1
+ export default function rms(fft) {
2
+ let sumOfSquares = 0
3
+ for (let i = 0; i < fft.length; i++) {
4
+ const normalized = fft[i] / 255
5
+ sumOfSquares += normalized * normalized
6
+ }
7
+ return Math.sqrt(sumOfSquares / fft.length)
8
+ }
@@ -1,25 +1,25 @@
1
- export default function spectralCentroid(fft) {
2
- const computed = calculateSpectralCentroid(fft) // Process FFT data
3
- return computed * 1.5
4
- }
5
- function calculateSpectralCentroid(ampSpectrum) {
6
- if (!ampSpectrum.length) return null // Early exit if the spectrum is empty
7
-
8
- let numerator = 0
9
- let denominator = 0
10
-
11
- // Calculate the weighted sum (numerator) and the sum of the amplitudes (denominator)
12
- ampSpectrum.forEach((amplitude, index) => {
13
- numerator += index * amplitude
14
- denominator += amplitude
15
- })
16
-
17
- // Avoid dividing by zero
18
- if (denominator === 0) return null
19
-
20
- const centroidIndex = numerator / denominator
21
- // Normalize the centroid index to be between 0 and 1
22
- const normalizedCentroid = centroidIndex / (ampSpectrum.length - 1)
23
-
24
- return normalizedCentroid
25
- }
1
+ export default function spectralCentroid(fft) {
2
+ const computed = calculateSpectralCentroid(fft) // Process FFT data
3
+ return computed * 1.5
4
+ }
5
+ function calculateSpectralCentroid(ampSpectrum) {
6
+ if (!ampSpectrum.length) return null // Early exit if the spectrum is empty
7
+
8
+ let numerator = 0
9
+ let denominator = 0
10
+
11
+ // Calculate the weighted sum (numerator) and the sum of the amplitudes (denominator)
12
+ ampSpectrum.forEach((amplitude, index) => {
13
+ numerator += index * amplitude
14
+ denominator += amplitude
15
+ })
16
+
17
+ // Avoid dividing by zero
18
+ if (denominator === 0) return null
19
+
20
+ const centroidIndex = numerator / denominator
21
+ // Normalize the centroid index to be between 0 and 1
22
+ const normalizedCentroid = centroidIndex / (ampSpectrum.length - 1)
23
+
24
+ return normalizedCentroid
25
+ }
@@ -1,17 +1,17 @@
1
- export default function spectralCrest(fft) {
2
- const computed = calculateSpectralCrest(fft) // Process FFT data
3
- return computed * 100
4
- }
5
-
6
- function calculateSpectralCrest(fftData) {
7
- // Find the maximum amplitude in the spectrum
8
- const maxAmplitude = Math.max(...fftData)
9
-
10
- // Calculate the sum of all amplitudes
11
- const sumAmplitudes = fftData.reduce((sum, amplitude) => sum + amplitude, 0)
12
-
13
- // Calculate the Spectral Crest
14
- const spectralCrest = sumAmplitudes !== 0 ? maxAmplitude / sumAmplitudes : 0
15
-
16
- return spectralCrest
17
- }
1
+ export default function spectralCrest(fft) {
2
+ const computed = calculateSpectralCrest(fft) // Process FFT data
3
+ return computed * 100
4
+ }
5
+
6
+ function calculateSpectralCrest(fftData) {
7
+ // Find the maximum amplitude in the spectrum
8
+ const maxAmplitude = Math.max(...fftData)
9
+
10
+ // Calculate the sum of all amplitudes
11
+ const sumAmplitudes = fftData.reduce((sum, amplitude) => sum + amplitude, 0)
12
+
13
+ // Calculate the Spectral Crest
14
+ const spectralCrest = sumAmplitudes !== 0 ? maxAmplitude / sumAmplitudes : 0
15
+
16
+ return spectralCrest
17
+ }
@@ -1,28 +1,28 @@
1
- export default function spectralEntropy(fft) {
2
- return calculateSpectralEntropy(fft) // Process FFT data
3
- }
4
-
5
- function toPowerSpectrum(fftData) {
6
- return fftData.map((amplitude) => Math.pow(amplitude, 2))
7
- }
8
-
9
- function calculateSpectralEntropy(fftData) {
10
- const powerSpectrum = toPowerSpectrum(fftData)
11
- // Normalize the power spectrum to create a probability distribution
12
- const totalPower = powerSpectrum.reduce((sum, val) => sum + val, 0)
13
- if (totalPower === 0) return 0
14
- const probabilityDistribution = new Float32Array(powerSpectrum.length)
15
- for (let i = 0; i < powerSpectrum.length; i++) {
16
- probabilityDistribution[i] = powerSpectrum[i] / totalPower
17
- }
18
-
19
- const entropy = probabilityDistribution.reduce((sum, prob) => {
20
- if (prob > 0) {
21
- const logProb = Math.log(prob)
22
- return sum - prob * logProb
23
- } else {
24
- return sum
25
- }
26
- }, 0)
27
- return entropy / Math.log(probabilityDistribution.length)
28
- }
1
+ export default function spectralEntropy(fft) {
2
+ return calculateSpectralEntropy(fft) // Process FFT data
3
+ }
4
+
5
+ function toPowerSpectrum(fftData) {
6
+ return fftData.map((amplitude) => Math.pow(amplitude, 2))
7
+ }
8
+
9
+ function calculateSpectralEntropy(fftData) {
10
+ const powerSpectrum = toPowerSpectrum(fftData)
11
+ // Normalize the power spectrum to create a probability distribution
12
+ const totalPower = powerSpectrum.reduce((sum, val) => sum + val, 0)
13
+ if (totalPower === 0) return 0
14
+ const probabilityDistribution = new Float32Array(powerSpectrum.length)
15
+ for (let i = 0; i < powerSpectrum.length; i++) {
16
+ probabilityDistribution[i] = powerSpectrum[i] / totalPower
17
+ }
18
+
19
+ const entropy = probabilityDistribution.reduce((sum, prob) => {
20
+ if (prob > 0) {
21
+ const logProb = Math.log(prob)
22
+ return sum - prob * logProb
23
+ } else {
24
+ return sum
25
+ }
26
+ }, 0)
27
+ return entropy / Math.log(probabilityDistribution.length)
28
+ }