An open API service indexing awesome lists of open source software.

https://github.com/mljs/spectra-fitting


https://github.com/mljs/spectra-fitting

hacktoberfest

Last synced: about 1 year ago
JSON representation

Awesome Lists containing this project

README

          

# ml-spectra-fitting

[![NPM version][npm-image]][npm-url]
[![build status][ci-image]][ci-url]
[![npm download][download-image]][download-url]

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 kind of shapes:

| Name | Equation |
| ------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| Gaussian | |
| Lorentzian | |
| Pseudo Voigt | |

where

| | | |
| --------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------ |

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

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

## Installation

`$ npm install 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,
},
},
],
};
```

For data with and combination of signals with shapes between gaussian and lorentzians, we could use the kind pseudovoigt to fit the data.

```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,
},
},
],
};
```

## License

[MIT](./LICENSE)

[npm-image]: https://img.shields.io/npm/v/ml-spectra-fitting.svg
[npm-url]: https://npmjs.org/package/ml-spectra-fitting
[ci-image]: https://github.com/mljs/spectra-fitting/workflows/Node.js%20CI/badge.svg?branch=main
[ci-url]: https://github.com/mljs/spectra-fitting/actions?query=workflow%3A%22Node.js+CI%22
[download-image]: https://img.shields.io/npm/dm/ml-spectra-fitting.svg
[download-url]: https://npmjs.org/package/ml-spectra-fitting