MATLAB Programming by ewghwehws

VIEWS: 4 PAGES: 34

									MATLAB Programming
     Why MATLAB is used ?
• It can use a simple instruction to compute
  a very complex mathematical function.

• It provides high resolution two
  dimensional and three dimensional
  graphic plot functions for signal analysis.
     Why MATLAB is used ?
• It can play the sound and display an
  image in a personal computer or a
  workstation locally or from the computer
  network with the X window interface.

• MATLAB programs are portable in many
  computer systems.
     Why MATLAB is used ?
• It provides interface with the programming
  language C to enhance its programming
  ability and execution speed.

• We can write a simple program easily to
  simulate an application which might takes
  many hours or many days in C or Fortran.
1.MATLAB is very suitable for signal
  processing. MATLAB programs is portable to
  any computer if it is installed with MATLAB
  and its tool-boxes. Once you initiate
  MATLAB software you see a prompt >> to
  indicate that MATLAB is waiting for your
  command. Each command can be interpreted
  as soon as you type in. To understand a
  command you can type help name, where
  name is the name of a command. For
  example,
>> help sinc
• MATLAB will output the information about
  the sinc function.
• A variable in MATLAB
  can be a scalar, a vector or a matrix; For
  example,

 >> x = 3
 x=
    3
 >> y = 1:5
 y=
    1 2 3         4   5
>> z=[1:3;4:6;7:9]
 z=
    1 2 3
    4 5 6
    7 8 9
2.To understand the variables in the MATLAB
  environment you can type whos to access
  their information. For example,
>> whos
Name Size Elements Bytes Density Complex

  x 1 by 1        1     8     Full    No
  y 1 by 5        5    40     Full    No
  z 1 by 9        9    72     Full    No

Grand total is 15 elements using 120 bytes
3.MATLAB provides the following arithmetic
  operators to compute the arithmetic operations
  between a and b. There are several cases that
the following operations are valid:
(a) both a and b are scalar.
(b) a is a vector or a matrix and b is scalar.
(c) both a and b are vectors or matrixes with the
    same dimension.
     Notation         Function
     a+b              plus
     a-b              minus
     a*b              multiplication
     a/b              division
     a=b              assignment
For example,

>> a = 5;
>> b = 2;
>> a+b
ans = 7

>> z
z= 1    2   3
    4   5   6
    7   8   9
>> a+z
ans =
  6 7 8
  9 10 11
 12 13 14

>> a*z
ans =
 5 10 15
 20 25 30
 35 40 45
>> z*z
ans =
   30 36 42
   66 81 96
  102 126 150

>> z^2
ans =
   30 36 42
   66 81 96
  102 126 150
4.MATLAB provides the following logic
  operators to compute the logic operations
  between a and b, where a and b must have the
  same dimension.
       a==b             equal
       a< >b            not equal
       a&b              logic AND
       a|b              logic OR
       a                logic NOT
  >> a = [0 1]
   a=
       0     1
>> b = ~a
b=
    1    0
>> a & b
ans =
    0    0
>> a | b
ans =
    1    1
>> a == b
ans =
    0    0
5.MATLAB provides the following array
  operators to compute the array operations
  between a and b, where a and b must have the
  same dimension.
       a.*b        array multiplication
       a./b        array division
  a=
      1        2
  >> b = [2,3]
  b=
      2        3
>> a .^ b
ans =
       1   8
>> a .* b
ans =
       2   6
>> a ./b
ans =
    0.5000 0.6667
The following program plots 1 Hz cosine signal
cos(2t). Actually, it’s digital signal. However,
we can look it as a real-value analog signal.

 t = -1:0.01:1;
 y = cos(2 * pi * t);
 fig;
 plot(t,y);
 xlabel(‘t’);
 ylabel(‘f(t)’);
 set(gca, ‘Ylim’,[-2,2]);
 print -deps cos1
Suppose the filename of the program is cos1.m
you type cos1 to execute the program. For
example,
>> cos1
6.MATLAB provides the following
  instructions for triangular, hyperbolic,
  exponential and logarithmic functions,

f(t)        Function of the signal
cos(t)      a cosine signal
sin(t)      a sine signal
tan(t)      a tangent signal
acos(t)     an inverse cosine signal
asin(t)     an inverse sine signal
atan(t)     an inverse tangent signal
atan2(t)    a four quadrant inverse tangent signal
exp(t)     an exponential signal
log(t)     a natural logarithm signal
log10(t)   a common logarm signal
sqrt(t)    a square signal
cosh(t)    a hyperbolic cosine signal
sinh(t)    a hyperbolic sine signal
tanh(t)    a hyperbolic tangent signal
acosh(t)   an inverse hyperbolic cosine signal
asinh(t)   an inverse hyperbolic sine signal
atanh(t)   an inverse hyperbolic tangent signal
sign(t)    a signum signal
Note that the function atan2(b,a) requires two
variables and returns a value in [-, ] and
atan(t) just requires one variable returns a value
in [-/2, /2].

atan(-0.5)
ans = -0.4636

atan2(-1,2)
ans = -0.4636
atan2(1,-2)
ans = 2.6779

atan(0.5)
ans = 0.4636

atan2(1,2)
ans = 0.4636

atan2(-1, -2)
ans = -2.6779
7.MATLAB provides the following special
  operations to for a complex valued signal.

    Notation   Function
    abs(x)     the absolute value of x
    real(x)    the real value of x
    imag(x)    the imaginary part of x
    conj(x)    the conjugate value of x
    angle(x)   the phase angle of x
8.MATLAB provides the following special
  operations to round a real number.
  ceil(x)    round x toward plud infinity
  fix(x)     round x toward toeard 0
  floor(x)   round x toward minus infinity
  round(x)   round x toward nearest integer
              a
9.If a complex e can be computed in MATLAB.
  For example,

 a = sqrt(-1)
 a=
     0 + 1.0000i
 exp(a)
 ans = 0.5403 + 0.8415i
10.The instruction save test store all variables in
   the file called test.mat. The instruction load
   test retrieves all variable from test.mat
 x=1
 y=2
 z=3
 % Save all variable in test.mat
 save test
 % Clear the workspace
 clear
% Check all variables are retrieved or not ?
 load test
 x
 y
 z
% Save x and y in test.mat
 save test x y
 % Clear the workspace
 clear
 % Check all variables are retrieved or not ?
 load test
 x
 y
11.A sound file can be stored in the mat-file.
   Usually, it has two variables Fs and y, where Fs
   is the sampling rate and the variable y contains
   the sound signal. To load the sound file
   sound.mat and plot and play the sound in the
   following program can be used:

   load sound
   % The duration of the sound in second
   t = (1:length(y))/Fs;
   % Plot the sound
   Plot(t,y)
% Play the sound
sound(y,Fs)

In the Unix system there are several mat-files
in the directory called /usr/local/matlab4.2c/to
These sound files can be retrieved and played.
12.The contents of the current figure can be saved
   as a file with the following instruction:
   (a) print -dps filename
   (b) print -dpsc filename
   (c) print -deps filename
   (d) print -depsc filename
   to save a black and white postscript file, a
   color postscript file, a block and white
   encapsulated postscript file and a color
   encapsulated postscript file.
13.The instruction ezplot(f) plots the function f,
   where f is a symbolic expression with the
   variable t and t  [-2, 2]. The instruction
   ezplot(f,[t1,t2]) plots the function f(t), where t
    [t1,t2]. The instruction ezplot(f, [t1,t2],fig) plots
   the function f(t), where t  [t1,t2], with the
   figure number fig.
   f = ‘x^3’
   ezplot(f);
   ezplot(f,[0,1000])
14.The absolute value of x can be computed as

  y = ‘(x^2)^(1/2)’
  ezplot(y)
15. The function tri(t), rec(t), u(t), f(t)= |t| can not
    be defined symbolically. Therefore, it is
    difficult to use the instruction ezplot to plot
    these function. In the following program listing
    there are three main programs called unit.m,
    rec1.m and tri1.m and there are two functions
    called rec.m and tri,m.Figure 1.7, 1.8 and 1.9
    are plotted by the commands unit, rec1 and
    tri1, respectively.

								
To top