WaveSimulator.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace WpfTest1.Toolkits
  7. {
  8. class WaveSimulator
  9. {
  10. //产生模拟波形的方法
  11. //generate a signal combining from 0-5 hz
  12. //take amplitude and phase shift into account
  13. public static List<double> getSimulatedWave()
  14. {
  15. double Fs = 100; //Sampling frequency
  16. double T = 1.0 / Fs; //Sample time
  17. int L = 1500; //Length of signal
  18. List<double> t = new List<double>(); //Time vector
  19. for (int i = 0; i < L; ++i)
  20. {
  21. t.Add(i * T);
  22. //System.Console.WriteLine("t[i]:" + t[i].ToString());
  23. }
  24. List<double> f = new List<double>(); //frequency
  25. for (int i = 1; i < 6; ++i)
  26. {
  27. f.Add(i);
  28. }
  29. //phase
  30. //int pstep = 0;
  31. int count = f.Count();
  32. //phase = 0:pstep:count * pstep;
  33. List<double> phase = new List<double>();
  34. for(int i = 0; i < count; ++i)
  35. {
  36. phase.Add(0);
  37. }
  38. //phase(2) = 0 / 180;
  39. //phase(3) = 00 / 180;
  40. //phase(4) = 0 / 180;
  41. //phase(5) = 0 / 180;
  42. //amplitude
  43. List<double> amplitude = new List<double>();
  44. for (int i = 0; i < count; ++i)
  45. {
  46. amplitude.Add(1);
  47. }
  48. for(int i = 2 - 1; i <= count - 1; ++i)
  49. {
  50. amplitude[i] = 0.9 * amplitude[i - 1];
  51. }
  52. amplitude[2-1] = 0.8;
  53. amplitude[3-1] = 0.1;
  54. amplitude[4-1] = 0.1;
  55. amplitude[5-1] = 0.2;
  56. List<double> y = new List<double>();
  57. for (int i = 0; i < t.Count; ++i)
  58. {
  59. y.Add(0);
  60. }
  61. for(int j = 1 - 1; j <= t.Count - 1; ++j)
  62. {
  63. for(int i = 1 - 1; i <= count - 1; ++i)
  64. {
  65. y[j] = y[j] + amplitude[i] * Math.Sin(2 * Math.PI * f[i] * t[j] + phase[i]);
  66. //System.Console.WriteLine("y[i]:"+ y[i].ToString());
  67. }
  68. }
  69. for(int i = 0; i < y.Count; ++i)
  70. {
  71. y[i] = 1000 * y[i] + 2000;
  72. }
  73. return y;
  74. }
  75. }
  76. }