MATLAB: Determining the phase shift from the samples of the sinusoidal wave

amplitudedspMATLABphase shiftsignalsinusoidal

Hello, I have two waves like A and B. (A represents the input of the system, B output.) What I know is the frequency and amplitude of the A wave, and what I do not know is the amplitude and phase of the B wave. Can I access phase and amplitude information using the samples of A and B? (The code below is for collecting data, normally I don't know the phase and amplitude of B, I just know samples.)
w = 5;
gain = 2;
phase = pi/2;
A = 5*sin(w*t);
B = 5*gain*sin(w*t + phase);

Best Answer

Assuming that w is known, and that you have an identical time array for each of A and B, and that t and B are column arrays, then
c = [sin(w*t) cos(w*t)]\B
amplitude = norm(c)
phase = atan2(c(2),c(1))
That's the full ampllitude, so in your case gain = amplitude/5.