MATLAB program for Z-Transform of finite duration sequence

Program Code
%ztransform of finite duration sequence
clc;
close all;
clear all;
syms 'z';
disp('If you input a finite duration sequence x(n), we will give you its z-transform');
nf=input('Please input the initial value of n = ');
nl=input('Please input the final value of n = ');
x= input('Please input the sequence x(n)= ');
syms 'm';
syms 'y';
f(y,m)=(y*(z^(-m)));
disp('Z-transform of the input sequence is displayed below');
k=1;
for n=nf:1:nl
    answer(k)=(f((x(k)),n));
   k=k+1;
end
disp(sum(answer));



Example of Output
If you input a finite duration sequence x(n), we will give you its z-transform
Please input the initial value of n = 0
Please input the final value of n = 4
Please input the sequence x(n)= [1 0 3 -1 2]
Z-transform of the input sequence is displayed below

3/z^2 - 1/z^3 + 2/z^4 + 1


Explanation of the program code
clc;
It clears all input and output from the Command Window display giving clean screen. It removes items from workspace, freeing up system memory. After using clc, the scroll bar cannot be used to see the history of functions, but still the up arrow can be used to recall statements from the command history.

close all;
It deletes all figures whose handles are not hidden.

clear all;
It removes all variables from the workspace. This frees up system memory.

syms ‘z’;
This statement creates symbolic variable z. syms function is used to creates symbolic variable.

disp(‘If you input a finite duration sequence x(n), we will give you its Z-Transform.’);
This statement displays the sentence
If you input a finite duration sequence x(n), we will give you its Z-Transform.
disp() function displays its arguments.

nf=input(‘Please input the initial value of n ’);
This statement displays the sentence
Please input the initial value of n.
After displaying this sentence, it waits for the user to input the initial value of n using keyboard. The value entered by the user will be stored in nf.

nl=input(‘Please input the final value of n ’);
This statement displays the sentence
Please input the final value of n.
After displaying this sentence, it waits for the user to input the final value of n using keyboard. The value entered by the user will be stored in nl.

x=input(‘Please input the sequence x(n)= ’);
This statement displays the sentence
Please input the sequence x(n)=
After that it waits for the user to input all the elements of the sequence x(n) using keyboard.  The user should type the opening square bracket [  before entering the first element of x(n). After entering the [ the user should input all the elements of the sequence x(n) in the correct order. The user should press the spacebar key after typing each element of the sequence x(n), except the last element.  After typing the last element of the sequence x(n), the user should type the closing square bracket ]. All the elements of the sequence x(n) entered by the user will be stored in the array x, in the same order as they are entered by the user. The first element of x(n) will be stored as the first element of the array x, the second element of x(n) will be stored as the second element of the array x, and so on.

syms ‘m’;
This statement creates symbolic variable m.

syms ‘y’;
This statement creates symbolic variable y.

f(y,m)=(y*(z^(-m)));
This statement defines a function f. This function f defined here, can take two arguments. This function f outputs an expression of the form y*(z^(-m)) in terms of z, in which there is the numerical value of the first argument in the place of y and the numerical value of the second argument in the place of m. For example, if the function f takes 3 as the first argument and 4 as the second argument, we get the output as 3/z^4 . We know that z^(-4) is 1/z^4 . In this program, the same function f(y,m) defined in this statement, will be later used in a for loop, with x(k) as y and n as m to calculate the z-transform of each element of the input sequence x(n).

disp(‘Z-transform of the input sequence is displayed below’);
This statement displays the sentence
Z-transform of the input sequence is displayed below

k=1;
This statement assigns the value 1 to the variable k.

for n=nf:1:nl
answer (k)= (f((x(k)),n));
k=k+1;
end
This for loop calculates the z-transform of each element of the input sequence and stores each of those z-transforms as elements of the array answer. I am going to explain each statement in this for loop.

for n=nf:1:nl
The above mentioned for loop starts with this statement. The value of n will be same as the value of nf during the first iteration of this loop. The value of n is incremented by one for each successive iterations of the loop. The iterations of the loop continues till the value of n becomes equal to the value of nl. The final iteration of the loop takes place when the value of n becomes equal to the value of nl.
The syntax of for loop is
for variable=initial value:increment to/decrement from the initial value during each iteration:final value
statements in the loop
.
.
.
.
end

answer(k)=(f((x(k)),n));
The RHS of this statement calculates the z-transform of one element of the input sequence x using the function f(y,m) with y=k and m=n and stores the z-transform of each element of x(n) as the corresponding element of the array answer. During the first iteration of this for loop, k=1, x(k)=x(1) and n=nf. So during the first iteration of this loop, this statement calculates the z-transform of the first element of the input sequence and stores the result as the first element of the array answer. During the second iteration of this loop, this statement calculates the z-transform of the second element of the input sequence and stores the result as the second element of the array answer and so on till the value of n becomes equal to the value of nf.

k=k+1;
This statement increments the value of k by 1 during each iteration of the for loop.

end
This statement terminates this for loop.

disp(sum(answer));
The final line of output of this program is displayed on the execution of this statement. sum(answer); adds all the elements of the array answer to find their sum, and disp(sum(answer)); displays the sum thus obtained. In this program, the sum of all elements of the array answer is the z-transform of the input sequence.
___________________________

20 comments:

  1. thanks ... it helps me a lot.... thumps up!!

    ReplyDelete
    Replies
    1. Respected Sir,
      Thank you for your valuable words of encouragement.
      So kind of you to say like that! I felt so happy to hear those words from you :)

      Thank you for spending your valuable time to visit my simple blog. You are always welcome to visit this simple blog whenever you want to. I am always at your service.
      Wish you and your dear and near ones all happiness and good health.

      Delete
  2. Thank you sir :)

    ReplyDelete
    Replies
    1. Respected Sir,

      Please let me know your name. You can send your name to s5electronicsandcommunication@gmail.com

      Thank you for your valuable words of encouragement.
      So kind of you to say like that! I felt so happy to hear those words from you :)

      Thank you for spending your valuable time to visit my simple blog. You are always welcome to visit this simple blog whenever you want to. I am always at your service.
      Wish you and your dear and near ones all happiness and good health :)

      Delete
  3. hi . How can i find inverse z transform of the output

    ReplyDelete
    Replies
    1. Respected Sir,
      You can find the inverse transform of the output using the following program

      clc;
      close all;
      clear all;
      syms n;
      syms k;
      syms f(z);
      f(z) = input('Please input a function to obtain its inverse z transform ');
      disp(iztrans(f(z)));

      Thank you for asking me a question and thus allowing me to serve you. I am always at your service.
      Thank you for spending your valuable time to visit my humble weblog. I am greatly obliged to you.

      The URL of this weblog is http://s5electronicsandcommunication.blogspot.com

      Wish you happiness, good health and success in life.
      May your friends and relatives enjoy good health, happiness and success in life.

      Delete
  4. clc
    close all
    clear all
    syms z
    x=input('enter sequence');
    l=length(x);
    xz=zeros(1,l)
    for n=0:l;
    xz(n+1)=xz(n+1)+(x(n+1)*z^(-n));
    end
    ztransform=xz


    This code is not working.. what is the problem?

    ReplyDelete
    Replies
    1. Respected Sir,
      Most humbly I request you to tell me what the purpose of this code was, and what input was entered by you and what output you obtained. Only then can I understand what the problem is. After that I will try my level best to answer your question.
      So Please reply.
      Thank you for asking me a question. It is my pleasure to answer your question. I am always at your service.
      Thank you for spending your valuable time to visit my humble weblog. I am greatly obliged to you.

      Delete
  5. How to find a z transform of given signal.I repeat given signal not sequence.Thanks in Advance.....

    ReplyDelete
    Replies
    1. Respected Sir,
      ztrans(signal, index, point);
      is the syntax for the command to calculate the z-transform of a signal.
      When you type this command in a program to calculate the z-transform of a signal,
      the word ‘signal’ in the syntax has to be replaced with the function of the signal in terms of variables,
      the word ‘index’ in the syntax has to be replaced with the value of transformation index of the signal and the word 'point' in the syntax has to be replaced with the value of evaluation point of the signal.
      Before this command you have to type in a separate line the command
      syms var1 var2 var3 var4 var5;
      and replace the words var1, var2, var3, var4 and var5 with the letters used as variables in the function of the signal. Any number of variables can be used with syms command.
      Thank you for asking me this question and thus allowing me to serve you. I am always at your service. In future too, please feel free to ask me questions. You are always welcome. I wish you good health, happiness and a successful career. I wish your family and all your friends, happiness and good health. My e-mail is s5electronicsandcommunication@gmail.com

      Delete
  6. sir can u plzz upload the inverse z transform matlab program

    ReplyDelete
    Replies
    1. Respected Sir,
      You can find the inverse z transform using the following program
      clc;
      close all;
      clear all;
      syms n;
      syms k;
      syms f(z);
      f(z) = input('Please input a function to obtain its inverse z transform ');
      disp(iztrans(f(z)));

      Thank you for asking me a question and thus allowing me to serve you. I am always at your service.
      Thank you for spending your valuable time to visit my humble blog. I am greatly obliged to you for your visiting this blog.

      The URL of this weblog is http://s5electronicsandcommunication.blogspot.com. You can follow us on Facebook at https://www.facebook.com/s5electronicsandcommunicationblogspotcom-for-signal-processing-programs-689962257841349/
      I wish you happiness, good health and success. May your friends and relatives too enjoy good health, happiness and success.

      Delete
    2. sir,without using iztrans command like above code you uploaded

      Delete
  7. Can we find inverse z transform without using 'iztrans' function? If yes then please give me one sample program for the same.

    ReplyDelete
  8. I was recommended this blog through my cousin. I'm not sure whether this publish is written by him as no one else recognise such
    detailed about my difficulty. You are incredible! Thanks!

    ReplyDelete
    Replies
    1. Respected Sir/ Madam,
      I thank you and your cousin.
      My name is Anju K. I wrote this MATLAB program for Z-Transform of finite duration sequence. It is not written by your cousin.
      May you, your cousin and all friends and relatives of yours, have good health and happiness. I am greatly obliged to you and your cousin. Please tell me your name and your cousin's name.

      Delete
  9. Wrong code not working showing error at answer(k)

    ReplyDelete
    Replies
    1. Respected Sir / madam,
      Thank you very much for telling me about this.
      I ran this code and it gave me the correct answer. When I ran this code, it did not show any error.
      So, please tell me what input you gave. You can tell me by writing the input as a comment below this.
      Also please copy paste the entire program code given in this page to your computer and after that please try to run this program once again.
      I am the the person who wrote this program code.
      My name is Anju K.
      May you be healthy, happy and successful.

      Delete
  10. Sir, may i know the code in making the graph base on the formula of z transform?

    ReplyDelete
    Replies
    1. Respected sir/ madam,
      May you, your relatives, friends and well wishers be healthy and happy. I am always happy to be at your service.
      I have written the answer to your question at http://s5electronicsandcommunication.blogspot.com/2012/12/matlab-program-to-plot-zeros-and-poles.html . You can read my answer if you copy-paste the link http://s5electronicsandcommunication.blogspot.com/2012/12/matlab-program-to-plot-zeros-and-poles.html to the address bar of your browser and then press the 'Enter' key. If you love my answer after reading it, then most humbly, I request you to deposit 20 Indian Rupees into my bank account.
      My name: Anju K.
      My bank account number: 30221619108
      Bank: State Bank of India, Chakkarakkal branch, India.
      IFSC code: SBIN0070728
      SWIFT code: SBININBB
      BIC code: SBININBB
      My email: tc9749@gmail.com

      Delete

Please write your opinion about this MATLAB program here, only in English.

For sale

For sale
Indian currency Ten-rupee note in good condition. Issued by the Reserve Bank of India between 22 December 1992 and 21 December 1997. Serial number 52T332603 . Those interested to buy, please contact: Anju K, Email: tc9749@gmail.com . Address: 'Sreyas', House no.7/296, Pulliodi, Kathiroor village, Ponniam east PO, via Ponniam west, Thalassery, Kannur, Kerala - 670641, India.