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: Add STFT function to Function class #620

Merged
merged 23 commits into from
Aug 18, 2024
Merged
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6013462
Add STFT function to Function class
AdvaitChandorkar07 Jun 12, 2024
027e7f0
Added feature: Short-Time Fourier Transform function
AdvaitChandorkar07 Jun 12, 2024
98fbb6d
Added feature: Short-Time Fourier Transform function
AdvaitChandorkar07 Jun 12, 2024
f61f681
Merge branch 'develop' into feature/stft-function
AdvaitChandorkar07 Jul 12, 2024
4f682bc
"Variable name changes in stft"
AdvaitChandorkar07 Jul 13, 2024
a319a54
"Variable and function name formatting"
AdvaitChandorkar07 Jul 13, 2024
e47ec12
Merge branch 'develop' into feature/stft-function
AdvaitChandorkar07 Jul 13, 2024
257238c
Merge branch 'feature/stft-function' of github.com:AdvaitChandorkar07…
AdvaitChandorkar07 Jul 13, 2024
cd35030
"Better Example"
AdvaitChandorkar07 Jul 14, 2024
511f000
Add STFT function to Function class
AdvaitChandorkar07 Jun 12, 2024
a2d373f
Added feature: Short-Time Fourier Transform function
AdvaitChandorkar07 Jun 12, 2024
5f60c54
Added feature: Short-Time Fourier Transform function
AdvaitChandorkar07 Jun 12, 2024
236552a
"Variable name changes in stft"
AdvaitChandorkar07 Jul 13, 2024
f141fe2
"Variable and function name formatting"
AdvaitChandorkar07 Jul 13, 2024
efc2583
"Better Example"
AdvaitChandorkar07 Jul 14, 2024
c4b7f62
Merge branch 'feature/stft-function' of github.com:AdvaitChandorkar07…
AdvaitChandorkar07 Jul 18, 2024
03faf61
Fixed the doctest
AdvaitChandorkar07 Jul 18, 2024
3c7f548
"Spectrogram example"
AdvaitChandorkar07 Jul 25, 2024
c5d2b96
Merge branch 'develop' into feature/stft-function
AdvaitChandorkar07 Jul 25, 2024
fee5e71
Merge branch 'develop' into feature/stft-function
Gui-FernandesBR Aug 4, 2024
403b5f6
Merge branch 'develop' into feature/stft-function
Gui-FernandesBR Aug 13, 2024
5f23551
small fixes to STFT function
Gui-FernandesBR Aug 18, 2024
526ab11
Merge branch 'develop' into feature/stft-function
Gui-FernandesBR Aug 18, 2024
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
73 changes: 73 additions & 0 deletions rocketpy/mathutils/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,79 @@ def to_frequency_domain(self, lower, upper, sampling_frequency, remove_dc=True):
extrapolation="zero",
)

def to_stft(
self, lower, upper, sampling_frequency, window_size, step_size, remove_dc=True
):
Gui-FernandesBR marked this conversation as resolved.
Show resolved Hide resolved
'''
Performs the Short-Time Fourier Transform (STFT) of the Function and
returns the result. The STFT is computed by applying the Fourier transform
to overlapping windows of the Function.

Parameters
----------
lower : float
Lower bound of the time range.
upper : float
Upper bound of the time range.
sampling_frequency : float
Sampling frequency at which to perform the Fourier transform.
window_size : float
Size of the window for the STFT, in seconds.
step_size : float
Step size for the window, in seconds.
remove_dc : bool, optional
If True, the DC component is removed from each window before computing the Fourier transform.

Returns
-------
list of Function
A list of Functions, each representing the STFT of a window.

Examples
--------
Gui-FernandesBR marked this conversation as resolved.
Show resolved Hide resolved
>>> from rocketpy import Function
>>> import numpy as np
>>> main_frequency = 10 # Hz
>>> time = np.linspace(0, 10, 1000)
>>> signal = np.sin(2 * np.pi * main_frequency * time)
>>> time_domain = Function(np.array([time, signal]).T)
>>> stft_result = time_domain.to_stft(
... lower=0, upper=10, sampling_frequency=100,
... window_size=1, step_size=0.1
... )
>>> for window in stft_result:
... peak_frequencies_index = np.where(window[:, 1] > 0.001)
... peak_frequencies = window[peak_frequencies_index, 0]
>>> print(peak_frequencies)
[[-10. 10.]]
Gui-FernandesBR marked this conversation as resolved.
Show resolved Hide resolved
'''
# Get the time domain data
sampling_time_step = 1.0 / sampling_frequency
sampling_range = np.arange(lower, upper, sampling_time_step)
sampled_points = self(sampling_range)
window_samples = int(window_size * sampling_frequency)
step_samples = int(step_size * sampling_frequency)
Gui-FernandesBR marked this conversation as resolved.
Show resolved Hide resolved

stft_results = []
for start in range(0, len(sampled_points) - window_samples + 1, step_samples):
windowed_samples = sampled_points[start : start + window_samples]
if remove_dc:
windowed_samples -= np.mean(windowed_samples)
Gui-FernandesBR marked this conversation as resolved.
Show resolved Hide resolved
fourier_amplitude = np.abs(
np.fft.fft(windowed_samples) / (window_samples / 2)
)
fourier_frequencies = np.fft.fftfreq(window_samples, sampling_time_step)
stft_results.append(
Function(
source=np.array([fourier_frequencies, fourier_amplitude]).T,
inputs="Frequency (Hz)",
outputs="Amplitude",
interpolation="linear",
extrapolation="zero",
)
)
return stft_results

def low_pass_filter(self, alpha, file_path=None):
"""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
Expand Down
Loading