<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Chapter 10 :: Coding For Chemists</title>
    <link>https://codingforchemistsbook.com/book_material/chapter-10/index.html</link>
    <description>Data Chapter 10 uses the same data as for Chapter 9&#xA;Alternatively, individual files can be found in the Data section.&#xA;Code from chapter &#39;&#39;&#39; Find the baseline of a small peak by smoothing the data first Requires: An FTIR spectrum in .csv format Written by: Chris Johnson and Ben Lear (authors@codechembook.com) v1.0.0 - 250226 - initial version &#39;&#39;&#39; import numpy as np import scipy.signal as sps from codechembook.quickPlots import quickScatter from codechembook.quickTools import quickOpenFilename, quickPopupMessage from codechembook.symbols.chem import wavenumber as wn # Get the spectrum file names quickPopupMessage(message = &#39;Select a CSV file containing the FTIR spectrum.&#39;) data_file = quickOpenFilename(filetypes = &#39;CSV files, *.csv&#39;) # Read the file wavenumber, absorption = np.genfromtxt(data_file, delimiter = &#39;,&#39;, unpack = True) # Apply Savitzky-Golay smoothing with a window of 100 and an order of 3 smooth_absorption = sps.savgol_filter(absorption, 100, 3) # Get the baseline connecting the limits of integration base_min, base_max = 1790, 1870 # the limits abs_min = smooth_absorption[np.argmin(np.abs(wavenumber - base_min))] abs_max = smooth_absorption[np.argmin(np.abs(wavenumber - base_max))] m = (abs_max - abs_min) / (base_max - base_min) b = abs_min - m * base_min baseline = m * wavenumber + b # Plot all of the spectra to allow the limits of integration to be determined fig = quickScatter(x = wavenumber, y = [absorption, smooth_absorption, baseline], name = [&#39;raw data&#39;, &#39;smoothed data&#39;, &#39;baseline&#39;], output = None) # Modify each trace to have the appearance we want fig.update_traces(selector = 0) fig.update_traces(selector = 1, line = dict(width = 4)) fig.update_traces(selector = 2, line = dict(width = 4, dash = &#39;dash&#39;)) # Find the y axis range to show just the data we want to see in the plot int_range = (wavenumber &gt; 1750) &amp; (wavenumber &lt; 1900) ymin, ymax = np.min(absorption[int_range]), np.max(absorption[int_range]) # Format the plot area fig.update_yaxes(title = &#39;absorption&#39;, range = [ymin - 0.1*(ymax-ymin), 1.1 * ymax]) fig.update_xaxes(title = f&#39;wavenumber /{wn}&#39;, range = [1750, 1900]) fig.update_layout(template = &#39;simple_white&#39;) fig.show(&#39;png&#39;) fig.write_image(format = &#39;png&#39;, file = &#39;SmoothIR.png&#39;) Solutions to Exercises Targeted exercises Comparing smoothing approaches Exercise 0 Imagine you have data with uncertainties for the dependent variables. You are considering smoothing using either binning or moving averages. The bin width and the moving average window would be the same size. After smoothing, which approach results in the largest absolute uncertainty? Which approach results in the largest relative uncertainties? You can, of course, use code to answer this question, if you want.</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <managingEditor>authors@codingforchemistsbook.com (Benjamin Lear and Christopher Johnson)</managingEditor>
    <webMaster>authors@codingforchemistsbook.com (Benjamin Lear and Christopher Johnson)</webMaster>
    <atom:link href="https://codingforchemistsbook.com/book_material/chapter-10/index.xml" rel="self" type="application/rss+xml" />
  </channel>
</rss>