Friday, February 15, 2019

Z-Transform in Digital Signal Processing using MATLAB


Z-TRANSFORM
 THEORY: As discussed in the class, Z-transform is an extremely useful mathematical tool for the analysis and design of discrete time systems. Z-transform is defined as.

MATLAB Symbolic Toolbox gives the z-transform of a function

Example1:
To find out Z transform of the following and enter the following commands. 
>>syms z n
>> ztrans(1/4^n)
ans =

z/(z - 1/4)

 POLES AND ZEROS: H (z) is defined as the ratio of the z-transform of the output to the z-transform of the input when all initial conditions are zero.

 Mathematically,

 The roots of the denominator of H(z) are called “poles” and those of the numerator are called “zeros”. The above equation has M zeros and N poles. A system is said to be stable if all of its poles lie inside of a unit circle on the z-plane. Following example illustrates the use of MATLAB to compute zero and poles of a transfer function and to plot them onto the z-plane.

Example2: Find poles and zeros of the following pulse transfer function and plot them onto the z-plane.



num = [2 -1]; % numerator of H(z)
den = [1 -0.1 -0.02];% denominator of H(z)
zplane(num,den)

 On the z-plane, “x” indicates the poles and “0” indicate the zeros.
 Is this system stable? Why?

The given system is a stable system, because its poles are inside the unit circle.
Exercise
Q 1.                      Find Z transform of following

>>syms z n
>>ztrans(1+n)


>>syms z n
>>a =4;
>>ztrans(a^n+a^-n)

Q 2.                      Find poles and zeros plot of following
>>num = [2.25 -2.1 -3.95 -1.6 -0.2]; % numerator of H(z)
>>den = [4 -2.96 0.8 -0.1184 -0.0064];% denominator of H(z)
>>zplane(num,den)



>>num = [0 1 0.5]; % numerator of H(z)
>>den = [1 3/5 2/25];% denominator of H(z)
>>zplane(num,den)










Thursday, February 7, 2019

How to create functions in MATLAB, Digital Signaling Processing


DIGITAL SIGNALING PROCESSING
FUNCTIONS AND FUNCTION FILES IN MATLAB
OBJECTIVE: To understand concept of Function in MATLAB and to make function files.
Many functions are programmed inside MATLAB as built-in functions, and can be used in mathematical expressions simply by typing their name with an argument example
 are sin (x), cos (x), sqrt(x), and exp (x). Frequently, in computer programs, there is a need to calculate the value of functions that are not built-in. When a function expression is simple and needs to be calculated only once, it can be typed as part of the program. However, when a function needs to be evaluated many times for different value of arguments it is convenient to create a ''user defined" function. Once the new function is created (saved) it can be used just like the built-in functions.

CREATING A FUNCTION FILE:  Function files are created and edited, like script files, in the Editor/Debugger Window. This window is opened from the Command Window. In the File menu, select New, and then select m-file.

FUNCTION DEFINITION LINE: -
The first executable line in a function file must be the function definition line. Otherwise the file is considered a script file. The function definition line:
• Defines the file as a function file
• Defines the name of the function.
• Defines the number and order of the input and output arguments.

Function [output arguments] =function_name [input arguments]

INPUT AND OUTPUT ARGUMENTS: -
The input and output arguments are used to transfer data into and out of the function. The input arguments are listed inside parentheses following the function name. Usually, there is at least one
input argument, although it is possible to have a function that has no input arguments. If there are more than one, the input arguments are separated with commas.

EXAMPLE:
Write a function file for the function of the following expression. For the input to the function is x and output is f(x).
Write the function such that x can be a vector. Use the function to calculate:

a1)       f(x) for x =6.
a2)       f(x) for x = 1,3,5,7,9, and 11.

Open the Editor/Debugger Window. This window is opened from the Command Window. In the File menu, select New, and then select m-file. Once the Editor/Debugger Window opens write the following function in it

     function [y] = exp4one(x)
     y= (x.^4.*sqrt(3*x+5))./(x.^2+1.^2);


a     1)      Calculating the function for x = 6 can be done by typing exp4one(6) in the Command window
>> exp4one(6)
ans = 167.9837

2)      To calculate the function for several values of x, a vector with the values of x is
 first created, and then used for the argument of the function.
>> x = 1:2:11
x =
     1     3     5     7     9    11
>> exp4one(x)
ans = 1.4142   30.3074  107.5033  244.8549  452.6173  739.7802

    EXERCISE:
Q.1: Write down a function that takes value of frequency and at output gives a cosine wave.

Q.2: Write down a function that takes value of frequency and generate sawtooth, square and triangular wave in on plot window. (Frequency =0.5)
function [y] = freq(x)
n=0:0.1:40;
y=sawtooth(2*pi*x*n);
subplot(311)
    plot(y);
    y=square(2*pi*x*n);
subplot(312)
    plot(y);
   t = 0:0.1:15;
y=sawtooth(t,x);
subplot(313)
plot(t,y);


Q.3: Write down a function which solve the numerical problems of chapter 1 of your text book (detail of this question will be explained in the lab)
function [y] = exp4one(x)
    y= (x.^6.*sqrt(3))./(x.^2+x.^9);




Sunday, February 3, 2019

DIGITAL SIGNAL PROCESSING PROPERTIES OF DISCRETE-TIME SYSTEMS IN THE TIME DOMAIN WITH MATLAB


DIGITAL SIGNAL PROCESSING
PROPERTIES OF DISCRETE-TIME SYSTEMS IN THE TIME DOMAIN WITH MATLAB

LINEARITY: For a linear discrete-time system, if y1[n] and y2[n] are the responses to the input sequences x1[n] and x2[n], respectively, then for an input

x[n] = αx1[n] + β x2[n]           (1)
the response is given by
y[n] = αy1[n] + β y2[n]            (2)

The superposition property of Eq. (2) must hold for any arbitrary constants α and β and for all possible inputs x1[n] and x2[n]. If Eq. (2) does not hold for at least one set of nonzero values of α and β, or one set of nonzero input sequences x1[n] and x2[n], then the system is nonlinear.
TIME-INVARIANT: For a time-invariant discrete-time system, if y1[n] is the response to an input x1[n], then the response to an input
x[n] = x1[n no]
is simply          
y[n] = y1[n no]
Where no is any positive or negative integer. The above relation between the input and output must hold for any arbitrary input sequence and its corresponding output. If it does not hold for at least one input sequence and its corresponding output sequence, the system is time-varying.
LTI-SYSTEM:  A linear time-invariant (LTI) discrete-time system satisfies both the linearity and the time-invariance properties.
LINEAR AND NONLINEAR SYSTEMS
We now investigate the linearity property of a causal system.
Consider the system given by
y[n]0.4 y[n1]+0.75 y[n2] = 2.2403 x[n]+2.4908 x[n1]+2.2403 x[n2]            (3)
MATLAB Program P1 is used to simulate the system of Eq. (3), to generate three different input sequences x1[n], x2[n], and x[n] = a · x1[n]+b · x2[n], and to compute and plot the corresponding output sequences y1[n], y2[n], and y[n].
% Program P1
% Generate the input sequences
clf;
n = 0:40;
a = 2;b = -3;
x1 = cos(2*pi*0.1*n);
x2 = cos(2*pi*0.4*n);
x = a*x1 + b*x2;
num = [2.2403 2.4908 2.2403];
den = [1 -0.4 0.75];
ic = [0 0]; % Set zero initial conditions
y1 = filter(num,den,x1,ic); % Compute the output y1[n]
y2 = filter(num,den,x2,ic); % Compute the output y2[n]
y = filter(num,den,x,ic); % Compute the output y[n]
yt = a*y1 + b*y2;
d = y - yt; % Compute the difference output d[n]
% Plot the outputs and the difference signal
subplot(3,1,1)
stem(n,y);
ylabel('Amplitude');
title('Output Due to Weighted Input: a \cdot+ x_{1}+[n]+ b \cdot+ x_{2}+[n]');
subplot(3,1,2)
stem(n,yt);
ylabel('Amplitude');
title('Weighted Output: a \cdot+ y_{1}+[n] + b \cdot+y_{2}+[n]');
subplot(3,1,3)
stem(n,d);
xlabel('Time index n'); ylabel('Amplitude');
title('Difference Signal');
RESULTS:

QUESTIONS
Q.1:  Consider another system described by:
y[n] = x[n]+x[n 1].
Modify y1[n], y2 Program P1 to compute the output sequences [n], and y[n] of the above system. Compare y[n] with yt[n]. Are these two sequences equal? Is this system linear?
MATLAB CODE:
clf;
n = 0:40;
a = 2;b = -3;
x1 = cos(2*pi*0.1*n);
x2 = cos(2*pi*0.4*n);
x = a*x1 + b*x2;
num = [1 1];
den = [1];
ic = [0]; % Set zero initial conditions
y1 = filter(num,den,x1,ic); % Compute the output y1[n]
y2 = filter(num,den,x2,ic); % Compute the output y2[n]
y = filter(num,den,x,ic); % Compute the output y[n]
yt = a*y1 + b*y2;
d = y - yt; % Compute the difference output d[n]
% Plot the outputs and the difference signal
subplot(3,1,1)
stem(n,y);
ylabel('Amplitude');
title('Output Due to Weighted Input: a \cdot+ x_{1}+[n]+ b \cdot+ x_{2}+[n]');
subplot(3,1,2)
stem(n,yt);
ylabel('Amplitude');
title('Weighted Output: a \cdot+ y_{1}+[n] + b \cdot+y_{2}+[n]');
subplot(3,1,3)
stem(n,d);
xlabel('Time index n'); ylabel('Amplitude');
title('Difference Signal');

RESULTS:

Time-Invariant and Time-Varying Systems
We next investigate the time-invariance property (of a causal system.
 Consider again the system given by Eq. (3). MATLAB Program P2 is used to simulate the system of Eq. (3), to generate two different input sequences x[n] and x[n - D], and to compute and plot the corresponding output sequences y1[n], y2[n], and the difference y1[n] - y2[n + D].

% Program P2
% Generate the input sequences
clf;
n = 0:40; D = 10;a = 3.0;b = -2;
x = a*cos(2*pi*0.1*n) + b*cos(2*pi*0.4*n);
xd = [zeros(1,D) x];
num = [2.2403 2.4908 2.2403];
den = [1 -0.4 0.75];
ic = [0 0];% Set initial conditions
% Compute the output y[n]
y = filter(num,den,x,ic);
% Compute the output yd[n]
yd = filter(num,den,xd,ic);
% Compute the difference output d[n]
d = y - yd(1+D:41+D);
% Plot the outputs
subplot(3,1,1)
stem(n,y);
ylabel('Amplitude');
title('Output y[n]');grid;
subplot(3,1,2)
stem(n,yd(1:41));
ylabel('Amplitude');
title(['Output Due to Delayed Input x[n ', num2str(D),']']);grid;
subplot(3,1,3)
stem(n,d);
xlabel('Time index n'); ylabel('Amplitude');
title('Difference Signal');grid;
RESULT:

Q.3: Consider another system described by:
y[n] = nx[n] + x[n 1]   
Modify Program P2 to simulate the above system and determine whether this system is
Time-invariant or not.
clf;
n = 0:40; D = 10;a = 3.0;b = -2;
x = a*cos(2*pi*0.1*n) + b*cos(2*pi*0.4*n);
xd = [zeros(1,D) x];
num = [n 1];
den = [1];
ic = [n];% Set initial conditions
% Compute the output y[n]
y = filter(num,den,x,ic);
% Compute the output yd[n]
yd = filter(num,den,xd,ic);
% Compute the difference output d[n]
d = y - yd(1+D:41+D);
% Plot the outputs
subplot(3,1,1)
stem(n,y);
ylabel('Amplitude');
title('Output y[n]');grid;
subplot(3,1,2)
stem(n,yd(1:41));
ylabel('Amplitude');
title(['Output Due to Delayed Input x[n ', num2str(D),']']);grid;
subplot(3,1,3)
stem(n,d);
xlabel('Time index n'); ylabel('Amplitude');
title('Difference Signal');grid;

RESULTS:   It’s a time variant system.