Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. // Lab2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <math.h>
  7. #include <fstream>
  8. #include <stdlib.h>
  9. #include <time.h>
  10.  
  11. using namespace std;
  12.  
  13. int sample_count(float time, float sample) {
  14. int sample_count = 0;
  15.  
  16. for (float i = 0; i <= time; i += sample) {
  17. sample_count++;
  18. }
  19.  
  20. return sample_count;
  21. }
  22.  
  23. void amplitude_modulation(float amplitude_info, float frequency_info, float phase_info, float amplitude_carry, float frequency_carry, float phase_carry, float time, float sample, float kAM, fstream& file, string name)
  24. {
  25. file.open(name, ios::out);
  26.  
  27. int N = sample_count(time, sample);
  28. float *m = new float[N]; //sygnał nośny
  29. float *s = new float[N]; //sygnał informacyjny
  30. float *w = new float[N]; //sygnał zmodulowany
  31. float i = 0;
  32.  
  33. for (int t = 0; t < N; t++)
  34. {
  35. s[t] = amplitude_info * sin(2 * M_PI * frequency_info * i + phase_info);
  36. m[t] = amplitude_carry * sin(2 * M_PI * frequency_carry * i + phase_carry);
  37. w[t] = m[t] * ((kAM * s[t]) + 1);
  38. file << s[t] << "," << m[t] << "," << w[t] << endl;
  39. i += sample;
  40. if (i > time) {
  41. break;
  42. }
  43. }
  44.  
  45. delete m, s, w;
  46. file.close();
  47. }
  48.  
  49. void frequency_modulation(float amplitude_signal, float pulsation_signal, float phase_signal, float time, float sample, float amplitude, float frequency, float kFM, fstream& file, string name)
  50. {
  51. file.open(name, ios::out);
  52.  
  53. int N = sample_count(time, sample);
  54.  
  55. float *s = new float[N];
  56. float *w = new float[N];
  57. float i = 0;
  58.  
  59. int index = 0;
  60. for (float i = 0; i <= time; i += sample) {
  61. s[index] = amplitude_signal * sin(pulsation_signal * i + phase_signal);
  62. index++;
  63. }
  64.  
  65. for (int t = 0; t < N; t++)
  66. {
  67. float O = (2 * M_PI * frequency * t) + (kFM *s[t]);
  68. w[t] = amplitude * cos(O);
  69. file << s[t] << "," << w[t] << endl;
  70. i += sample;
  71. }
  72.  
  73. file.close();
  74. }
  75.  
  76. int main()
  77. {
  78. fstream modulation;
  79. amplitude_modulation(1, 40, 1, 2, 20, 1, 2, 0.001, 2, modulation, "amplitude_modulation.csv");
  80. frequency_modulation(1, 251.327, 1, 2, 0.001, 1, 5, 20, modulation, "frequency_modulation.csv");
  81. system("pause");
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement