Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-ece authored Jun 2, 2021
1 parent 3b9dcc1 commit 035da06
Show file tree
Hide file tree
Showing 7 changed files with 536 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Fitness.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function [Error] = Fitness(R, Hfilt, Wfilt, N, Ord, Logcoef, alpha, beta, gamma, Cst2)
T1 = 0;
T2 = 0;
Cst = R(1) + 1;
Hfsiz = size(Hfilt, 1);
Rsiz1 = size(R, 1);
Hfilt_N = zeros(Hfsiz, 1);
q_N = zeros(Hfsiz, 1);
F = zeros(Hfsiz, 1);
Error = zeros(Rsiz1, 1);

Rsiz2 = size(R,2) - Cst2;
Bipo = zeros(Rsiz1, Rsiz2/2);
Aipo = zeros(Rsiz1, Rsiz2/2 + Cst2);

Aipo(:,1) = 1;
for i = 1:Rsiz1
for j = 1:Rsiz2/2
Bipo(i,j) = R(i,j + 1);
% Aipo(i,1 + j) = R(i,(Rsiz2/2) + j + 1); %### For O < 5
if (j < Cst) %### For O >= 5
Aipo(i,1 + j) = R(i,(Rsiz2/2) + j + 1); %### For O >= 5
end %### For O >= 5
end

[q,Q] = freqz(Bipo(i,:),Aipo(i,:),Hfsiz);


Hfilt_N = Hfilt .* N; % output with input white noise
q_N = q .* N; % output with input white noise
for j = 1:Hfsiz
F(j,1) = (abs(Hfilt_N(j) - q_N(j)))^ 2;
end
P_f = roots(Aipo);
T1 = sum(abs(P_f(find(abs(P_f) >= 1))));
T2 = sum(abs(P_f));
Error(i,1) = (alpha*((1/Hfsiz)*sum(F))) + (beta*((R(1)/(Ord-1)) * (T1/T2)));
Hfilt_N = 0;
q_N = 0;
end
end

10 changes: 10 additions & 0 deletions Main_IIR.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function [O h w] = Main_IIR()

O = 5;
b = [0.1084 0.5419 1.0837 1.0837 0.5419 0.1084]; %for case4
a = [1 0.9853 0.9738 0.3864 0.1112 0.0113]; %for case4

[h,w]=freqz(b,a,200);

end

24 changes: 24 additions & 0 deletions Matching.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function [Bipo Aipo Z_f P_f] = Matching(R, Cst2)

Rsiz = size(R,2) - Cst2;
Cst = R(1) + 1;

Bipo = zeros(1, Rsiz/2);
Aipo = zeros(1, Rsiz/2 + Cst2);
Aipo(1,1) = 1;
for i = 1:Rsiz/2
% for i = 1:5
Bipo(1,i) = R(i + 1);
% Aipo(1,i + 1) = R((Rsiz/2) + i + 1); %### For O < 5
if (i < Cst) %### For O >= 5
Aipo(1,i + 1) = R((Rsiz/2) + i + 1); %### For O >= 5
end %### For O >= 5
end
Z_f = roots(Bipo);
P_f = roots(Aipo);

end




97 changes: 97 additions & 0 deletions MonteCarlo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
clc;
clear all;
close all
tic

%% IIR Filter Fitness

[O Hfilt Wfilt] = Main_IIR();

Logcoef = 1;
alpha = 0.6; % 0.4 1 0.5 0.5
beta = 0.4; % 0.6 0 0.0 0.1
gamma = 0.0; % 0.0 1 0.5 0.4
Cst1 = 2; % number 1 for O<5 and 2 for >= for PSO function
Cst2 = 0; % number 1 for O<5,0 for >= for Fitness/IIRSOA function
nvar = (2 * (O - 1)) + Cst1;
pnvar = 0;
maxit = 1000000;

xmin(1) = 1;
xmax(1) = (O - 1);
xmin(2:nvar) = repmat(-2, 1, nvar - 1);
xmax(2:nvar) = repmat(+2, 1, nvar - 1);
Xpos = zeros(maxit,nvar);
Xfit = zeros(maxit,1);
N = rand(size(Hfilt,1),1);
% gbest = zeros(1,nvar);
gbest = 0;
gbestfit = inf;
meanfit = zeros(maxit,1);
minfit = zeros(maxit,1);

%% Main loop

for it = 1:maxit
Xpos(it,:) = xmin + (xmax - xmin) .* rand(1,nvar);
Xpos(it,1) = round(Xpos(it,1));
pnvar = (2 * Xpos(it,1)) + Cst1;
if pnvar < nvar
Xpos(it,pnvar+1:nvar) = 0;
end

%---------------------------------------------------------------------
Xfit(it) = Fitness(Xpos(it,1:pnvar),Hfilt,Wfilt,N,O,Logcoef,alpha,beta,gamma,Cst2);
%---------------------------------------------------------------------

if Xfit(it) < gbestfit
gbest = Xpos(it,1:pnvar);
gbestfit = Xfit(it);
end
meanfit(it) = sum(Xfit)/it;
minfit(it) = min(Xfit(1:it));
D = it/maxit;
if ( D == 1/5) | ( D == 2/5) | ( D == 3/5) | ( D == 4/5)
it
end
end

time = toc
gbest
gbestfit
Minimum_Fitness = minfit(it)
Mean_Fitness = meanfit(it)
[Bsoa Asoa Z_f P_f] = Matching(gbest,Cst2)

% xx = [1,100,200,300,400,500];
% pdf(Xfit([1,100,200,300,400,500]),xx)

figure(1);
plot(1:maxit,meanfit,'r--','LineWidth',2)
ylim([-1 inf]);
% xlim([-1 inf]);
legend('MeanFitness')
xlabel('Iterations')
ylabel('Value')
title('Monte Carlo');
grid on

figure(2);
plot(1:maxit,minfit,'b--','LineWidth',2);
axis([0 inf -.01 inf])
grid on
legend('MinFitness')
xlabel('Iterations')
ylabel('Value')
title('Monte Carlo');

figure(3);
zplane(Z_f,P_f); %%% Displays the poles and zeros of discrete-time systems.
legend('Zero','Pole');
xlabel('Real Part');
ylabel('Imaginary Part');
title('MonteCarlo: Pole-Zero Plot');




Loading

0 comments on commit 035da06

Please sign in to comment.