phase difference between two sines with wt and 2wt frequencies
6 views (last 30 days)
Show older comments
Daniel Mella
on 26 Dec 2020
Commented: Star Strider
on 30 Dec 2020
Hi all,
I have two data vectors (x,y) that very fairly follow these equations:


how can I calculate ϕ from the data? At the moment I have been fitting two dummy sine functions with different ϕ until it matches the data shape (x,y). I also tried to fit sine functions but I don't know how to relate the phase angle I get for x and y. I think I'm missing something really obvious here.
Regards,
0 Comments
Accepted Answer
Star Strider
on 26 Dec 2020
Edited: Star Strider
on 26 Dec 2020
I usually let the Symbolic Math Toolbox do the ‘heavy lifting’ in situations like these (particularly because it doesn’t make the stupid algrbra errors I’m prone to making):
syms Ax Ay phi t w x y
Y = Ay * sin(w*t);
X = Ax * cos(2*w*t + phi);
phi_sln = solve(X == Y, phi)
producing:
phi_sln =
acos((Ay*sin(t*w))/Ax) - 2*t*w
- acos((Ay*sin(t*w))/Ax) - 2*t*w
This assumes that you have some idea of what
and
and ω are, however that should be apparent from the magnitudes of the data.
A numerical parameter estimation approach:
t = linspace(0,5).'; % Column Vector
Ax = 2; % Define Parameter
Ay = 3; % Define Parameter
w = 5; % Define Parameter
phi = pi/4; % Define Parameter
y = Ay*sin(w*t) + randn(size(t))*0.05; % Create Data
x = Ax*cos(2*w*t + phi) + randn(size(t))*0.05; % Create Data
objfcn = @(b,t) [b(1).*sin(b(2).*t), b(3).*cos(2*b(2).*t + b(4))]; % Objective Function
ypks = islocalmax(y); % Estimate ‘w’
estw = 2*pi/(mean(diff(t(ypks)))); % Estimate ‘w’
B = fminsearch(@(b) norm([y, x] - objfcn(b,t)), [max(y); estw; max(x); rand]); % Estimate Parameters
yxest = objfcn(B, t); % Fitted Data
figure
plot(t, y, t, x)
hold on
plot(t, yxest, '--')
hold off
grid
legend('y', 'x', 'y_{est}', 'x_{est}')
Note that ‘x’ and ‘y’ have to be essentially noise-free for this to work, so some filtering may be necessary to remove high-frequency and low-frequency noise if any exist in your signals.
EDIT — (26 Dec 2020 at 17:36)
A much more robust estimator, especially in the presence of noise, for the radian frequency ‘w’ is:
yzx = find(diff(sign(y))); % Estimate ‘w
estw = pi/(mean(diff(t(yzx)))); % Estimate ‘w’
I am leaving my original code unchanged, however this version is likely a better option.
.
8 Comments
Star Strider
on 30 Dec 2020
I would not put phase variables in both
terms, since the actual phase difference is then not easily determined.
I provided a reliable way to determine the phase, based on the actual data and reasonably reliable nonlinear parameter estimation techniques. The parameter estimates are what the functions determined, and while the phase ϕ may be shifted by integer multiples of π, they remain reliable.
The results I got for ϕ I posted in my previous Comment. Numerical computations can vary slightly between runs. It is straightforward to use nlparci with the results of lsqcurvefit (it will be necessary to request additional outputs from lsqcurvefit, such as the residuals and the Jacobian) to see what the 95% confidence limits are for ϕ. That will give you a reasonable idea of its accuracy, and whether it is statistically different from 0 (if it is not, the confidence limits will have oppposite signs, and it is not necessary to include it in the model).
If my Answer helped you solve your problem, please Accept it!
.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!












