DSP Lab 1 PDF
DSP Lab 1 PDF
DSP Lab 1 PDF
The basic elements in Matlab are numbers, variables, and operators. The basic type of variable is the
matrix. One-dimensional matrices (row or column matrices) are called vectors.
Note that
● Discrete-time sequences are represented in Matlab by vectors (row or column matrices);
● Numbers are represented with high precision and can be complex;
● Indexes of matrixes always start from 1 and not from 0.
The colon operator : can be used to generate sequences of numbers in a given range.
>> a=1:10
a =
1 2 3 4 5 6 7 8 9 10
>> b=2:3:10 % from 2 to 10 with step 3
b =
2 5 8
The colon operator is also used to access certain row/column elements of a matrix. B=A(:,1) is the
subset of A given by “column 1, all the rows”; the meaning of A(:) is “all the elements of A, read
column by column and regarded as a single column”. See “help :” at the prompt of Matlab for further
details.
>> A=[1 2 3; 4 5 6; 7 8 9];
>> A(2:3,:)
ans =
4 5 6
7 8 9
1
The command size(A) provides the size, in rows and columns, of the matrix A, while length(a)
returns the length of a vector a.
MATLAB provides many useful functions to create special matrices. These include:
● zeros(M,N) for creating a matrix of all zeros,
● ones(M,N) for creating matrix of all ones,
● eye(N) for creating an N×N identity matrix.
When time axes other than the sequence 1,2,3,...,N (or 0,1,2,...,N-1 with a unitary shift of the index) need
to be represented for the sequence x, a time-axis vector t or a sample-index vector n will be associated to
the vector x.
The basic plotting command is plot(t,x), which generates a plot of x values versus t values.
The following set of commands creates a list of sample points, evaluates the sine function at those points,
and then generates a plot of a simple sinusoidal wave, putting axis labels and title on the plot.
t = 0:0.01:2; % sample points from 0 to 2 in steps of 0.01
x = sin(2*pi*t); % Evaluate sin(2 t)
plot(t,x,'b'); % Create plot with blue line
xlabel('t in sec'); ylabel('x(t)'); % Label axes
title('Plot of sin(2\pi t)'); % Title plot
For plotting a discrete-time sequence we will use the stem command which displays each data value
with a small circle at the end of a line connecting it to the horizontal axis.
2
n = 0:1:40; % sample index from 0 to 40
x = sin(0.1*pi*n); % Evaluate sin(0.1 n)
stem(n,x); % Stem-plot
xlabel('n'); ylabel('x(n)'); % Label axes
title('Stem Plot of sin(0.1 \pi n)'); % Title plot
If we want to create multiple plots within a graphical window, we can use the function subplot.
subplot(r,c,i) splits the current graphical windows into a matrix of subplots (r rows by c
columns) and activate the i-th subplot.
figure % open new figure window
subplot(2,1,1) % split into one column of two subplots and
% select the first one
The following commands generate a real-valued exponential sequence x[n] = (0.9)n , 0 ≤ n ≤ 100
>> n = [0:100]; x = (0.9).^n;
3
The rand(1,N) generates a length N random sequence whose elements are uniformly distributed
between [0, 1]. The randn(1,N) generates a length N Gaussian random sequence with mean 0 and
variance 1.
To generate P periods of a periodic sequence x̃[n] from one period {x[n], 0 ≤ n ≤ N −1}, we can copy
x[n] P times:
>> xtilde = [x,x,x,x,x,x];
but a more efficient way is to use the operator (:) on a matrix with columns that replicate the sequence x'
>> xtilde = x' * ones(1,P); % P columns of x; x is a row vector
>> xtilde = xtilde(:); % long column vector
>> xtilde = xtilde'; % long row vector
4
The following function sigadd implements the sample-by-sample addition of sequences. This
operation requires that the sequences be defined over a common sample interval represented by n. This is
obtained using the logical operation of intersection “&”, relational operations like “<=” and “==”, and the
find function.
function [y,n] = sigadd(x1,n1,x2,n2)
% implements y(n) = x1(n)+x2(n)
% -----------------------------
% [y,n] = sigadd(x1,n1,x2,n2)
%
y = sum sequence over n, which includes n1 and n2
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be different from n1)
%
n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)
y1 = zeros(1,length(n)); y2 = y1; % initialization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1+y2; % sequence addition
A similar function sigmult implements the sample-by-sample multiplication (with the operator “.*”)
function [y,n] = sigmult(x1,n1,x2,n2)
% implements y(n) = x1(n)*x2(n)
% -----------------------------
% [y,n] = sigmult(x1,n1,x2,n2)
%
y = product sequence over n, which includes n1 and n2
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be different from n1)
%
n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)
y1 = zeros(1,length(n)); y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1 .* y2; % sequence multiplication
The shifting function sigshift has no effect on the vector x, but the support vector n is changed by
adding k to each element.
function [y,n] = sigshift(x,m,k)
% implements y(n) = x(n-k)
% -------------------------
% [y,n] = sigshift(x,m,k)
%
n = m+k; y = x;
5
The sigfold function implements the sign-reversal of the sample indexes n to get y[n]=x[-n]
function [y,n] = sigfold(x,n)
% implements y(n) = x(-n)
% -----------------------
% [y,n] = sigfold(x,n)
%
y = fliplr(x); n = -fliplr(n);
The summation of the sample values of x[n] between n1 and n2 is obtained with
>> sum(x(n1:n2))
while the product of the sample values of x[n] between n1 and n2 is obtained with
>> prod(x(n1:n2))
∗
| |
Any arbitrary real-valued sequence x[n] can be decomposed into its even and odd components. This
decomposition is implemented by the following evenodd function
function [xe, xo, m] = evenodd(x,n)
% Real signal decomposition into even and odd parts
% -------------------------------------------------
% [xe, xo, m] = evenodd(x,n)
%
if any(imag(x) ~= 0)
error('x is not a real sequence')
end
m = -fliplr(n);
m1 = min([m,n]); m2 = max([m,n]); m = m1:m2;
nm = n(1)-m(1); n1 = 1:length(n);
x1 = zeros(1,length(m));
x1(n1+nm) = x; x = x1;
xe = 0.5*(x + fliplr(x)); xo = 0.5*(x - fliplr(x));
6
Examples:
1. Generate and plot the sequence x[n] = δ[n + 2] − 2δ[n − 3], −5 ≤ n ≤ 5
n = [-5:5];
x = impseq(-2,-5,5) - 2*impseq(3,-5,5);
stem(n,x); title('Sequence in Example 1')
xlabel('n'); ylabel('x(n)');
4. Generate and plot, for −30 ≤ n ≤ 29, the periodical sequence x̃[n] obtained from one period
x[n]=n+1, 0 ≤ n ≤ 9
n = [-30:29];
x = [1:10];
xtilde = x' * ones(1,6); % one period in each column
xtilde = (xtilde(:))'; % reshape into one single row
stem(n,xtilde); title('Sequence in Example 4')
xlabel('n'); ylabel('x(n)');
5. Let x[n] = {1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1}, where the underline denotes the sample at n=0.
Determine and plot the following sequences
A. x1[n] = 2x[n − 5] − 3x[n + 4]
B. x2[n] = x[3 − n] + x[n] x[n − 2]
% A.
n = -2:10; x = [1:7,6:-1:1];
7
[x11,n11] = sigshift(x,n,5);
[x12,n12] = sigshift(x,n,-4);
[x1,n1] = sigadd(2*x11,n11,-3*x12,n12);
stem(n1,x1); title('Sequence in Example 5.A')
xlabel('n'); ylabel('x1(n)');
% B.
n = -2:10; x = [1:7,6:-1:1];
[x21,n21] = sigfold(x,n);
[x21,n21] = sigshift(x21,n21,3);
[x22,n22] = sigshift(x,n,2);
[x22,n22] = sigmult(x,n,x22,n22);
[x2,n2] = sigadd(x21,n21,x22,n22);
n = [-10:10]; x = zeros(1,length(n));
for k = -5:5
x = x + exp(-abs(k))*impseq(2*k ,-10,10);
end
stem(n,x); title('Sequence in Example 6')
xlabel('n'); ylabel('x(n)');
7. Generate the complex-valued signal x[n] = e(−0.05+j0.3)n , −30 ≤ n ≤ 30 and plot its magnitude, phase, the
real part, and the imaginary part in four separate subplots
8
subplot(2,2,4);
stem(n,(180/pi)*angle(x)); title('Phase'); xlabel('n')
Compare the obtained plots with the graphical representation in the complex plane produced by
figure; polar(angle(x),abs(x),'-o')
8. Decompose x[n] = u[n] − u[n − 10] into its even and odd components
n = [0:10];
x = stepseq(0,0,10)-stepseq(10,0,10);
[xe,xo,m] = evenodd(x,n);
Exercises
1. Generate and plot the following sequences
x1[n] = 3δ[n + 2] + 2δ[n] – δ[n – 3] + 5δ[n – 7], −5 ≤ n ≤ 15
x2[n] = 10u[n] − 5u[n – 5] − 10u[n – 10] + 5u[n – 15]
x3[n] = e0.1n (u[n + 20] – u[n – 10])
x4[n] = 2(cos(0.49π n) + cos(0.51π n)), −200 ≤ n ≤ 200
x5[n] = 3 sin(0.01π n) · cos(0.5π n), −200 ≤ n ≤ 200
x6[n] = e−0.05n ·sin(0.1π n + π/3), 0 ≤ n ≤ 100
x7[n] = {. . . , 3, 1, 0, -1, 2, . . .} periodic (plot 10 periods)
2. Let x[n] = {2, 4, −3, 1, −5, 4, 7} (underline indicates n=0). Generate and plot the following sequences
x1[n] = 2x[n – 3] + 3x[n + 4] – x[n]
x2[n] = x[n + 3]x[n – 2] + x[1 – n]x[n + 1]
x3[n] = 2e0.5n x[n] + cos (0.1π n) · x [n + 2] , −10 ≤ n ≤ 10
3. Decompose the following sequences into their even and odd components and plot them
x1[n] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
x2[n] = e0.1n · (u[n + 5] – u[n – 10])
x3[n] = cos(0.2π n + π/4), −20 ≤ n ≤ 20
x4[n] = e−0.05n ·sin(0.1π n + π/3) ·u[n]
9
Plots of exercise 1.
Plots of exercise 2
Plots of exercise 3
10