Determining the phase shift from the samples of the sinusoidal wave?

23 views (last 30 days)
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);

Accepted Answer

David Goodmanson
David Goodmanson on 1 Apr 2020
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.
  4 Comments
Berke Ogulcan Parlak
Berke Ogulcan Parlak on 2 Apr 2020
Edited: Berke Ogulcan Parlak on 2 Apr 2020
Hi David,
This worked really well for me! But I will have one last question for you. There is something I do not understand in the mathematical background. What is the mathematical meaning of 'c' you calculate here?
David Goodmanson
David Goodmanson on 2 Apr 2020
HI Berke,
the idea is that you have a column vector B, and m linearly independent column vectors vec1, vec2, ... vecm. All of these vectors are nx1. You want to find m coefficients such that c1*vec1 + c2*vec2 ...+ cm*vecm is the best fit to B. In your case m = 2 so put column vectors sin(w*t) and cos(w*t) side-by-side to form a 2xn matrix M. Then in matrix notation, with time varying down the columns,
[ sin(w*t) cos(w*t) ] [c1] [ B(t) ]
[ . . ] [c2] [ . ]
[ . . ] x = [ . ]
[ . . ] [ . ]
[ . . ] [ . ]
This works since everything in the sine column is mulitplied by c1 and everything in the cos column is mulitplied by c2. As is usually the case there are more equations than unknowns, so this has to be solved approximately. The matrix eqn is M*c = B, and and in Matlab this is solved in the least-squares sense with left divide,
c = M\B
So now
B = c(1)*sin(w*t) + c(2)*cos(w*t) = amplitude*sin(w*t + phase)
and proceed from there.
Easy as it is to use, there is a lot going on behind the scenes with the left divide symbol. Left divide (backslash) is basically how Mathworks got its start. I still think it's the best function in Matlab history, although the way things are going maybe some fabulous neural network function will eventually take the honor.

Sign in to comment.

More Answers (0)

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!