MFO算法优化滤波器设计——matlab

 从阻抗出发计算滤波器的ABCD矩阵,这里是二阶BVD模型的级联

function [ABCD_params]=count_ABCD_fun(f00,n,ccl)

N_ccl=length(ccl);
%把ccl变成三列的矩阵
C0CmLm=zeros(N_ccl/3,3);
%for kk=1:N_ccl/3
    k1=1;
    ii=1;
    for k1=1:N_ccl/3
        C0CmLm(k1,1)=ccl(ii);
        ii=ii+1;
        C0CmLm(k1,2)=ccl(ii);
        ii=ii+1;
        C0CmLm(k1,3)=ccl(ii);
        ii=ii+1;
    end
%end

    C0CmLm(:,1)=C0CmLm(:,1)*10^(-14);
    C0CmLm(:,2)=C0CmLm(:,2)*10^(-17);
    C0CmLm(:,3)=C0CmLm(:,3)*10^(-11);
    
    
% C0CmLm_10=[10^(-15),10^(-18),10^(-12);
%         10^(-15),10^(-18),10^(-12);
%         10^(-15),10^(-18),10^(-12);
%         10^(-15),10^(-18),10^(-12)];
    
%C0CmLm=C0CmLm.*C0CmLm_10;  

%计算ABCD矩阵
N=size(C0CmLm,1);% 矩阵行数

if n==0
   for i=1:N
       Z(i)=1./(1j.*f00.*C0CmLm(i,1)+1./(1j.*f00.*C0CmLm(i,3)+1./(1j.*f00.*C0CmLm(i,2))));
       if i==1
           ABCD_params=[1,Z(i);0,1];
       else
          if mod(i,2)==0   %   偶数
          ABCD_params=ABCD_params*[1,0;1/Z(i),1];
          else           
          ABCD_params=ABCD_params*[1,Z(i);0,1];    
          end 
       end
   end
else
    for i=1:N
       Z(i)=1./(1j.*f00.*C0CmLm(i,1)+1./(1j.*f00.*C0CmLm(i,2)+1./(1j.*f00.*C0CmLm(i,3))));
        if i==1
           ABCD_params=ABCD_params*[1,0;1/Z(i),1];
       else
          if mod(i,2)==0   %   偶数
          ABCD_params=ABCD_params*[1,Z(i);0,1];    
          else           
          ABCD_params=ABCD_params*[1,0;1/Z(i),1];
          end 
       end
   end
end

从kt2出发计算

function [ABCD_params]=count_ABCD_fun(f00,n,ccl)
% 固定变量kt2,Q,Rs 不定变量fs,C0
kt2=0.1;
Q=2000;
Rs=0;

% 
% Rm=pi^2/(8*2*pi*fs*C0*kt2*Q);
% Lm=pi^2/(8*(2*pi*fs)^2*C0*kt2);
% Cm=(8*C0*kt2)/(pi^2)
% Zm=Rm+1j*f00*Lm+1/(1j*f00*Cm);
% Z_R=Rs+1/(1/Zm+1j*f00*C0);

N_ccl=length(ccl);
%把ccl变成三列的矩阵
fsC0=zeros(N_ccl/2,2);

  
    ii=1;
    for k1=1:N_ccl/2
        fsC0(k1,1)=ccl(ii);
        fsC0(k1,1)=fsC0(k1,1)*10^(6);
        ii=ii+1;
        fsC0(k1,2)=ccl(ii);
        fsC0(k1,2)=fsC0(k1,2)*10^(-15);
        ii=ii+1;
    end
% fsC0

    
    
 

%计算ABCD矩阵
N=size(fsC0,1);% 矩阵行数

if n==0
   for i=1:N
       %Z(i)=1./(1j.*f00.*fsC0(i,1)+1./(1j.*f00.*fsC0(i,3)+1./(1j.*f00.*fsC0(i,2))));
       Rm=(pi^2)/(8*2*3.14159*fsC0(i,1)*fsC0(i,2)*kt2*Q);
       Lm=(pi^2)/(8*(2*3.14159*fsC0(i,1))^2*fsC0(i,2)*kt2);
       Cm=(8*fsC0(i,2)*kt2)/(pi^2);
       Zm=Rm+1j*f00*Lm+1/(1j*f00*Cm);
       Z_R(i)=Rs+1/(1/Zm+1j*f00*fsC0(i,2));
       
       if i==1
           ABCD_params=[1,Z_R(i);0,1];
       else
          if mod(i,2)==0   %   偶数
          ABCD_params=ABCD_params*[1,0;1/Z_R(i),1];
          else           
          ABCD_params=ABCD_params*[1,Z_R(i);0,1];    
          end 
       end
   end
else
    for i=1:N
      % Z(i)=1./(1j.*f00.*fsC0(i,1)+1./(1j.*f00.*fsC0(i,2)+1./(1j.*f00.*fsC0(i,3))));
       Rm=pi^2/(8*2*pi*fsC0(i,1)*fsC0(i,2)*kt2*Q);
       Lm=pi^2/(8*(2*pi*fsC0(i,1))^2*fsC0(i,2)*kt2);
       Cm=(8*fsC0(i,2)*kt2)/(pi^2);
       Zm=Rm+1j*f00*Lm+1/(1j*f00*Cm);
       Z_R(i)=Rs+1/(1/Zm+1j*f00*fsC0(i,2));
        if i==1
           ABCD_params=ABCD_params*[1,0;1/Z_R(i),1];
       else
          if mod(i,2)==0   %   偶数
          ABCD_params=ABCD_params*[1,Z_R(i);0,1];    
          else           
          ABCD_params=ABCD_params*[1,0;1/Z_R(i),1];
          end 
       end
   end
end

 计算滤波器零极点并处理,也许不需要处理需要实部虚部的同时考虑优化。

function [chz,chp]=count_z_p_fun(n,C0CmLm)
%%%  算零极点,开头是串联n=0,是并联n=1

syms f00

ABCD_params=count_ABCD_fun(f00,n,C0CmLm);

S21=2/(ABCD_params(1,1)+ABCD_params(1,2)/50+ABCD_params(2,1)*50+ABCD_params(2,2));
S11=(ABCD_params(1,1)+ABCD_params(1,2)/50-ABCD_params(2,1)*50-ABCD_params(2,2))/(ABCD_params(1,1)+ABCD_params(1,2)/50+ABCD_params(2,1)*50+ABCD_params(2,2));
[E3,~]=numden(S21);
[E5,~]=numden(S11);
%[E7,E8]=numden(Y22)
ft6=solve(E3,f00);
ft7=solve(E5,f00);
%ft8=solve(E7,f00)
F=subs(ft7);
p=vpa(F,8);%真实值

F1=subs(ft6);
z=vpa(F1,8);

real_z=real(z);
real_p=real(p);


k1=1;
for i1=1:length(real_z)
    if real_z(i1)>0
        ch_z(k1)=real_z(i1)/(3.14159*2);
        k1=k1+1;
    end
end
k2=1;
 for i3=1:length(real_p)
     if real_p(i3)>0
         ch_p(k2)=real_p(i3)/(3.14159*2);
         k2=k2+1;
     end
 end
chz=sort(ch_z);
chp=sort(ch_p);


得到零极点的同时得到一些数

function [ch_z,ch_p,p_S11,s_S21]=count_z_p_fun(n,C0CmLm)
%%%  算零极点,开头是串联n=0,是并联n=1

syms f00

ABCD_params=count_ABCD_fun(f00,n,C0CmLm);

S21=2/(ABCD_params(1,1)+ABCD_params(1,2)/50+ABCD_params(2,1)*50+ABCD_params(2,2));
S11=(ABCD_params(1,1)+ABCD_params(1,2)/50-ABCD_params(2,1)*50-ABCD_params(2,2))/(ABCD_params(1,1)+ABCD_params(1,2)/50+ABCD_params(2,1)*50+ABCD_params(2,2));
[E3,~]=numden(S21);
[E5,~]=numden(S11);
%[E7,E8]=numden(Y22)
ft6=solve(E3,f00);
ft7=solve(E5,f00);
%ft8=solve(E7,f00)
F=subs(ft7);
p=vpa(F,8);

F1=subs(ft6);
z=vpa(F1,8);

real_z=real(z);
real_p=real(p);

chz=sort(real_z);
chp=sort(real_p);

% for i4=1:length(chp)
% p_S11(i4)=subs(S11,f00,chp(i4));
% p_S11(i4)=20*log10(abs(p_S11(i4)));
% end
k1=1;

for i1=1:length(chz)
    if chz(i1)>0
        ch_z(k1)=chz(i1)/(3.14159*2);
        k1=k1+1;
    end
end

k2=1;
 for i3=1:length(chp)
     if chp(i3)>0
         p_S11(k2)=subs(S11,f00,chp(i3));
         p_S11(k2)=20*log10(abs(p_S11(k2)));
         ch_p(k2)=chp(i3)/(3.14159*2);
         k2=k2+1;
     end
 end


chp_1=(ch_p(1)+(ch_p(2)-ch_p(1))*0.2)*3.14159*2;
s_S21(1)=subs(S21,f00,chp_1);
s_S21(1)=20*log10(abs(s_S21(1)));

chp_4=(ch_p(4)-(ch_p(4)-ch_p(3))*0.2)*3.14159*2;
s_S21(2)=subs(S21,f00,chp_4);
s_S21(2)=20*log10(abs(s_S21(2)));

S  参数绘制  dBS21 dBS11

function S_para_plot_fun(n,n_f,ini)
%%%%%%%%%%%%%%%画S参数图像的函数%x是频率,开头是串联n=0,是并联n=1

syms f00
ABCD_params=count_ABCD_fun(f00,n,ini);

S21=2/(ABCD_params(1,1)+ABCD_params(1,2)/50+ABCD_params(2,1)*50+ABCD_params(2,2));

S11=(ABCD_params(1,1)+ABCD_params(1,2)/50-ABCD_params(2,1)*50-ABCD_params(2,2))/(ABCD_params(1,1)+ABCD_params(1,2)/50+ABCD_params(2,1)*50+ABCD_params(2,2));

%F_S11=subs(S11,{'c01', 'cm1', 'lm1','c03', 'cm3', 'lm3','c02', 'cm2', 'lm2','c04', 'cm4', 'lm4'},{1*10^(-12),0.06634*10^(-12),31.2*10^(-9),1*10^(-12),0.06634*10^(-12),33.2*10^(-9),1*10^(-12),0.06634*10^(-12),31.2*10^(-9),1*10^(-12),0.06634*10^(-12),33.2*10^(-9)});


f0 = 18.85*10^9:10000000:25.133*10^9;
plot_S11=subs(S11,{'f00'},f0);

%figure(n_f)

plot(f0/(2*3.14159),20*log10(abs(plot_S11)))
hold on
%F_S21=subs(S21,{'c01', 'cm1', 'lm1','c03', 'cm3', 'lm3','c02', 'cm2', 'lm2','c04', 'cm4', 'lm4'},{1*10^(-12),0.06634*10^(-12),31.2*10^(-9),1*10^(-12),0.06634*10^(-12),33.2*10^(-9),1*10^(-12),0.06634*10^(-12),31.2*10^(-9),1*10^(-12),0.06634*10^(-12),33.2*10^(-9)})
plot_S21=subs(S21,{'f00'},f0);

plot(f0/(2*3.14159),20*log10(abs(plot_S21)))

  数据初始化:

把不能当滤波器的数剔除掉,可以考虑之后多加一些限制条件

%______________________________________________________________________________________________
%  Moth-Flame Optimization Algorithm (MFO)                                                            
%  Source codes demo version 1.0                                                                      
%                                                                                                     
%  Developed in MATLAB R2011b(7.13)                                                                   
%                                                                                                     
%  Author and programmer: Seyedali Mirjalili                                                          
%                                                                                                     
%         e-Mail: ali.mirjalili@gmail.com                                                             
%                 seyedali.mirjalili@griffithuni.edu.au                                               
%                                                                                                     
%       Homepage: http://www.alimirjalili.com                                                         
%                                                                                                     
%  Main paper:                                                                                        
%  S. Mirjalili, Moth-Flame Optimization Algorithm: A Novel Nature-inspired Heuristic Paradigm, 
%  Knowledge-Based Systems, DOI: http://dx.doi.org/10.1016/j.knosys.2015.07.006
%_______________________________________________________________________________________________

% This function creates the first random population of moths

%%
% C0:500-1500
% Cm:66000-67000
% Lm:31000-32000
%    33000-34000
   
%%
function X=initialization(SearchAgents_no,dim,ub,lb,target_z,target_p)
 display('MFO is initialization');

kk=1;

while kk<=SearchAgents_no%  只把符合滤波器的数做初始值

     for ii=1:dim  
     ub_i=ub(ii);
     lb_i=lb(ii);
     %X1(kk,ii)=rand(1).*(ub_i-lb_i)+lb_i+ini(ii);
     X1(kk,ii)=rand(1).*(ub_i-lb_i)+lb_i;
     end
    [temp_z,temp_p]=count_z_p_fun(0,X1(kk,:));
    temp_z=eval(temp_z);%转换为真实值
    temp_p=eval(temp_p);
     
    %判断是否为4个零点
    if length(temp_p)~=4
       t=0;
    else
       l_target=length(target_p);
       l_temp=length(temp_p);
       if temp_p(1)<target_p(1)
           k_p_min=temp_p(1);
       else
           k_p_min=target_p(1);
       end
     
       if temp_p(l_temp)<target_p(l_target)
           k_p_max=target_p(l_target);
       else
           k_p_max=temp_p(l_temp);
       end
     
      for jj=1:length(temp_z)
      if temp_z(jj)>k_p_min && temp_z(jj)<k_p_max   %   判断是否为滤波器
             t=0;
              display('0')
             break
      end
      t=1;
      end
    end
    
      if t==1
      X(kk,:)=X1(kk,:);
      kk=kk+1;
      display('ini+1')
      end
end   
end


MFO主函数

%______________________________________________________________________________________________
%  Moth-Flame Optimization Algorithm (MFO)                                                            
%  Source codes demo version 1.0                                                                      
%                                                                                                     
%  Developed in MATLAB R2011b(7.13)                                                                   
%                                                                                                     
%  Author and programmer: Seyedali Mirjalili                                                          
%                                                                                                     
%         e-Mail: ali.mirjalili@gmail.com                                                             
%                 seyedali.mirjalili@griffithuni.edu.au                                               
%                                                                                                     
%       Homepage: http://www.alimirjalili.com                                                         
%                                                                                                     
%  Main paper:                                                                                        
%  S. Mirjalili, Moth-Flame Optimization Algorithm: A Novel Nature-inspired Heuristic Paradigm, 
%  Knowledge-Based Systems, DOI: http://dx.doi.org/10.1016/j.knosys.2015.07.006
%_______________________________________________________________________________________________
% You can simply define your cost in a seperate file and load its handle to fobj 
% The initial parameters that you need are:
%__________________________________________
% fobj = @YourCostFunction
% dim = number of your variables
% Max_iteration = maximum number of generations
% SearchAgents_no = number of search agents
% lb=[lb1,lb2,...,lbn] where lbn is the lower bound of variable n
% ub=[ub1,ub2,...,ubn] where ubn is the upper bound of variable n
% If all the variables have equal lower bound you can just
% define lb and ub as two single number numbers

% To run MFO: [Best_score,Best_pos,cg_curve]=MFO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj)
%______________________________________________________________________________________________

function [Best_flame_score,Best_flame_pos,Convergence_curve]=MFO(N,Max_iteration,lb,ub,dim,target_z,target_p)

display('MFO is optimizing your problem');
%Initialize the positions of moths
Moth_pos=initialization(N,dim,ub,lb,target_z,target_p);

Convergence_curve=zeros(1,Max_iteration);

Iteration=1;

% Main loop
while Iteration<Max_iteration+1
%       Number of flames Eq. (3.14) in the paper
    Flame_no=round(N-Iteration*((N-1)/Max_iteration));
    
    for i=1:size(Moth_pos,1)
        
        % Check if moths go out of the search spaceand bring it back  
        %a = b > c    b>c a=1;

        Flag4ub=Moth_pos(i,:)>ub;%大于大的
        Flag4lb=Moth_pos(i,:)<lb;%小于小的
        Moth_pos(i,:)=(Moth_pos(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;  %赋值边界
        
        % Calculate the fitness of moths
        %Moth_fitness(1,i)=fobj(Moth_pos(i,:));  
        [z,p]=count_z_p_fun(0,Moth_pos(i,:));%从小到大排列

        Moth_fitness(1,i)=count_cost(z,p,target_z,target_p);
    end
       Moth_fitness
    if Iteration==1
        % Sort the first population of moths
        [fitness_sorted I]=sort(Moth_fitness);%I是对应的原来位置的索引
        sorted_population=Moth_pos(I,:);
        
        % Update the flames
        best_flames=sorted_population;
        best_flame_fitness=fitness_sorted;
    else
        
        % Sort the moths
        double_population=[previous_population;best_flames];
        double_fitness=[previous_fitness best_flame_fitness];
        
        [double_fitness_sorted I]=sort(double_fitness);
        double_sorted_population=double_population(I,:);
        
        fitness_sorted=double_fitness_sorted(1:N);
        sorted_population=double_sorted_population(1:N,:);
        
        % Update the flames
        best_flames=sorted_population;
        best_flame_fitness=fitness_sorted;
    end
    
    % Update the position best flame obtained so far
    Best_flame_score=fitness_sorted(1);
    Best_flame_pos=sorted_population(1,:);
      
    previous_population=Moth_pos;
    previous_fitness=Moth_fitness;
    
    % a linearly dicreases from -1 to -2 to calculate t in Eq. (3.12)
    a=-1+Iteration*((-1)/Max_iteration);
    
    for i=1:size(Moth_pos,1)
        
        for j=1:size(Moth_pos,2)
            if i<=Flame_no % Update the position of the moth with respect to its corresponsing flame
                
                % D in Eq. (3.13)
                distance_to_flame=abs(sorted_population(i,j)-Moth_pos(i,j));
                b=1;
                t=(a-1)*rand+1;
                
                % Eq. (3.12)
                Moth_pos(i,j)=distance_to_flame*exp(b.*t).*cos(t.*2*pi)+sorted_population(i,j);
            end
            
            if i>Flame_no % Upaate the position of the moth with respct to one flame
                
                % Eq. (3.13)
                distance_to_flame=abs(sorted_population(i,j)-Moth_pos(i,j));
                b=1;
                t=(a-1)*rand+1;
                
                % Eq. (3.12)
                Moth_pos(i,j)=distance_to_flame*exp(b.*t).*cos(t.*2*pi)+sorted_population(Flame_no,j);
            end
            
        end
        
    end
    
    Convergence_curve(Iteration)=Best_flame_score;
    
    % Display the iteration and best optimum obtained so far
    if mod(Iteration,50)==0
        display('At iteration: ')
        Iteration
        display(' the best fitness is ')
        Best_flame_score
        display(' the best pose is ')
        Best_flame_pos=sorted_population(1,:)
        figure(Iteration/50) %  删除后图片会最后显示
         hold on
        S_para_plot_fun(0,Iteration/50,sorted_population(1,:))
        %display(['At iteration ', num2str(Iteration), ' the best fitness is ', num2str(Best_flame_score)]);
    end
    Iteration=Iteration+1; 
end





main   函数

%______________________________________________________________________________________________
%  Moth-Flame Optimization Algorithm (MFO)                                                            
%  Source codes demo version 1.0                                                                      
%                                                                                                     
%  Developed in MATLAB R2011b(7.13)                                                                   
%                                                                                                     
%  Author and programmer: Seyedali Mirjalili                                                          
%                                                                                                     
%         e-Mail: ali.mirjalili@gmail.com                                                             
%                 seyedali.mirjalili@griffithuni.edu.au                                               
%                                                                                                     
%       Homepage: http://www.alimirjalili.com                                                         
%                                                                                                     
%  Main paper:                                                                                        
%  S. Mirjalili, Moth-Flame Optimization Algorithm: A Novel Nature-inspired Heuristic Paradigm, 
%  Knowledge-Based Systems, DOI: http://dx.doi.org/10.1016/j.knosys.2015.07.006
%_______________________________________________________________________________________________
% You can simply define your cost in a seperate file and load its handle to fobj 
% The initial parameters that you need are:
%__________________________________________
% fobj = @YourCostFunction
% dim = number of your variables
% Max_iteration = maximum number of generations
% SearchAgents_no = number of search agents
% lb=[lb1,lb2,...,lbn] where lbn is the lower bound of variable n
% ub=[ub1,ub2,...,ubn] where ubn is the upper bound of variable n
% If all the variables have equal lower bound you can just
% define lb and ub as two single number numbers

% To run MFO: [Best_score,Best_pos,cg_curve]=MFO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj)
%______________________________________________________________________________________________

clear all 
clc
target_z=[3.4050*10^(9),3.5970*10^(9)];
%target_p=[3.4235*10^(9),3.47*10^(9),3.5295*10^(9),3.5785*10^(9)];%从小到大
target_p=[3.4311*10^(9),3.47*10^(9),3.5295*10^(9),3.5693*10^(9)];%从小到大
SearchAgents_no=30; % Number of search agents种群大小

%%Function_name='F2'; % Name of the test function that can be from F1 to F23 (Table 1,2,3 in the paper)

Max_iteration=500; % Maximum numbef of iterations迭代次数

% Load details of the selected benchmark function. dim:待优化变量
%%[lb,ub,dim,fobj]=Get_Functions_details(Function_name);

dim=12;
%最大边界
ub=[140,6700,3250,170,6700,3400,140,6700,3200,170,6700,3400];
%ub=[50,50,50,50,50,50,50,50,50,50,50,50];
%初始值
%ini=[100,6634,3120,100,6634,3320,100,6634,3120,100,6634,3320];
lb=[30,6500,3100,50,6500,3200,30,6500,3100,50,6500,3200];
%最小边界
%lb=[-50,-50,-50,-50,-50,-50,-50,-50,-50,-50,-50,-50];

[Best_score,Best_pos,cg_curve]=MFO(SearchAgents_no,Max_iteration,lb,ub,dim,target_z,target_p);

% % figure('Position',[284   214   660   290])
% % %Draw search space
% % subplot(1,2,1);
% % func_plot(Function_name);
% % title('Test function')
% % xlabel('x_1');
% % ylabel('x_2');
% % zlabel([Function_name,'( x_1 , x_2 )'])
% % grid off
Best_score
Best_pos
%Draw objective space
%%subplot(1,2,2);
figure(Max_iteration/50+1) 
hold on
semilogy(cg_curve,'Color','b')
title('Convergence curve')
xlabel('Iteration');
ylabel('Best flame (score) obtained so far');

axis tight
grid off
box on
legend('MFO')

%display(['The best solution obtained by MFO is : ', num2str(Best_pos)]);
%display(['The best optimal value of the objective funciton found by MFO is : ', num2str(Best_score)]);

        



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值