Signals & Systems Lab.-Manual
Signals & Systems Lab.-Manual
Signals & Systems Lab.-Manual
1. Basic signals……………………………………………………………………………………………2
2. Operations on signals……………………………………………………………………………..5
7. Fourier Transform………………………………………………………………………………..12
8. Image processing…………………………………………………………………………………..13
The unit step function u(t) is basically a mathematical function that is defined by:
⎧1 ,t > 0
u (t ) = ⎨
⎩0 ,t < 0
So, it is clear that the function is undefined at zero because of its discontinuity.
The unit step function is defined in MATLAB as follows:
The heaviside function is defined easily such that it can shifted or reversed as follows:
>> g=heaviside(t-3);
>> figure(1)
>> plot(t,g)
>> axis([-15 15 -1 2])
>> h=heaviside(t+4);
>> figure(2)
>> plot(t,h)
>> axis([-15 15 -1 2])
>> k=heaviside(-t);
>> figure(3)
>> plot(t,k)
>> axis([-15 15 -1 2])
>> l=heaviside(-2*t+2);
>> figure(4)
>> plot(t,l)
>> axis([-15 15 -1 2])
⎧1 ,n ≥ 0
u[n] = ⎨
⎩0 ,n < 0
While, MATLAB uses the same continuous-time expression of u(t) for discrete-time
unit step function causing an error in the definition of u[n] in MATLAB.
>> n=-10:10;
>> figure(1)
>> f=heaviside(n); % note that it is not defined at n=0 (mistake)
>> stem(n,f)
>> axis([-15 15 -1 2])
So, you should take care while using MATLAB with discrete time unit step functions.
It is also possible to do shifting, reversing, and scaling for the discrete-time unit step
function as shown in the following example.
>> n=-10:10;
>> f1=heaviside(n+3);
>> f2=heaviside(-n+3);
>> f3=0.5.^n;
>> f4=f1.*f2.*f3;
>> subplot(221)
>> stem(n,f1)
>> grid
>> title(‘u[n+3]’);
>> subplot(222)
>> stem(n,f2)
>> grid
>> title(‘u[-n+3]’);
>> subplot(223)
>> stem(n,f3)
>> grid
>> title(‘u[n+3]*u[-n+3]’);
>> subplot(224)
>> stem(n,f4)
>> grid
>> title(‘(0.5)^n*u[n+3]*n[-n+3]’);
The unit impulse function is not a function in the strict sense mean while it is
very useful in the mathematical analysis of signals and specially signal which has
discontinuities or sudden changes.
It is defined as:
⎧undefined (∞) ,t = 0
δ (t ) = ⎨
⎩0 ,t ≠ 0
We usually care for the area of the impulse delta or dirac function as its width is
1
usually and its height is Δ where Δ tends to zero keeping the area equal to 1.
Δ
MATLAB defines the delta function in the same manner, so you can’t see its value at
t=0, also you can do shifting and reversing on it as in the following example:
>> t=-10:0.01:10;
>> f=dirac(-t+5);
>> plot(t,f)
The problem occurs in the definition of MATLAB for the discrete-time delta function
as it uses the same continuous-time expression while the discrete-time delta is defined
as follows:
⎧1 ,t = 0
δ [ n] = ⎨
⎩0 ,t ≠ 0
>> n=-10:10;
>> f=dirac(-n+2);
>> stem(n,f)
The most important properties of the delta function are the multiplication and
convolution with other signals. The delta function can be multiplied by any other
signal as follows:
x ( t ) × δ ( t − t o ) = x (t o ) × δ ( t − t o )
∞
∫ x (t ) × δ (t − to )dt = x (to )
−∞
>> syms t;
>> x=2*sin(t);
>> f=x.*dirac(t-pi/2) % see the value of f.
>> g=int(f,t,-inf,inf)
>> syms t;
>> f=heaviside(t-5);
>> diff(f,t)
>> g=dirac(t+2);
>> int(g,t)
>> int(g,t,-inf,inf)
The last example ensures that the delta function is the derivative of the unit step
function and hence the integration of the delta function form –ve infinity till t leads to
the unit step function while integrating the delta function from –ve infinity to +ve
infinity is always equal to one which is the are under its curve.
2. Operations Signals
Signals can be added and subtracted on condition which is that they be of the
same length when defined in MATLAB in order to add or subtract the adjacent
elements or values of the signals.
>> n=-10:10;
>> x1= 0.5.^n;
>> x2= 0.8.^n;
>> x=x1+x2;
>> y=x1-x2;
>> subplot(221)
Signals can be multiplied and divided on condition that they are of the same
length. You should be aware not to divide by zero. Finally, it is very important to
remind you of the dot operator that is used in vectors to access element by element
not the whole matrix or vector because our signals will be horizontal vectors in most
of cases.
>> n=-10:10;
>> x1= 0.6.^n;
>> x2= 0.2.^n;
>> x=x1.*x2;
>> y=x1./x2;
>> subplot(221)
>> stem(n,x1)
>> subplot(222)
>> stem(n,x2)
>> subplot(223)
>> stem(n,x)
>> subplot(224)
>> stem(n,y)
You can get the minimum and the maximum of any signal simply using the
min and max instructions. You can also search for all minimums and maximums
using the find instruction as shown in the following example.
>> n=-20:20;
>> x=cos(pi*n/4);
>> stem(n,x)
>> hold
>> xmin=min(x);
>> xmax=max(x);
>> pmin=find(x==xmin);
>> pmax=find(x==xmax);
>> ymin=x(pmin);
>> ymax=x(pmax);
>> stem(n(pmin),ymin,’filled’)
>> stem(n(pmax),ymax,’filled’)
For any discrete-time signal you can calculate the average energy and average
power using the following formulas:
N2
Eav = ∑ x[ n]
2
N1
N2
1
∑
2
Pav = x[ n]
N 2 − N1 + 1 N1
>> n=-10:10;
>> x=(0.5).^n.*heaviside(n);
>> E=sum(abs(x).^2)
>> Pav=E/length(x)
Another method for calculating the energy is as follows:
>> E=x.*(x.’)
Task: Try to take a signal from a user and calculate its energy and power.
>> n=-10:10;
>> x=heaviside(n-1);
>> y=heaviside(n+2);
>> z= heaviside(-n+3);
>> k= heaviside(-n-2);
>> l= heaviside(2*n-1);
>> m= heaviside(0.5*n+1);
>> h=heaviside(-0.5*n-2);
>> g= heaviside(-2*n+3);
>> subplot(421)
>> stem(n,x)
>> subplot(422)
>> stem(n,y)
You are now required to search for the solution of the following task using your
experience in manual1 and MATLAB help.
Problem:
⎧α n , N1 ≤ n ≤ N2
x[n] = ⎨
⎩0 , else where
>> n1=-3:3;
>> x1=ones(1,length(n1));
>> n2=-2:2;
>> x2=(0.5).^n2;
>> y=conv(x1,x2);
>> n3=-5:5;
>> subplot(311)
>> stem(n1,x1)
>> subplot(312)
>> stem(n2,x2)
>> subplot(313)
>> stem(n3,y)
Note that the reverse operation is called deconvolution in which you provide the
result of the convolution and one of the convoluted signals and will return the other
convoluted signal.
Type:
MATLAB can be used for the calculation of the fourier series coefficient of
any discrete-time signal using a predefined function called fast fourier transform or
by developing a code based on the basic definition of the discrete-time Fourier series.
1
ak =
N
∑ x[n].e − jkωo n
n =< N >
2π
Where: N is the period of the signal x[n] and ωo = .
N
We will start by calculating the fourier series coefficient using a function that we will
develop based on the definition of the fourier series coefficient.
On MATLAB, type:
function [a]=FScoeff(x)
%This function calculates the fourier series coeff of any signal.
%User should define one period of the signal and call the
%function for it.
N=length(x); % the period of the signal x[n]
n=0:N-1;
for k=0:N-1
a(k+1)=(1/N)*sum(x.*exp(-j*k*2*pi*n/N));
end
For example, the signal x[n]=cos(pi*n/4) is periodic of period N=8, so we will define
the signal in one period and call the function for it as follows.
>> n=0:7;
>> x=cos(pi*n/4);
>> a=FScoeff(x)
Where: a-1=a1=0.5 but note that we calculate for positive k using the property of the
discrete-time signals: ak=ak+N then we will have a1=a-1+8=a7=0.5.
So the result will be all zeros except for the these two coefficients.
The other method for calculating the fourier series coefficient is as follows:
a=(1/N)*fft(x)
where: N is the period of the signal x[n] and x is one period of the signal. The
instruction is an abbreviation of fast fourier transform.
>> n=0:7;
>> x=cos(pi*n/4) ;
>> N=length(x);
>> a=(1/N)*fft(x)
7. Fourier Transform
You can find the frequency domain representation of any signal x(t) or x[n]
using the fourier transform but in symbolic form using the fourier and then you can
substitute a range of frequencies to plot the spectrum of the signal (its frequency
domain representation).
For example if you would like to get the fourier transform of the following signal
given below and plot its frequency spectrum:
x (t ) = te −2t u (t )
>> xs=laplace(x)
>> xz=ztrans(x)
8. Image processing
It will be just an introduction for the image processing using MATLAB and
you can depend on your self for further knowledge.
>> A=imread(‘C:\PICS\FLOWER.jpg’);
>> size(A) % A is a 3-D matrix
>> B=A(:,:,1); % B is a 2-D matrix; grey scale
>> imshow(B)
>> C=A(:,:,2);
>> imshow(C)
>> D=A(:,:,3);
>> imshow(D);
>> imwrite(B,‘C:\PICS\FLOWER1.jpg’);
>> imwrite(C,‘C:\PICS\FLOWER2.jpg’);
>> imwrite(D,‘C:\PICS\FLOWER3.jpg’);
>> imfinfo(‘C:\PICS\FLOWER.jpg’)
>> A(1:10,:,1)=0;
>> imshow(A)
>> A(1:100,1:100,1)=255;
>> imshow(A)
>> E=rand(600,600,3);
>> imshow(E)
Note that :
• X OR 1=1
• X OR 0=X
• X AND 1=X
• X AND 0=0
Hence you can define a matrix of zeros or ones using the ones and zeros instructions
and try anding and oring it with the grey scale image-matrix to follow how it will be
black and white according to the results shown above.
BEST WISHES
Signals & Systems Lab.-Manual(2) - 14 - MATLAB-2007