npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

octave-bands

v1.1.0

Published

Frequency limits for fractional Octave Bands

Downloads

9

Readme

octave-bands

Build status NPM Version License

Frequency limits for fractional Octave Bands.

A small, lightweight package for calculating the frequency bounds for fractional octave bands.
Calculate the frequency bounds for 1/2, 1/3, ... 1/N octave bands.

Install

npm install octave-bands
import { octaves, bandwidth, equalizer } from 'octave-bands';

Usage

Get frequency limits for 11 octave bands:

import { octaves } from 'octave-bands';

const bands = octaves(); // or `octaves(1)`
console.table(bands);

/*
┌─────────┬───────────┬────────┬───────────┐
│ (index) │     0     │   1    │     2     │
├─────────┼───────────┼────────┼───────────┤
│    0    │  11.049   │ 15.625 │  22.097   │
│    1    │  22.097   │ 31.25  │  44.194   │
│    2    │  44.194   │  62.5  │  88.388   │
│    3    │  88.388   │  125   │  176.777  │
│    4    │  176.777  │  250   │  353.553  │
│    5    │  353.553  │  500   │  707.107  │
│    6    │  707.107  │  1000  │ 1414.214  │
│    7    │ 1414.214  │  2000  │ 2828.427  │
│    8    │ 2828.427  │  4000  │ 5656.854  │
│    9    │ 5656.854  │  8000  │ 11313.708 │
│   10    │ 11313.708 │ 16000  │ 22627.417 │
└─────────┴───────────┴────────┴───────────┘
*/

The octaves function returns an array of band frequencies, where each band is a tuple of its low, center and high frequency in the form of [low, center, high].

To get only the center frequencies:

import { octaves } from 'octave-bands';

const bands = octaves();
console.table(bands.map(([low, center, high]) => center));

/*
┌─────────┬────────┐
│ (index) │ Values │
├─────────┼────────┤
│    0    │ 15.625 │
│    1    │ 31.25  │
│    2    │  62.5  │
│    3    │  125   │
│    4    │  250   │
│    5    │  500   │
│    6    │  1000  │
│    7    │  2000  │
│    8    │  4000  │
│    9    │  8000  │
│   10    │ 16000  │
└─────────┴────────┘
*/

Get one-third-octave bands:

import { octaves } from 'octave-bands';

const bands = octaves(1 / 3);
console.table(bands);

/*
┌─────────┬───────────┬───────────┬───────────┐
│ (index) │     0     │     1     │     2     │
├─────────┼───────────┼───────────┼───────────┤
│    0    │   13.92   │  15.625   │  17.538   │
│    1    │  17.538   │  19.686   │  22.097   │
...............................................
│   18    │  890.899  │   1000    │ 1122.462  │
...............................................
│   31    │ 17959.393 │ 20158.737 │ 22627.417 │
└─────────┴───────────┴───────────┴───────────┘
*/

Get the frequency limits for a 21-band equalizer covering an audio spectrum from 20Hz to 20kHz

import { equalizer } from 'octave-bands';

const bands = equalizer(21, { spectrum: [20, 20000] });
console.table(bands);

/*
┌─────────┬───────────┬───────────┬───────────┐
│ (index) │     0     │     1     │     2     │
├─────────┼───────────┼───────────┼───────────┤
│    0    │  16.828   │    20     │   23.77   │
│    1    │   23.77   │  28.251   │  33.576   │
│    2    │  33.576   │  39.905   │  47.427   │
│    3    │  47.427   │  56.368   │  66.993   │
...............................................
│   19    │ 11913.243 │ 14158.916 │ 16827.903 │
│   20    │ 16827.903 │   20000   │ 23770.045 │
└─────────┴───────────┴───────────┴───────────┘
*/

To get the fractional bandwidth constant:

import { bandwidth } from 'octave-bands';

console.log('octave bandwidth', bandwidth()); // ~0.707
console.log('1/2 octave bandwidth', bandwidth(1 / 2)); // ~0.348
console.log('1/3 octave bandwidth', bandwidth(1 / 3)); // ~0.232

API

octaves(fraction: number = 1, options?: Options): [number, number, number][]

Calculates the frequencies for fractional octave bands.

Arguments:

  • fraction - octave fraction (defaults to 1)
  • options - additional options:
type Options: {
    center?: number,
    spectrum?: [number, number]
}
  • center the center frequency of the band that will be used as a starting point to calculate other bands (defaults to 1000).
  • spectrum - a two-numbers array representing the min and max values to include for center frequencies (defaults to [15, 21000])

Returns:

  • [number, number, number][] returns an array where each element represents the frequency bounds of a band [low, center, high]

equalizer(bands: number, options?: Options): [number, number, number][]

Calculates the frequency bounds for a given number of bands that should cover an audio spectrum.
This is similar to octaves function, but instead of using octave's fraction to calculate the frequencies, it uses the number of bands to output.

Arguments:

  • bands - the number of bands to output

  • options - additional options:

type Options: {
    spectrum?: [number, number]
}
  • spectrum - a two-numbers array representing the min and max values to include for center frequencies (defaults to [15, 21000])

Returns:

  • [number, number, number][] returns an array where each element represents the frequency bounds of a band [low, center, high]

bandwidth(fraction: number): number

Calculates the constant bandwidth per 1/N-octave.

Arguments:

  • fraction - octave fraction (defaults to 1)

Returns:

  • number the bandwidth constant

Formula

The following formula for calculating 1/N octave bands in a given audio spectrum (ex. ~20Hz to ~20KHz) is used:

// 1/N: one-Nth-octave

f_center_0 = 1000

// Center frequency of i-th band
f_center(i) = f_center(i-1) * 2^(1 / N) = f_center(i+1) * 1 / 2^(1 / N)

// Lower center frequency of n-th band
f_low(i) = f_center(i) * 1 / 2^(1 / 2*N)

// Higher center frequency of n-th band
f_high(i) = f_center(i) * 2^(1 / 2*N)

// And for every i-th band holds:
spectrum[0] <= f_center(i) <= spectrum[1]

// Fractional band width per 1/N-octave band (constant):
BW = (f_high - f_low) / f_center // for every one-Nth-octave band

License

MIT