How to plot a time-delayed ersion of a signal ??

cananyone tell me how to plot the time-delayed version of a signal ?
Imagine the simplest case, y=cos(x), I want to delay it by 2 units. But I get some strange results!
clc ; clear all; close all x=(0.00001:0.01:2)*pi; y=cos(x); plot(x,y) hold on [Nrow Ncol]=size(y); m=2; % Delay step z=zeros([Nrow Ncol]); for i=3:Ncol-m j=i-m; z(i,1)=y(j,1);
end plot(z,'r')

댓글 수: 1

Jan
Jan 2011년 3월 31일
Please use code formatting to improve the readability of the code.
"Some strange results" is a vague description only. It is always a good idea to describe the results with as much details as needed to solve the problem.

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

 채택된 답변

Paulo Silva
Paulo Silva 2011년 3월 31일

1 개 추천

x=0:0.01:2*pi
y=cos(x)
plot(x,y)
hold on
d=2;
y=[ones(1,d)*nan y(1:end-d)];
plot(x,y,'r')
legend('signal',['delayed signal by' num2str(d) 'units'])

댓글 수: 5

Negar
Negar 2011년 3월 31일
Thanks a lot Paulo, it works; But could you please explain to me what this line in your code is?
y=[ones(1,d)*nan y(1:end-d)];
Paulo Silva
Paulo Silva 2011년 3월 31일
ones(1,d) creates a vector with just a row and d columns, all values are ones, nan values are not plotted by plot so the code inserts those nan just to fill the first d values, y(1:end-d) removes d elements from the end of y, this is needed so the number of elements of y are equal to the number of elements of x, plot(x,y) requires that in order to work properly.
Jan
Jan 2011년 3월 31일
"nan(1, d)" is nicer and faster than "ones(1, d)*nan".
Negar
Negar 2011년 3월 31일
Thanks a lot :)
Amit Aryal
Amit Aryal 2015년 3월 27일
x=0:0.01:2*pi y=cos(x) plot(x,y) hold on d=2; y=[ones(1,d)*nan y(1:end-d)]; plot(x,y,'r') legend('signal',['delayed signal by' num2str(d) 'units'])
I tried the above code but no delay signal produced. Both signals superimposed one on top of another!

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

추가 답변 (0개)

카테고리

태그

질문:

2011년 3월 31일

댓글:

2015년 3월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by