Skip to content

Commit 403bbba

Browse files
committed
fix: calibrateX if not enough peaks found
1 parent f6c8dc8 commit 403bbba

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/filters/x/__tests__/calibrateX.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,44 @@ describe('calibrateX', () => {
8787
Float64Array.from([1, 1, 1, 1, 700, 1, 5, 1, 1, 1, 1, 1, 1]),
8888
);
8989
});
90+
91+
it('2 peaks', () => {
92+
let data = {
93+
x: Float64Array.from([
94+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
95+
20,
96+
]),
97+
y: Float64Array.from([
98+
1, 1, 1, 1, 1, 700, 1, 1, 1, 1, 1, 1, 1, 1, 1, 700, 1, 1, 1, 1, 1,
99+
]),
100+
};
101+
102+
let gsdOptions = {
103+
minMaxRatio: 0.4,
104+
realTopDetection: true,
105+
smoothY: true,
106+
sgOptions: {
107+
windowSize: 5,
108+
polynomial: 3,
109+
},
110+
};
111+
112+
let shifted = calibrateX(data, {
113+
targetX: 6,
114+
gsd: gsdOptions,
115+
nbPeaks: 2,
116+
});
117+
// because we look for the real maximum it is not exactly 2
118+
expect(shifted.data.x).toMatchCloseTo(
119+
Float64Array.from([
120+
-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
121+
16,
122+
]),
123+
);
124+
expect(shifted.data.y).toStrictEqual(
125+
Float64Array.from([
126+
1, 1, 1, 1, 1, 700, 1, 1, 1, 1, 1, 1, 1, 1, 1, 700, 1, 1, 1, 1, 1,
127+
]),
128+
);
129+
});
90130
});

src/filters/x/calibrateX.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function calibrateX(
6565
.sort((a, b) => b.y - a.y)
6666
.slice(0, nbPeaks);
6767

68-
if (peaks.length === 0) return { data };
68+
if (peaks.length < nbPeaks) return { data };
6969

7070
const middle = xMean(peaks.map((peak) => peak.x));
7171
return { data: { x: xAdd(data.x, targetX - middle), y: data.y } };

0 commit comments

Comments
 (0)