generator.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // 振幅数据生成器(7个典型CNC机床阶段)
  2. // 阶段:待机、启动、自检、粗加工、精加工、回零、结束
  3. function randomInt(min, max) {
  4. return Math.floor(Math.random() * (max - min + 1)) + min;
  5. }
  6. function generateStageData(length, base, fluctuation) {
  7. return Array.from({ length }, () => base + (Math.random() - 0.5) * fluctuation);
  8. }
  9. function generateAmplitudeCycle() {
  10. const stageNames = [
  11. '待机', '启动', '自检', '粗加工', '精加工', '回零', '结束'
  12. ];
  13. const stageCount = stageNames.length;
  14. let stages = [];
  15. for (let i = 0; i < stageCount; i++) {
  16. stages.push(15); // 每阶段15秒
  17. }
  18. const duration = stages.reduce((a, b) => a + b, 0);
  19. // 各阶段振幅特征,顺序必须和stageNames一致
  20. const stageParams = [
  21. { base: 10, fluctuation: 2 }, // 待机
  22. { base: 18, fluctuation: 4 }, // 启动
  23. { base: 15, fluctuation: 3 }, // 自检
  24. { base: 30, fluctuation: 8 }, // 粗加工
  25. { base: 22, fluctuation: 5 }, // 精加工
  26. { base: 14, fluctuation: 3 }, // 回零
  27. { base: 11, fluctuation: 2 } // 结束
  28. ];
  29. let cycle = [];
  30. for (let i = 0; i < stageCount; i++) {
  31. const { base, fluctuation } = stageParams[i];
  32. cycle = cycle.concat(generateStageData(stages[i], base, fluctuation));
  33. }
  34. // 随机插入一个异常点
  35. const abnormalIndex = randomInt(0, cycle.length - 1);
  36. cycle[abnormalIndex] = cycle[abnormalIndex] + randomInt(20, 40); // 远超正常范围
  37. return { cycle, stages, duration, stageNames };
  38. }
  39. module.exports = { generateAmplitudeCycle };