Saturday, May 2, 2009

Furthur signal operations in Octave

Let us add two signals
The octave function is like this
function [y,n] = sigadd (x1, n1, x2, n2)
% x1 , n1 signal 1 x2 n2 signal 2

n= min ( min(n1),min(n2)): max(max(n1),max(n2)); % find the total length of the sequence
y1= zeros(1,length(n));
y2=y1 ;
y1 (find ( ( n>= min ( n1)) & (n <=max (n1))==1)) =x1; % padd x1 to y1 y2 (find ( ( n>= min ( n2)) & (n <=max (n2))==1)) =x2; % padd x2 to y2
y= y1+y2;

See the results

octave:1> x1= [ 1 1 2 1];
octave:2> n1=[ -1 0 1 2];
octave:3> x2=[1 2 3 4];
octave:4> n2=[-2 -1 0 1];
octave:5> [y n]= sigadd(x1,n1,x2,n2)
y =

1 3 4 6 1

n =

-2 -1 0 1 2



Similarly the you can multiply two signals with the following function.


function [y,n] = sigmult (x1, n1, x2, n2)
% x1 , n1 signal 1 x2 n2 signal 2

n= min ( min(n1),min(n2)): max(max(n1),max(n2)); % find the total length of the sequence
y1= zeros(1,length(n));
y2=y1 ;
y1 (find ( ( n>= min ( n1)) & (n <=max (n1))==1)) =x1; % padd x1 to y1 y2 (find ( ( n>= min ( n2)) & (n <=max (n2))==1)) =x2; % padd x2 to y2
y= y1.*y2;



Results for the above vectors will be


octave:6> [y n]= sigmult(x1,n1,x2,n2)
y =

0 2 3 8 0

n =

-2 -1 0 1 2

Friday, May 1, 2009

Representing sequences in Octave

  A sequence has a reference point which is represented on paper by an up arrow. In octave we represent a se quence as 2 vectors. 
  eg 
  x= [ 1,2,2,3,3]
  n = [-1,0,1,2,3]
 The vector n represent time markings. the 0th postion of x[n] is marked by 0 in the vector n.

Delta function


Let us implement a delta function in Gnu Octave. Since we cannot implement infinite length sequences in Octave we will limit the length of the sequence to some finite value.

 Write the following code and save it as impseq.m

  function [x,n] =impseq (n0,n1,n2)
  n=[n1:n2]; x=[ (n-n0) == 0];

  The function takes 3 arguements. n1 and n2 are starting and ending points in time. n0 is the point at which the impulse occurs.
See the output below.

octave:47> [x,n]= impseq ( 4 ,0,10)
x =

  0  0  0  0  1  0  0  0   0  0  0

n =

  0  1  2  3   4  5  6  7  8  9  10

Unit Step 

Now let us build a step sequence u(n)

 Write an m file as below.

function [x,n] = stepseq(n0,n1,n2)
  n=[n1:n2];x= [ (n-n0)>=0 ];

 Here again n1 and n2 are time markings and n0 is the point at which step starts.

octave:48> [x,n]= stepseq ( 4 ,0,10)
x =

  0 0 0 0 1 1 1 1 1 1 1

n =

  0 1 2 3 4 5 6 7 8 9 10


Real exponential sequence

 > n= [0:10]; 
 > x=(0.9).^n
x =

 Columns 1 through 8:

  1.00000 0.90000 0.81000 0.72900 0.65610 0.59049 0.53144 0.47830

 Columns 9 through 11:

  0.43047 0.38742 0.34868

Complex exponetial sequence
 > x= exp( ( 2 + 3j )*n)
x =

 Columns 1 through 3:

  1.0000e+00 + 0.0000e+00i -7.3151e+00 + 1.0427e+00i 5.2424e+01 - 1.5256e+01i

 Columns 4 through 6:

  -3.6758e+02 + 1.6626e+02i 2.5155e+03 - 1.5995e+03i -1.6733e+04 + 1.4324e+04i

 Columns 7 through 9:

  1.0747e+05 - 1.2223e+05i -6.5870e+05 + 1.0062e+06i 3.7693e+06 - 8.0471e+06i

 Columns 10 and 11:

  -1.9182e+07 + 6.2796e+07i 7.4837e+07 - 4.7936e+08i
Sinusoidal sequence
  
  > x= 3* cos( 0.1*pi*n +pi*3) + 2 *sin(0.5* pi *n)
x =

 Columns 1 through 8:

  -3.00000 -0.85317 -2.42705 -3.76336 -0.92705 2.00000 0.92705 -0.23664

 Columns 9 through 11:

  2.42705 4.85317 3.00000