Advertisement
aidanozo

Untitled

Dec 9th, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.82 KB | None | 0 0
  1. function [] = mcmmp_arx2(A, B, N, nr)
  2.  
  3. lambda = 1;
  4.  
  5. if(nargin < 4)
  6.     nr = 100;
  7. end
  8.  
  9. u = sign(randn(N, nr));
  10. e = randn(N, nr) * lambda^2;
  11. y = filter(B, A, u) + filter(1, A, e);
  12.  
  13. teta = zeros(4, nr);
  14.  
  15. for i=1:nr
  16.  
  17.     [ry, k] = xcov(y(:, i),'biased');
  18.     ry = ry(k>=0);
  19.  
  20.     [ru, k] = xcov(u(:, i),'biased');
  21.     ru = ru(k>=0);
  22.  
  23.     [ryu, k] = xcov(y(:, i), u(:,i), 'biased');
  24.     ryu = ryu(k>=-1);
  25.  
  26.     [ruy, k] = xcov(u(:,i), y(:, i), 'biased');
  27.     ruy = ruy(k>=-2);
  28.    
  29.     R = [ry(1) ry(2) -ryu(2) -ryu(3);
  30.         ry(2) ry(1) -ryu(1) -ryu(2);
  31.         -ruy(3) -ruy(4) ru(1) ru(2);
  32.         -ruy(2) -ruy(3) ru(2) ru(1)];
  33.  
  34.     r = [-ry(2);
  35.          -ry(3);
  36.           ruy(2);
  37.           ruy(1)];
  38.  
  39.     t = R \ r;
  40.     teta(:, i) = t;
  41.  
  42.     eroare = y(:,i) - [[0; -1*y(1:(N-1),i)] [0; 0; -1*y(1:(N-2),i)] [0; u(1:(N-1),i)] [0; 0; u(1:(N-2), i)]]*t;
  43.     deviatiastandard(i, 1) = norm(eroare)/sqrt(N-size(teta,1));
  44.     dispersia(i,1) = deviatiastandard(i)^2;
  45. end
  46.  
  47. a1_m = mean(teta(1,:));
  48. a2_m = mean(teta(2,:));
  49. b1_m = mean(teta(3,:));
  50. b2_m = mean(teta(4,:));
  51.  
  52. figure
  53. plot(dispersia)
  54. hold on
  55. plot(zeros(nr, 1) + lambda^2);
  56. hold off
  57. title('Model ARX[2, 2]');
  58. xlabel('Numarul realizarii');
  59. ylabel('Varianta');
  60. legend('zgomot estimat', 'zgomot real');
  61.  
  62. x_limits = xlim;
  63. y_limits = ylim;
  64.  
  65. x_text = mean(x_limits);
  66. y_text1 = y_limits(2) - 0.05 * (y_limits(2) - y_limits(1));
  67. y_text2 = y_limits(2) - 0.1 * (y_limits(2) - y_limits(1));
  68.  
  69. text(x_text, y_text1, ['Parametri adevarati: ', sprintf('%9.4f',[A(2) A(3) B(2) B(3)])], ...
  70.     'HorizontalAlignment', 'center', 'VerticalAlignment', 'top', 'FontWeight', 'bold');
  71. text(x_text, y_text2, ['Parametri estimati: ', sprintf('%9.4f',[a1_m a2_m b1_m b2_m])], ...
  72.     'HorizontalAlignment', 'center', 'VerticalAlignment', 'top', 'FontWeight', 'bold');
  73.  
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement