Advertisement
aidanozo

Untitled

Nov 24th, 2024
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.89 KB | None | 0 0
  1. function [] = mcmmp_arx2(A, B, N, nr)
  2.  
  3. lambda = 1;
  4.  
  5. % Verificarea corectitudinii parametrilor de apel ai rutinei
  6. if(nargin < 4)
  7.     nr = 100;
  8. end
  9.  
  10. % Generare de date
  11. u = sign(randn(N, nr));
  12. e = randn(N, nr) * lambda^2;
  13. y = filter(B, A, u) + filter(1, A, e);
  14.  
  15. teta = zeros(4, nr);
  16.  
  17. % Aplicare metoda MCMMP
  18. for i=1:nr
  19.  
  20.     % Ry
  21.     [ry, k] = xcov(y(:, i),'biased');
  22.     ry = ry(k>=0);
  23.  
  24.     % Ru
  25.     [ru, k] = xcov(u(:, i),'biased');
  26.     ru = ru(k>=0);
  27.  
  28.     % Ryu
  29.     [ryu, k] = xcov(y(:, i), u(:,i), 'biased');
  30.     ryu = ryu(k>=-1);
  31.  
  32.     % Ruy
  33.     [ruy, k] = xcov(u(:,i), y(:, i), 'biased');
  34.     ruy = ruy(k>=-2);
  35.    
  36.     % Cacul matrice de covarianta a datelor (R)
  37.     R = [ry(1) ry(2) -ryu(2) -ryu(3);
  38.         ry(2) ry(1) -ryu(1) -ryu(2);
  39.         -ruy(3) -ruy(4) ru(1) ru(2);
  40.         -ruy(2) -ruy(3) ru(2) ru(1)];
  41.  
  42.     % Calcul vector de covarianta a datelor (r)
  43.     r = [-ry(2);
  44.          -ry(3);
  45.           ruy(2);
  46.           ruy(1)];
  47.  
  48.     % Estimare parametrii pentru realizarea curenta
  49.     t = R \ r;
  50.     teta(:, i) = t;
  51.  
  52.     % Estimare zgomot alb si dispersie
  53.     % La eroare avem matrice Toeplitz
  54.     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;
  55.     deviatiastandard(i, 1) = norm(eroare)/sqrt(N-size(teta,1));
  56.     dispersia(i,1) = deviatiastandard(i)^2;
  57. end
  58.  
  59. % Calcul medie parametrii estimati
  60. a1_m = mean(teta(1,:));
  61. a2_m = mean(teta(2,:));
  62. b1_m = mean(teta(3,:));
  63. b2_m = mean(teta(4,:));
  64.  
  65. % Afisare grafic
  66. figure
  67. plot(dispersia)
  68. hold on
  69. plot(zeros(nr, 1) + lambda^2);
  70. hold off
  71. title('Model ARX[2, 2]');
  72. xlabel('Numarul realizarii');
  73. ylabel('Varianta');
  74. legend('zgomot estimat', 'zgomot real');
  75. text(2, 1.2, ['Parametri adevarati a2 a3 b2 b3 : ', sprintf('%9.4f',[A(2) A(3) B(2) B(3)])]);
  76. text(2, 0.8, ['Parametri estimati  a1m a2m b1m b2m: ', sprintf('%9.4f',[a1_m a2_m b1_m b2_m])]);
  77.  
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement