mljs / spectra-processing

Various methods to help spectra processing
https://mljs.github.io/spectra-processing/
MIT License
7 stars 12 forks source link

feat: implement resampling within xSampling to handle array lengths larger than the original size #200

Closed josoriom closed 11 months ago

codecov[bot] commented 11 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (6c2a8bb) 91.69% compared to head (48f89bc) 91.70%.

:exclamation: Current head 48f89bc differs from pull request most recent head 32a5c04. Consider uploading reports for the commit 32a5c04 to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #200 +/- ## ========================================== + Coverage 91.69% 91.70% +0.01% ========================================== Files 168 168 Lines 3286 3305 +19 Branches 824 824 ========================================== + Hits 3013 3031 +18 - Misses 266 267 +1 Partials 7 7 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

josoriom commented 11 months ago

@lpatiny I found a function (xSampling) in this package that performs down-sampling

josoriom commented 11 months ago

I would like you add the following test case (and I'm not convinced about the current result). Not sure what you expect the algorithm to do.

xSampling([0], { length: 2 });
xSampling([1, 2], { length: 3 });
xSampling([1, 2], { length: 5 });
xSampling([1, 2, 3], { length: 5 });
xSampling([1, 2, 4], { length: 5 });
xSampling([1, 2, 4], { length: 7 });
xSampling([1, 2, 3], { length: 4 });
xSampling([1, 2, 4], { length: 4 });
xSampling([1, 2, 1], { length: 4 });
xSampling([1, 2, -1], { length: 5 });

Do you have a reference link to the current implementation because the results seems difficult to justify.

Defining the step size was where I made the mistake. The source from wikipedia Linear interpolation

$$ y = y_0 \cdot \left(1 - \frac{x - x_0}{x_1 - x_0}\right) + y_1 \cdot \frac{x - x_0}{x_1 - x_0} $$

result[i] = array[floor] * (1 - diff) + array[ceil] * diff;

where

let diff = currentIndex - floor;

and

ceil - floor = 1