Вы находитесь на странице: 1из 15

Program 1: To plot a circle of given radius and calculate its area.

During execution, this program requests the radius of circle.

%Program to draw a circle of given radius and calculate its area

clear all; clc;


r = input ('Enter radius of the circle= ');
disp ('Area of a circle is '); A = pi*r^2
theta = 0:pi/20:2*pi;
x=r*cos(theta);
y=r*sin(theta);
plot (x,y);
axis equal
grid on;

Output:
Enter radius of the circle= 5
Area of a circle is

A =

78.53981633974483

>>

-1

-2

-3

-4

-5
-6 -4 -2 0 2 4 6

1
Program 2: Linear Convolution

The LTI discrete-time system is completely characterized by its impulse response; that is,
knowing the impulse response, we can compute the output of the system to any arbitrary
input.

If h(n ) is the impulse response of the system, then its response y (n ) , to the input x(n ) can be
obtained by relation,


y (n ) = x(k )h(n − k )
k = −∞

This program computes linear convolution of two finite duration sequences. During
execution, this program requests the input sequence and the impulse response.

% Illustration of Convolution
% Enter the sequences in square brackets

clc; clear all;

h = input('Type in the impulse response of the system h(n) = ');


x = input('Type in the input sequence x(n)= ');
c = conv(h, x);
disp('The convolved sequence =');disp(c)

L = length (h)-1;
l = 0:1:L;
subplot (2,2,4);
stem(l,h); grid on
xlabel('Time index n'); ylabel('Amplitude');
title ('The impulse response h(n)');

T = length (x)-1;
t = 0:1:T;
subplot (2,2,3);
stem (t,x); grid on
xlabel('Time index n'); ylabel('Amplitude');
title ('The input sequence x(n)');

M = length(c)-1;
n = 0:1:M;
subplot(2,2,1:2);
stem(n,c); grid on
xlabel('Time index n'); ylabel('Amplitude');
title ('Convolution of h(n) and x(n)');

2
Output:

Type in the impulse response of the system h(n) = [5 -2 3 0 -1 -3]


Type in the input sequence x(n)= [-3 1 4 0 -2 1]
The convolved sequence =
-15 11 9 -5 5 17 -15 -9 2 5 -3
>>
Convolution of h(n) and x(n)
20

10
Amplitude

-10

-20
0 1 2 3 4 5 6 7 8 9 10
Time index n
The input sequence x(n) The impulse response h(n)
4 6

4
2
Amplitude

Amplitude

2
0
0
-2
-2

-4 -4
0 2 4 6 0 2 4 6
Time index n Time index n

3
Program 3: Discrete time Fourier Transform

To compute frequency response of the Moving-Average Filter, whose impulse response is


given by,
1
, 0 ≤ n ≤ M − 1,
h(n ) = M
0, otherwise.
The frequency response is given by,
( )

H e jω = h(n )e − jωn
n = −∞

1 1 − e − jMω 1 sin (Mω 2 ) − j ( M −1)ω 2


( )
∴ H e jω =
1 M −1 − jωn
M n =0
e =
M 1− e − jω
=
M sin (ω 2 )
e

From the above, the magnitude and phase responses of the moving-average filter are obtained
as,
1 sin (Mω 2 )
∴ H (e jω ) = ,
M sin (ω 2 )

θ (ω ) = −
(M − 1)ω + π M /2
u ω−
2πk
,
2 k =1 M
where, u (ω ) is a unit step function.

% Program to compute DTFT


% To generate the frequency response of a moving average filter
% for M=5 and M=14.

clear all; clc;

% Generate the filter coefficients


h1 = ones(1,5)/5; h2 = ones(1,14)/14;

% Compute the frequency responses


[H1,w] = freqz(h1, 1, 256);
[H2,w] = freqz(h2, 1, 256);

% Compute and plot the magnitude responses


m1 = abs(H1); m2 = abs(H2);
subplot(2,1,1);
plot(w/pi,m1,'r-',w/pi,m2,'b--');grid
ylabel('Magnitude'); xlabel('\omega/\pi');title('Magnitude Plot');
legend('M=5','M=14');

% Compute and plot the phase responses


ph1 = angle(H1)*180/pi; ph2 = angle(H2)*180/pi;
subplot(2,1,2);
plot(w/pi,ph1,w/pi,ph2);grid
ylabel('Phase, degrees');xlabel('\omega/\pi');title('Phase Plot');
legend('M=5','M=14');

4
Output:

Magnitude Plot
1
M=5
0.8 M=14
Magnitude

0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
ωπ
/
Phase Plot
100
M=5
M=14
Phase, degrees

-100

-200
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
ω/ π

Above figure shows the magnitude and phase responses of the moving-average filters M = 5
and M = 14.

Observations:

It can be seen that in the range from ω = 0 to ω = π , the magnitude has a maximum value of
unity at ω = 0 , and has zeros at ω = 2πk M with integer values of k.

The phase function exhibits discontinuities of π at each zero of H (e jω ) and is linear


elsewhere with a slope of − (M − 1) / 2 .

It can also be seen that both the magnitude and phase functions are periodic in ω with a
period 2π .

5
Program 4: Discrete time Fourier Transform

To determine the M-point DFT of the following N-point sequence:

1, 0 ≤ n ≤ N − 1,
u (n ) =
0, otherwise.
The discrete Fourier transform (DFT) of the length-N time-domain sequence x(n ) is defined
by,

N −1
X (k ) = x(n )e − j 2πkn / N , 0 ≤ k ≤ N −1
n =0

During execution, the program requests the input data N and M. To ensure correct DFT
values must be greater than or equal to N. After they are entered, it computes the M-point
DFT and plots the original N–point sequence and the magnitude and phase of the DFT
sequence. Here N = 8 and M = 16.

% Illustration of DFT Computation

clc; clear all;

% Read in the length N of sequence and the desired length M of the


DFT
N = input('Type in the length of the sequence = ');
M = input('Type in the length of the DFT = ');

% Generate the length-N time-domain sequence


u = [ones(1,N)];

% Compute its M-point DFT


U = fft(u,M);

% Plot the time-domain sequence and its DFT


t = 0:1:N-1;
subplot (2,2,1:2);
stem(t,u); grid
title('Original time-domain sequence')
xlabel('Time index n'); ylabel('Amplitude')

subplot(2,2,3)
k = 0:1:M-1;
stem(k,abs(U)); grid
title('Magnitude of the DFT samples')
xlabel('Frequency index k'); ylabel('Magnitude')
subplot(2,2,4)
stem(k,angle(U)); grid
title('Phase of the DFT samples')
xlabel('Frequency index k'); ylabel('Phase')

6
Output:

Type in the length of the sequence = 8


Type in the length of the DFT = 16
>>

Original time-domain sequence


1

0.8
Amplitude

0.6

0.4

0.2

0
0 1 2 3 4 5 6 7
Time index n
Magnitude of the DFT samples Phase of the DFT samples
8 2

6 1
Magnitude

Phase

4 0

2 -1

0 -2
0 5 10 15 0 5 10 15
Frequency index k Frequency index k

7
Program 5: Inverse Discrete time Fourier Transform

To determine the N-point IDFT of the following K-point sequence:

k K , 0 ≤ k ≤ K − 1,
v(k ) =
0, otherwise.
The inverse discrete Fourier transform (IDFT) of the sequence X (k ) is defined by,

N −1
1
x(n ) = X (k )e j 2πkn / N , 0 ≤ k ≤ N −1
N k =0

As the program runs, it calls for the input data consisting of the length of the DFT and the
length of the IDFT. It then computes the IDFT of the ramp DFT sequence and plots the
original DFT sequence and it’s IDFT. Here K = 8 and N = 16.

% Illustration of IDFT Computation

clc; clear all;

% Read in the length K of the DFT and the desired length N of the
IDFT
K = input('Type in the length of the DFT = ');
N = input('Type in the length of the IDFT = ');

% Generate the length-K DFT sequence


k = 0:K-1;
V = k/K;

% Compute its N-point IDFT


v = ifft(V,N);

% Plot the DFT and its IDFT


subplot(2,2,1:2)
stem(k,V); grid
xlabel('Frequency index k'); ylabel('Amplitude')
title('Original DFT samples')

subplot(2,2,3)
n = 0:N-1;
stem(n,real(v)); grid
title('Real part of IDFT samples')
xlabel('Time index n'); ylabel('Amplitude')

subplot(2,2,4)
stem(n,imag(v)); grid
title('Imaginary part of IDFT samples')
xlabel('Time index n'); ylabel('Amplitude')

8
Output:

Type in the length of the DFT = 8


Type in the length of the IDFT = 16
>>

Original DFT samples


1

0.8
Amplitude

0.6

0.4

0.2

0
0 1 2 3 4 5 6 7
Frequency index k
Real part of IDFT samples Imaginary part of IDFT samples
0.3 0.2

0.2 0.1
Amplitude

Amplitude

0.1 0

0 -0.1

-0.1 -0.2
0 5 10 15 0 5 10 15
Time index n Time index n

Observation:

Note that even though the DFT sequence is real, its IDFT is a complex time-domain sequence
as expected.

9
Program 6: Linear Convolution of two finite-length Sequences Using the DFT-IDFT

DFT-based implementation of the linear convolution of two finite-length sequences can be


easily understood by following diagram.

Input sequence Zero-Padding (N+M -1)-


of length-N, x(n) with (M -1) zeros point DFT

(N+M -1)- y(n)


x point DFT
Impulse
response of Zero-Padding (N+M -1)-
length-M, h(n) with (N -1) zeros point DFT

Following program can be used to determine the linear convolution of two finite-length
sequences via the DFT-based approach and to compare the result using a direct linear
convolution.

The input data to be entered in vector format inside square brackets are the two sequences to
be convolved.

This program plots the sequence obtained using the DFT-based approach and the sequence
obtained using linear convolution procedure and their difference.

% Linear Convolution Via the DFT

clc; clear all;


% Read in the two sequences

x = input('Type in the first sequence = ');


h = input('Type in the second sequence = ');

% Determine the length of the result of convolution


L = length(x)+length(h)-1;

% Compute the DFTs by zero-padding


XE = fft(x,L); HE = fft(h,L);

% Determine the IDFT of the product


y1 = ifft(XE.*HE);

% Plot the sequence generated by DFT-based convolution and


% the error from direct linear convolution
n = 0:L-1;
subplot(2,1,1)
stem(n,y1); grid
xlabel('Time index n');ylabel('Amplitude');
title('Result of DFT-based linear convolution')

10
y2 = conv(x,h);
subplot(2,1,2)
error = y1-y2;
stem(n,error); grid
xlabel('Time index n');ylabel('Amplitude')
title('Error sequence')

pause

stem(n,y2); grid
xlabel('Time index n');ylabel('Amplitude');
title('Result of linear convolution')

Output:

Type in the first sequence = [2 0 -1 4 3 -2]


Type in the second sequence = [0 -1 4 3 0 -3 1]
>>

Result of DFT-based linear convolution


30

20
Amplitude

10

-10

-20
0 2 4 6 8 10 12
-15
Time index n
x 10 Error sequence
2
Amplitude

-2
0 2 4 6 8 10 12
Time index n

This figure shows the convolution sequence obtained using the DFT-based approach and the
error sequence.

Error sequence is the difference between the convolution sum computed using DFT-based
approach and the direct linear convolution approach.

11
Result of DFT-based linear convolution
30

20
Amplitude

10

-10

-20
0 2 4 6 8 10 12
Time index n
Result of linear convolution
30

20
Amplitude

10

-10

-20
0 2 4 6 8 10 12
Time index n

Observations:

Note that the convolution sum obtained using DFT-based approach and the direct linear
convolution approach is almost same.

The two non-zero samples of very small amplitudes in the error sequence are a result of
finite-precision arithmetic employed in the computation of the DFT in MATLAB.

12
Program 7: Determination of Pole-zero plot of a rational z-Transform

Find poles and zeros of following function,

2 z 4 + 16 z 3 + 44 z 2 + 56 z + 32
X (z ) =
3 z 4 + 3 z 3 − 15 z 2 + 18 z − 12

During execution, the program requests the coefficients of numerator and denominator. Enter
these coefficients in vector format inside square brackets.

% Determination of the poles and zeros of a Rational z-Transform.

clc; clear all;


num = input('Type in the numerator coefficients = ');
den = input('Type in the denominator coefficients = ');
[z,p,k]=tf2zp(num,den);
disp('The zeros are:');disp(z)
disp('The poles are:');disp(p)
disp('The gain constant is:');disp(k)

zplane(num,den);grid

Output:

Type in the numerator coefficients = [2 16 44 56 32]


Type in the denominator coefficients = [3 3 -15 18 -12]

The zeros are:


-4.00000000000000
-2.00000000000001
-1.00000000000000 + 1.00000000000000i
-1.00000000000000 - 1.00000000000000i

The poles are:


-3.23606797749979
1.23606797749979
0.50000000000000 + 0.86602540378444i
0.50000000000000 - 0.86602540378444i

The gain constant is:


0.66666666666667

>>

13
Following figure shows the pole-zero plot developed by the program.

1.5

0.5
Imaginary Part

-0.5

-1

-1.5

-2

-4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1


Real Part

14
Program 8: Determination Rational form of z-Transform from its Pole-zero locations

Determine rational form of z-transform if the pole and zero locations are given as:

The zeros are at: z1 = 0.21, z2 = 3.14, z3 = - 0.3 + j0.5, z4 = -0.3 – j0.5
The poles are at: p1 = -0.45, p2 = 0.67, p3 = 0.81 + j0.72, p4 = 0.81 – j0.72

% Determination of the Rational z-Transform from its Poles and Zeros

format long
clc; clear all;
zr = input('Type in the zeros as a row vector = ');
pr = input('Type in the poles as a row vector = ');

% Transpose zero and pole row vectors


z = zr'; p = pr';
k = input('Type in the gain constant = ');

[num, den] = zp2tf(z, p, k);


disp('Numerator polynomial coefficients'); disp(num);
disp('Denominator polynomial coefficients'); disp(den);

Output:

Type in the zeros as a row vector =


[0.21, 3.14, -0.3 + 0.5i, -0.3 - 0.5i]

Type in the poles as a row vector =


[-0.45, 0.67, 0.81 + 0.72i, 0.81 - 0.72i]

Type in the gain constant = 2.2

Numerator polynomial coefficients


Columns 1 through 5

2.2000 -6.0500 -2.22332 -1.635392 0.4932312

Denominator polynomial coefficients


Columns 1 through 5

1.0000 -1.8400 1.2294 0.23004 -0.35411175

From above results, we can thus arrive at the desired expression:

2.2 z 4 − 6.05 z 3 − 2.22332 z 2 − 1.63592 z + 0.4932312


X (z ) =
z 4 − 1.84 z 3 + 1.2294 z 2 + 0.23004 z − 0.35411175

15

Вам также может понравиться