Home > matlab code, Uncategorized > create_tf_equation

create_tf_equation

A function which creates an annotation based plot that shows the transfer function of a system given the systems b and a coefficients.

% This function creates a transfer function given a set of b and a
% coefficients
%
% example usage: 
%             create_tf_equation([0.23 0.5], [1 -0.34 -0.23432]);
%
function create_tf_equation(b,a, varargin)

%To Do: cater for s-domain
if(~length(b))
    error('b must have some values')
end
if(~length(a))
    error('a must have some values')
end

b_str = '';
for k = 1:length(b)
    if( k ==1)
        z_var = '';
        sign_var = '';
    else
        z_var = ['z^{' num2str((k-1)*-1) '}'];
        if(b(k) < 0)
            sign_var = ' - ';
            b(k) = b(k)*-1;
        else
            sign_var = ' + ';
        end
    end
    b_str = [ b_str sign_var num2str(b(k)) z_var];
end


a_str = '';
for k = 1:length(a)
    if( k ==1)
        z_var = '';
        sign_var = '';
    else
        z_var = ['z^{' num2str((k-1)*-1 ) '}'];
        if(a(k) < 0)
            sign_var = ' - ';
            a(k) = a(k)*-1;
        else
            sign_var = ' + ';
        end
    end
    a_str = [ a_str sign_var num2str(a(k)) z_var];
end

if(length(a) == 1)
    H_str = b_str;
else
    
    H_str = ['\frac{' b_str   '}{' a_str '}'];
end
fh = figure
text('Interpreter','latex',...
       'String',['$$H(z) = ' H_str ' $$'],...
       'Position',[0,0.2],...
       'FontSize',14);
   set(gca, 'visible', 'off')
   fwidth = round(100 + max([length(a_str) length(b_str)])*8.5); 
   set(fh, 'position', [232   246 fwidth      54]);
   
Categories: matlab code, Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: