Skip to content

Commit

Permalink
fix: result was wrong if only 1 point
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Dec 5, 2024
1 parent f2eeaba commit e12dacd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/SpectrumGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export class SpectrumGenerator {
Math.ceil((lastValue - this.from) / this.interval),
);
const middlePoint = Math.round((xPosition - this.from) / this.interval);

// PEAK SHAPE MAY BE ASYMMETRC (widthLeft and widthRight) !
// we calculate the left part of the shape

Expand Down Expand Up @@ -351,12 +352,13 @@ export class SpectrumGenerator {
* Resets the generator with an empty spectrum.
*/
public reset() {
const spectrum = this.data;

for (let i = 0; i < this.nbPoints; i++) {
spectrum.x[i] = this.from + i * this.interval;
if (this.nbPoints === 1) {
this.data.x[0] = (this.from + this.to) / 2;
} else {
for (let i = 0; i < this.nbPoints; i++) {
this.data.x[i] = this.from + i * this.interval;
}
}

return this;
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/__tests__/generateSpectrum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,40 @@ describe('generateSpectrum', () => {
});

describe('generateSpectrum with one peak and small window', () => {
it('No points', () => {
const spectrum = generateSpectrum([{ x: 1, y: 10 }], {
generator: {
from: 0,
to: 2,
nbPoints: 0,
peakWidthFct: () => 0.1,
shape: {
kind: 'gaussian',
},
},
});
expect(spectrum).toStrictEqual({
x: Float64Array.from([]),
y: Float64Array.from([]),
});
});
it('Very small window, only 1 point', () => {
const spectrum = generateSpectrum([{ x: 1, y: 10 }], {
generator: {
from: 0,
to: 2,
nbPoints: 1,
peakWidthFct: () => 0.1,
shape: {
kind: 'gaussian',
},
},
});
expect(spectrum).toStrictEqual({
x: Float64Array.from([1]),
y: Float64Array.from([10]),
});
});
it('should work with shape 9/3, peak width 0.2', () => {
const spectrum = generateSpectrum([[10, 1]], {
generator: {
Expand Down

0 comments on commit e12dacd

Please sign in to comment.