clear all
lag = pi/2;
amp_x = 2;
amp_y = 4;
Fs = 1024;
t = 0:1/Fs:0.5*(Fs-1)/Fs;
npts = length(t);
x = amp_x * (sin(2*pi*t*20) + ...
0.25*randn(1,npts)) + 5;
y = amp_y * (sin(2*pi*t*20 + lag)...
+ 0.25*randn(1,npts));
x = x - mean(x);
y = y - mean(y);
figure(1)
plot(t,x,t,y);
xlabel('Time (s)');
ylabel('Amplitude');
legend('Signal x(t):sin(2*pi*t*20)',...
'Signal y(t):sin(2*pi*t*20+lag)');
X=fft(x);
Y=fft(y);
NumUniquePts = ceil((npts+1)/2);
figure(2)
subplot(211);
f = (0:NumUniquePts-1)*Fs/npts;
plot(f,abs(X(1:NumUniquePts)));
title('X(f) : Magnitude response');
ylabel('|X(f)|')
subplot(212)
plot(f,abs(Y(1:NumUniquePts)));
title('Y(f) : Magnitude response')
xlabel('Frequency (Hz)');
ylabel('|Y(f)|')
figure(3)
subplot(211)
plot(f,angle(X(1:NumUniquePts)));
title('X(f) : Phase response');
ylabel('Phase (rad)');
subplot(212)
plot(f,angle(Y(1:NumUniquePts)));
title('Y(f) : Phase response');
xlabel('Frequency (Hz)');
ylabel('Phase (rad)');
[mag_x idx_x] = max(abs(X));
[mag_y idx_y] = max(abs(Y));
px = angle(X(idx_x));
py = angle(Y(idx_y));
phase_lag = py - px
amplitude_ratio = mag_y/mag_x
Best Answer