How to scale and shift discrete time functions simultaneously?
40 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Usman Siddique
le 5 Mar 2016
Modifié(e) : Usman Siddique
le 6 Mar 2016
I am unable to both amplitude and time scale these discrete time functions simultaneously in the form x3[n]=x1[2n]*x3[3n] AND x4[n]=2*x1[n/2] + 4*x3[n/3] Some help would be appreciated
if true
clear all
close all
n=-20:20;
x1= 5*cos((2*pi*n)/8); %function# 1 x1[n]
x2= -8*exp(-(n/6).^2); %function# 2 x2[n]
a=x1.*x2
x3[n]=x1[2n]*x3[3n] *% how to implement this ??*
x4[n]=2*x1[n/2] + 4*x3[n/3] *% how to implement this ??*
subplot 311
stem(n,x1,'filled','b');
xlabel('[n]');
title('x1[n] * x2[n]');
grid on;
subplot 312
grid on;
subplot 313
grid on;
% code
end
2 commentaires
Star Strider
le 5 Mar 2016
Please post the original problem statement. I cannot understand what you want to do from the code you posted.
Réponse acceptée
John BG
le 6 Mar 2016
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
% x3=x1(2n)*x2(3n)
% x4=2*x1(n/2) + 4*x2(n/3)
% x5=x1.*x2
or perhaps this
% x3=x1(2n)*x2(3n)
% x4=2*x1(n/2) + 4*x3(n/3)
% x5=x1.*x2
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); % x1
nx2=n;x2=-8*exp(-(n/6).^2); % x2

From:
[1] Digital Signal Processing using MATLAB, by V.Ingle J.Proakis http://www.amazon.co.uk/Digital-Signal-Processing-2011-01-01/dp/B017WQA13U/ref=sr_1_7?ie=UTF8&qid=1457224905&sr=8-7&keywords=digital+signal+processing+with+MATLAB+Ingle+Proakis
% 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]); % don't forget to decimate
nx1_dec2=nx1_dec2/2 % and rescale the reference vectors
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) % Q=1 means x1(n/2)
[x2_int2,nx2_int2]=insample(x2,nx2,2) % Q=2 means x2(n/3)
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 % i assume here you want 'a' plotted, that i renamed x5
subplot 312;plot(nx3,x3);grid on % i assume here you want x3
subplot 313,plot(nx4,x4);grid on % and here x4

or with stem, as you wish
subplot 311;stem(nx5,x5);grid on % i assume here you want 'a' plotted, that i renamed x5
subplot 312;stem(nx3,x3);grid on % i assume here you want x3
subplot 313,stem(nx4,x4);grid on % and here x4
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)
% implements y(n)=x(n-k)
% x and m sequences, m for instance [-3:3]
% k scalar defines delay the following way;
% k>0 means delay k value the numeral of vector x(n)
% k=0 od nothing
% when k<0 this function will fold and delay
% AKA advance the input sequence
n=m-k;
y=x;
or
% function sigshift2 y(n)=x(n-k)
% input sequence x(n)
% k: scalar, delay
% generating second numeral sequence length(n)+delay k inside the function
% k<0 function delays sequence, increasing length of resulting sequence to include
% all delayed samples. When function k>0 advances sequence, cancelling
% samples that fall n<1 to avoid having to answer questions like, why
% sequences do not start on n=0? MATLAB CONVENTION; these basic sequences are
% vectors and the convention so far is that n=1 is the first element of the
% array, but you can start numeral vector TO BE DEFINED IN ADVANCE to this
% function usage, as m=[0,1,2, .. N].
% The basic sigshift function defined in page 27 DOES NOT WORK!
% not even changing last line from th efutile y=x to y(m)=x(n) BECAUSE IT'S
% SUPPOSED TO WORK FOR ANY DELAY/ADVANCE.
%
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 sigshift4 y(n)=x(n-I*k)
% input sequence x(n)
% k: scalar, delay
% I: interpolation factor
% generating second numeral sequence length(n)+delay k inside the function
% k<0 function delays sequence, increasing length of resulting sequence to include
% all delayed samples. When function k>0 advances sequence, cancelling
% samples that fall n<1 to avoid having to answer questions like, why
% sequences do not start on n=0? MATLAB CONVENTION; these basic sequences are
% vectors and the convention so far is that n=1 is the first element of the
% array, but you can start numeral vector TO BE DEFINED IN ADVANCE to this
% function usage, as m=[0,1,2, .. N].
% The basic sigshift function defined in page 27 DOES NOT WORK!
% not even changing last line from th efutile y=x to y(m)=x(n) BECAUSE IT'S
% SUPPOSED TO WORK FOR ANY DELAY/ADVANCE.
%
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
% chapter 2, sigadd, 4 vectors have to be passed x1, n1, x2, n2
% y(n)=x1(n)+x2(n)
% function call format: [y,n]=sigadd(x1,n1,x2,n2)
function [y,n]=sigadd(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2)); % y(n) duration
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)=(-n)
%
function [y,n]=sigfold(x,n)
y=fliplr(x);
4.- Signals multiplication
% chapter 2; sequence multiplication,sample to sample
% {x1(n)}•{x2(n)}={x1(n)•x2(n)}
% 4 vectors; x1, n1, x2 and n2
%
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)
% interpolation repeating Q times
Lx=length(x)
ny=1:1:(Lx*(Q+1));Ly=length(ny)
y=upsample(x,(Q+1))
for j=1:(Q+1):(Ly-2)
%y(j) range 2:Q:2*(Q+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)
% decimation y(n)=x(n*M)
% length(y)=length(x)/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
r=2
y=x([1:1+r:end])
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.
1 commentaire
Plus de réponses (0)
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!