function HW2

close all

data = csvread('CRS-8_Telemetry.csv',1,0);
time = data(:,1);
speed = data(:,2)*1000/3600;   % Convert to m/s

% Plot the raw data - speed-v-time

plot(time,speed,'LineWidth',2,'Color',[1,0,0]);
axis = gca;
grid on
xlabel('Time (s)')
ylabel('Speed (m/s)')

% Calculate and plot the acceleration

for i=1:length(time)-1
    a(i) = (speed(i+1)-speed(i))/(time(i+1)-time(i));
end
figure
plot(time(1:end-1),a,'LineWidth',1,'Color',[1,0,0]);
axis = gca;
grid on
xlabel('Time (s)')
ylabel('Acceleration (m/s^2)')
title('Raw acceleration')

% Smooth the acceleration and plot it

a_smoothed(1) = a(1) ;
for i=2:length(time)-1
    a_smoothed(i) = 0.8*a_smoothed(i-1) + 0.2*a(i);
end
figure
plot(time(1:end-1),a_smoothed,'LineWidth',1,'Color',[1,0,0])
grid on
xlabel('Time (s)')
ylabel('Acceleration (m/s^2)')
title('Smoothed acceleration')

% Integrate the speed to find altitude -v- time and plot

x = cumtrapz(time,speed);
figure
plot(time,x/1000,'LineWidth',2,'Color',[1,0,0])
grid on
xlabel('Time (s)')
ylabel('Altitude (km)')

end