I took a triangular voltage signal and taking it's time derivative I obtained the current through a pure capacitor. Then I took different portion of voltage and current signals, took their Fourier transform (as if I am taking the STFT with rectangular gate function) and obtained the magnitude & phase from FFT. I obtained the sampling frequency by the formula- Fs = N/(t_final-t_initial) where t_initial & t_final are the time of the starting point and ending point of the portion of signal I choose.
For pure capacitor, I expect $+90^0$ phase shift of each current components with respect to corresponding voltage components. But depending on the portion of signal I choose, sometimes I find $+90^0$, sometimes $+270^0=-90^0$, sometimes $+60^0$!!! In some cases I don't see any meaningful result (see 3rd figure from left)! In addition, different frequency component shows different phase relation.
So, am I doing any mistake? How to obtain correct phase information at every portion of the signal?
clc; clear; %% Generate the signal to be processed C=10e-6; %10 micro-farad t_full=[1:500]*1e-6; v1=1*t_full(1:end/2); %1st Ramp of the triangle v2=v1(end)-1*(t_full(end/2+1:end)-t_full(end/2)); %2nd Ramp of the triangle v_full=[v1 v2]; %Triangular voltage signal dv=gradient(v_full); dv_dt=dv./(t_full(2)-t_full(1)); i_full=C*dv_dt; %Capacitor current =C*dv/dt %% Choose the portion of the full signal for STFT %Total signal has 500 points. st=40; %starting point of the signal portion np=125; %ending point of the signal portion t=t_full(st:st+np); v=v_full(st:st+np); i=i_full(st:st+np); %% Calculate sampling frequency N = length(t); % Length of signal under study t_final=t(N);t_initial=t(1); Fs = N/(t_final-t_initial); % Sampling frequency %% Obtain the FFT V = fft(v)/N; I = fft(i)/N; mag_V_full = abs(V); mag_V = mag_V_full(1:N/2+1);mag_V(2:end-1) = 2*mag_V(2:end-1); %FFT is symmetric, taking positive frequency only phi_V_full=angle(V);phi_V = phi_V_full(1:N/2+1); mag_I_full = abs(I); mag_I = mag_I_full(1:N/2+1);mag_I(2:end-1) = 2*mag_I(2:end-1); %FFT is symmetric, taking positive frequency only phi_I_full=angle(I);phi_I = phi_I_full(1:N/2+1); f = Fs/N*(0:(N/2)); del_phi=(phi_V-phi_I)/pi*180; %Phase shift between voltage & current component %% steming/ploting result subplot(4,1,1) [hAx,hLine1,hLine2] =plotyy(t,i,t,v); xlabel('time (s)') ylabel(hAx(1),'Current (A)'); % left y-axis ylabel(hAx(2),'Voltage (V)'); % right y-axis subplot(4,1,2) stem(f/1e3,mag_V) ylabel '|V|' xlim([-0.05 50]) subplot(4,1,3) stem(f/1e3,mag_I) xlim([-0.05 50]) ylabel '|I|' subplot(4,1,4) stem(f/1e3,del_phi) %We need to see the phase shift of the dominating frequency component. xlim([-0.05 50]) ylabel 'angle of Z=(Phi_V-Phi_I)' xlabel 'Frequency (kHz)'