Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <vector>
  4. #include <math.h>
  5. #include <algorithm>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9. struct Data
  10. {
  11. double h;
  12. double dist;
  13. vector<double> alpha;
  14. double w;
  15. double d;
  16. };
  17. double funct(const double& x)
  18. {
  19. return sin(x) + 0.5;
  20. }
  21. double random(double Min, double Max)
  22. {
  23. double f = (double)rand() / RAND_MAX;
  24. return Min + f * (Max - Min);
  25. }
  26. double noise(const double& f, const double& a1, const double& a2)
  27. {
  28. return f + random(a1, a2);
  29. }
  30. double xk(const int& k, const double& xmax, const double& xmin, const int& K)
  31. {
  32. return xmin + k * (xmax - xmin) / K;
  33. }
  34. vector <double> function_noise(const vector<double>& x, const double& a1, const double& a2)
  35. {
  36. vector <double> func(101);
  37. for (auto i = 0; i <= 100; i++)
  38. {
  39. func[i] = noise(x[i], a1, a2);
  40. }
  41. return func;
  42. }
  43. vector <double> root_mean_sqare(const vector<double>& foo, vector<double>& alpha, const int& r)
  44. {
  45. vector<double> rms(101);
  46. int M = (r - 1) / 2;
  47. for (auto i = 0; i <= 100; i++)
  48. {
  49. double sum = 0;
  50. for (auto j = i - M; j <= i + M; j++)
  51. {
  52. try
  53. {
  54. foo.at(j);
  55. sum += alpha[j + M - i] / foo[j];
  56. }
  57. catch (const out_of_range & s) { sum += 0; }
  58. }
  59. rms[i] = sum;
  60. }
  61. return rms;
  62.  
  63. }
  64.  
  65. void set_alpha(vector<double>& alpha, const int& r)
  66. {
  67. alpha.clear();
  68. alpha.resize(r);
  69. alpha[(r - 1) / 2] = random(0, 1);
  70. for (auto i = (r - 1) / 2 - 1; i > 0; i--)
  71. {
  72. double sum1 = 0;
  73. for (auto i1 = i; i1 < r - i - 1; i1++) sum1 += alpha[i1];
  74. alpha[i] = 0.5 * random(0, 1 - sum1);
  75. sum1 = 0;
  76. }
  77. for (auto i = (r - 1) / 2 + 1; i < r - 1; i++) alpha[i] = alpha[r - i - 1];
  78. double sum2 = 0;
  79. for (auto i = 1; i < r - 1; i++) sum2 += alpha[i];
  80. alpha[0] = 0.5 * (1 - sum2);
  81. alpha[r - 1] = 0.5 * (1 - sum2);
  82. }
  83. double prob(const double& P, const double& ep, const double& xmax, const double& xmin)
  84. {
  85. return (log(1 - P)) / (log(1 - (ep) / (xmax - xmin)));
  86. }
  87. double dist(const double& w, const double& d)
  88. {
  89. return sqrt(w * w + d * d);
  90. }
  91. double w(const vector<double>& f_filtred)
  92. {
  93. double w = 0, z = 0;
  94. for (auto i = 1; i <= 100; i++)
  95. {
  96. z = abs(f_filtred[i] - f_filtred[i - 1]);
  97. if (z > w) w = z;
  98. }
  99. return w;
  100. }
  101. double d(const vector<double>& f_filtred, const vector<double>& f_noise)
  102. {
  103. double d = 0, z = 0;
  104. for (auto i = 0; i <= 100; i++)
  105. {
  106. z = abs(f_filtred[i] - f_noise[i]);
  107. if (z > d) d = z;
  108. }
  109. return d;
  110. }
  111. double J(double h, double w, double d)
  112. {
  113. return h * w + (1 - h) * d;
  114. }
  115. int main()
  116. {
  117. //const double M_PI = 3.141592653589793238463;
  118. vector<Data> data;
  119. int r1 = 3, r2 = 5;
  120. vector <double> alpha, alphamin;
  121. double xmin = 0, xmax = M_PI;
  122. double a1 = -0.25, a2 = 0.25;
  123. int K = 100;
  124. int L = 10;
  125. double h = 0;
  126. double P = 0.95;
  127. double ep = 0.01;
  128. double W, Wmin;
  129. double D, Dmin;
  130. double Dist, Distmin = 99999;
  131. double N = prob(P, ep, xmax, xmin);
  132. vector<double>Xk(101);
  133. for (int i = 0; i <= K; i++) Xk[i] = xk(i, xmax, xmin, K);
  134. vector<double> Fk(101);
  135. for (auto i = 0; i <= K; i++) Fk[i] = funct(Xk[i]);
  136. cout << endl << "function" << endl;
  137. for (auto i : Fk) cout << i << endl;
  138. cout << endl << endl;
  139. cout << endl << "noise" << endl;
  140. vector<double> f_noise = function_noise(Fk, a1, a2);
  141. for (auto i : f_noise) cout << i << endl;
  142. cout << endl << endl;
  143. //r=3
  144. vector<double> f_filtred;
  145. cout << " h dist alpha w d" << endl;
  146. for (auto l = 0; l <= 10; l++)
  147. {
  148. for (auto i = 0; i < N; i++)
  149. {
  150. set_alpha(alpha, r1);
  151. f_filtred = root_mean_sqare(f_noise, alpha, r1);
  152. W = w(f_filtred);
  153. D = d(f_filtred, f_noise);
  154. Dist = dist(W, D);
  155. if (Dist < Distmin)
  156. {
  157. Distmin = Dist;
  158. Wmin = W;
  159. Dmin = D;
  160. alphamin = alpha;
  161. }
  162. }
  163. h = (double)l / L;
  164. data.push_back({ h, Distmin, alphamin, Wmin, Dmin });
  165. cout << fixed << setprecision(1) << h << " " << setprecision(4) << Distmin << " [ ";
  166. for (auto i : alphamin) cout << i << " ";
  167. cout << "] " << Wmin << " " << Dmin << endl;
  168. }
  169. f_filtred = root_mean_sqare(f_noise, alphamin, r1);
  170. for (auto i : f_filtred) cout << i << endl;
  171. cout << endl;
  172. sort(data.begin(), data.end(), [](auto a, auto b) {return a.dist < b.dist; });
  173. cout << endl << " h* J w d" << endl;
  174. cout << setprecision(1) << data[0].h << " " << setprecision(4) << J(data[0].h, data[0].w, data[0].d) << " " << data[0].w << " " << data[0].d << endl;
  175. cout << endl << endl;
  176. //r = 5
  177. Distmin = 999999;
  178. data.clear();
  179. f_filtred.clear();
  180. alphamin.clear();
  181. Dmin = 0;
  182. Wmin = 0;
  183. cout << endl << endl;
  184. cout << " h dist alpha w d" << endl;
  185. for (auto l = 0; l <= 10; l++)
  186. {
  187. for (auto i = 0; i < N; i++)
  188. {
  189. set_alpha(alpha, r2);
  190. f_filtred = root_mean_sqare(f_noise, alpha, r2);
  191. W = w(f_filtred);
  192. D = d(f_filtred, f_noise);
  193. Dist = dist(W, D);
  194. if (Dist < Distmin)
  195. {
  196. Distmin = Dist;
  197. Wmin = W;
  198. Dmin = D;
  199. alphamin = alpha;
  200. }
  201. }
  202. h = (double)l / L;
  203. data.push_back({ h, Distmin, alphamin, Wmin, Dmin });
  204. cout << fixed << setprecision(1) << h << " " << setprecision(4) << Distmin << " [ ";
  205. for (auto i : alphamin) cout << i << " ";
  206. cout << "] " << Wmin << " " << Dmin << endl;
  207. }
  208. f_filtred = root_mean_sqare(f_noise, alphamin, r2);
  209. for (auto i : f_filtred) cout << i << endl;
  210. cout << endl;
  211. sort(data.begin(), data.end(), [](auto a, auto b) {return a.dist < b.dist; });
  212. cout << endl << " h* J w d" << endl;
  213. cout << setprecision(1) << data[0].h << " " << setprecision(4) << J(data[0].h, data[0].w, data[0].d) << " " << data[0].w << " " << data[0].d << endl;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement