Exercise 7
Part 1 — APIs and Interpolation
In this first part, you will be using the free weather information API mentioned in the lecture. Download the necessary CSV file from here: https://learn-data-science-site.vercel.app/locations.csv
Open and inspect this file with pandas
Plot the relative locations of the cities based on longitude and latitude
- Plot Garching (48.26, 11.67) in a different color than the other cities. Note: please do not add Garching to the DataFrame.
- You may negate the Earth's curvature for this plot due to the relative closeness of the cities
- Add the name of each city to the plot. Hint: look up
plt.text()
Check out the API documentation of this site we briefly looked at in class: https://open-meteo.com/en/docs
- Write a python function using
requeststhat takes in the latitude and longitude of a location and directly returns the current temperature as a float. Build in a small delay (at least 1s) before each request to avoid rate limiting.- Recall: use Python's built-in
timemodule for this delay.
- Recall: use Python's built-in
- Apply this function to your DataFrame to get a new column corresponding the current temperature at each location.
- Write a python function using
Use the
CloughTocher2DInterpolatorfrom SciPy to create an interpolator which can estimate the temperature anywhere between the given cities.- Use numpy's
linspaceandmeshgridto generate a coordinate grid (X and Y variables below) - Get the interpolated temperature at each of these X,Y coordinates (Z below)
- Create a mesh plot with a color bar using the following code:
plt.pcolormesh(Y, X, Z) plt.colorbar() plt.show()- Plot your cities on top of this mesh grid plot (likely you can copy the code from above for easy overplotting)
- Use numpy's
Finally, use your interpolator to estimate the current temperature in Garching. Does this look close to the current temperature given on your phone?
Part 2 - Fast Fourier Transform
- Download the audio file of Chopin's Etude Op 25 No 1 from here: https://learn-data-science-site.vercel.app/IMSLP528192-PMLP01970-chopin_-Study_Op_25-1.wav
- Use the following code to open the file with SciPy and perform an FFT
sample_rate, audio_data = scipy.io.wavfile.read(wav_filepath)
print(f"Shape of stereo audio: {audio_data.shape}")
left_channel, right_channel = audio_data[:,0], audio_data[:,1]
magnitude = np.abs(scipy.fft.rfft(left))
frequency = scipy.fft.rfftfreq(len(left), 1 / sample_rate)
- Plot frequency vs magnitude:
- Find a suitable maximum cutoff for the frequency of the plot, so you can more clearly see the important part of the frequency spac
- Hint: use
plt.xlim()
- Hint: use
- How many peaks do you see more or less?
- Find a suitable maximum cutoff for the frequency of the plot, so you can more clearly see the important part of the frequency spac
- What frequency is heard most in the piece of music? I.e you must calculate the frequency that has the highest peak in this plot above.
- Use this chart to find the musical note closest to this frequency: https://muted.io/note-frequencies
- E.g. if you found the frequency to be 46Hz, the closest musical note would be F#1.
- Whatever note you find is the secret word ✨