Skip to content

Commit

Permalink
MNT: improve the low pass filter and document an example
Browse files Browse the repository at this point in the history
- Persist the inputs, outputs and title of the original Function
- Improve the method docstring
- Add an example to the .rst file
  • Loading branch information
Gui-FernandesBR committed Jan 23, 2024
1 parent ead9219 commit 7895a1e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions docs/user/function.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
13 changes: 9 additions & 4 deletions rocketpy/mathutils/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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.
Expand All @@ -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)

Check warning on line 1153 in rocketpy/mathutils/function.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/mathutils/function.py#L1153

Added line #L1153 was not covered by tests

return Function(
source=filtered_signal,
inputs=self.__inputs__,
outputs=self.__outputs__,
interpolation=self.__interpolation__,
extrapolation=self.__extrapolation__,
title=self.title,
)

# Define all presentation methods
Expand Down

0 comments on commit 7895a1e

Please sign in to comment.