Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
PHYS2320 Computing 2 Lab 3 Worksheet
You will find it in the Semester 1 Workshops->Workshop 3 - Week 5 folder on the PHYS2320 Minerva pages. There are three versions, pdf, html and notebook.
Aims
- Load and Plot numeric data
- Indexing and manipulating numpy arrays
- Using numpy functions to find our information about data
- Interpolating values
Getting Started
jupyter notebook
jupyter lab
Attendance Records
Do not forget to register your attendance at a Computer Labs session!
https://phys2320.leeds.ac.uk/attendance/register
Tasks
Task 1
Using numpy.genfromtxt() open the data file Fourier Data.csv into a numpy array.
The first column of the data file is a time, the second and subsequent columns are the terms of a Fourier series:
F(t) = a1 sin ωt + a2 sin 2ωt + a3 sin 3ωt + ⋯ + an sin nωt
(i.e. the second column contains values of a1 sin ωt, the third column contains values of a2 sin 2ωt and so on.)
Task 2
Your overall task is to write a function that will read a comma separated data file with two columns - Time(s) and Signal(V) and find out various information about he data.
1. Find the largest value of Signal(V) in the file - V_max and the corresponding time t_max.
2. Find hte smallest value of Signal(V) in the file V_min and the corresponding time t_min.
3. V_max should be positive and V_min should be negative, and there should be one point where the Signal(V) crosses zero. Find an estimate for the time t_0 where the signal crossed zero. Note that this is likely to be between data points in the file so you will have to interpolate the data to find the zero crossing point.
A tempalte for the function you shiould write for the mini-assessment is provided below.
Detailed Description and Marking Scheme
You can download a sample data set from the module website at https://phys2320.leeds.ac.uk/peak_find/. When you click on the Submit button the site will generate a unique set of data based on your username. The file is comma separated data values with a single row of header on the first line and between 25 and 75 datapoints. The function you write will be tested on a similar (but not identical!) file - the data used to test your function is generated from the same website.
You should use the code below as your starting point:
Marking Scheme
You get one mark for each value in the answers dictionary that is correct. If your code returns answers that are not correct then it will be checked with a second data file and if it returns answers that are different from the first data file (but not correct) then it will get half marks. (i.e. if you make an attempt to get the right answers but don't succeed you get partial credit - but if you simply return fixed numbers then you don't get marks.)
You must submit your function via the link below by midnight on Thursday 14th
November (that is the midnight between Thursday and Friday). For students whosubmitted by the deadline, results should come out one week after the deadline.
If you have mitigating circumstances and require an extension, then please email the Physics Student Support email.
Hints and Tips
Task 1
The first place to start after you have downloaded the file is to open it in a text editor and inspect it to decide what is the best way to read it.
In this case, you know that numpy.genfromtxt is going to be needed - so you what to see if the file has any column headers and what character is used to separate the columns.
You also want to find where the file has been saved (probably your Downloads folder).
You will want to start by importing numpy and matplotlib.pyplot. By convention, we
You don't have to use np and plt but it is probably a good idea to stick to the conventions as it makes your code easier for someone else to work with.
Once you have loaded the data from the file, you can check how many dimensions it has, and what shape is is. After that you can use the indexing operations on a numpy array to pull out just the bits that you need to the do the tasks.
Indexing arrays to get at the bits you want is covered in the first video tutorial in the numpy series. In many ways it is similar to lists:
data[0]
gets the first element of |*data*
For the first parts you will want to use a for loop to extract the correct column and then add the plot to a figure (so you have one graph with 5 different lines on it) - you should aim to give each line a suitable label and add a legend to the plot.
For the second part, you will probably want to make use of the np.sum function and understand what the axis parameter does.
Interpolating Data
Before doing the mini-assessment task, we will need to loom at interpolation. Interpolation is the process of estimating the value of some measurement or function between known data points. Imagine we have a set of (x,y) data points as below:
and we want to know what the measurement would have had we looked at x values between the black data points. To do this we need to make some assumptions about how the y values would have varied in the gaps between the black data points. We can't know this for sure, so we are estimating what the result would have been. This is the process of interpolating.
The simplest assumption is that y varied in a straight line - and so we would estimate the intermediate points as below:
This is called linear interpolation.
Of course looking at the data you might expect y to vary smoothly between the data points, and by considering two data points on either side of the position we are interested in, we could fit a cubic function and this would give perhaps a more realistic estimate of the intermediate value:
Interpolation in Python
What interp1d gives us is something that we can call like a function, passing it any value of x that lies within the range of the data points we have (we can't interpolate outside of this range - this is extrapolation and is even more dodgy that interpolating values...!)
Task 2
The first thing your function needs to do is to read in the data from file that it is given and get the two columns as two variables. This is rather similar to task 1 above. After this you need to identify the maximum and minimum values of the signal. You may want to do some judicious googling of numpy and maximum value to find the appropriate functions.
To find the corresponding times you will first need to find the index pf the maximum and minimum values in the signal - then you can use those indices to find the corresponding times.
The final item t_0 is by far and away the most difficult to compute - there is unlikely to be a data point exactly at Signal(V)=0 so you need to interpolate the data to find exactly where it crosses the y=0 axis. As we will see in the lab session, in Python interpolating data gives you something that you can call with an x value that will return the corresponding y value.
This is still not quite the correct thing for the mini-assessment as you know the y value and want the x value - on the figure below it is where the two dotted lines intersect.
We will see next week that there are function that can do this, but for now an easy thing to do is simply to swap the x and y arrays when doing the interpolation and get time as a function signal. This is not quite straightforward because now there are some values of Signal(V) have two different values of time(s) and the interpolating function struggles tobe sensible. The figure below shows the raw data and the results of trying to simply interpolate all the time(s) values as a function of the Signal(V).
The way to solve this is to do the interpolation only for the part of the data between t_max and t_min - to ensure that there is only one value of time(s) for each value of Singal(V). This is shown as the orange line above and gives a much more sensible value. You should have all the information you need from finding values of t_max and t_min to get the correct sections of the time(s) and Signal(V) to do this interpolation.
Testing your Code
As with the first mini-assessment, submitting code that does not work - for whatever reason - will get you 0 marks. So it is important to check that your code at least runs. In the template code cell in the Task 2 section, the function def and body are surrounded by the code necessary to run and test the function. To do this you should: