Skip to content

Commit 08d3a42

Browse files
Merge pull request #538 from RocketPy-Team/enh/minor_adjst_function_filters
ENH: improve Function.low_pass_filter and adds docs
2 parents ead9219 + 7895a1e commit 08d3a42

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ straightforward as possible.
4040
- MNT: Add repr method to Parachute class [#490](https://github.com/RocketPy-Team/RocketPy/pull/490)
4141
- ENH: Function Reverse Arithmetic Priority [#488](https://github.com/RocketPy-Team/RocketPy/pull/488)
4242
- DOC: Update Header Related Docs
43+
- MNT: improve the low pass filter and document an example [#538](https://github.com/RocketPy-Team/RocketPy/pull/538)
4344

4445
### Fixed
4546

docs/user/function.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,30 @@ Let's export the gaussian function to a CSV file:
438438
import os
439439
os.remove("gaussian.csv")
440440

441+
f. Filter data
442+
~~~~~~~~~~~~~~
443+
444+
Since rocketpy version 1.2.0, the ``Function`` class supports filtering the
445+
source data. This is accomplished by the method :meth:`rocketpy.Function.low_pass_filter`
446+
and allows for easy data filtering for further analysis.
447+
448+
Let's filter an example function:
449+
450+
.. jupyter-execute::
451+
452+
x = np.linspace(-4, 4, 1000)
453+
y = np.sin(x) + np.random.normal(0, 0.1, 1000)
454+
455+
f = Function(list(zip(x, y)), inputs="x", outputs="f(x)")
456+
457+
# Filter the function
458+
f_filtered = f.low_pass_filter(0.5)
459+
460+
# Compare the function with the filtered function
461+
Function.compare_plots(
462+
[(f, "Original"), (f_filtered, "Filtered")], lower=-4, upper=4
463+
)
464+
441465
........
442466

443467
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`.

rocketpy/mathutils/function.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,10 @@ def to_frequency_domain(self, lower, upper, sampling_frequency, remove_dc=True):
11181118
)
11191119

11201120
def low_pass_filter(self, alpha, file_path=None):
1121-
"""Implements a low pass filter with a moving average filter
1121+
"""Implements a low pass filter with a moving average filter. This does
1122+
not mutate the original Function object, but returns a new one with the
1123+
filtered source. The filtered source is also saved to a CSV file if a
1124+
file path is given.
11221125
11231126
Parameters
11241127
----------
@@ -1128,7 +1131,7 @@ def low_pass_filter(self, alpha, file_path=None):
11281131
filtered function returned will match the function the smaller
11291132
alpha is, the smoother the filtered function returned will be
11301133
(but with a phase shift)
1131-
file_path : string
1134+
file_path : string, optional
11321135
File path or file name of the CSV to save. Don't save any CSV if
11331136
if no argument is passed. Initiated to None.
11341137
@@ -1146,14 +1149,16 @@ def low_pass_filter(self, alpha, file_path=None):
11461149
alpha * self.source[i] + (1 - alpha) * filtered_signal[i - 1]
11471150
)
11481151

1149-
# Save the new csv file with filtered data
11501152
if isinstance(file_path, str):
1151-
np.savetxt(file_path, filtered_signal, delimiter=",")
1153+
self.savetxt(file_path)
11521154

11531155
return Function(
11541156
source=filtered_signal,
1157+
inputs=self.__inputs__,
1158+
outputs=self.__outputs__,
11551159
interpolation=self.__interpolation__,
11561160
extrapolation=self.__extrapolation__,
1161+
title=self.title,
11571162
)
11581163

11591164
# Define all presentation methods

0 commit comments

Comments
 (0)