fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. #define PI 3.14159265
  8. #define e 2.71828182
  9.  
  10. int main()
  11. {
  12. double period;
  13. double frequency;
  14. double angularfrequency;
  15. double amplitude;
  16. double time=0;
  17. double samplingrate;
  18. double endtime;
  19. double starttime;
  20. double result;
  21. double phase;
  22. double alpha;
  23. double gradient;
  24.  
  25.  
  26. /*periodic rectangular signal*/
  27.  
  28. cout << "enter the frequency of the signal" << endl;
  29. cin >> frequency;
  30.  
  31. cout << "enter the amplitude of the signal" << endl;
  32. cin >> amplitude;
  33.  
  34. cout << "enter the sampling rate of the signal" << endl;
  35. cin >> samplingrate;
  36.  
  37. cout << "enter the start time of the signal" << endl;
  38. cin >> starttime;
  39.  
  40. cout << "enter the end time of the signal" << endl;
  41. cin >> endtime;
  42.  
  43. time = starttime;
  44.  
  45. period = 1 / frequency;
  46.  
  47. for(time = starttime; time <= endtime; time += (1/samplingrate))
  48. {
  49.  
  50. if (period <= time <= period/2)
  51. {
  52. result = amplitude;
  53. }
  54. else
  55. {
  56. result = (-1)*amplitude;
  57. }
  58.  
  59. cout << time << " " << result << endl;
  60. }
  61. }
  62.  
Success #stdin #stdout 0s 3432KB
stdin
0.5
1.5
10.0
0.0
2.7
stdout
enter the frequency of the signal
enter the amplitude of the signal
enter the sampling rate of the signal
enter the start time of the signal
enter the end time of the signal
0	1.5
0.1	1.5
0.2	1.5
0.3	1.5
0.4	1.5
0.5	1.5
0.6	1.5
0.7	1.5
0.8	1.5
0.9	1.5
1	1.5
1.1	1.5
1.2	1.5
1.3	1.5
1.4	1.5
1.5	1.5
1.6	1.5
1.7	1.5
1.8	1.5
1.9	1.5
2	1.5
2.1	1.5
2.2	1.5
2.3	1.5
2.4	1.5
2.5	1.5
2.6	1.5