Skip to content

Commit fa842db

Browse files
committed
fix: solve problem with first value of lastK = -1 in gsd.
Solve #6
1 parent 92fb43c commit fa842db

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/gsd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function gsd(x, yIn, options) {
165165

166166
let signals = new Array(minddY.length);
167167
let signalsLen = 0;
168-
let lastK = 0;
168+
let lastK = -1;
169169
let possible, frequency, distanceJ, minDistance, gettingCloser;
170170
for (let j = 0; j < minddY.length; ++j) {
171171
frequency = X[minddY[j]];

test/test.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
'use strict';
22

33
var fs = require('fs');
4-
var peakPicking = require('..');
4+
const gsd = require('..').gsd;
5+
6+
function lorentzian(x, x0 = 0, gamma = 1) {
7+
return (gamma * gamma) / (Math.PI * gamma * (gamma * gamma + (x - x0) * (x - x0)));
8+
}
59

610
describe('Global spectra deconvolution simple simulated spectrum', function () {
711

812
// Test case obtained from Pag 443, Chap 8.
913
it('Should provide the right result ...', function () {
1014
var spectrum = JSON.parse(fs.readFileSync('./test//C2.json', 'utf-8'));
11-
var result = peakPicking.gsd(spectrum[0], spectrum[1], {//noiseLevel: 0.001,
15+
var result = gsd(spectrum[0], spectrum[1], {//noiseLevel: 0.001,
1216
minMaxRatio: 0,
1317
realTopDetection: true,
1418
smoothY: false
@@ -23,5 +27,27 @@ describe('Global spectra deconvolution simple simulated spectrum', function () {
2327
//result[1].width.should.approximately(0.006,5e-4);
2428

2529
});
30+
31+
it('Should give 10 peaks', function () {
32+
const size = 300;
33+
const fourth = size / 11;
34+
var times = new Array(size);
35+
var tic = new Array(size);
36+
console.log("here2");
37+
38+
for (var i = 0; i < size; ++i) {
39+
times[i] = i;
40+
tic[i] = lorentzian(i, fourth) + 2* lorentzian(i, 2*fourth) + lorentzian(i, 3*fourth) + 2* lorentzian(i, 4*fourth) + lorentzian(i, 5*fourth)+ 2* lorentzian(i, 6*fourth) + lorentzian(i, 7*fourth)+ 2* lorentzian(i, 8*fourth) + lorentzian(i, 9*fourth)+ 2* lorentzian(i, 10*fourth);
41+
}
42+
var ans = gsd(times, tic, {
43+
noiseLevel: 0,
44+
realTopDetection: false,
45+
smoothY: false,
46+
sgOptions: {windowSize: 5, polynomial: 3}
47+
});
48+
49+
ans.length.should.equal(10);
50+
51+
});
2652
});
2753

0 commit comments

Comments
 (0)