Git Product home page Git Product logo

discrete-wavelets's Introduction

Build Status Coverage Status GitHub License NPM Version Monthly Downloads

This library is no longer actively maintained. This means that no new functionality or documentation is added.

Discrete Wavelets

A Discrete Wavelet Transform (DWT) library for the web.

This library is well tested. Still, it may contain some errors. Therefore it is recommended to double check the results with another library such as PyWavelets. If you find any errors, please let me know by opening an issue or a pull request.

Importing this library

Node Modules

  • Run npm install discrete-wavelets
  • Add an import to the npm package import wt from 'discrete-wavelets';
  • Then you can use the library in your code.

CDN

  • Put the following script tag <script src="https://cdn.jsdelivr.net/npm/discrete-wavelets@5/dist/discrete-wavelets.umd.min.js"></script> in the head of your HTML file.
  • Then you can use the library in your code.

Types

The library uses the following types:

PaddingMode

The following values for PaddingMode are supported at the moment:

Name Value Description
Zero Padding 'zero' Adding zeros.
Constant Padding 'constant' Replication of border values.
Symmetric Padding 'symmetric' Mirroring of samples.
Reflect Padding 'reflect' Reflecting of samples.
Periodic Padding 'periodic' Treating signal as a periodic one.
Smooth Padding 'smooth' Signal extended as a straight line.
Antisymmetric Padding 'antisymmetric' Mirroring and negation of samples.

You can get a list of the supported signal extension modes:

console.log(wt.Modes.modes);
// expected output: Array ['zero', 'constant', 'symmetric', 'periodic', 'smooth', 'reflect', 'antisymmetric']

Wavelets

The following Wavelet types are supported at the moment:

Wavelet Aliases
Daubechies 1 / Haar 'db1', 'D2', 'haar'
Daubechies 2 'db2', 'D4'
Daubechies 3 'db3', 'D6'
Daubechies 4 'db4', 'D8'
Daubechies 5 'db5', 'D10'
Daubechies 6 'db6', 'D12'
Daubechies 7 'db7', 'D14'
Daubechies 8 'db8', 'D16'
Daubechies 9 'db9', 'D18'
Daubechies 10 'db10', 'D20'

API

The library offers the following functions:

  • Discrete Wavelet Transform (DWT)
    • dwt: Single level Discrete Wavelet Transform.
    • wavedec: 1D wavelet decomposition. Transforms data by calculating coefficients from input data.
  • Inverse Discrete Wavelet Transform (IDWT)
    • idwt: Single level inverse Discrete Wavelet Transform.
    • waverec: 1D wavelet reconstruction. Inverses a transform by calculating input data from coefficients.
  • Other
    • energy: Calculates the energy as sum of squares of an array of data or coefficients.
    • maxLevel: Determines the maximum level of useful decomposition.
    • pad: Extends a signal with a given padding mode.

dwt

Single level Discrete Wavelet Transform.

Arguments

  • data (number[]): Input data.
  • wavelet (Wavelet): Wavelet to use.
  • mode (PaddingMode): Signal extension mode. Defaults to 'symmetric'.

Return

coeffs (number[][]): Approximation and detail coefficients as result of the transform.

Example

var coeffs = wt.dwt([1, 2, 3, 4], "haar");

console.log(coeffs);
// expected output: Array [[2.1213203435596425, 4.9497474683058326], [-0.7071067811865475, -0.7071067811865475]]

wavedec

1D wavelet decomposition. Transforms data by calculating coefficients from input data.

Arguments

  • data (number[]): Input data.
  • wavelet (Wavelet): Wavelet to use.
  • mode (PaddingMode): Signal extension mode. Defaults to 'symmetric'.
  • level (number): Decomposition level. Defaults to level calculated by maxLevel function.

Return

coeffs (number[][]): Coefficients as result of the transform.

Example

var coeffs = wt.wavedec([1, 2, 3, 4], "haar");

console.log(coeffs);
// expected output: Array [[4.999999999999999], [-1.9999999999999993], [-0.7071067811865475, -0.7071067811865475]]

Be aware that due to floating point imprecision the result diverges slightly from the analytical solution [[5], [-2], [-0.7071067811865475, -0.7071067811865475]]

idwt

Single level inverse Discrete Wavelet Transform.

Arguments

  • approx (number[]): Approximation coefficients. If undefined, it will be set to an array of zeros with length equal to the detail coefficients.
  • detail (number[]): Detail coefficients. If undefined, it will be set to an array of zeros with length equal to the approximation coefficients.
  • wavelet (Wavelet): Wavelet to use.

Return

rec (number[]): Approximation coefficients of previous level of transform.

Example

var rec = wt.idwt(
  [(1 + 2) / Math.SQRT2, (3 + 4) / Math.SQRT2],
  [(1 - 2) / Math.SQRT2, (3 - 4) / Math.SQRT2],
  "haar"
);

console.log(rec);
// expected output: Array [0.9999999999999999, 1.9999999999999996, 2.9999999999999996, 3.9999999999999996]

Be aware that due to floating point imprecision the result diverges slightly from the analytical solution [1, 2, 3, 4]

waverec

1D wavelet reconstruction. Inverses a transform by calculating input data from coefficients.

Arguments

  • coeffs (number[][]): Coefficients as result of a transform.
  • wavelet (Wavelet): Wavelet to use.

Return

data (number[]): Input data as result of the inverse transform.

Example

var data = wt.waverec([[5], [-2], [-1 / Math.SQRT2, -1 / Math.SQRT2]], "haar");

console.log(data);
// expected output: Array [0.9999999999999999, 1.9999999999999996, 2.999999999999999, 3.999999999999999]

Be aware that due to floating point imprecision the result diverges slightly from the analytical solution [1, 2, 3, 4]

energy

Calculates the energy as sum of squares of an array of data or coefficients.

Argument

  • values (number[] | number[][]): Array of data or coefficients.

Return

energy (number): Energy of values as the sum of squares.

Examples

console.log(wt.energy([-1, 2, 6, 1]));
// expected output: 42

console.log(wt.energy([[5], [-2], [-1 / Math.SQRT2, -1 / Math.SQRT2]]));
// expected output: 30

maxLevel

Determines the maximum level of useful decomposition.

Arguments

  • dataLength (number): Length of input data.
  • wavelet (Wavelet): Wavelet to use.

Return

maxLevel (number): Maximum useful level of decomposition.

Examples

var maxLevel = wt.maxLevel(4, "haar");

console.log(maxLevel);
// expected output: 2
var maxLevel = wt.maxLevel(1024, "haar");

console.log(maxLevel);
// expected output: 10

pad

Extends a signal with a given padding mode.

Arguments

  • data (number[]): Input data.
  • padWidths ([number, number]): Widths of padding at front and back.
  • mode (PaddingMode): Signal extension mode.

Return

pad (number[]): Data with padding.

Example

var pad = wt.pad([42, 51], [2, 1], "zero");

console.log(pad);
// expected output: Array [0, 0, 42, 51, 0]

NPM scripts

  • npm install: Install dependencies
  • npm test: Run test suite
  • npm start: Run npm run build in watch mode
  • npm run test:watch: Run test suite in interactive watch mode
  • npm run test:prod: Run linting and generate coverage
  • npm run build: Generate bundles and typings, create docs
  • npm run lint: Lints code

discrete-wavelets's People

Contributors

dependabot[bot] avatar symmetronic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

discrete-wavelets's Issues

Dead link and example

Hi there,

Thank you for your interesting module :) I all new to wavelets but I'm trying to use it to smoothening data / denoising signals.

  1. FYI: This link here is dead: https://symmetronic.github.io/covid-19-dwt-analysis/
  2. How do I prune coefficients which I would like to remove - so I can adjust the smoothening level of the signal?

const coeffs = wt.wavedec(points, waveletType);

const prunedCoeffs = ??

const smoothedData = wt.waverec(prunedCoeffs, waveletType);

Thank you so much!

continuous wavelet transform

Hi, thanks for this lib !

What I need to do is to measure the energy / power of two variables on a time-series

I was wondering if this library includes the continuous wavelet transform (cwt ) mode for time series ?

Thanks !

Getting undefined error with anything other then HAAR/db1

Using any wavelet other than HAAR/db1 results in 'dec' undefined.

dwt.transform([...pts], 'db2');

TypeError: Cannot read property 'dec' of undefined
at Function.dwt.transform (/workspace/node_modules/discrete-wavelets/dist/discrete-wavelets.umd.js:246:38)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.