%% Voltage Monitor Circuit Test Results


%% Voltage Transfer Function
% This test used a lab supply to test the total transfer (filter included)
% for the voltage ranges from 0 to 333, which was the upper limit of the
% power supply. 
close all; clear; 

Vin = [ 0 20.3 40.0 60.0 80.8 100.5 120.2 140.9 160.6 180.5 200.3 220.8 ...
    240.7 260.6 280.7 300.8 320.8 333.5];

Vout = [0.0 0.0 0.120 0.270 0.440 0.590 0.750 0.918 1.07 1.23 1.39 1.55 ...
    1.72 1.88 2.08 2.21 2.38 2.47];

VoFilt = [0.020 0.178 0.335 0.494 0.658 0.811 0.966 1.13 1.29 1.44 1.60 ...
    1.77 1.92 2.08 2.25 2.41 2.58 2.68];

% Fit a line 
[p, S] = polyfit(Vin,Vout,1)
[pf,Sf] = polyfit(Vin, VoFilt,1);
figure;
plot (Vin, (p(1)*Vin+p(2)),'b-', ...
    Vin, (pf(1)*Vin + pf(2)),'g-',...
    Vin, Vout, 'b.', ...
    Vin, VoFilt,'g.');
xlabel('Input Voltage (V)');
ylabel('Output Voltage (V)');
legend(['V_{OUT} = ' num2str(p(1)) 'V_{IN} + ' num2str(p(2))],...
    ['V_{OUTFILT} = ' num2str(pf(1)) 'V_{IN} + ' num2str(pf(2))],...
    'Location','SouthEast');
title('Voltage Sense Circuit Response');
grid on;


%% Frequency Analysis of the Filter Designed for PowerBuoy Voltage and Current Sensing
% First define the frequencies measured:

f = [1 100:100:900 1e3:1e3:18e3];

% Single input was sinusoidal, offset by 600mV and Vpp was:

Vpp = 2.14;

% Now enter the results (Vpp)

VoutPP = [2.18 2.14 2.14 2.16 2.16 2.16 2.18 2.18 2.2 2.22 2.24 2.40 ...
    1.16 0.40 0.18 0.12 0.08 0.06 .016 0.012 0.010 0.0076 0.0060     ...
    0.0046 0.0038 0.0032 0.0032 0.0030];

% Enter Phase Difference (Degrees

Phase = [ 2.1 3.4 11.5 17 23 27 32 40 45 50 57 131.5 135 180+95 180+74 ...
    180+63 180+50 0 0 0 0 0 0 0 0 0 0 0 ];

% Calculate the gain

Gain = 20*log10(VoutPP./Vpp);

figure
subplot(211);

semilogx(f,Gain,'.-');
grid on; 
ylabel('Voltage Gain (dB)');
title('Filter Frequency Response for 2V Peak-Peak Positive Sinusoid');
subplot(212);
semilogx(f,Phase,'.-');
ylabel('Phase (deg)');
xlabel('Frequency (Hz)');
grid on;

% Eric Martin 30 APRIL 2015