-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwoSine_amplitudeRatio_PhaseLag.m
executable file
·58 lines (49 loc) · 1.31 KB
/
twoSine_amplitudeRatio_PhaseLag.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function [amplitude_ratio,phase_lag] = twoSine_amplitudeRatio_PhaseLag(dataIn,dataOut)
% function twoSine_amplitudeRatio_PhaseLag(dataIn,dataOut,Ts)
% computes the amplitude ratio and phase differnce between two sinusoidal
% signals at the same frequency
% inputs:
% dataIn: input sinusoidal signal
% dataOut: output sinusoidal signal
% Ts: sampling time
% outputs:
% amplitude_ratio:
% output magnitude
% ----------------
% input magnitude
% phase_lag: output phase - input phase
%
% Xu Chen
% 2012-05-16
% 2015-02-08
% 2022-04-29
if nargin < 2
error('insufficient inputs')
end
x = dataIn;
y = dataOut;
% remove bias
x = x - mean(x);
y = y - mean(y);
if length(x) ~= length(y)
error('Lengths of inputs and outputs are not the same.')
end
npts = length(x);
% take the FFT
X=fft(x);
Y=fft(y);
% Calculate the numberof unique points
NumUniquePts = ceil((npts+1)/2);
% Determine the max value and max point.
% This is where the sinusoidal is located if the signal to noise ratio is large.
[mag_x idx_x] = max(abs(X));
[mag_y idx_y] = max(abs(Y));
% determine the phase difference
% at the maximum point.
px = phase(X(idx_x));
py = phase(Y(idx_y));
phase_lag = py - px;
% phas_lag = mod(phase_lag+pi,2*pi)-pi;
% determine the amplitude scaling
amplitude_ratio = mag_y/mag_x;