DSP Matlab Practice Final
DSP Matlab Practice Final
1. Introduction :
MATLAB is a high-performance language for technical computing. It integrates
computation, visualization, and programming in an easy-to-use environment where problems
and solutions are expressed in familiar mathematical notation.
Typical uses include Math and computation, Algorithm development, Data acquisition,
Modeling, simulation, and prototyping, Data analysis, exploration, and visualization,
Scientific and engineering graphics, Application development, including graphical user
interface building.
MATLAB is an interactive system whose basic data element is an array
that does not require dimensioning. This allows you to solve many technical computing
problems, especially those with matrix and vector formulations, in a fraction of the time it
would take to write a program in a scalar non interactive language such as C or Fortran. The
name MATLAB stands for matrix laboratory.
Quitting MATLAB: To end your MATLAB session, select File > Exit MATLAB in the
desktop, or type quit in the Command Window. You can run a script file named finish.m
each time MATLAB quits that, for example, executes functions to save the workspace.
2. MATLAB desktop:
On starting MATLAB the MATLAB desktop appears, containing tools (graphical user
interfaces ) for managing files , variables and applications associated with MATLAB. It
contains :
(i) Command Window and Command History
Command Window : Use the Command Window to enter variables and to run functions and
M-file scripts. Press the up arrow key to recall a statement previously typed. Edit the
statement as needed and then press Enter to run it.
Command History : Statements entered in the Command Window are logged in the
Command History. From the Command History, previously run statements can be viewed,
copied and executed. M-file can be created from selected statements.
(ii) Current Directory Browser and Search Path : MATLAB file operations use the current
directory and the search path as reference points. Any file required to run must either be in
the current directory or on the search path.
(iii) Workspace Browser and Array Editor :The MATLAB workspace consists of the set of
variables (named arrays) built up during a MATLAB session and stored in memory. The
1
variables can be added to the workspace by using functions, running M-files, and loading
saved workspaces. To delete variables from the workspace, select the variables and select
Edit > Delete.
Array Editor : Double-click a variable in the Workspace browser, or use open var variable
name, to see it in the Array Editor. Use the Array Editor to view and edit a visual
representation of variables in the workspace.
(iv) Editor/Debugger : Editor/Debugger is used to create and debug M-files, which are
programs to run MATLAB functions. The Editor/Debugger provides a graphical
user interface for text editing, as well as for M-file debugging. To create or edit an M-file use
File > New or File > Open, or use the edit function.
The important commands/ functions are as below :
1. clc (Remove items from workspace, freeing up system memory) clears all input and
output from the Command Window display, giving "clean screen." 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.
2. close (Remove specified figure): close deletes the current figure or the specified figure(s).
It optionally returns the status of the close operation.
3. xlabel, ylabel, zlabel (Label x-, y-, and z-axis) : Each axes graphics object can have
one label for the x-, y-, and z-axis. The label appears beneath its respective axis in a two-
dimensional plot and to the side or beneath the axis in a three-dimensional plot.
xlabel('string') labels the x-axis of the current axes.
ylabel(...) and zlabel(...) label the y-axis and z-axis, respectively, of the current axes.
4. title( Add title to current axes) : Each axes graphics object can have one title. The title is
located at the top and in the center of the axes.
title('string') outputs the string at the top and in the center of the current axes.
5. figure (create figure graphics object) : figure creates figure graphics objects. Figure
objects are the individual windows on the screen in which MATLAB displays graphical
output.
6. subplot (Create axes in tiled positions): subplot divides the current figure into
rectangular panes that are numbered row wise. Each pane contains an axes object.
Subsequent plots are output to the current pane.
h = subplot(m,n,p) or subplot(mnp) breaks the figure window into an m-by-n matrix of
small axes, selects the pth axes object for the current plot, and returns the axes handle.
The axes are counted along the top row of the figure window, then the second row, etc.
For example,
subplot(2,1,1), plot(income)
subplot(2,1,2), plot(outgo) plots income on the top half of the window and outgo on the
bottom half.
7. stem (Plot discrete sequence data) : A two-dimensional stem plot displays data as lines
extending from a baseline along the x-axis. A circle (the default) or other marker whose
y-position represents the data value terminates each stem.
2
stem(Y) Plots the data sequence Y as stems that extend from equally spaced and
automatically generated values along the x-axis. When Y is a matrix, stem plots all
elements in a row against the same x value.
stem(X,Y) plots X versus the columns of Y. X and Y must be vectors or matrices of the
same size. Additionally, X can be a row or a column vector and Y a matrix with
length(X) rows.
8. bar(Plot bar graph (vertical and horizontal)) : A bar graph displays the values in a
vector or matrix as horizontal or vertical bars.
bar(Y) draws one bar for each element in Y. If Y is a matrix, bar groups the bars
produced by the elements in each row. The x-axis scale ranges from 1 up to length(Y)
when Y is a vector, and 1 to size(Y,1), which is the number of rows, when Y is a matrix.
barh(...) and h = barh(...) create horizontal bars. Y determines the bar length. The vector
x is a vector defining the y-axis intervals for horizontal bars.
9. plot ( 2-D line plot) :
plot(Y) Plots the columns of Y versus their index if Y is a real number. If Y is complex,
plot(Y) is equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the imaginary
component is ignored.
plot(X1,Y1,...) Plots all lines defined by Xn versus Yn pairs. If only Xn or Yn is a
matrix, the vector is plotted versus the rows or columns of the matrix,
depending on whether the vector's row or column dimension matches the matrix. If Xn is
a scalar and Yn is a vector, disconnected line objects are created and plotted as discrete
points vertically at Xn.
10. input (Request user input) : The response to the input prompt can be any MATLAB
expression, which is evaluated using the variables in the current workspace.
user_entry = input('prompt') Displays prompt as a prompt on the screen, waits for
input from the keyboard, and returns the value entered in user_entry. user_entry =
input('prompt', 's') returns the entered string as a text variable rather than as a variable
name or numerical value.
11. zeros (Create array of all zeros) :
B = zeros(n) Returns an n-by-n matrix of zeros. An error message appears if n is not a
scalar.
12. ones (Create array of all ones) :
Y = ones(n) Rreturns an n-by-n matrix of 1s. An error message appears if n is not a
scalar.
13. exp (Exponential) :
Y = exp(X) The exp function is an elementary function that operates element-wise on
arrays. Its domain includes complex numbers.
Y = exp(X) returns the exponential for each element of X.
3
w = conv(u,v) convolves vectors u and v. Algebraically, convolution is the same
operation as multiplying the polynomials whose coefficients are the elements of u and v.
16. xcorr (Cross-correlation) :
c = xcorr(x,y) returns the cross-correlation sequence in a length 2*N-1 vector, where x
and y are length N vectors (N>1). If x and y are not the same length, the shorter vector is
zero-padded to the length of the longer vector.
17. filter (1-D digital filter) :
y = filter(b,a,X) filters the data in vector X with the filter described by numerator
coefficient vector b and denominator coefficient vector a. If a(1) is not equal to 1, filter
normalizes the filter coefficients by a(1). If a(1) equals 0, filter returns an error.
18. poly (Polynomial with specified roots) :
r = roots(p) which returns a column vector whose elements are the roots of the
polynomial specified by the coefficients row vector p. For vectors, roots and poly are
inverse functions of each other, up to ordering, scaling, and round off error.
19. tf(Convert unconstrained MPC controller to linear transfer function) :
sys=tf(MPCobj) The tf function computes the transfer function of the linear
controller ss(MPCobj) as an LTI system in tf form corresponding to the MPC controller
when the constraints are not active. The purpose is to use the linear equivalent control in
Control System Toolbox for sensitivity and other linear analysis.
20. freqz (Frequency response of filter ) :
[h,w] = freqz(ha) returns the frequency response vector h and the corresponding
frequency vector w for the adaptive filter ha. When ha is a vector of adaptive filters, freqz
returns the matrix h. Each column of h corresponds to one filter in the vector ha.
21. abs (Absolute value and complex magnitude) :
abs(X) returns an array Y such that each element of Y is the absolute value of the
corresponding element of X.
22. fft (Discrete Fourier transform) :
Y = fft(X) Y = fft(X) returns the discrete Fourier transform (DFT) of vector X,
computed with a fast Fourier transform (FFT) algorithm.
23. mod (Modulus after division) :
M = mod(X,Y) returns X - n.*Y where n = floor(X./Y). If Y is not an integer and the
quotient X./Y is within round off error of an integer, then n is that integer. The inputs X
and Y must be real arrays of the same size, or real scalars.
24. sqrt (Square root) :
B = sqrt(X) returns the square root of each element of the array X. For the elements of
X that are negative or complex, sqrt(X) produces complex results.
25. ceil (Round toward infinity) :
B = ceil(A) rounds the elements of A to the nearest integers greater than or equal to A.
For complex A, the imaginary and real parts are rounded independently.
26. fir1(Window-based finite impulse response filter design) :
b = fir1(n,Wn) returns row vector b containing the n+1 coefficients of an order n lowpass
FIR filter. This is a Hamming-window based, linear-phase filter with normalized cutoff
frequency Wn. The output filter coefficients, b, are ordered in descending powers of z.
27. buttord (Butterworth filter order and cutoff frequency) :
4
[n,Wn] = buttord(Wp,Ws,Rp,Rs) returns the lowest order, n, of the digital Butterworth
filter that loses no more than Rp dB in the passband and has at least Rs dB of attenuation in
the stopband. The scalar (or vector) of corresponding cutoff frequencies, Wn, is also
returned. Use the output arguments n and Wn in butter.
28. fliplr (Flip matrix left to right) :
B = fliplr(A) returns A with columns flipped in the left-right direction, that is, about a
vertical axis.If A is a row vector, then fliplr(A) returns a vector of the same length with the
order of its elements reversed. If A is a column vector, then fliplr(A) simply returns A.
29. min ( Smallest elements in array) :
C = min(A) returns the smallest elements along different dimensions of an array. If A is a
vector, min(A) returns the smallest element in A.If A is a matrix, min(A) treats the
columns of A as vectors, returning a row vector containing the minimum element from
each column. If A is a multidimensional array, min operates along the first nonsingleton
dimension.
30. max ( Largest elements in array) :
C = max(A) returns the largest elements along different dimensions of an array.If A is a
vector, max(A) returns the largest element in A.If A is a matrix, max(A) treats the columns
of A as vectors, returning a row vector containing the maximum element from each
column. If A is a multidimensional array, max(A) treats the values along the first non-
singleton dimension as vectors, returning the maximum value of each vector.
31. find (Find indices and values of nonzero elements) :
ind = find(X) locates all nonzero elements of array X, and returns the linear indices of
those elements in vector ind. If X is a row vector, then ind is a row vector; otherwise, ind is
a column vector. If X contains no nonzero elements or is an empty array, then ind is an
empty array.
32. residuez (z-transform partial-fraction expansion ) :
residuez converts a discrete time system, expressed as the ratio of two polynomials, to
partial fraction expansion, or residue, form. It also converts the partial fraction expansion
back to the original polynomial coefficients.
[r,p,k] = residuez(b,a) finds the residues, poles, and direct terms of a partial fraction
expansion of the ratio of two polynomials, b(z) and a(z). Vectors b and a specify the
coefficients of the polynomials of the discrete-time system b(z)/a(z) in descending powers
of z.
5
Dsp Lab.
clc;
close all;
clear all;
k=input('enter value of k=');
n1=input('enter value of n1=');
n2=input('enter value of n2=');
n=n1:1:n2;
x=(n-k)==0;
stem(n,x);
xlabel('---> n');
ylabel('amplitude');
title('unit impulse');
unit impulse
1
0.9
0.8
0.7
0.6
amlitude
0.5
0.4
0.3
0.2
0.1
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
---> n
Figure 1
Command window
6
2.Unit step signal
clc;
close all;
k=input('enter value of k=');
n1=input('enter value of n1=');
n2=input('enter value of n2=');
n=n1:1:n2;
x=(n-k)>=0;
stem(n,x);
xlabel('---> n');
ylabel('amlitude');
title('unit step');
unit step
1
0.9
0.8
0.7
0.6
amlitude
0.5
0.4
0.3
0.2
0.1
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
---> n Figure 2
Command window
7
3.Unit ramp signal
clc;
close all;
k=input('enter value of k=');
n1=input('enter value of n1=');
n2=input('enter value of n2=');
n=n1:1:n2;
x=(n-k)>=0;
y=n;
z=x.*y;
stem(n,z);
xlabel('---> n');
ylabel('amlitude');
title('unit ramp');
unit ramp
10
6
amlitude
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
---> n
Figure 3
8
4.1 Real exponential signals(r-real number)
clc;
close all;
n1=input('enter value of n1=');
n2=input('enter value of n2=');
a=input('enter value of a=');
n=n1:1:n2;
x=a.^n;
stem(n,x);
xlabel('---> n');
ylabel('amlitude');
title('real exponential signal');
2.5
2
amlitude
1.5
0.5
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
Figure 4 ---> n
Command window
9
Command window
1
amlitude
-1
-2
-3
-10 -8 -6 -4 -2 0 2 4 6 8 10
---> n
Figure 5
real exponential signal
Command window 7
5
enter value of n2=10
4
amlitude
2
10
1
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
---> n
Command window
2
amlitude
-2
-4
-6
-10 -8 -6 -4 -2 0 2 4 6 8 10
---> n
Figure 7
11
Real exponential 2×2-matrix
clc;
close all;
n1=input('enter value of n1=');
n2=input('enter value of n2=');
a=input('enter value of a=');
b=input('enter value of b=');
c=input('enter value of c=');
d=input('enter value of d=');
n=n1:1:n2;
x=a.^n;
y=b.^n;
z=c.^n;
w=d.^n;
subplot (2,2,1);stem(n,x);
xlabel('---> n');
ylabel('amlitude');
title('real exponential signal (0.9).^n');
subplot (2,2,2);stem(n,y);
xlabel('---> n');
ylabel('amlitude');
title('real exponential signal (-0.9).^n');
subplot (2,2,3);stem(n,z);
xlabel('---> n');
ylabel('amlitude');
title('real exponential signal (1.2).^n');
12
subplot (2,2,4);stem(n,w);
xlabel('---> n');
ylabel('amlitude');
title('real exponential signal (-1.2).^n');
n n
real exponential signal (0.9). real exponential signal (-0.9).
3 4
2 2
amlitude
amlitude
0
1
-2
0
-10 -5 0 5 10 -10 -5 0 5 10
---> n ---> n
n n
real exponential signal (1.2). real exponential signal (-1.2).
6
5
4
amlitude
amlitude
0
2
0 -5
-10 -5 0 5 10 -10 -5 0 5 10
---> n ---> n
Figure 8
Command window
13
enter value of d=-1.2
clc;
close all;
n1=input('Enter value of n1=');
n2=input('Enter value of n2=');
n=n1:10:n2;
r1=0.999;
r2=1.001;
x1=r1.^n;
x2=r2.^n;
y1=x1.*cos(0.01*pi*n);
y2=x2.*cos(0.01*pi*n);
subplot(2,1,1);
plot(n,y1);title('r=0.999<1');
subplot(2,1,2);
plot(n,y2);title('r=1.001>1');
Command window
Enter value of n1=-1000
Enter value of n2= 5000
14
5. magnitude and phase plot
clc;
close all;
n1=input('enter value of n1=');
n2=input('enter value of n2=');
alpha=0.1+0.3j;
n=n1:1:n2;
x=exp(alpha*n);subplot(2,2,1);stem(n,real(x));
title('real part');xlabel('---n---');
subplot(2,2,2);stem(n,imag(x));
title('imaginary part');xlabel('---n---');
subplot(2,2,3);stem(n,abs(x));
title('magnitude part');xlabel('---n---');
subplot(2,2,4);stem(n,(180/pi)*angle(x));
title('phase part');xlabel('---n---');
15
real part imaginary part
1 2
0
1
-1
0
-2
-1
-10 -5 0 5 10 -10 -5 0 5 10
---n--- ---n---
magnitude part phase part
3 200
100
2
0
1
-100
0 -200
-10 -5 0 5 10 -10 -5 0 5 10
---n--- ---n---
Figure 9
Command window
result
R =
1.0989 + 0.0000i
-0.0495 - 0.5425i
-0.0495 + 0.5425i
p =
16
1.0000 + 0.0000i
0.4500 + 0.7794i
0.4500 - 0.7794i
c =
[]
z-plane
17
b. Discrete
n=-10:100;
x=(n)>=0;
b=1;
a=[1,-0.9,0.81];
y=filter(b,a,x);
stem(n,y)
18
Response of the system to unit step signal
Y(n)=[1.099+1.088*0.9n cos((𝝿n/3)-8֯
5 ]u(n)
Code
n=-10:100;
x=(n)>=0;
y1=1.099+1.088*((0.9).^n).*cos((pi*n/3)-0.47*pi);
y=y1.*x;
plot(n,y);xlabel('n');ylabel('y(n)');title('cos(pi*n/3-85)');
Y(n)=[1.099+1.088*0.9n cos((𝝿n/3)+8֯
5 ]u(n)
Code
n=-10:100;
x=(n)>=0;
y1=1.099+1.088*((0.9).^n).*cos((pi*n/3)+0.47*pi);
y=y1.*x;
plot(n,y);xlabel('n');ylabel('y(n)');title('cos(pi*n/3+85)');
19
Response of the system to unit step signal for non-zero initial
condition
y(n)=0.9y(n-1)-0.81y(n-2)+x(n)if y(-1)=y(-2)=1
b=[1.09,-0.9,0.81];
a=[1,-1.9,1.71,-0.81];
[R,p,c]=residuez(b,a)
zplane(b,a)
20
Result
R =coefficients
1.0989 + 0.0000i
-0.0045 - 0.0488i
-0.0045 + 0.0488i
p =poles
1.0000 + 0.0000i
0.4500 + 0.7794i
0.4500 - 0.7794i
c =
[]
z-plane
21
8.CTFT(X(w))
clc;
close all;
a=input('enter value of a=');
n=-20:1:20;
b=a.^abs(n);
22
c=fft(b);
d=angle(c);
e=imag(c);
f=real(c);
g=abs(f);
subplot(2,2,1);stem(n,b);title('x(n)');xlabel('---n---');
subplot(2,2,2);plot(n,d);title('phase');xlabel('---n---');
subplot(2,2,3);plot(n,e);title('imaginary part');
xlabel('---n---');
subplot(2,2,4);plot(n,g);title('X(w)');
Command window
x(n) phase
1 4
0.5 0
-2
0 -4
-20 -10 0 10 20 -20 -10 0 10 20
---n--- ---n---
imaginary part X(w)
0.5 20
15
0 10
-0.5 0
-20 -10 0 10 20 -20 -10 0 10 20
---n---
Figure 10
Command window
23
x(n) phase
1 2
0.5 1
0 0
-0.5 -1
-1 -2
-20 -10 0 10 20 -20 -10 0 10 20
---n--- ---n---
imaginary part X(w)
20 0.6
10
0.4
0
0.2
-10
-20 0
-20 -10 0 10 20 -20 -10 0 10 20
---n---
Figure 11
Command window
x(n) phase
40 2
30 1
20 0
10 -1
0 -2
-20 -10 0 10 20 -20 -10 0 10 20
---n--- ---n---
imaginary part X(w)
20
400
10
300
0
200
-10 100
-20 0
-20 -10 0 10 20 -20 -10 0 10 20
---n---
Figure 12
Command window
24
enter value of a=-1.2
x(n) phase
40 2
20 1
0 0
-20 -1
-40 -2
-20 -10 0 10 20 -20 -10 0 10 20
---n--- ---n---
imaginary part X(w)
200 40
100 30
0 20
-100 10
-200
0
-20 -10 0 10 20 -20 -10 0 10 20
---n---
Figure 13
25
close all;
w1=input('enter value of w1=');
w2=input('enter value of w2=');
L=input('enter value of L=');
w=w1:w2;
a=sin(L*pi/180*w/2);
b=sin((pi/180)*w/2);
c=a./b;
d=exp(-j*w*(pi/180)*(L-1)/2);
e=c.*d;
f=abs(e);
g=angle(e);
subplot(2,1,1);plot(w*(pi/180),f);
xlabel('w');ylabel('|X(w)|');title('DTFT');
subplot(2,1,2);plot(w*(pi/180),g);
xlabel('w');title('phase angle');
DTFT
10
|X(w)|
0
-8 -6 -4 -2 0 2 4 6 8
w
phase angle
4
-2
-4
-8 -6 -4 -2 0 2 4 6 8
w
Command window
26
close all;
N=input('enter value of N=');
L=input('enter value of L=');
k1=-(N/2)+1;
k2=(N/2)-1;
k=k1:k2;
w=2*pi*k/N;
a=sin(L*w/2);
b=sin(w/2);
c=a./b;
d=exp(-1i*w*(L-1)/2);
e=c.*d;
f=abs(e);
g=angle(e);
subplot(2,1,1);stem(k,f);
xlabel('k');ylabel('|X(k)|');title('DFT');
subplot(2,1,2);stem(k,g);
xlabel('k');title('phase angle');
DFT
10
|X(k)|
0
-25 -20 -15 -10 -5 0 5 10 15 20 25
k
phase angle
4
-2
-4
-25 -20 -15 -10 -5 0 5 10 15 20 25
k
Command window
10.2 DFT(0->N)
27
clc;
close all;
N=input('enter value of N=');
L=input('enter value of L=');
k1=0
k2=N-1;
k=k1:k2;
w=2*pi*k/N;
a=sin(L*w/2);
b=sin(w/2);
c=a./b;
d=exp(-1i*w*(L-1)/2);
e=c.*d;
f=abs(e);
g=angle(e);
subplot(2,1,1);stem(k,f);
xlabel('k');ylabel('|X(k)|');title('DFT');
subplot(2,1,2);stem(k,g);
xlabel('k');title('phase angle');
DFT
10
|X(k)|
0
0 5 10 15 20 25 30 35 40 45 50
k
phase angle
2
-2
-4
0 5 10 15 20 25 30 35 40 45 50
k
command window
Phase shift
28
n=0:50;
x=cos(0.1*pi*n);
a=plot(n,x);
hold on
y=cos(0.1*pi*n-2*pi/3);
b=plot(n,y);
hold on
z=cos(0.1*pi*n+2*pi/3);
c=plot(n,z);
legend([a,b,c],'cos(0.1*pi*n)','cos(0.1*pi*n2*pi/3))',
'cos(0.1*pi*n+2*pi/3)');
29
clc;
close all;
N=input('enter value of N=');
w1=input('enter value of w1=');
w2=input('enter value of w2=');
k=0:N-1;
w=w1:w2;
a=exp(-0j*w*(pi/180));
b=2*exp(-1j*w*(pi/180));
c=3*exp(-2j*w*(pi/180));
d=a+b+c;
l=abs(d);
s=angle(d);
e=exp(-0j*(360/N)*k*(pi/180));
f=2*exp(-1j*(360/N)*k*(pi/180));
g=3*exp(-2j*(360/N)*k*(pi/180));
h=e+f+g;
t=abs(h);p=angle(h);
subplot(2,1,1);
x=stem((2*180*k/N)*(pi/180),t);
hold on
y=plot(w*(pi/180),l);
xlabel('w in pi radian');ylabel('|x(k)|');title('DFT');
legend([x,y],'DFT','DTFT');
subplot(2,1,2);v=plot(w*(pi/180),s);
xlabel('w in pi radian');title('Phase angle');
hold on
u=stem((2*180*k/N)*(pi/180),p);legend([u,v],'DFT','DTFT');
Command window
enter value of N=8
enter value of w1=0 enter value of w2=360
30
11.Circular convolution
For x1= {2, 1, 2, 1}, x2= {1, 2, 3, 4}
Code
n1=input('value of n1 =');
n2=input('value of n2 =');
n=n1:1:n2;
a1=2.*((n-0)==0);
a2=1.*((n-1)==0);
a3=2.*((n-2)==0);
a4=1.*((n-3)==0);
x1=a1+a2+a3+a4;
z1=1.*((n-0)==0);
z2=2.*((n-1)==0);
z3=3.*((n-2)==0);
z4=4.*((n-3)==0);
x2=z1+z2+z3+z4;
w=conv(x1,x2);
y1=cconv(x1,x2,4);
y2=cconv(x1,x2,5);
y3=cconv(x1,x2,6);
display(x1);
display(x2);
display(w);
display(y1);
display(y2);
display(y3);
subplot(2,3,1); stem(n,x1);title('x1(n)');xlabel('---> n');
subplot(2,3,2); stem(n,x2);title('x2(n)');xlabel('---> n')
subplot(2,3,3); stem(w);title('convolution');ylabel('w(n)');xlabel('---> n');
subplot(2,3,4); stem(y1);title('circular conv
N=4');ylabel('y1(n)');xlabel('---> n')
subplot(2,3,5); stem(y2);title('circular conv
N=5');ylabel('y2(n)');xlabel('---> n')
subplot(2,3,6); stem(y3);title('circular conv
N=6');ylabel('y3(n)');xlabel('---> n')
Command window
value of n1 =0
value of n2 =3
x1 =
31
2 1 2 1
x2 =
1 2 3 4
w =
2 5 10 16 12 11 4
y1 =
14 16 14 16
y2 =
13 9 10 16 12
y3 =
6 5 10 16 12 11
32
x= [1 3 2 1];
y= [2 1 2 1];
z= conv(x, y);
w= cconv(x, y, 4);
m= fft(x);
a=sqrt(2);
x0=6;
x1=1+a-1j*(3+a);
x2=-2-2j;
x3=1-a+1j*(3-a);
x4=2;
x5=1-a-1j*(3-a);
x6=-2+2j;
x7=1+a+1j*(3+a);
x=[x0 x1 x2 x3 x4 x5 x6 x7];
a=ifft(x,8);
display(a);
Command window
b =
1 2 3 4
a =
33
13. Filter design
>>fdatool--enter
command window
enter value of M=7
hR =
hBL =
hHN =
34
hHM =
hBR =
35
Lpf coeficients with length M=61 (rectangular windowing)
M=input('enter value of M=');
n=0:M-1;
w=0:180;
a=(1./(pi*(n-((M-1)/2))));
b=sin((pi/4)*(n-((M-1)/2)));
hd=a.*b;
wR=1;
hR=hd.*wR;
display(hR);
Command window
36
hR =
Columns 1 through 18
Columns 19 through 36
Columns 37 through 54
Columns 55 through 61
-0.0106
----------------------------------------------------------------
37
Lpf rectangular window design M=61
w=0:180;
l3=exp(-30j*w*(pi/180));
a=-0.0106;
b=-0.0078*exp(-1j*w*(pi/180));
c=0*exp(-2j*w*(pi/180));
d=0.0083*exp(-3j*w*(pi/180));
e=0.0122*exp(-4j*w*(pi/180));
f=0.009*exp(-5j*w*(pi/180));
g=-0*exp(-6j*w*(pi/180));
h=-0.0098*exp(-7j*w*(pi/180));
i=-0.0145*exp(-8j*w*(pi/180));
j=-0.0107*exp(-9j*w*(pi/180));
k=0*exp(-10j*w*(pi/180));
l=0.0118*exp(-11j*w*(pi/180));
m=0.0177*exp(-12j*w*(pi/180));
n=0.0132*exp(-13j*w*(pi/180));
o=-0*exp(-14j*w*(pi/180));
p=-0.015*exp(-15j*w*(pi/180));
q=-0.0227*exp(-16j*w*(pi/180));
r=-0.0173*exp(-17j*w*(pi/180));
s=0*exp(-18j*w*(pi/180));
t=0.0205*exp(-19j*w*(pi/180));
u=0.0318*exp(-20j*w*(pi/180));
v=0.025*exp(-21j*w*(pi/180));
x=-0*exp(-22j*w*(pi/180));
y=-0.0322*exp(-23j*w*(pi/180));
z=-0.0531*exp(-24j*w*(pi/180));
a1=-0.045*exp(-25j*w*(pi/180));
b1=0*exp(-26j*w*(pi/180));
c1=0.075*exp(-27j*w*(pi/180));
d1=0.1592*exp(-28j*w*(pi/180));
e1=0.2251*exp(-29j*w*(pi/180));
f1=0.25*exp(-30j*w*(pi/180));
g1=0.2251*exp(-31j*w*(pi/180));
h1=0.1592*exp(-32j*w*(pi/180));
i1=0.075*exp(-33j*w*(pi/180));
j1=0*exp(-34j*w*(pi/180));
k1=-0.045*exp(-35j*w*(pi/180));
l1=-0.0531*exp(-36j*w*(pi/180));
m1=-0.0322*exp(-37j*w*(pi/180));
n1=-0*exp(-38j*w*(pi/180));
o1=0.025*exp(-39j*w*(pi/180));
p1=0.0318*exp(-40j*w*(pi/180));
q1=0.0205*exp(-41j*w*(pi/180));
r1=0*exp(-42j*w*(pi/180));
s1=-0.0173*exp(-43j*w*(pi/180));
t1=-0.0227*exp(-44j*w*(pi/180));
u1=-0.015*exp(-45j*w*(pi/180));
38
v1=-0*exp(-46j*w*(pi/180));
x1=0.0132*exp(-47j*w*(pi/180));
y1=0.0177*exp(-48j*w*(pi/180));
z1=0.0118*exp(-49j*w*(pi/180));
a2=0*exp(-50j*w*(pi/180));
b2=-0.0107*exp(-51j*w*(pi/180));
c2=-0.0145*exp(-52j*w*(pi/180));
d2=-0.0098*exp(-53j*w*(pi/180));
e2=-0*exp(-54j*w*(pi/180));
f2=0.009*exp(-55j*w*(pi/180));
g2=0.0122*exp(-56j*w*(pi/180));
h2=0.0083*exp(-57j*w*(pi/180));
i2=0*exp(-58j*w*(pi/180));
j2=-0.0078*exp(-59j*w*(pi/180));
k2=-0.0106*exp(-60j*w*(pi/180));
h3=a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+x+y+z;
h4=a1+b1+c1+d1+e1+f1+g1+h1+i1+j1+k1+l1+m1+n1+o1+p1+q1+r1+s1+t1+u1+v1+x
1+y1+z1;
h5=a2+b2+c2+d2+e2+f2+g2+h2+i2+j2+k2;
h6=h3+h4+h5;
h7=abs(h6);
h8=h7/max(h7);
h9=20*log(h8);
subplot(2,2,1);plot(w*(1/180),h8);xlabel('w');title('|H(w)|');
subplot(2,2,2);plot(w*(1/180),h9);xlabel('w');title('|H(w)| in dB');
subplot(2,2,3);
ab=plot((w*(1/180)),h6);
hold on
cd=plot(w*(1/180),l3);
legend([ab,cd],'filtered signal','original signal');
39