How to add the new delayed signal to the PLOT !

Hey all, My code below is working, however by looking at the plot it seems it didn't draw the new delayed signals, It just plotted the second signal? Is it something to do with [xi,f]=ksdensity(Y+delay); I am not adding the delay?
x = sample1(:,1);
X = (x).';
y = sample2(:,1);
Y = (y).';
figure;
clf
fisrt = subplot(3,1,1);
[xi,f]=ksdensity(X);
plot(f,xi);
line(repmat(X,2,1),repmat([0;0.1*max(xi)],1,length(X)),'color','r' );
second = subplot(3,1,2);
[xi,f]=ksdensity(Y);
plot(f,xi);
line(repmat(Y,2,1),repmat([0;0.1*max(xi)],1,length(Y)),'color','r' );
[Rxx,lags] = xcorr(X,Y);
[Z,delay] = max(Rxx);
lags(delay);
if delay>0
hold(first,'on');
[xi,f]=ksdensity(Y+delay);
plot(first,f,xi);
else
hold(second,'on');
[xi,f]=ksdensity(X+delay);
plot(second,f,xi);
end
Cheers,

2 comentarios

Fangjun Jiang
Fangjun Jiang el 29 de Ag. de 2011
See my comment in your previous question. You accepted the answer. What further work have you done?
http://www.mathworks.com/matlabcentral/answers/14446-plot-problem
Fangjun Jiang
Fangjun Jiang el 30 de Ag. de 2011
The same mistake was pointed out even earlier. So, instead of making the same mistake again and again, please make your own effort to read, understand and learn it.
http://www.mathworks.com/matlabcentral/answers/13110-signal-processing

Iniciar sesión para comentar.

 Respuesta aceptada

Rick Rosson
Rick Rosson el 29 de Ag. de 2011
Honglei's answer is correct, and it explains why simply adding 2 to an existing signal does not shift the signal in time, but merely shifts every value upward by 2.
To shift a signal in time, you need to insert zeros at the beginning of the signal, an operation which is often called zeropadding.
Please try the following example:
% define a simple signal:
x = (1:20)';
% now shift the signal in time by zeropadding at the beginning:
N = 5;
y = [ zeros(N,1) ; x ];
% Also, let's zeropad x at the end so that it has the same length as y:
x = [ x ; zeros(N,1) ];
% Create a simple time domain:
n = (1:size(x,1))';
% plot both signals:
figure;
plot(n,x,n,y);
legend('x','y');
You should see that y is a shifted version of x.
HTH.
Rick

2 comentarios

lamawal Jonathan
lamawal Jonathan el 30 de Nov. de 2014
Hello All! i'm on a similar problem (zero pad a signal), the slight difference is that my signal is a vector of single row and 2400 columns. each time i zero pad, it gives me the following error;
"Error using vertical
Dimensions of matrices being concatenated are not consistent."
i even tried running your code several times but it repeats same error on line 5 (y = [ zeros(N,1) ; x ];) of your code. pls, help i'm very new to matlab and it's very important for me.
Honglei Chen
Honglei Chen el 1 de Dic. de 2014
Your signal is a row vector, so it should be y=[zeros(1,N) x].

Iniciar sesión para comentar.

Más respuestas (2)

Honglei Chen
Honglei Chen el 29 de Ag. de 2011
Hi Susan,
X+delay does not delay the signal, it merely adds an offset of delay's value to X. To delay a signal, you need to shift it in samples. One way to do it is to pad zeros in the front. For example, assume your delay is 1 second and your sampling frequency is also 1 second, then you need to insert 1 zero in front of signal, i.e., make
X_delay = [0 X];
HTH

9 comentarios

Susan
Susan el 29 de Ag. de 2011
Thank you Honglei, I did the delay part already and I calculated how much the signal is delayed but now I am trying to add the calculated delay to the signal that I found out it was delayed but looking at the figure it doesn't add the delay time.. Its this line mainly what I am talking about [xi,f]=ksdensity(Y+delay); ?? Do you know whether I am adding the calculated delay here or not?
Honglei Chen
Honglei Chen el 29 de Ag. de 2011
If I understand correctly, Y is your signal so Y+delay simply adds an offset to your signal, not delays it.
Susan
Susan el 29 de Ag. de 2011
Yeah, It is a signal and delay is a value represent the calculated delay between the two signals.. I am basically saying the X and Y are two signals and delay is the delay value between them then since delay is positive Y is the delayed signal so I want to add the delay value I calculated so It will be similar to X now?.. Thats what I am trying to achieve.. How can I add the calculated delay value to the signal I said it was delayed and in this case Y.. Hope I explained it well !!
Fangjun Jiang
Fangjun Jiang el 29 de Ag. de 2011
I can't believe it! If x and y are two similar signals, y is 2 seconds lagging behind x, to make x and y match, you need to SHIFT y signal 2 seconds forward (move left in time axle), or shift x signal 2 seconds backward (move right in time axle). You can't add 2 to the numerical value of x or y. I've explained this three times! You just couldn't get it, could you?!
Susan
Susan el 29 de Ag. de 2011
I am aware of this theory behind it.. obviously I know it should be shifted but applying it was an issue.. There are manners and ways to reply to a post, if you feel its so stupid etc.. SIMPLE don't answer.. This is a professional forum and I believe rude and offence acts are not allowed. I am here to learn, If you believe my question is stupid, silly or whatever, no one forced you to answer !!
Honglei Chen
Honglei Chen el 29 de Ag. de 2011
It is difficult to do it without format so I'll open a new answer here to illustrate what I mean.
Susan
Susan el 29 de Ag. de 2011
I hope your aware what you doing is so not professional, Its affecting on your "intelligence".. The more you live, the more you will see.. In this manner you will be amazed daily !
Fangjun Jiang
Fangjun Jiang el 29 de Ag. de 2011
I am amazed by the way you learn. The way for "applying it" has been given in this answer and many other answers. If you spend more time reading it and understanding it, instead of asking the same question repeatedly, your problem may already be solved.
lamawal Jonathan
lamawal Jonathan el 30 de Nov. de 2014
Hello All! i'm on a similar problem (zero pad a signal), the slight difference is that my signal is a vector of single row and 2400 columns. each time i zero pad, it gives me the following error;
"Error using vertical
Dimensions of matrices being concatenated are not consistent."
i even tried running your code several times but it repeats same error on line 5 (y = [ zeros(N,1) ; x ];) of your code. pls, help i'm very new to matlab and it's very important for me.

Iniciar sesión para comentar.

Honglei Chen
Honglei Chen el 29 de Ag. de 2011
Hi Susan,
Let's say I have two signals x and y. To make it simple, let's say
x = [1 1 1 1 1 ]
so if y is the same as x but delayed by 2 samples, you would see
y = [0 0 1 1 1]
for the first 5 samples. However, if you do
x+2
you will get
[3 3 3 3 3]
which is not the delayed version.
HTH

3 comentarios

Susan
Susan el 29 de Ag. de 2011
Thank you, It did help !
lamawal Jonathan
lamawal Jonathan el 30 de Nov. de 2014
Hello All! i'm on a similar problem (zero pad a signal), the slight difference is that my signal is a vector of single row and 2400 columns. each time i zero pad, it gives me the following error;
"Error using vertical
Dimensions of matrices being concatenated are not consistent."
i even tried running your code several times but it repeats same error on line 5 (y = [ zeros(N,1) ; x ];) of your code. pls, help i'm very new to matlab and it's very important for me.
Honglei Chen
Honglei Chen el 1 de Dic. de 2014
Your signal is a row vector, so it should be y=[zeros(1,N) x].

Iniciar sesión para comentar.

Categorías

Más información sobre Data Distribution Plots en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Ag. de 2011

Comentada:

el 1 de Dic. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by