# needle-haystack-1d

> find scaled+shifted fuzzy copies of a 1-d 'needle' within a 1-d 'haystack'

Latest version **0.0.112** (published 2023-04-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install needle-haystack-1d
pnpm add needle-haystack-1d
yarn add needle-haystack-1d
bun add needle-haystack-1d
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.112 |
| Published | 2023-04-23 |
| First published | 2023-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 8.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | stonkpunk |
| Maintainers | stonkpunk |
| Keywords | needle, haystack, search, 1d, correlation |

## Links

- npm: https://www.npmjs.com/package/needle-haystack-1d
- Homepage: https://github.com/stonkpunk/my-npm-modules/tree/main/needle-haystack-1d
- npm.io page: https://npm.io/package/needle-haystack-1d

## Dependencies (4)

- [ndarray](https://npm.io/package/ndarray.md) ^1.0.19
- [regression](https://npm.io/package/regression.md) ^2.0.1
- [ndarray-convolve](https://npm.io/package/ndarray-convolve.md) ^2.0.0
- [ndarray-normalize](https://npm.io/package/ndarray-normalize.md) ^1.0.0

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 0.0.112 (latest) — 2023-04-23
- 0.0.111 — 2023-04-23
- 0.0.11 — 2023-04-23
- 0.0.1 — 2023-04-23

## README

# needle-haystack-1d

fuzzy-matching between a 1-d "needle" time series and a "haystack" time series. find scaled+shifted copies of the "needle" within the "haystack".

similar to `phase-align` but with multiple fuzzy results [and limited to 1-d data].

## Installation

```sh
npm i needle-haystack-1d
```

## Usage 

```javascript
var {getNeedleScaleShift, getMSE, getNeedleHaystackData, removeOverlaps} = require('needle-haystack-1d');

var needle = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var haystack = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0,0,0,0,0,0,0,0,0,0,0];

haystack = haystack.map(f=>f*10+5); //scale+shift the haystack

var doRemoveOverlaps = false; //default false
var res = getNeedleHaystackData(needle, haystack, doRemoveOverlaps);
console.log(res);

//doRemoveOverlaps = false
// {
//     indicesWithCorrelations: [  // [hitIndex, normalized correlation] sorted by correlation
//         [ 21, 2.251800553427485 ],
//         [ 20, 2.2069626632689396 ],
//         [ 22, 2.1621247731103908 ],
//         ...
// }

//doRemoveOverlaps = true
// {
//     indicesWithCorrelations: [
//         [ 21, 2.251800553427485 ],
//         [ 10, -0.5281486364024173 ],
//         [ 31, -0.6447271508146415 ]
//     ]
// }

//note - correlations are normalized by npm `ndarray-normalize` to have mean zero and std 1 

//extract the top "hit" from the haystack
var futurePeriods = 0; //add extra 'future' periods to the results?
var [topHitIndex, topCorrelation] = res.indicesWithCorrelations[0];
var shifted = getNeedleScaleShift(needle, haystack, topHitIndex, futurePeriods);
console.log(shifted);

// {
//     slope: 10,
//     yIntercept: -5,
//     extractedHaystackSection: [ //original data from haystack
//          5, 15, 25, 35, 45,
//          55, 65, 75, 85, 95
//     ],
//     extractedHaystackSectionRescaled: [ //data scaled and shifted to resemble needle
//         1, 2, 3, 4,  5,
//         6, 7, 8, 9, 10
//     ]
// }

//now we get the mean squared error [MSE] between
// the original needle and the extracted, scaled+shifted needle
var mse = getMSE(needle, shifted.extractedHaystackSectionRescaled);
console.log({mse})

// { mse: 0 }

//remove overlaps in existing results: 
//var windowSize = needle.length;
//res = removeOverlaps(res, windowSize); 
```

## See Also

- [phase-align](https://www.npmjs.com/package/phase-align)
- [ndarray-convolve](https://www.npmjs.com/package/ndarray-convolve) - the `correlate` function does most of the work here for us


[![stonks](https://i.imgur.com/UpDxbfe.png)](https://www.npmjs.com/~stonkpunk)

---
_Source: https://npm.io/package/needle-haystack-1d · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
