# ml-spectra-fitting

> Fit spectra using gaussian or lorentzian

Latest version **6.2.2** (published 2026-08-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install ml-spectra-fitting
pnpm add ml-spectra-fitting
yarn add ml-spectra-fitting
bun add ml-spectra-fitting
```

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 6.2.2 |
| Published | 2026-08-03 |
| First published | 2020-10-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 5 |
| Unpacked size | 126 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Andres Castillo |
| Maintainers | stropitek, targos, lpatiny, mljs-bot |
| Keywords | Optimization, Lorentzian, Gaussian, pseudovoigt |

## Links

- npm: https://www.npmjs.com/package/ml-spectra-fitting
- Repository: https://github.com/mljs/spectra-fitting
- Issues: https://github.com/mljs/spectra-fitting/issues
- npm.io page: https://npm.io/package/ml-spectra-fitting

## Dependencies (5)

- [ml-direct](https://npm.io/package/ml-direct.md) ^1.0.0
- [cheminfo-types](https://npm.io/package/cheminfo-types.md) ^1.15.0
- [ml-spectra-processing](https://npm.io/package/ml-spectra-processing.md) ^14.29.0
- [ml-levenberg-marquardt](https://npm.io/package/ml-levenberg-marquardt.md) ^5.0.1
- [ml-peak-shape-generator](https://npm.io/package/ml-peak-shape-generator.md) ^5.1.0

## Recent versions

- 6.2.2 (latest) — 2026-08-03
- 4.1.0-pre.1661357854 (pre) — 2022-08-24
- 6.2.1 — 2026-07-08
- 6.2.0 — 2026-07-07
- 6.1.0 — 2026-06-03
- 6.0.1 — 2026-05-05
- 5.0.1 — 2025-06-13
- 5.0.0 — 2025-06-13
- 4.2.4 — 2024-09-26
- 4.2.3 — 2024-03-07
- 4.2.2 — 2024-03-06
- 4.2.1 — 2023-03-24
- 4.2.0 — 2022-09-23
- 4.1.1 — 2022-08-26
- 4.1.0 — 2022-08-05
- … 25 more at https://npm.io/package/ml-spectra-fitting/versions

## README

# ml-spectra-fitting

[![NPM version](https://img.shields.io/npm/v/ml-spectra-fitting.svg)](https://www.npmjs.com/package/ml-spectra-fitting)
[![npm download](https://img.shields.io/npm/dm/ml-spectra-fitting.svg)](https://www.npmjs.com/package/ml-spectra-fitting)
[![test coverage](https://img.shields.io/codecov/c/github/mljs/spectra-fitting.svg)](https://codecov.io/gh/mljs/spectra-fitting)
[![license](https://img.shields.io/npm/l/ml-spectra-fitting.svg)](https://github.com/mljs/spectra-fitting/blob/main/LICENSE)

This is a spectra fitting package to optimize the position (x), max intensity (y),
full width at half-maximum (FWHM = width) and the ratio of gaussian contribution (mu) if it's required.
It supports three kinds of shapes:

| Name         |                                                                                                                            Equation                                                                                                                             |
| ------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| Gaussian     |                                                                 <img src="https://tex.cheminfo.org/?tex=y%20%5Ccdot%20exp%20%5Cleft%5B%5Cfrac%7B%5Cdelta%7D%7B2%20%5Csigma%5E2%7D%5Cright%5D"/>                                                                 |
| Lorentzian   |                                                                             <img src="https://tex.cheminfo.org/?tex=y%5Ccdot%5Cfrac%7B%5Cgamma%7D%7B%5Cdelta%20%2B%20%5Cgamma%7D"/>                                                                             |
| Pseudo Voigt | <img src="https://tex.cheminfo.org/?tex=y%20*%20%5Cleft%5B%5Cmu%20%5Ccdot%20exp%20%5Cleft%5B%5Cfrac%7B%5Cdelta%7D%7B2%20%5Csigma%5E2%7D%5Cright%5D%20%2B%20(1%20-%20%5Cmu)%20%5Ccdot%20%5Cfrac%7B%5Cgamma%7D%7B%5Cdelta%20%2B%20%5Cgamma%7D%20%5Cright%5D%0A"/> |

where

| <img src="https://tex.cheminfo.org/?tex=%5Cdelta%20%3D%20%5Cleft(t%20-%20x%5Cright)%5E2%0A"/> | <img src="https://tex.cheminfo.org/?tex=%5Csigma%20%3D%20%5Cfrac%7BFWHM%7D%7B2%5Csqrt%7B2%20%5Ccdot%20Ln(2)%7D%7D"/> | <img src="https://tex.cheminfo.org/?tex=%5Cgamma%3D%5Cleft(FWHM%5Cright)%5E2"/> |
| --------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------ |

It is a wrapper of [ml-levenberg-marquardt](https://github.com/mljs/levenberg-marquardt)

## [API Documentation](https://mljs.github.io/spectra-fitting/)

## Installation

```console
npm i ml-spectra-fitting
```

## Example

```js
import { optimize } from 'ml-spectra-fitting';
import { SpectrumGenerator } from 'spectrum-generator';

const generator = new SpectrumGenerator({
  nbPoints: 101,
  from: -1,
  to: 1,
});

// by default the kind of shape is gaussian;
generator.addPeak({ x: 0.5, y: 0.2 }, { fwhm: 0.2 });
generator.addPeak(
  { x: -0.5, y: 0.2 },
  {
    shape: {
      kind: 'lorentzian',
      fwhm: 0.1,
    },
  },
);

//points to fit {x, y};
let data = generator.getSpectrum();
console.log(JSON.stringify({ x: Array.from(data.x), y: Array.from(data.y) }));
//the approximate values to be optimized, It could coming from a peak picking with ml-gsd
let peaks = [
  {
    x: -0.5,
    y: 0.22,
    shape: {
      kind: 'gaussian',
      fwhm: 0.25,
    },
  },
  {
    x: 0.52,
    y: 0.18,
    shape: {
      kind: 'gaussian',
      fwhm: 0.18,
    },
  },
];

// the function receive an array of peak with {x, y, fwhm} as a guess
// and return a list of objects
let fittedParams = optimize(data, peaks, { shape: { kind: 'pseudoVoigt' } });

console.log(fittedParams);
const result = {
  error: 0.12361588652854476,
  iterations: 100,
  peaks: [
    {
      x: -0.5000014532421942,
      y: 0.19995307937326137,
      shape: {
        kind: 'pseudoVoigt',
        fwhm: 0.10007670374735196,
        mu: 0.004731136777288483,
      },
    },
    {
      x: 0.5001051783652894,
      y: 0.19960010175400406,
      shape: {
        kind: 'pseudoVoigt',
        fwhm: 0.19935932346969124,
        mu: 1,
      },
    },
  ],
};
```

## Linked parameters

You can link one parameter across multiple peaks so they share one optimization
variable.

For each linked peak, the actual parameter value is reconstructed as:

```text
actualValue = sharedVariable * factor + offset
```

By default, `factor = 1` and `offset = 0`.

```js
import { optimize } from 'ml-spectra-fitting';

const result = optimize(data, peaks, {
  parameters: {
    x: { optimize: false },
  },
  linkedParameters: [
    {
      parameter: 'fwhm',
      peaks: [{ id: 'left' }, { id: 'right' }],
    },
    {
      parameter: 'y',
      peaks: [
        { id: 'left', factor: 1 },
        { id: 'right', factor: 2 },
      ],
    },
  ],
});
```

`linkedParameters.peaks[].id` accepts either the peak index or peak `id`.

If the linked parameter is `y`, `offset` is interpreted in the original Y scale.

## License

[MIT](./LICENSE)

---
_Source: https://npm.io/package/ml-spectra-fitting · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
