Академический Документы
Профессиональный Документы
Культура Документы
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
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:
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
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.
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.
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
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.
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:
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
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.
% 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 = ');
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:
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
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.
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:
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
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.
zplane(num,den);grid
Output:
>>
13
Following figure shows the pole-zero plot developed by the program.
1.5
0.5
Imaginary Part
-0.5
-1
-1.5
-2
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
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 = ');
Output:
15