{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Use of normalisation function\n", "\n", "In this example we normalise a spectrum to different values: area, maximum, or min-max.\n", "\n", "## Signal creation\n", "\n", "Below we create a fake Gaussian signal for the example." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# we load the necessary libraries\n", "%matplotlib inline\n", "import numpy as np\n", "import scipy\n", "import rampy\n", "\n", "# the following code is for the rendering of plotly graphs in Rampy docs, you don't need it\n", "# Set renderer for Jupyter notebooks\n", "import plotly.io as pio\n", "pio.renderers.default = 'notebook'\n", "\n", "# and we create the signal\n", "nb_points =100\n", "x = np.sort(np.random.uniform(0,100,nb_points)) # increasing point\n", "y = 120.0*scipy.stats.norm.pdf(x,loc=50,scale=5)\n", "\n", "fig = rampy.plot_spectrum(x, y, title = \"our spectrum\", xaxis_title=\"X\", yaxis_title=\"Y\")\n", "fig.show()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We can consider that the area of the Gaussian peak should be equal to 1, as it is the value of the intergral of a Gaussian distribution.\n", "\n", "To normalise the spectra, we can do:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "y_norm_ = rampy.normalise(y, x=x, method=\"area\")\n", "\n", "fig = rampy.plot_spectrum(x, y_norm_, title = \"Area normalisation\", xaxis_title=\"X\", yaxis_title=\"Y\")\n", "fig.show()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We could also just want the signal to be comprised between 0 and 1, so we normalise to the maximum:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "y_norm_ = rampy.normalise(y,method=\"intensity\")\n", "\n", "fig = rampy.plot_spectrum(x, y_norm_, title = \"Intensity normalisation\", xaxis_title=\"X\", yaxis_title=\"Y\")\n", "fig.show()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Now, if our signal intensity was shifted from 0 by a constant, the \"intensity\" method will not work well. For instance, I can add 0.1 to `y` and plot it." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "y2 = y + 1\n", "\n", "fig = rampy.plot_spectrum(x, y2, title = \"Shifted signal\", xaxis_title=\"X\", yaxis_title=\"Y\")\n", "fig.show()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "In this case, the \"intensity\" method will not work well:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "y_norm_ = rampy.normalise(y2,method=\"intensity\")\n", "\n", "fig = rampy.plot_spectrum(x, y_norm_, title = \"Shifted signal normalised\", xaxis_title=\"X\", yaxis_title=\"Y\")\n", "fig.show()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The signal remains shifted from 0. For safety, we can do a min-max normalisation, which will put the minimum to 0 and maximum to 1:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "y_norm_ = rampy.normalise(y2,method=\"minmax\")\n", "\n", "fig = rampy.plot_spectrum(x, y_norm_, title = \"Min-max normalisation\", xaxis_title=\"X\", yaxis_title=\"Y\")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "gpvisc", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }