Skip to content

Commit aec28f8

Browse files
committed
Add sine wave with noise data generator
1 parent 4499e61 commit aec28f8

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

packages/layerchart/src/lib/utils/genData.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,47 @@ export function getSpiral({
190190
};
191191
});
192192
}
193+
194+
interface SineWaveOptions {
195+
numPoints: number;
196+
frequency?: number;
197+
amplitude?: number;
198+
noiseLevel?: number;
199+
phase?: number;
200+
xMin?: number;
201+
xMax?: number;
202+
}
203+
204+
export function generateSineWave(options: SineWaveOptions) {
205+
const {
206+
numPoints,
207+
frequency = 1,
208+
amplitude = 1,
209+
noiseLevel = 0,
210+
phase = 0,
211+
xMin = 0,
212+
xMax = 2 * Math.PI,
213+
} = options;
214+
215+
if (numPoints <= 0) {
216+
throw new Error('Number of points must be greater than 0');
217+
}
218+
219+
const points: { x: number; y: number }[] = [];
220+
const xStep = (xMax - xMin) / (numPoints - 1);
221+
222+
for (let i = 0; i < numPoints; i++) {
223+
const x = xMin + i * xStep;
224+
225+
// Generate base sine wave
226+
const sineValue = amplitude * Math.sin(frequency * x + phase);
227+
228+
// Add random noise if specified
229+
const noise = noiseLevel > 0 ? (Math.random() - 0.5) * 2 * noiseLevel : 0;
230+
const y = sineValue + noise;
231+
232+
points.push({ x, y });
233+
}
234+
235+
return points;
236+
}

0 commit comments

Comments
 (0)