i have been working on calculating phase difference between 2 sine waves, i have been successful, i needed to know if there is a more optimized model? Adding to this, 1> is there any realtime dataset for validating the code i have written?
any references will be appriciated! Thank you
import numpy as np
import math
# create fake data
nsr = 1e-1
r = np.random.normal(size=(20000, 2)) * nsr
In line 4 we are setting nsr = 0.1, which represents the noise standard deviation.
Random noise is generated from 5, r represents the noise that’s created.
#ph_diff = math.pi * np.random.uniform()
ph_diff = np.radians(180)
print(ph_diff)
print('true phase diff (radians):', ph_diff, '\t(degrees):', ph_diff / (2 * math.pi) * 360)
Here in line 7 the true Phase difference is set to 180 degrees, and the same is printed in degress as well as radians.
omega = 13.56e6
the frequency is set to 13.56MHz
t = np.arange(20000) / 500e6
the time steps are set here with steps of 80 microseconds
rdata = np.zeros((len(t), 2))
rdata is created with the 1st column being time generated above and the second column being filled with zeros.
ph0 = 2 * math.pi * np.random.uniform()
Random phase offset is generated and assigned to ph0
rdata[:, 0] = np.sin(2 * math.pi * omega * t)# + ph0)
rdata[:, 1] = np.sin(2 * math.pi * omega * t + ph_diff)# + ph0)
Two sine waves are generated with the above specific parameters,
1> good sine wave (ideal)
2> sine wave with phase difference
these are saved into the rdata generated previously in such away that good wave data is the first column and the one with the phase difference is placed next to it.
rdata = rdata + r
noise is added to the whole rdata.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(rdata)
data = scaler.transform(rdata)
we are standardizing the data here.
Scalar is fitted with noisy data, calculating mean and standard deviation.
c = np.cov(np.transpose(data))
the data is transformed with scaling parameters.
print('cov: ', c)
phi = np.arccos(c[0, 1])
print('phase estimate (radians): ', phi, '(degrees): ', phi / math.pi * 180)
The covariance matrix c is printed,
The estimated phase difference in rad and degree is printed