Hi Usman
If you find these answer lines of any help solving your question, please click on the thumbs-up vote link:
You say you want to implement the following operation
x3[n]=x1[2n]*x3[3n]
x4[n]=2*x1[n/2] + 4*x3[n/3]
as compact as possible ..
let's get started
1.- MATLAB does not use brackets [] to index signals to a reference vector like n.
To index vector components, use x1(3) or x1([2 3 4]) same as x([2:4]) or x3(end)
now that the basics basics are clear, more basics
2.- have you noticed that you don't use x2?
you write 'a=x1.*x2' but then 'a' doesn't show up anywhere else.
You probably mean the following
or perhaps this
I am showing how to solve the second group. If it's the case you want the second pair or another combination, modify accordingly.
3.- The start signals are
format long
nx1=n;x1=5*cos((2*pi*n)/8);
nx2=n;x2=-8*exp(-(n/6).^2);
From:
% ANSWER:
[x5,nx5]=sigmult(x1,nx1,x2,nx2)
figure(3);plot(nx5,x5)
x1_dec2=x1([1:2:end]);
nx1_dec2=nx1([1:2:end]);
nx1_dec2=nx1_dec2/2
x2_dec3=x1([1:3:end])
nx2_dec3=nx2([1:3:end])
x2_dec3 =
-5.00 3.54 -0.00 -3.54 5.00
-3.54 0.00 3.54 -5.00 3.54
0.00 -3.54 5.00 -3.54
nx2_dec3 =
-20.00 -17.00 -14.00 -11.00 -8.00
-5.00 -2.00 1.00 4.00 7.00
10.00 13.00 16.00 19.00
the potential problem with the last 2 commands is that because you have so few samples per cycle of x1, and both base signals are symmetric around n=0 you probably want such symmetry preserved in x3, so instead of
[x3,nx3]=dnsample(x2,nx2,3)
x3 =
-0.00 -0.02 -0.15 -0.84 -2.94
-6.23 -8.00 -6.23 -2.94 -0.84
-0.15 -0.02 -0.00
nx3 =
1.00 2.00 3.00 4.00 5.00
6.00 7.00 8.00 9.00 10.00
11.00 12.00 13.00
if you want result symmetric, here you can use x3 but not this nx3.
The quick fix is to shift the reference vector 7 samples left
nx3=nx3-7
nx3 =
-6.00 -5.00 -4.00 -3.00 -2.00
-1.00 0 1.00 2.00 3.00
4.00 5.00 6.00
If you try to decimate the reference vector first, for instance
nx2_dec3=[nx2(1:3:20) nx2(21) nx2(21:3:end)]
nx2_dec3 =
-20.00 -17.00 -14.00 -11.00 -8.00
-5.00 -2.00 0 0 3.00
6.00 9.00 12.00 15.00 18.00
do not use it because this doubles nx2(21)=0
or
nx2_dec3=[nx2(1:3:20) nx2(21) nx2(23:3:end)]
nx2_dec3=
-20.00 -17.00 -14.00 -11.00 -8.00
-5.00 -2.00 0 2.00 5.00
8.00 11.00 14.00 17.00 20.00
the reference vector would have more samples than the decimated signal x3
% ANSWER:
[x3,nx3]=dnsample(x2,nx2,3);nx3=nx3-7
figure(4);plot(nx3,x3)
And now let's go for x4=2*x1(n/2) + 4*x2(n/3)
MATLAB interpolation functions , both linear interpolation
yi = interp1q(x,y,xi)
vq = interp1(x,v,xq)
require you to supply in advance the answer reference vectors, the xi or xq that would be called nyi or nvq, following V.Ingle J.Proakis [1] good practice.
Instead let's use [y,ny]=insample(x,nx,Q) where interpolate Q=1 means y=x(n/2)
[x1_int1,nx1_int1]=insample(x1,nx1,1)
[x2_int2,nx2_int2]=insample(x2,nx2,2)
nx1_int1=nx1_int1-floor(length(nx1_int1)/2)
nx2_int2=nx2_int2-floor(length(nx2_int2)/2)
x1_int1=2*x1_int1
x2_int2=4*x2_int2
ANSWER:
[x4,nx4]=sigadd(x1_int1,nx1_int1,x2_int2,nx2_int2)
nx4=nx4-floor(length(nx4)/2)
figure(5);plot(nx4,x4);grid on
not centering the components of x4, nx1 and nx2, but centering nx4:
centering nx1 nx2 and nx4:
a closing note regarding the way you tried to use subplot: without the plot after subplot, it does not work.
figure(6);
subplot 311;plot(nx5,x5);grid on
subplot 312;plot(nx3,x3);grid on
subplot 313,plot(nx4,x4);grid on
or with stem, as you wish
subplot 311;stem(nx5,x5);grid on
subplot 312;stem(nx3,x3);grid on
subplot 313,stem(nx4,x4);grid on
let me know if you need further detail, and if you found this answer in any way useful, please click on the thumbs-up vote link above,
thanks in advance
John
Following support functions, some of them used above, some others you and other readers may find useful.
1.- Signals time shifting
function [y,n]=sigshift(x,m,k)
n=m-k;
y=x;
or
function [y,m]=sigshift2(x,k)
m=[1:1:length(x)+k];
y=zeros(1,m)
if (k>0),
for q=1:1:length(x)-k
y(q)=x(q+k);
end; return;
end;
if (k<0),
for q=1:1:length(x)
y(q+abs(k))=x(q);
end; return;
end;
y=x;
or
function y=sigshift4(x,k,I)
m=[1:1:length(x)+k];
y=zeros(1,m)
if (k>0),
for q=1:I:length(x)-k
y(q)=x(q+k);
end; return;
end;
if (k<0),
for q=1:I:length(x)
y(q+abs(k))=x(q);
end; return;
end;
y=x;
2.- Signal addition
function [y,n]=sigadd(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;
3.- Signals folding
function [y,n]=sigfold(x,n)
y=fliplr(x);
4.- Signals multiplication
function [z,pointer]=sigmult(x_1,n_1,x_2,n_2)
pointer=min(min(n_1),min(n_2)):max(max(n_1),max(n_2));
y_1=zeros(1,length(pointer));y_2=y_1;
y_1(find((pointer>=min(n_1))&(pointer<=max(n_1))==1))=x_1;
y_2(find((pointer>=min(n_2))&(pointer<=max(n_2))==1))=x_2;
z=y_1.*y_2;
5.- Signals interpolation
MATLAB has the following functions:
interp interp1 interp2 interp3
the numerals refer to the interpolation order. Straight lines between adjacent points is interp1. However none of these functions supplies the reference vector of the interpolated signal along with the interpolated signal
function [y,ny] = insample(x,nx,Q)
Lx=length(x)
ny=1:1:(Lx*(Q+1));Ly=length(ny)
y=upsample(x,(Q+1))
for j=1:(Q+1):(Ly-2)
for i=2:1:(Q+1)
v=floor((i-2)/Q)
y(i+(j-1))=y(v+j)
end
end
6.- Signals decimation
function [y,m] = dnsample(x,nx,M)
m=1:1:floor(length(x)/M);
y=zeros(1,length(m));
y(1)=x(1);
Lx=length(m);
for q=1:1:Lx,
y(q)=x(q*M);
end
command decimate(x,r) means remove r samples out of every r+1 samples, MATLAB decimate start example is:
t = 0:.00025:1;
x = sin(2*pi*30*t) + sin(2*pi*60*t);
y=decimate(x,2)
subplot 211
stem(0:120,x(1:121),'filled','markersize',3)
grid on
xlabel 'Sample number',ylabel 'Original'
subplot 212
stem(0:120,y(1:121),'filled','markersize',3)
grid on
xlabel 'Sample number',ylabel 'Decimated'
the same way that in the interpolation, you may not want the higher order decimation that the command decimate performs, because for instance, in the MATLAB decimate start example shown just above this line, with r=2, the original signal shows x(0)=0
but the decimated signal xd(0)>0 because of the filter applied by default, read more about the command decimate to modify the filter used by MATLAB. With dmsample x(0)=xd(0)=0
So as summary from the easiest way to decimate
to any function you may write, it's important to keep the reference vector with each respective signal, and it's worth making sure that properties like symmetry remain.