time shifting

조회 수: 33 (최근 30일)
ap singh
ap singh 2011년 2월 20일
답변: Saurabh 2024년 9월 15일
i want the program to advance or delay the signal... by taking input from user whether to advance or delay & by what number?.... please use switch statement?.... any signal like-ramp,step,exponential in it i am using subplot to plot the original signal & shifted version of the signal..
i have a program something like this--
*clear all
close all
clc
disp('given function is')
x=(-10:10);
for i=1:1:21
if(x(i)>0)
y(i)=exp(x(i));
else
y(i)=0;
end
end
subplot(1,2,1)
stem(x,y)
disp('enter whether to advance or delay')
s=input('1>advance\n 2>Delay')
n=input('enter the no of step by which signal is to be shifted')
switch(s)
case 1
[
for i=1+n:1:21+n
if(x(i)>0)
y(i)=1;
else
y(i)=0;
end
]
case 2
[
for i=1-n:1:21-n
if(x(i)>0)
y(i)=1;
else
y(i)=0;
end
]
subplot(1,2,2)
stem(x,y)
but when i run the program it gives error in the 'for' command in case 1.
  댓글 수: 3
Matt Tearle
Matt Tearle 2011년 2월 20일
Can you please explain in more detail what you're trying to do? Do you have a time base vector t and a signal y(t) and you want the user to input a dt... and then do... what with it exactly?
ap singh
ap singh 2011년 2월 21일
i am begneer to matlab...
so Paulo Silva & Matt if you can help me in some more programs i will be very thankfull to u...
i need code to this programm
tell me the problem in the above program
thxs

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2011년 2월 21일
Omit the square brackets around the case blocks and close the SWITCH and IF blocks with an END.
switch(s)
case 1
for i=1+n:1:21+n
if(x(i)>0)
y(i)=1;
else
y(i)=0;
end
end
case 2
for i=1-n:1:21-n
if(x(i)>0)
y(i)=1;
else
y(i)=0;
end
end
end
  댓글 수: 1
ap singh
ap singh 2011년 2월 21일
will this program work??
is this right??...

댓글을 달려면 로그인하십시오.

추가 답변 (6개)

Sk Group
Sk Group 2021년 2월 8일
편집: Sk Group 2021년 10월 27일

Walter Roberson
Walter Roberson 2011년 2월 21일
You cannot have an 'if' statement inside of [] . But you don't need to mark the beginning and end of the code for an individual case: all code is executed until the next 'case' or unmatched 'end'.
  댓글 수: 1
ap singh
ap singh 2011년 2월 21일
ok but but my college sir told me that square [] are to be put so i put them.... now you hav told me to do it without brackets i will try it.. thnks

댓글을 달려면 로그인하십시오.


Matt Tearle
Matt Tearle 2011년 2월 21일
On top of the syntax issues Walter and Jan point out...
1. Why use switch? You could easily use a simple if/|else|. But why not just flip the sign of the offset. Something like n = sign(1.5-s)*n
2. You can make the unshifted signal in a couple of lines:
y = exp(x);
y(x<=0) = 0;
3. What you have in the switches will cause an error because it will try to reference x(22) (or higher), but x only has 21 elements. What are you trying to do here? I would have thought that what you want mathematically is y(x+dx). In which case you could repeat the above lines with a shifted x:
x2 = x + dx;
y = exp(x2);
y(x2<=0) = 0;
Also, what you have there sets y to just 0 or 1, rather than 0 or exp(x). Was that what you intended?
  댓글 수: 1
ap singh
ap singh 2011년 2월 21일
yes sure i can..
but i am learning the use of the SWITCh statement...

댓글을 달려면 로그인하십시오.


ap singh
ap singh 2011년 2월 21일
thanks to
Walter Roberson,Matt,Jan Simon,Paulo Silva for commenting & answering to my question!!!.....
meet u 2mmrw

ap singh
ap singh 2011년 2월 22일
as Jan Simon remove square brackets
i have written it like
clear all
close all
clc
disp('given function is')
x=(-10:10);
for i=1:1:21
if(x(i)>0)
y(i)=exp(x(i));
else
y(i)=0;
end
end
subplot(1,2,1)
stem(x,y)
disp('Enter whether to advance or delay')
s=input('1>advance\n 2>Delay=\n')
n=input('enter the no of step by which signal is to be shifted=')
switch(s)
case 1
for i=1+n:1:21+n
if(x(i)>0)
y(i)=exp(x(i));
else
y(i+n)=0;
end
end
case 2
for i=1-n:1:21-n
if(x(i)>0)
y(i)=exp(x(i));
else
y(i-n)=0;
end
end
end
subplot(1,2,2)
stem(x,y)
but still gives error as
" given function is Enter whether to advance or delay 1>advance 2>Delay= 1
s =
1
enter the no of step by which signal is to be shifted=1
n =
1
??? Attempted to access x(22); index out of bounds because numel(x)=21.
Error in ==> Untitled2 at 21 if(x(i)>0)"
please help...
  댓글 수: 1
Jan
Jan 2011년 2월 22일
Do not access elements out of range:
for i=1+n:1:21+n
==>for i=1+n:min(21+n, length(x))
(a:1:b is the same as a:b)
and
for i=1-n:1:21-n
==>for i=1:21-n
You cannot access x(22) or x(0).

댓글을 달려면 로그인하십시오.


Saurabh
Saurabh 2024년 9월 15일
clear all
close all
clc
disp('given function is')
x=(-10:10);
for i=1:1:21
if(x(i)>0)
y(i)=exp(x(i));
else
y(i)=0;
end
end
subplot(1,2,1)
stem(x,y)
disp('Enter whether to advance or delay')
s=input('1>advance\n 2>Delay=\n')
n=input('enter the no of step by which signal is to be shifted=')
switch(s)
case 1
for i=1+n:1:21+n
if(x(i)>0)
y(i)=exp(x(i));
else
y(i+n)=0;
end
end
case 2
for i=1-n:1:21-n
if(x(i)>0)
y(i)=exp(x(i));
else
y(i-n)=0;
end
end
end
subplot(1,2,2)
stem(x,y)

카테고리

Help CenterFile Exchange에서 Signal Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by