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

No comments:

Post a Comment