Measure phase between two discrete signals
Show older comments
Good morning,
I have two sampled signals, corresponding to two sines. These signals are sampled with a low sampling rate, i.e. I get few samples within the measurement. Furthermore they are real signals and I have a certain amount of noise compared to the amplitude of the signal.
I would like to get the amplitude, which I think is done well and the phase difference between them, which is the part where I am getting problems.(Because I know that the value it returns is not correct)
Can you give me some advice?
Thank you!
clc
clear all
close all
%% Reading CSV from CC
%Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 3);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ";";
% Specify column names and types
opts.VariableNames = ["Time", "AmpPert", "Vout"];
opts.VariableTypes = ["double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Import the data
ADC = readtable("D:\Doctorado\10. Pruebas\2. RespuestaFrec\3. Digital\divisor\SegundoIntento\160kHz.csv", opts);
%% Clear temporary variables
clear opts
%% Entry of characteristic data
fs = 663e3; % Sampling frequency (samples per second)
dt = 1/fs; % seconds per sample
AdcTime = ADC.Time*dt; % Samples convertsion to time
OffsetVout = mean(ADC.Vout); %Measurement of fignal offset
ADC.Vout = ADC.Vout-OffsetVout; %Removing DC
AVout = smoothdata(ADC.Vout,"gaussian",15); %smoothing the signal
% AVout=ADC.Vout;
OffsetPert = mean(ADC.AmpPert); %Measurement of fignal offset
ADC.AmpPert = ADC.AmpPert-OffsetPert; %Removing DC
APert = smoothdata(ADC.AmpPert,"gaussian",15); %smoothing the signal
% APert=ADC.AmpPert;
figure(1)
plot(AdcTime,AVout,'r')
hold on
plot(AdcTime,50*APert,'b')
legend('\DeltaVout','\DeltaPert')
%% Extraction of disturbance information
Y = fft(APert); %Calculation of the FFT
L =length(Y);
%Magnitude acquisition
P2 = abs(Y/L); %Calculation of the magnitude of harmonics
P1Pert = P2(1:L/2+1); %Only the positive spectrum
P1Pert(1:end-1) = 2*P1Pert(1:end-1); %Add the part of the negative part - which is the reflection - therefore by 2
f = fs*(0:(L/2))/L;
%Phase adquisition
tol = 3e-3;
Y(abs(Y) < tol) = 0; %Remove harmonics minor to tolerance
thetaPert = angle(Y);
thetaPert = thetaPert(1:L/2+1);
thetaPert = abs(thetaPert*180/pi);
%% Extraction of Vout information
YVout = fft(AVout); %Calculation of the FFT
%Magnitude acquisition
P2Vout = abs(YVout/L); %Calculation of the magnitude of harmonics
P1Vout = P2Vout(1:L/2+1); %Only the positive spectrum
P1Vout(1:end-1) = 2*P1Vout(1:end-1); %Add the part of the negative part - which is the reflection - therefore by 2
%Phase adquisition
tol = 0.3;
YVout(abs(YVout) < tol) = 0; %Remove harmonics minor to tolerance
thetaVout = angle(YVout);
thetaVout = thetaVout(1:L/2+1);
thetaVout = abs(thetaVout*180/pi);
%% Gain and phase calculations
% Disturbance signal
AmplitudePert = max(P1Pert);
Index = find(P1Pert==AmplitudePert);
Frec = f(Index)
%Vout signal
AmplitudeVout = P1Vout(Index);
%Gain calculation
Gain =20*log10(AmplitudeVout/AmplitudePert)
%Phase difference calculation
fase = (thetaVout(Index) - thetaPert(Index))
figure(2)
loglog(f,P1Vout,'r');
hold on
grid on; grid minor
loglog(f,P1Pert,'b')
%Another method to determine phase
[pksPert,locsPert] = findpeaks(APert); %Find peaks of disturbance
[pksVout,locsVout] = findpeaks(AVout); %Find peaks of disturbance
figure(1)
plot(AdcTime(locsVout), AVout(locsVout), '+r')
plot(AdcTime(locsPert), 50*APert(locsPert), '+r')
%Not working well
if length(locsVout)>length(locsPert)
difference = locsVout(2:end)-locsPert;
elseif length(locsVout)==length(locsPert)
difference = locsVout-locsPert;
else
difference = NaN;
end
difference = mean(difference);
differenceTime = difference*dt;
phasePeaks = differenceTime*Frec*360;
result = ['La frecuencia es ',num2str(Frec),' La ganancia es ',num2str(Gain),' .La fase es ',num2str(fase), ' o mediate picos ', num2str(FasePeaks)];
disp(result)
Accepted Answer
More Answers (0)
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
