Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Tuesday, April 20, 2010

Plotting Discontinuous Graph

From one of the problem in here :









I've got some clue from here. So, here is my solution :
t = -5:0.01:5;     % total range of the graph

q1 = t<-4;            % range 1
q2 = t>=-4 & t<3;     % range 2 
q3 = t>=3;            % range 3

y = zeros(size(t));    % initialization

y(q1) = 0;             % curve 1
y(q2) = t(q2) + 2;     % curve 2
y(q3) = t(q3) - 2;     % curve 3

plot(t,y); axis([-5 5 -4 7])

And here is the graph :
















Okay. The x(t) above is quite simple. How if it is --in example-- an polynomial equation? The concept remains the same. But, don't forget to use the dot operator, because we will power each of array member.

Monday, April 19, 2010

Plotting Continuous Signal #1 - Unit Step Function

x(t) = 4u(t) + 2sin(3t)




Matlab's inline function is convenient for creating unit step function.

Code :
t = -pi:0.01:pi;
u = inline('t >= 0');  
y = 4*u(t) + 2*sin(3*t);

plot(t,y,'r');grid on;
axis([-4 4 -3 7]);
xlabel('t'); ylabel('x(t)');
title('x(t) = 4u(t) + 2sin(3t)')

(From exercise 1C, chapter 1, book Fundamentals of Signals and Systems, 2nd ed. (Edward W. Kanen, Bonnie S. Heck))

Sunday, April 18, 2010

Demonstrating Convolution in Matlab

This simulation demonstrates convolution between two signals.
Graph 1 -> signal A
Graph 2 -> signal B
Graph 3 -> convolution of A and B




The code :


clear all;

x = [0 0 -1 2 3 -2 0 1 0 0];
dtx = -2:7;     % non-zero range of x
y = [1 -1 2 4];
dty = 8:11;     % non-zero range of y
z = conv(x,y);
dtz = 6:18;     % non-zero range of z

% plotting
dt = -2:20;
subplot(311);stem(dt,[x zeros(size(8:20))]);
title('Signal A');

subplot(312);stem(dt,[zeros(size(-2:7)) ...
y zeros(size(12:20))]);
title('Signal B');

subplot(313);stem(dt,[zeros(size(-2:5)) ...
z zeros(size(19:20))]);
title('A * B');

I use the example from here, at page 3.