\section{Exercises}
\begin{footnotesize} 
\subsection*{Targeted exercises} 

\subsubsection*{Exercises for Section \ref{Refactoring_code_to_turn_it_into_functions_you_can_reuse}} 

\begin{enumerate}\setcounter{enumi}{-1}
	\item  Refactor the final code from Chapter 1. Create separate functions to handle the printing output, the  \lstinline{calcPlateVols()} functionality, and defining the geometry and concentrations of the well plate. The latter should take as arguments: the number of rows, the number of columns and the starting and ending concentrations and ionic strengths.

	\item  Refactor the final code from Chapter 9. Produce a function that will automatically background subtract  data passed to it.

	
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Computing_derivatives_using_numpy.gradient}} 

\begin{enumerate}\setcounter{enumi}{1}
	\item  For each of the following functions, determine the accuracy of a numerical derivative.  Create data with an $x$-axis from 0 to $2\pi$ with 10 data points.  Make a figure with two subplots, where the top plot shows the function, its analytical derivative, and its numerical derivative using the forward-backward method, and the bottom plot shows the difference between the numerical and analytical solutions.

	\begin{enumerate}
	\item sine
	\item Gaussian
	\item Lorentzian
	
\end{enumerate}
\item  Starting with your code from Exercise 2 Create a plot of the root-mean-squared deviation between the numerical and analytical solutions vs. number of data points for data with 10, 20, 30, 40, and 50 points.  What do you notice?


\item  Redo Exercise 3 using only forward derivatives and compare the accuracy of the two methods.

\end{enumerate}


\subsubsection*{Exercises for Section \ref{Importing_objects_from_Python_files_in_arbitrary_locations}} 
\begin{enumerate}\setcounter{enumi}{4}
	\item  Return to the final code for Chapter 7, and replace the block that imported code from an external script with code that uses \lstinline{importFromPy()}.

	\item  Using \lstinline{importFromPy()} get the $x$ and $y$ data from the final code in Chapter 2.  Then use \lstinline{importFromPy()} to get the final extinction coefficient from the code in Chapter 5. Then make a plot of the data, but rescale the $y$ values to be in terms of concentration, rather than absorbance. Label the axes appropriately.

	
\end{enumerate}
\subsection*{Comprehensive exercises} 
\begin{enumerate}\setcounter{enumi}{6}
	\item  Derivatives tend to amplify noise. Using the X-ray powder diffraction data from the website for this book, compare the results when computing the derivative first, then performing Savitzky-Golay filtering to the opposite.  Which one produces more noise?  You should do this in two steps, separately computing derivatives and smoothing, but note (for future use) that \lstinline{scipy.savgol_filter()} can give you derivatives automatically with the \lstinline{deriv} keyword.

	
	\item  Write your own peak finding algorithm using derivatives. Using the file `HCl-IR.csv,' from the website for this book, which contains the FTIR spectrum of HCl, create a code that finds peaks without using \lstinline{scipy.find_peaks()}. Remember that at a peak, the first derivative is zero and the second derivative is negative.
    \begin{itemize}
        \item Read the file and plot the data, its first derivative, and its second derivative. Note that the first and second derivatives may be much smaller or larger than the data, so you may want to apply a scaling factor to each such that they are all about the same height as the data. Zoom in on a smaller peak to confirm that the derivatives look as expected.
	\item A peak should be a maximum in the signal, a negative-going zero crossing in the first derivative, and a minimum for the second derivative. Since noise looks like peaks to a computer, you will also need to have a "threshold," or a minimum intensity in the spectrum that distinguishes a peak from noise. Any zero crossing at a point where the signal is less than the threshold should be ignored.
    \end{itemize}
    
	The first derivative is unlikely to be precisely zero anywhere. It is much easier to find the point just before and just after the zero crossing of the first derivative and pick as the peak whichever of the two points is higher in the signal. Plot the peaks you picked on top of the data using a scatter plot.  Zoomed in to a few peaks to confirm that your code works correctly.
	
	We have also provided three other files, another FTIR spectrum `CO2-IR.csv,' NMR spectrum `NMR.csv', and a powder diffraction pattern "KBr.csv': Using this same code (changing only the thresholds and scaling factors for the derivatives), find the peaks.  Note that the diffraction data is noisy - comment on how does this affect your peak picker.  What could you do about it?

    \begin{itemize}
        \item A good trick can be to `weight' the derivatives to make it easier to tell the real peaks from the noise ones. This can be accomplished by multiplying the derivative by the signal, so that the amplitude of the derivative at each peak is enhanced. Implement this and see how this impacts your results.
	  \item You could get a more accurate estimate of the peak by explicitly determining the zero crossing.  For the two points in your derivative data that are just above and just below zero, compute x value at which the derivative crosses zero.  You can do this by computing the slope, $m = (y_2 - y_1)/(x_2 - x_1)$, with point 2 as the point just below zero and point 1 the point just above zero.  Then, solve the same equation for $x_2$ (your zero crossing) where $y_2 = 0$ and $m$ is the slope you just computed. Implement this and comment on how it impacts your results. 
    \end{itemize}

      
\item  Sometimes spectroscopists find it easier to determine the number of peaks in a complex, overlapping band, by taking the derivative of the spectrum. To figure out why, and apply this to generate initial guesses for the code at the end of Chapter 6. Hints: You can answer the fundamental question by making your own overlapping peaks and taking their derivative. Then, ask yourself what are the different ways to find peaks!

\end{enumerate}
\end{footnotesize} 
