You are on page 1of 20

Bio-signal Processing Lab.

Experiment 2
Signal Processing in MATLAB
Eden T. and Elbetel T.
Exercise
% Plotting in 3-D space
x=0:.1:100;
y=cos(x);
z=sin(x);
plot3(x,y,z)
xlabel('x')
ylabel('y=cos(x)')
zlabel('z = sin(x)')
Discrete signals
% Generation of Impulse sequence
x=-2:1:2;
y=[0 0 1 0 0];
stem(x,y);
xlabel('Time');
ylabel('Amplitude');
title('Impulse sequence');
Signal processing

• We perform this processing to determine


• “How much” signal is present
• How long a signal lasts,
• To perform signal detection(by determining
when a signal contains useful information
rather than just background noise).
Signal processing

• Shifting : advancing and delaying.

• Folding : finding the mirror signal.

• Scaling: scale up/down signal amplitude.

• Convolution: to relate input with output signal


Shifting Discrete-time Signal
%Shifting a non-function Discrete-time signal
n = 0:8;
x = [0 1 5 2 1 3 6 4 5];
subplot(2,1,1);stem(n,x);
title('x(n) signal');
xlabel('n'); ylabel('x(n)');
m=n+2; y=x;% delayed signal
subplot(2,1,2);
stem(m,y); title('y(n)=x(n+2) signal');
xlabel('n'); ylabel('y(n)');
Folding A Discrete-time Signal
% Folding discret- time signal
n=0:8,
x=[0 0 1 2 3 4 5 4 3];
subplot(2,1,1);
stem(n,x);
title('x(n) signal');
m=-fliplr(n);
y=fliplr(x);
subplot(2,1,2);
stem(m,y);
title('y(n)=x(-n) signal')
Convolution
Discrete time systems: convolution
• MATLAB does provide a built-in function called
conv that computes the convolution between
two finite-duration sequences.
• The conv function assumes that the two
sequences begin at n=0 and is invoked by
y = conv (x, h);
Convolution
• While convolving two elements, there are different
key words in Mat-lab which are used to determine
the timing information of the final result y(n). Thus
words are:
– y(n)=conv(x , h, ‘same’) % to get the same timing with the
input x.

– y(n)=conv(x , h, ‘full’) % to get the total timing


Example
clc;
clear;
close;
x = [3, 11, 7, 0, -1, 4, 2];
h = [2, 3, 0, -5, 2, 1];
y = conv(x,h,'full')
y = conv(x,h,'same')
Exercise
t=0:.01:5;
h=ones(size(t));
x=2*exp(-2*t);
y=conv(x,h,'same');
plot(x,y);
title('x(n)*h(t)')
Example
%generate the linear convolution
x=input('enter the input sequence:');
n1=input('enter the input sequence interval:');
h=input('enter the impulse response:');
n2=input('enter the impulse response interval:');
subplot(2,2,1)
stem(n1,x);
xlabel('time');
ylabel('amplitude');
title('input sequence');
subplot(2,2,2)
stem(n2,h);
Example
xlabel('time');
ylabel('amplitude');
title('impulse response');
subplot(2,1,2)
n=min(n1)+min(n2):1:max(n1)+max(n2);
y=conv(x,h);
stem(n,y);
xlabel('time');
ylabel('amplitude');
title('linear convolution');
disp(x);
disp(h);
disp(y);
Linear Convolution
COMMAND WINDOW
Enter the input sequence:[1 2 3 2]
Enter the input sequence interval:0:1:3
Enter the impulse response:[2 3 1 2]
Enter the impulse response interval:-1:1:2
Exercise
Given the following two sequences
• Determine the convolution y(n)=x(n)∗h(n) and
plot x(n), h(n) and y(n) in one figure.
•NB: the range of y(n) will be [xi+hi : xf+hf ]
Exercise
n1=-3:1:3;
n2=-1:1:4;
n3=-4:1:7;
x = [3, 11, 7, 0, -1, 4, 2];
h = [2, 3, 0, -5, 2, 1];
subplot (2,3,1);
stem(n1,x);
title('input sequence');
subplot (2,3,2);
stem(n2,h);
title('impulse sequence');
subplot (2,3,3);
y = conv(x, h);
stem(n3,y);
title(‘convolution of input and impulse');
Writing Lab report
• Aim of the experiment
• What kind of software is used
• Code and interpretation
• Name, ID, school, course title, name of
experiment
• Publish the code and convert PDF
Home work
• Try the following MATLAB program and write
the interpretation of each.
It should include :
• Name, ID, Group, subgroup, school, course
title.
• Interpretation on comment section.
• Note: this homework is individual assignment
and holds 10mark.
Home Work
1.Plot the following Complex exponential signal
(label your graph).
n=-10:10;
w=0.8;
x=exp(j*w*n);
plot(n,real(x));
plot(n,imag(x));
Home Work
2. Compute the following addition operation in mat lab
n=[1:10];
x1= sin(3*n);
plot(n,x1,'r')
hold on
title('sin wave');
x2= sin(2*n);
plot(n,x2,'g')
x=x1+ x2;
plot(n,x,'b')
hold off

You might also like