How can I shift and add a discrete-time signal simultaneously

37 次查看(过去 30 天)
I know how to shift and add a signal separately but I'm unsure on how to do it together. So when I have these following conditions
n = -8:8;
x1 = [0 0 0 0 0 0 0 1 2 3 2 1 0 0 0 0 0];
x2 = [0 0 0 0 0 3 -3 2 -2 1 -1 0.5 -0.5 0 0 0 0];
Then y[n] = x1[n-2]+x2[n]. What do I do?

回答(4 个)

Shagun Sharma
Shagun Sharma 2020-1-21
%As far as I understand, you want to shift and add a signal simulaneously, in your question which is
%y[n] = x1[n-2]+x2[n]
%where:
n = -8:8;
x1 = [0 0 0 0 0 0 0 1 2 3 2 1 0 0 0 0 0];
x2 = [0 0 0 0 0 3 -3 2 -2 1 -1 0.5 -0.5 0 0 0 0];
%Its good that you have both the signals of same length otherwise things would have been a bit different and we would have to %make signals of equal length first. But here i will try my best to cover both the aspects and please give my answer a vote if you %find it worthy.
%Let us first define new shifted range of x1.
n1=n-2;
%Now the approach I am going to explain can be used for both the cases i.e. where signals are of equal length or not.
%Define a signal 's1' of length same as the required output filled with zeros.
%Since the current signals are ranging from -8 to 8 (17 samples) but after shifting of one signal (n-2), its range will be from -10 %to 6,
%Thus our addition of signals will be from -10 to 6 for x1 after shifting 2 units and from -8 to 8 for x2. Therefore we must keep in %mind that our addition result y wil range from -10 to 8 overall.
%so we must define signals of this range.
n2= -10:8; %(simply speaking n1=min(min(n1),min(n)) : max(max(n1),max(n));)
s1=zeros(1,length(n2));
s2=zeros(1,length(n2));
%Now we shall be using find function to fill in the values at the indexes. For s1 the values will be of x1 with 2 units of left shift while %for s2 the values will be of x2 as it is since it is not being shifted.
s1(find(n2>=min(n1)&n2<=max(n1)==1))=x1;
%Here n2>=min(n1)&n2<=max(n1) is a logical condition which returns an array of 1 or 0 if the condition is true and the find %function finds those indices and simply assigns the value of x1 over there. This way we have successfully shifted the signal x1 %by 2 units leftwards using the logical condition of where to put values.
%Similarly
s2(find(n2>=min(n)&n2<=max(n)==1))=x2;
%Now since the dimensions of 2 signals are equal we can add our shifted and non shifted signal simultaneously.
y=zeros(1,length(n2));
y=s1+s2;
%To check the result:
figure;
subplot(4,1,1)
stem(n,x1)
title('x1[n]')
grid on
axis([-12 10 -5 5])
subplot(4,1,2)
stem(n,x2)
title('x2[n]')
grid on
axis([-12 10 -5 5])
subplot(4,1,3)
stem(n2,s1)
title('x1[n-2]')
grid on
axis([-12 10 -5 5])
subplot(4,1,4)
stem(n2,y)
title('y[n]')
grid on
axis([-12 10 -5 5])
% I hope I have answered your concern.

Default
Default 2020-6-23
编辑:Default 2020-6-23
This is easier than you might think. The two signals have the same length and are already zero-padded. Just use circshift to shift x1 to the right by 2 (the sign is negative). Then add the two signals.
n = -8:8;
x1 = [0 0 0 0 0 0 0 1 2 3 2 1 0 0 0 0 0];
x2 = [0 0 0 0 0 3 -3 2 -2 1 -1 0.5 -0.5 0 0 0 0];
yn = circshift(x1,2) + x2; % y[n] = x1[n-2]+x2[n]
stem(n,yn)
grid on

Vishwas
Vishwas 2017-9-14
In order to simultaneously shift and add two discrete time signals, we can only make use of indices of x1 and x2 which can only be positive integers. So, for y[n] = x1[n-2]+x2[n], n should always be greater than 3.
Based on your example, we can consider the scenarios with positive values of n
n = 3:17;
y(n) = x1(n-2) + 2*x2(n)
or a we can write a function and build a resultant signal values
function result = cal(n)
if n<3 || n>17
result = 0;
else
result = x1(n-2)+2*x2(n);
end
end
for i=1:max(size(x1))
y(i) = cal(i);
end

Sk Group
Sk Group 2021-2-8
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;

类别

Help CenterFile Exchange 中查找有关 Signal Generation and Preprocessing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by