%Analysis of NS67 Flight Data and TED (Turbine Electric Device) Payload
%data

%By Connor Meyers
%When Running, Make sure the files TED_EDIT.TXT and 67DATA.LOG are in the
%directory MATLAB is using as the active folder. These should be in the
%same folder as this script on the nearspace servers

%% SD File
load TED_EDIT.TXT
for k=1:23373
Voltages(k)=TED_EDIT(k,2);
end
Times(1)=0;
for k=2:length(Voltages)
    Times(k)=Times(k-1)+.25;%Seconds
end
Times_Min=Times/60;
Times_Hr=Times_Min/60;
figure; hold on;
plot(Times,Voltages,'b.');
[Vmax,ind_Vmax]=max(Voltages)
T_Vmax=Times(ind_Vmax)
plot(T_Vmax,Vmax,'rx');%red x is Vmax
T_burst=3900;%index 15601
V_burst=Voltages(15601);
plot(T_burst,V_burst,'gs');%green box is burst
xlabel 'Time(s)';ylabel 'Voltage (v)'; title 'Voltage Generated vs Time, NS67 (red x->Vmax, green box->burst)';
%% Log File
%ind 2263 flight start
%ind 6163 burst
%ind 8107 land

%columns: 1-time, 2-long,3-lat,4-alt,5-?

load 67DATA.LOG
a=1;
for k=2263:(2263+5843)
    Alts(a)=X67DATA(k,4);
    a=a+1;
end

k=1;
b=1;
while(k<length(Voltages))
   V_Alts(b)=Voltages(k);
   k=k+4;
   b=b+1;
end
for j=1:length(Alts)-1;
    AscentRate(j)=Alts(j+1)-Alts(j); %m/s
end
V_Alts=V_Alts(1:5843);

Vth=polyfit(AscentRate,V_Alts,6);
Vth_array=polyval(Vth,AscentRate);
figure; hold on;
plot(AscentRate,Vth_array,'gd'),plot(AscentRate,V_Alts,'r.');
xlabel 'AscentRate (m/s)';
ylabel 'Voltage (V)'; 
title 'Voltage Generated vs Ascent Rate, NS67 (Green->theory,Red->Data)';
axis([0,15,-.1,2.5]); %Only examining data during ascent
MeanVoltage=mean(V_Alts)
MedianVoltage=median(V_Alts)
ModeVoltage=mode(V_Alts)
RangeVoltage=range(V_Alts)
MeanAR=mean(AscentRate)
MedianAR=median(AscentRate)
ModeAR=mode(AscentRate)

x=0:.1:6.5;
y_cowl= polyval([0.0003,+0.0019,0.0803,0.8883],x);
%Best fit curve from NS67 MATLAB data
y_bare = polyval([0.0014,-0.0333,0.3078,-0.3167;],x);
%Best fit curve from car test excel file data
figure; hold on;
plot(x,y_cowl,'g'),plot(x,y_bare,'r')
title 'Cowl/Shroud Comparison, Best Fit Curves (Green->Shroud,Red->Bare)';
xlabel 'Vinfinity (m/s)'; ylabel 'Voltage Generated (V)';
Percent_Advantage=(mean(y_cowl)-mean(y_bare))/mean(y_bare)*100
%I'd like to repeat the car test for a more definitive test
