-
Notifications
You must be signed in to change notification settings - Fork 3
/
PSO_PDS.m
260 lines (185 loc) · 5.83 KB
/
PSO_PDS.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
clc, clear all, close all%, format long E
tic
%Define qual pessoa esta sendo analisada
person = 1;
woman=0;
multiplier=1;
if woman
multiplier = 1.3
end
if person == 1
%load dados_com_pausa_bill.mat
load dados_com_pausa.mat
N = length(pitch3);
Ts = 1/Fs;
elseif person == 2
load dados_com_pausa_garth.mat
N = length(pitch3);
Ts = 1/Fs;
end
%BILL
%Trecho 1
ini = 10000;
fim = 14000;
%Trecho 2
% ini = 428000;
% fim = 440000;
%Charolote
% ini = 5000;
% fim = 13000;
%entrada = amp3_filt(ini:fim).*glottal_out(ini:fim);
entrada = amp3_filt(ini:fim).*impulsetrain(ini:fim);
saida = signal(ini:fim);
%entrada2 = amp3_filt.*glottal_out;
entrada2 = amp3_filt.*impulsetrain;
%Parametros da Simulação
particle_number = 2000;
N = 801; %Número de interações
ordem_trato = 2;
NVz = 2*ordem_trato;
%field x vs y
%bottom = [0, 0, 0, 0, 0, 0]; % zk e Fk
%top = [1-1e-12, 20, 1-1e-12, 200, 1-1e-12, 10];
%bottom = zeros(1,NVz+1); % zk e Fk e Rz
bottom = [];
top = [];
lim_F = 0;
for index=1:2:NVz
bottom = [bottom 0.95, lim_F];
%top = [top 1-1e-15, 10*8^(index/2-0.5)];
top = [top 1-1e-15, (1+index)*20];
lim_F = top(end);
end
%N1 usualmente é ~3ms ou 132@44100Hz
%N2 usualmente é ~1.5ms ou 70@44100Hz
top = [top 40 150/multiplier 80/multiplier]; %Rz N1 N2
bottom = [bottom 1 10 5];
MAX_LAG = 1000;
goal = [(1/max(xcorr(saida/max(saida),saida/max(saida))))] % goal absoluto
eps = 0.70;
error_parameters = length(goal);
%Parameteros SWO
inertia = 0.9;
initial_inertia = 0.9;
self_confidence = 0.8;
toward_best = 0.01;
speed = 0.2;
z_best_global = 0;
X_best_global = zeros(length(bottom),1);
z_best_individual = zeros(particle_number,1);
X_best_individual = zeros(length(bottom)+2,particle_number);
particle_parameters = length(bottom);
index_best_global = 1;
%Inicialização do SWARM
for k=1:particle_parameters
particles(k,:) = bottom(k)*ones(1,particle_number)+(top(k)-bottom(k))*rand(1, particle_number);
end
for k = 1:particle_number
sign_rand = (rand > 0.5)*2 - 1;
particles_dpos(:,k) = sign_rand*initial_inertia*(bottom' + (top-bottom)'.*rand(particle_parameters,1));
end
% best_particle = 1;
% best_z = eps/100000000000000;
% z_best = ones(particle_number,error_parameters)/100000000000000;
% particles_best = 10*ones(particle_parameters, particle_number);
% m=0;
% error = [1e3];
%
% bla=0;
% toc
hist = zeros(N,particle_number);
%%
m = 0;
vetor_best_global = zeros(1,N);
%while max(abs(error) > eps)
%while ((z_best_global < eps) && (m < N) )
while m <N
m = m+1;
%error_acum = 0;
for i=1:particle_number
zk = particles(1:2:(2*ordem_trato-1),i);
Fk = particles(2:2:(2*ordem_trato),i);
Rz = particles(end-2,i);
N1 = round(particles(end-1,i));
N2 = round(particles(end,i));
%best_z = desired_function(best_Q, best_R);
[z, lag, Rz] = desired_function(zk, Fk, Rz, N1, N2, Ts, entrada, saida, MAX_LAG, goal);
particles(end-2,i) = Rz; % atualiza o Rz da partícula recalculado dentro da desired_function
hist(m,i) = z;
if z > z_best_individual(i)
z_best_individual(i) = z;
X_best_individual(:,i) = [z; zk; Fk; Rz; N1; N2; lag];
if z > z_best_global
z_best_global = z;
X_best_global = [z; zk; Fk; Rz; N1; N2; lag];
index_best_global = i;
end
end
end
for i=1:particle_number
%if particles(:,i) ~= particles(:,best_particle)
%if i ~= index_best_global
for j=1:particle_parameters
particles_dpos(j,i) = speed*(toward_best*rand*(X_best_global(j+1) - particles(j,i)) + self_confidence*rand*(X_best_individual(j+1,i) - particles(j,i)) + inertia*particles_dpos(j,i));
particles(j,i) = particles_dpos(j,i) + particles(j,i);
while particles(j,i) > top(j) || particles(j,i) < bottom(j)
%bla = bla+1
if particles(j,i) > top(j)
%particles(j,i) = top(j)-abs(particles_dpos(j,i)/2);
particles(j,i) = top(j)-(particles(j,i)-top(j));
particles_dpos(j,i) = -particles_dpos(j,i);
elseif particles(j,i) < bottom(j)
particles(j,i) = bottom(j)+(bottom(j)-particles(j,i));
particles_dpos(j,i) = -particles_dpos(j,i);
end
end
end
%end
end
vetor_best_global(m) = X_best_global(1);
end
%%
z_best_global
zk = X_best_global(2:1:(2+ordem_trato-1))
Fk = X_best_global((2+ordem_trato):1:(2+2*ordem_trato-1))
Rz = X_best_global(end-3)
N1 = X_best_global(end-2)
N2 = X_best_global(end-1)
T=Ts;
%V_z = Rz*tf([1 -1 zeros(1,2*length(zk)-1)],1,Ts);
V_z = Rz*tf([1 -1 ],1,Ts);
for i=1:length(zk)
Vk_z(i) = tf((1-2*abs(zk(i))*cos(2*pi*Fk(i)*T)+abs(zk(i))^2),[1 -2*abs(zk(i))*cos(2*pi*Fk(i)*T) abs(zk(i))^2],Ts);
V_z = V_z*Vk_z(i);
end
%%
entrada2 = glottal_model(entrada2,N1,N2);
saida_calc = lsim(V_z,entrada2);
%soundsc(saida_calc,Fs)
%soundsc(signal,Fs)
plot(saida_calc/max(saida_calc))
hold
plot(signal/max(signal),'r')
legend('saida calc.', 'original')
%%
figure,
plot(xcorr(saida_calc(ini:fim)/max(saida_calc(ini:fim)),signal(ini:fim)/max(signal(ini:fim))))
hold
plot(xcorr(signal(ini:fim)/max(signal(ini:fim)),signal(ini:fim)/max(signal(ini:fim))))
legend('saida calc.', 'original')
figure,
plot(xcorr(saida_calc/max(saida_calc),signal/max(signal)))
hold
plot(xcorr(signal/max(signal),signal/max(signal)))
legend('saida calc.', 'original')
saida_calc = lsim(V_z,entrada);
%soundsc(saida_calc,Fs)
%soundsc(signal,Fs)
figure
plot(saida_calc/max(saida_calc))
hold
plot(saida/max(saida),'r')
legend('saida calc.', 'original')
figure
plot(vetor_best_global)
toc