From 7895a1e7594194e47f397b7fc194a654f7f2024f Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Mon, 22 Jan 2024 23:30:36 -0500 Subject: [PATCH] MNT: improve the low pass filter and document an example - Persist the inputs, outputs and title of the original Function - Improve the method docstring - Add an example to the .rst file --- CHANGELOG.md | 1 + docs/user/function.rst | 24 ++++++++++++++++++++++++ rocketpy/mathutils/function.py | 13 +++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad4ac536b..d8b82a641 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ straightforward as possible. - MNT: Add repr method to Parachute class [#490](https://github.com/RocketPy-Team/RocketPy/pull/490) - ENH: Function Reverse Arithmetic Priority [#488](https://github.com/RocketPy-Team/RocketPy/pull/488) - DOC: Update Header Related Docs +- MNT: improve the low pass filter and document an example [#538](https://github.com/RocketPy-Team/RocketPy/pull/538) ### Fixed diff --git a/docs/user/function.rst b/docs/user/function.rst index 3b4b66ed0..b95496991 100644 --- a/docs/user/function.rst +++ b/docs/user/function.rst @@ -438,6 +438,30 @@ Let's export the gaussian function to a CSV file: import os os.remove("gaussian.csv") +f. Filter data +~~~~~~~~~~~~~~ + +Since rocketpy version 1.2.0, the ``Function`` class supports filtering the +source data. This is accomplished by the method :meth:`rocketpy.Function.low_pass_filter` +and allows for easy data filtering for further analysis. + +Let's filter an example function: + +.. jupyter-execute:: + + x = np.linspace(-4, 4, 1000) + y = np.sin(x) + np.random.normal(0, 0.1, 1000) + + f = Function(list(zip(x, y)), inputs="x", outputs="f(x)") + + # Filter the function + f_filtered = f.low_pass_filter(0.5) + + # Compare the function with the filtered function + Function.compare_plots( + [(f, "Original"), (f_filtered, "Filtered")], lower=-4, upper=4 + ) + ........ This guide shows some of the capabilities of the ``Function`` class, but there are many other functionalities to enhance your analysis. Do not hesitate in tanking a look at the documentation :class:`rocketpy.Function`. diff --git a/rocketpy/mathutils/function.py b/rocketpy/mathutils/function.py index f1b295e99..52ac95ea2 100644 --- a/rocketpy/mathutils/function.py +++ b/rocketpy/mathutils/function.py @@ -1118,7 +1118,10 @@ def to_frequency_domain(self, lower, upper, sampling_frequency, remove_dc=True): ) def low_pass_filter(self, alpha, file_path=None): - """Implements a low pass filter with a moving average filter + """Implements a low pass filter with a moving average filter. This does + not mutate the original Function object, but returns a new one with the + filtered source. The filtered source is also saved to a CSV file if a + file path is given. Parameters ---------- @@ -1128,7 +1131,7 @@ def low_pass_filter(self, alpha, file_path=None): filtered function returned will match the function the smaller alpha is, the smoother the filtered function returned will be (but with a phase shift) - file_path : string + file_path : string, optional File path or file name of the CSV to save. Don't save any CSV if if no argument is passed. Initiated to None. @@ -1146,14 +1149,16 @@ def low_pass_filter(self, alpha, file_path=None): alpha * self.source[i] + (1 - alpha) * filtered_signal[i - 1] ) - # Save the new csv file with filtered data if isinstance(file_path, str): - np.savetxt(file_path, filtered_signal, delimiter=",") + self.savetxt(file_path) return Function( source=filtered_signal, + inputs=self.__inputs__, + outputs=self.__outputs__, interpolation=self.__interpolation__, extrapolation=self.__extrapolation__, + title=self.title, ) # Define all presentation methods