How to ADD 3 discrete signals ? Using one function only ?
Show older comments
I have to add 3 discrete signal using function .
x1=[3,6,4];
n1=[-2,-1,4];
x2=[5,2,5];
n2=[-3,1,3];
x3=[-2,3,2];
n3=[-1.5,3,2];
i have tried but ..... can anyone tell me where the problem is ?
FUNCTION ---------------------------
function [y,n] = threesigadd_arif(x1,n1,x2,n2,x3,n3)
% implements y(n) = x1(n)+x2(n)+x3(n)
b1=min(min(n1),min(n2)):max(max(n1),max(n2));
n=min(min(b1),min(n3)):max(max(b1),max(n3));
y1 = zeros(1,length(n)); y2=y1; % initialization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y3(find((n>=min(n3))&(n<=max(n3))==1))=x3;
y = y1+y2+y3; % sequence addition
2 Comments
dpb
on 5 Jun 2016
I'm not sure what you want the result to be, but the error is that
>> n
n =
-3 -2 -1 0 1 2 3 4
>> (((n>=min(n3))&(n<=max(n3))==1))
ans =
0 0 1 1 1 1 1 0
>>
so find() returns a vector of length five and then you subsequently try to assign the three elements of x3 to those five elements in y3--that's a size mismatch.
You need to either have three locations to assign to or expand the RHS to a length that matches that of the LHS, one.
Arif Rubayet
on 5 Jun 2016
Answers (4)
Walter Roberson
on 5 Jun 2016
0 votes
In MATLAB, it is not possible to add 3 things using only 1 function. For more information on why not, please see http://www.mathworks.com/matlabcentral/answers/38787-what-can-be-programmed-without-any-built-in-functions
Najmus Sakib
on 5 Oct 2017
%%sum of three signals given:
n = -7 : 7 ;
u1=[3,6,4]; n1=[-2,-1,4];
u2=[5,2,5]; n2=[-3,1,3];
u3=[-2,3,2]; n3=[-1,3,2];
n1_ = n1(1) : n1(end) ; n2_ = n2(1) : n2(end) ; n3_ = n3(1) : n3(end) ;
t1 = zeros( 1 , length(n1_) ) ; t2 = zeros( 1 , length( n2_) ) ; t3 = zeros( 1 , length(n3_) ) ;
for i = 1 : length( n1) t1( find ( n1(i) == n1_ ) ) = u1(i) ; end
for i = 1 : length( n2) t2( find ( n2(i) == n2_ ) ) = u2(i) ; end
for i = 1 : length( n3) t3( find ( n3(i) == n3_ ) ) = u3(i) ; end
zz = [ n1(1) n2(1) n3(1) ] ; yy = [ n1( end ) n2( end ) n3( end ) ] ;
min_val = min(zz) ; max_val = max( yy ) ;
sum_interval = min_val : max_val ;
temp1 = zeros( 1 , length( sum_interval) ) ;
temp2 = temp1 ; temp3 = temp2 ;
temp1( find( sum_interval>= n1_(1) & sum_interval<= n1_(end) ) ) = t1 ; temp2( find( sum_interval>= n2_(1) & sum_interval<= n2_(end) ) ) = t2 ; temp3( find( sum_interval>= n3_(1) & sum_interval<= n3_(end) ) ) = t3 ;
sum_us = temp1 + temp2 + temp3 ;
figure( 07) stem ( sum_interval , sum_us ) , grid on ;
MATLAB CODE:
function [y n] = sigadd(x1,n1,x2,n2)
if n1(1)< n2(1)
a = n1(1)
x1 = [zeros(1,abs(n1(1)-n2(1))) x1]
else
a = n2(1)
x1 = [zeros(1,abs(n1(1)-n2(1))) x1]
end
if n1(end)>n2(end)
b = n1(end)
x2 = [x2 zeros(1,abs(n1(end)-n2(end)))]
else
b = n2(end)
x2 = [x2 zeros(1,abs(n1(end)-n2(end)))]
end
n = a:b;
y = x1+x2;
MATLAB CODE:
function [y n] = sigadd_another_method(x1,n1,x2,n2)
n = min(min(n1),min(n2)):max(max(n1),max(n2));
y1 = zeros(1,length(n));
y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y = y1+y2;
ZUHRA BANO
on 5 Feb 2022
0 votes
function [y n] = sigadd_another_method(x1,n1,x2,n2)
n = min(min(n1),min(n2)):max(max(n1),max(n2));
y1 = zeros(1,length(n));
y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y = y1+y2;
Categories
Find more on Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!