MATLAB: How to move a Signal..

signal processing

Dear all,
I am trying to learn some concepts of Signal processing to be able to work on my project. For the sample code below (Not my code but the idea is the same, mine is using random number generator), I have two signals and one is a delayed version of first one.. I want to know how can I move the Signal by the delay time I got so I will show that the two signals are the same indeed but one is delayed?
n = 0:299;
x = cos(pi/4*n);
xdft = fft(x);
k = 0:299;
D = 2;
phaseshift = exp(-1j*(2*pi*k*D)/length(x));
xdft = phaseshift.*xdft
% y is x delayed by two samples
y = ifft(xdft,'symmetric');
[Pxy,W] = cpsd(x,y,100,80,128);
[~,I] = max(abs(Pxy));
phase = atan2(-imag(Pxy(I)),real(Pxy(I)));
% the delay in samples prints out. The negative indicates delay.
round(phase*(1/(pi/4)))
plot(x(1:100),'k');
hold on;
plot(y(1:100),'b');
grid on; legend('Original','Delayed');
Thanks,

Best Answer

For delays that are an integer number of samples you can add a delay (zeros) in the plot:
plot([zeros(1, D), x(1:100)],'k');
plot([y(1:100), zeros(1, D)],'b');
For delays that are not an integer number of samples, where using your approach with "phaseshift" really shines, you really have to just rerun your code backwards to compensate for the delay.
Related Question