I like to add this to the figure with the deconvoluted signals as a subplot below the original fit data:ĭeconvoluted overlapping gaussian curves with residual. Residual_2gauss = y_array_2gauss - (_2gaussian(x_array, *popt_2gauss))įurther, we take the y-axis data (y_array_2gauss) and subtract the fit from it (_2gaussian(x_array, *popt_2gauss)), and assign this to residual_2gauss. If we add the following lines of code into our plotting cell, we can plot the two peaks on their own: This is where the gauss_peak_1 and _2 variables come into play. However, we want to be able to see the peaks on their own after they have been separated from one another. If we plot our fake two-gaussian data and the _2gaussian fit, we see that the data (red dots) is traced nicely by the fit (dashed black line). We want to define these variables because we can then feed them into our _1gaussian function to construct data for the stand-alone peaks. The pars variables are arrays which hold just the fitting parameters for the first and second peaks respectively. However, I added a few more lines of code to define pars_1, pars_2, and gauss_peak_1, gauss_peak_2.
Gauss_peak_2 = _1gaussian(x_array, *pars_2) Gauss_peak_1 = _1gaussian(x_array, *pars_1) Perr_2gauss = np.sqrt(np.diag(pcov_2gauss)) Popt_2gauss, pcov_2gauss = _fit(_2gaussian, x_array, y_array_2gauss, p0=)
Just like with the bi-exponential fit we previously investigated, in order to fit overlapping gaussian peaks, we need to define a function for the sum of two gaussians: In my research, I come across this case often when I am looking at Fluorescence spectra of species who emit at similar wavelength, and I am interested in the relative areas under the two curves thus I need to deconvolute (or separate) these two curves from one another. peak center and width) can’t be distinguished from one another. In the fake data above, you can see that there are two gaussian peaks whose features (i.e. We will consider a strongly overlapping region of data: It is extremely helpful to be able to fit several overlapping peaks, because usually spectral features are not well-resolved from one another, and even a portion of the tail of a gaussian curve can skew the fit of another curve if they are overlapping. Now that we can successfully fit a well-resolved single gaussian, peak, lets work on the more complicated case where we have several overlapping peaks which need to be convoluted from one another. This fit does a pretty good job at fitting the fake gaussian data. To do so, just like with linear or exponential curves, we define a fitting function which we will feed into a scipy function to fit the fake data:Īnd then plot our data along with the fit: We then want to fit this peak to a single gaussian curve so that we can extract these three parameters. First I created some fake gaussian data to work with (see notebook and previous post):Īs you can see, this generates a single peak with a gaussian lineshape, with a specific center, amplitude, and width. As was the previous post, this post was designed for the reader to follow along in the notebook, and thus this post will be explaining what each cell does/means instead of telling you what to type for each cell.įirst we will focus on fitting single and multiple gaussian curves. I have added the notebook I used to create this blog post, 181119_PeakFitting, to my GitHub repository which can be found here. With this post, I want to continue to inspire you to ditch the GUIs and use python to work up your data by showing you how to fit spectral peaks with line-shapes and extract an abundance of information to aid in your analysis. As I hope you have seen in Part I of this series, in can be quite empowering to be able to fit all of your data from scratch instead of using peak-fitting software in this case. The abundance of software available to help you fit peaks inadvertently complicate the process by burying the relatively simple mathematical fitting functions under layers of GUI features. Data Fitting in Python Part II: Gaussian & Lorentzian & Voigt Lineshapes, Deconvoluting Peaks, and Fitting Residuals