⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aaaa.m

📁 实现低通滤波器的功能
💻 M
字号:
function [sys,x0,str,ts] = aaaa(t,x,u,flag)
%SFUNTMPL General M-file S-function template
%   With M-file S-functions, you can define you own ordinary differential
%   equations (ODEs), discrete system equations, and/or just about
%   any type of algorithm to be used within a Simulink block diagram.
%
%   The general form of an M-File S-function syntax is:
%       [SYS,X0,STR,TS] = SFUNC(T,X,U,FLAG,P1,...,Pn)
%
%   What is returned by SFUNC at a given point in time, T, depends on the
%   value of the FLAG, the current state vector, X, and the current
%   input vector, U.
%
%   FLAG   RESULT             DESCRIPTION
%   -----  ------             --------------------------------------------
%   0      [SIZES,X0,STR,TS]  Initialization, return system sizes in SYS,
%                             initial state in X0, state ordering strings
%                             in STR, and sample times in TS.
%   1      DX                 Return continuous state derivatives in SYS.
%   2      DS                 Update discrete states SYS = X(n+1)
%   3      Y                  Return outputs in SYS.
%   4      TNEXT              Return next time hit for variable step sample
%                             time in SYS.
%   5                         Reserved for future (root finding).
%   9      []                 Termination, perform any cleanup SYS=[].
%
%
%   The state vectors, X and X0 consists of continuous states followed
%   by discrete states.
%
%   Optional parameters, P1,...,Pn can be provided to the S-function and
%   used during any FLAG operation.
%
%   When SFUNC is called with FLAG = 0, the following information
%   should be returned:
%
%      SYS(1) = Number of continuous states.
%      SYS(2) = Number of discrete states.
%      SYS(3) = Number of outputs.
%      SYS(4) = Number of inputs.
%               Any of the first four elements in SYS can be specified
%               as -1 indicating that they are dynamically sized. The
%               actual length for all other flags will be equal to the
%               length of the input, U.
%      SYS(5) = Reserved for root finding. Must be zero.
%      SYS(6) = Direct feedthrough flag (1=yes, 0=no). The s-function
%               has direct feedthrough if U is used during the FLAG=3
%               call. Setting this to 0 is akin to making a promise that
%               U will not be used during FLAG=3. If you break the promise
%               then unpredictable results will occur.
%      SYS(7) = Number of sample times. This is the number of rows in TS.
%
%
%      X0     = Initial state conditions or [] if no states.
%
%      STR    = State ordering strings which is generally specified as [].
%
%      TS     = An m-by-2 matrix containing the sample time
%               (period, offset) information. Where m = number of sample
%               times. The ordering of the sample times must be:
%
%               TS = [0      0,      : Continuous sample time.
%                     0      1,      : Continuous, but fixed in minor step
%                                      sample time.
%                     PERIOD OFFSET, : Discrete sample time where
%                                      PERIOD > 0 & OFFSET < PERIOD.
%                     -2     0];     : Variable step discrete sample time
%                                      where FLAG=4 is used to get time of
%                                      next hit.
%
%               There can be more than one sample time providing
%               they are ordered such that they are monotonically
%               increasing. Only the needed sample times should be
%               specified in TS. When specifying more than one
%               sample time, you must check for sample hits explicitly by
%               seeing if
%                  abs(round((T-OFFSET)/PERIOD) - (T-OFFSET)/PERIOD)
%               is within a specified tolerance, generally 1e-8. This
%               tolerance is dependent upon your model's sampling times
%               and simulation time.
%
%               You can also specify that the sample time of the S-function
%               is inherited from the driving block. For functions which
%               change during minor steps, this is done by
%               specifying SYS(7) = 1 and TS = [-1 0]. For functions which
%               are held during minor steps, this is done by specifying
%               SYS(7) = 1 and TS = [-1 1].

%   Copyright 1990-2007 The MathWorks, Inc.
%   $Revision: 1.18.2.2 $

%
% The following outlines the general structure of an S-function.
%



switch flag,

  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  case 0,
    [sys,x0,str,ts]=mdlInitializeSizes;

 
  
  case 3,
    sys=mdlOutputs(u);

  %%%%%%%%%%%%%%%%%%%%%%%
  % GetTimeOfNextVarHit %
  %%%%%%%%%%%%%%%%%%%%%%%
  case {1,2,4,9}
    sys=[];

  
  %%%%%%%%%%%%%%%%%%%%
  % Unexpected flags %
  %%%%%%%%%%%%%%%%%%%%
  otherwise
    DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));

end

% end sfuntmpl

%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes

%
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
%
% Note that in this example, the values are hard coded.  This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
%
sizes = simsizes;

sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 1;
sizes.NumInputs      = 2;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = (1/31978);   % at least one sample time is needed

sys = simsizes(sizes);

%
% initialize the initial conditions
%
x0  = [];

%
% str is always an empty matrix
%
str = [];

%
% initialize the array of sample times
%
ts  = [-1 0];

% end mdlInitializeSizes

%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
%function sys=mdlDerivatives(t,x,u)

%sys = [];

% end mdlDerivatives

%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
%function sys=mdlUpdate(t,x,u)

%sys = [];

% end mdlUpdate

%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(u)
%输入:IShapeOutput QShapeOutput
%输出:采样时钟输出

baud_frac = 0;%波特分量

Energy1 = 0;%第一点能量
Energy5 = 0;%第五点能量
Energy13 = 0;%第十三点能量
Energy = zeros(1,16);%一波特16点各点能量

th3 = 0.2;%Threshold for zero cross 
th2 = 0.012;%Threshold for Clock Recovery
clock_corrected = 0;%是否同步标志位
clear_clock_corrected = 0;%是否清零同步标志位
clerror = 0;%第十三点与第五点能量差
clerror1 = 0;
clerror_sig = 0;%经环路滤波器计算后能量差信息
clerror_sig1 = 0;
DPSK_CPLL2 = 0.1;%The current value contribution to clock recovery
CPLL1 = 0.65;%Filter coefficient for the Clock Recovery loop filter.


%位同步
            
            IShapeOutput = u(1);
            QShapeOutput = u(2);
            %在下一波特第8点时同步标志位清零并且输出采样时钟
            if (clear_clock_corrected == 1) && (baud_frac == 8)
                clear_clock_corrected = 0;
                clock_corrected = 0;
                sys = 1;%输出采样时钟
            else
                sys = 0;
            end

            baud_frac =  baud_frac + 1;%波特分量增加
            %计算当前点能量
            Energy(1,baud_frac) = IShapeOutput * IShapeOutput + QShapeOutput * QShapeOutput;
          
           
           %计算第一点能量
           if baud_frac == 1
              Energy1 = IShapeOutput * IShapeOutput + QShapeOutput * QShapeOutput;
           end
           %计算第十三点能量
           if baud_frac == 13
              Energy13 = IShapeOutput * IShapeOutput + QShapeOutput * QShapeOutput;
           end
         
           
          
           %计算第五点能量
           if (baud_frac == 5)&&(Energy1 < th3)
              Energy5 = IShapeOutput * IShapeOutput + QShapeOutput * QShapeOutput;
              
              %判断是否同步
              if (clock_corrected == 0)
                  clerror = Energy13 - Energy5;%计算能量差
                  clerror_sig = clerror * DPSK_CPLL2 + clerror_sig * CPLL1;%通过一阶环路计算位同步误差
                  clerror_sig_watch = [clerror_sig_watch clerror_sig];%环路滤波器输出矩阵
                 
              
                    %判决是否已锁定
                    if abs(clerror_sig) < th2%误差绝对值小于门限值
                       clock_corrected = 1;
                    end
              
                    if clerror_sig > 0
                     %误差大于二倍的门限
                        if clerror_sig > (th2 * 2)
                            baud_frac =  baud_frac - 2;%调整两个步长
                            %循环调整能量数组
                            temp1 = Energy(1,1);
                            temp2 = Energy(1,2);
                            for k = 1:1:14
                                Energy(1,k) = Energy(1,k+2);
                            end
                            Energy(1,15) = temp1;
                            Energy(1,16) = temp2;
                            Energy13 =  Energy(1,13); 
                            Energy1 =  Energy(1,1);
                             %误差大于一倍的门限
                        elseif clerror_sig > th2 
                                baud_frac =  baud_frac - 1;%调整一个步长
                                %循环调整能量数组
                                temp1 = Energy(1,1);
                                for k = 1:1:15
                                    Energy(1,k) = Energy(1,k+1);
                                end
                                Energy(1,16) = temp1;
                                Energy13 =  Energy(1,13); 
                                Energy1 =  Energy(1,1);
                        end
                    else
                        %误差小于负二倍的门限
                        if   clerror_sig < (-th2 * 2)
                             baud_frac =  baud_frac + 2;%调整两个步长
                             %循环调整能量数组
                             temp1 = Energy(1,15);
                             temp2 = Energy(1,16);
                             for k = 16:-1:3
                                 Energy(1,k) = Energy(1,k-2); 
                             end
                             Energy(1,2) = temp2;
                             Energy(1,1) = temp1;
                             Energy13 =  Energy(1,13); 
                             Energy1 =  Energy(1,1); 
                       elseif clerror_sig < -th2 
                             baud_frac =  baud_frac + 1;%调整一个步长
                             %循环调整能量数组
                             temp1 = Energy(1,16);
                             for k = 16:-1:2
                                 Energy(1,k) = Energy(1,k-1);
                             end
                             Energy(1,1) = temp1;
                             Energy13 =  Energy(1,13); 
                             Energy1 =  Energy(1,1);
                        end
                    end
              end
              %出错情况下,大幅度调整8个步长
              if (Energy13 < Energy1) && (Energy5 < Energy1)
                  baud_frac =  baud_frac + 8;
              end
           end
           
           if baud_frac >= 16
               baud_frac = 0;%一波特已完
               clear_clock_corrected = 1;%同步标志位清零条件部分满足
           end
%位同步结束  

% end mdlOutputs

%
%=============================================================================
% mdlGetTimeOfNextVarHit
% Return the time of the next hit for this block.  Note that the result is
% absolute time.  Note that this function is only used when you specify a
% variable discrete-time sample time [-2 0] in the sample time array in
% mdlInitializeSizes.
%=============================================================================
%
%function sys=mdlGetTimeOfNextVarHit(t,x,u)

%sampleTime = 1;    %  Example, set the next hit to be one second later.
%sys = t + sampleTime;

% end mdlGetTimeOfNextVarHit

%
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
%
%function sys=mdlTerminate(t,x,u)

%sys = [];

% end mdlTerminate

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -