Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: improve Function.low_pass_filter and adds docs #538

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@
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 @@
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
Loading