PSO ile FIR Filtre Katsayılarının Bulunması

Aşağıda PSO ile FIR filtre katsayılarının bulunduğu Matlab kodları verilmiştir.

  1.  
  2. g_PARTICLE = 100;
  3. g_PARAMETER = 100;
  4. g_ITERATION = 2000;
  5.  
  6. PARTICLE = rand(g_PARTICLE, g_PARAMETER);
  7. BEST_PARTICLE = PARTICLE;
  8. VELOCITY = rand(g_PARTICLE, g_PARAMETER);
  9. GLOBAL_BEST = zeros(1, g_PARAMETER);
  10.  
  11. f_1 = 10;
  12. f_2 = 500;
  13. f_3 = 1000;
  14. f_s = 5000;
  15. %% INPUT CREATE
  16. n_s = 1 : 1000;
  17. INPUT = cos(2 * pi * n_s * f_1 / f_s) + cos(2 * pi * n_s * f_2 / f_s) + ...
  18.         cos(2 * pi * n_s * f_3 / f_s);
  19. [n_a n_b] = fir1(g_PARAMETER - 1, 200 / f_s, 'low');
  20. OUTPUT = filter(n_a, n_b, INPUT);
  21. figure, plot(OUTPUT);
  22.  
  23. for n_p = 1 : g_PARTICLE
  24.     FITNESS_VALUE_old(n_p) = FILTER_BLOCK(PARTICLE(n_p, :), INPUT, OUTPUT);
  25. end
  26.  
  27. for n_i = 1 : g_ITERATION
  28.     [PARTICLE VELOCITY] = UPDATE_BLOCK(PARTICLE, BEST_PARTICLE,  VELOCITY, ...
  29.                           GLOBAL_BEST, g_PARTICLE);
  30.    
  31.     for n_p = 1 : g_PARTICLE
  32.         FITNESS_VALUE(n_p) = FILTER_BLOCK(PARTICLE(n_p, :), INPUT, OUTPUT);
  33.     end
  34.     ind = find(FITNESS_VALUE < FITNESS_VALUE_old);
  35.     FITNESS_VALUE_old(ind) = FITNESS_VALUE(ind);
  36.     BEST_PARTICLE(ind, : ) = PARTICLE(ind, : );
  37.     [min_data min_ind] = min(FITNESS_VALUE_old);
  38.     GLOBAL_BEST = BEST_PARTICLE(min_ind, : );
  39.     min_data
  40.    
  41. end
  42.  
  43. OUTPUT_TEST = filter(GLOBAL_BEST, 1, INPUT);
  44. figure, plot(OUTPUT_TEST);
  1. function FITNESS_VALUE = FILTER_BLOCK(PARTICLE, INPUT, OUTPUT)
  2.    
  3.     FILTER_OUTPUT = filter(PARTICLE, 1, INPUT);
  4.     ERROR = OUTPUT - FILTER_OUTPUT;
  5.     FITNESS_VALUE = sum(0.5 * ERROR.^2);
  1. function [PARTICLE VELOCITY] = UPDATE_BLOCK(PARTICLE, BEST_PARTICLE,  ...
  2.               VELOCITY, GLOBAL_BEST, g_PARTICLE)
  3.    
  4.     for n_i = 1 : g_PARTICLE
  5.         VELOCITY(n_i, : )= 0.76 * (VELOCITY(n_i, : ) ...
  6.             + 2.1 * rand * (BEST_PARTICLE(n_i, : ) - PARTICLE(n_i, : )) ...
  7.             + 2.1 * rand * (GLOBAL_BEST - PARTICLE(n_i, : ))) ...
  8.             + 2^-11 * rand(size(VELOCITY(n_i , : ))) - 2^-12;    
  9.     end
  10.    
  11.     VELOCITY = min(VELOCITY, 1.0 * ones(size(VELOCITY)));
  12.     VELOCITY = max(VELOCITY,-1.0 * ones(size(VELOCITY)));
  13.    
  14.     PARTICLE = PARTICLE + VELOCITY;