<jupyter_start><jupyter_text>Implement an AccelerometerIn this notebook you will define your own `get_derivative_from_data` function and use it to differentiate position data ONCE to get velocity information and then again to get acceleration information.In part 1 I will demonstrate what this process looks like and then in part 2 you'll implement the function yourself. -----  Part 1 - Reminder and Demonstration<jupyter_code># run this cell for required imports

from helpers import process_data
from helpers import get_derivative_from_data as solution_derivative
from matplotlib import pyplot as plt
# load the parallel park data
PARALLEL_PARK_DATA = process_data("parallel_park.pickle")

# get the relevant columns
timestamps    = [row[0] for row in PARALLEL_PARK_DATA]
displacements = [row[1] for row in PARALLEL_PARK_DATA]

# calculate first derivative
speeds = solution_derivative(displacements, timestamps)

# plot
plt.title("Position and Velocity vs Time")
plt.xlabel("Time (seconds)")
plt.ylabel("Position (blue) and Speed (orange)")
plt.scatter(timestamps, displacements)
plt.scatter(timestamps[1:], speeds)
plt.show()<jupyter_output><empty_output><jupyter_text>But you just saw that acceleration is the derivative of velocity... which means we can use the same derivative function to calculate acceleration!<jupyter_code># calculate SECOND derivative 
accelerations = solution_derivative(speeds, timestamps[1:])

# plot (note the slicing of timestamps from 2 --> end)
plt.scatter(timestamps[2:], accelerations)
plt.show()<jupyter_output><empty_output><jupyter_text>As you can see, this parallel park motion consisted of four segments with different (but constant) acceleration. We can plot all three quantities at once like this:<jupyter_code>plt.title("x(t), v(t), a(t)")
plt.xlabel("Time (seconds)")
plt.ylabel("x (blue), v (orange), a (green)")
plt.scatter(timestamps, displacements)
plt.scatter(timestamps[1:], speeds)
plt.scatter(timestamps[2:], accelerations)
plt.show()<jupyter_output><empty_output><jupyter_text>----  Part 2 - Implement it yourself!<jupyter_code>def get_derivative_from_data(position_data, time_data):
    
    if len(position_data) != len(time_data):
        raise(ValueError, "Data sets must have same length")
    
    # 2. Prepare empty list of speeds
    speeds = []
    
    # 3. Get first values for position and time
    previous_position = position_data[0]
    previous_time     = time_data[0]
    
    # 4. Begin loop through all data EXCEPT first entry
    for i in range(1, len(position_data)):
        
        # 5. get position and time data for this timestamp
        position = position_data[i]
        time     = time_data[i]
        
        # 6. Calculate delta_x and delta_t
        delta_x = position - previous_position
        delta_t = time - previous_time
        
        # 7. Speed is slope. Calculate it and append to list
        speed = delta_x / delta_t
        speeds.append(speed)
        
        # 8. Update values for next iteration of the loop.
        previous_position = position
        previous_time     = time
    
    return speeds
# Testing part 1 - visual testing of first derivative
#   compare this output to the corresponding graph above.
speeds = get_derivative_from_data(displacements, timestamps)

plt.title("Position and Velocity vs Time")
plt.xlabel("Time (seconds)")
plt.ylabel("Position (blue) and Speed (orange)")
plt.scatter(timestamps, displacements)
plt.scatter(timestamps[1:], speeds)
plt.show()
# Testing part 2 - visual testing of second derivative
#   compare this output to the corresponding graph above.
speeds = get_derivative_from_data(displacements, timestamps)
accelerations = get_derivative_from_data(speeds, timestamps[1:])

plt.title("x(t), v(t), a(t)")
plt.xlabel("Time (seconds)")
plt.ylabel("x (blue), v (orange), a (green)")
plt.scatter(timestamps, displacements)
plt.scatter(timestamps[1:], speeds)
plt.scatter(timestamps[2:], accelerations)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09357017763322492, 'fraction_numerical': 0.0070052539404553416, 'mean_word_length': 4.499312242090784, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '45300', 'license': 'MIT', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1221, 'n_words': 387, 'path': '3_4_Vehicle_Motion/Implement an Accelerometer.ipynb', 'repo_name': 'chinitaberrio/CV_nanodegree', 'size': '3997'}