-
Notifications
You must be signed in to change notification settings - Fork 1
/
PSO.m
150 lines (126 loc) · 4.78 KB
/
PSO.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
%% PSO for IIR filter design
clc;
clear all;
close all;
format shortg
prompt = {'Please enter the number of run:'};
title = 'PSO Algorithm';
dims = [1 45];
nline = 1;
definput = {'1','a'};
answer = inputdlg(prompt,title,dims,definput)
Run_Num = answer(1,:);Run_Num=str2num(Run_Num{:});
n = 0;
for n = 1:Run_Num
tic
n
%% IIR Filter Fitness
[Hfilt Wfilt] = IIR_main();
npop = 50;
nvar = 11;
w = 1;
maxit = 200;
wdamp = 0.99;
c1 = 2;
c2 = 2;
xmin = -2 ;
xmax = +2 ;
dx =xmax-xmin ;
vmax =0.1*dx ;
empty_particle.position =[] ;
empty_particle.velocity =[] ;
empty_particle.cost =[] ;
empty_particle.pbest =[] ;
empty_particle.pbestcost =[] ;
particle = repmat(empty_particle,npop,1) ;
N = rand(size(Hfilt,1),1) ;
gbest = zeros(maxit,nvar) ;
gbestcost = zeros(maxit,1) ;
for it = 1:maxit
if it == 1
gbestcost(1) = inf;
for i = 1:npop
particle(i).velocity = zeros(1,nvar) ;
particle(i).position = xmin+(xmax-xmin).*rand(1,nvar) ;
%%%%%%%%%%%%%%%%%%%%
particle(i).cost = Fitness(particle(i).position,Hfilt,Wfilt,N) ;
%%%%%%%%%%%%%%%%%%%%
particle(i).pbest = particle(i).position ;
particle(i).pbestcost = particle(i).cost ;
if particle(i).pbestcost<gbestcost(it)
gbest(it,:) = particle(i).pbest ;
gbestcost(it) = particle(i).pbestcost ;
end
end
else
gbest(it,:) = gbest(it-1,:) ;
gbestcost(it) = gbestcost(it-1) ;
for i = 1:npop
particle(i).velocity = w*particle(i).velocity...
+c1*rand*(particle(i).pbest-particle(i).position)...
+c2*rand*(gbest(it,:)-particle(i).position) ;
particle(i).velocity = min(max(particle(i).velocity,-vmax),vmax) ;
particle(i).position = particle(i).position+particle(i).velocity ;
particle(i).position = min(max(particle(i).position,xmin),xmax) ;
%%%%%%%%%%%%%%%%%%%
particle(i).cost = Fitness(particle(i).position,Hfilt,Wfilt,N) ;
%%%%%%%%%%%%%%%%%%%
if particle(i).cost<particle(i).pbestcost
particle(i).pbest = particle(i).position ;
particle(i).pbestcost = particle(i).cost ;
if particle(i).pbestcost<gbestcost(it)
gbest(it,:) = particle(i).pbest ;
gbestcost(it) = particle(i).pbestcost ;
end
end
end
end
disp(['Iter= ' num2str(it) ' // Best Cost = ' num2str(gbestcost(it))]);
w = w*wdamp ;
end
Bests(n) = gbestcost(it) ;
RunTime(n) = toc ;
end
disp([' ']);
disp([' ']);
disp(['-----------------------------------------------']);
disp(['Number of run = ' num2str(Run_Num)]);
disp([' ']);
disp([' ']);
disp(['**************** Statistical indexes : Time ****************']);
disp(['------------------------------------------------']);
disp(['Per run = ' num2str(RunTime)]);
disp(['Average = ' num2str(mean(RunTime))]);
disp(['Standard deviation = ' num2str(std(RunTime))]);
disp(['Maximum = ' num2str(max(RunTime))]);
disp(['Minimum = ' num2str(min(RunTime))]);
disp([' ']);
disp([' ']);
disp(['***************** Statistical indexes : Fitness ****************']);
disp(['-----------------------------------------------']);
disp(['Number of run = ' num2str(Run_Num)]);
disp(['Best cost per run = ' num2str(Bests)]);
disp(['Average = ' num2str(mean(Bests))]);
disp(['Standard deviation = ' num2str(std(Bests))]);
disp(['Maximum = ' num2str(max(Bests))]);
disp(['Minimum = ' num2str(min(Bests))]);
%% Implementation of IIR IIPO *******************************
disp([ ' Best Solution = ' num2str(gbest(it,:))])
[Bsoa Asoa Z_f P_f] = Matching(gbest(it,:))
IIR_main();
figure(1);
plot(gbestcost,'.b','LineWidth',1);
legend('Bests')
xlabel('Iteration')
ylabel('Fitness')
figure(2);
zplane(Z_f,P_f); %%% Displays the poles and zeros of discrete-time systems.
legend('Zero','Pole');
xlabel('Real Part');
ylabel('Imaginary Plot');
% title('Pole-Zero Plot in IPO');
figure(3);
H = abs(Hfilt);
Hdb=20*log10(H);
plot(Wfilt/512,Hdb);grid
% title('Magnitude response of a chebyshev I bandpass filter');