Skip to content

Fourier_Transform.py #11995

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

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 34 additions & 0 deletions maths/Transforms/Fourier_Transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np

Check failure on line 1 in maths/Transforms/Fourier_Transform.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

maths/Transforms/Fourier_Transform.py:1:1: INP001 File `maths/Transforms/Fourier_Transform.py` is part of an implicit namespace package. Add an `__init__.py`.
from typing import List, Union

Check failure on line 2 in maths/Transforms/Fourier_Transform.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

maths/Transforms/Fourier_Transform.py:2:1: UP035 `typing.List` is deprecated, use `list` instead

def fourier_transform(signal: List[Union[int, float]]) -> List[complex]:

Check failure on line 4 in maths/Transforms/Fourier_Transform.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

maths/Transforms/Fourier_Transform.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 4 in maths/Transforms/Fourier_Transform.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

maths/Transforms/Fourier_Transform.py:4:31: UP006 Use `list` instead of `List` for type annotation

Check failure on line 4 in maths/Transforms/Fourier_Transform.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP007)

maths/Transforms/Fourier_Transform.py:4:36: UP007 Use `X | Y` for type annotations

Check failure on line 4 in maths/Transforms/Fourier_Transform.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

maths/Transforms/Fourier_Transform.py:4:59: UP006 Use `list` instead of `List` for type annotation
"""
Compute the discrete Fourier transform (DFT) of a signal.

Args:
signal (List[Union[int, float]]): A list of numerical values representing the input signal.

Check failure on line 9 in maths/Transforms/Fourier_Transform.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/Transforms/Fourier_Transform.py:9:89: E501 Line too long (95 > 88)

Returns:
List[complex]: The Fourier transform of the input signal as a list of complex numbers.

Check failure on line 12 in maths/Transforms/Fourier_Transform.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/Transforms/Fourier_Transform.py:12:89: E501 Line too long (90 > 88)

Example:
>>> fourier_transform([1, 2, 3, 4])
[(10+0j), (-2+2j), (-2+0j), (-2-2j)]

Note:
This is a basic implementation of the DFT and can be optimized using FFT for larger datasets.

Check failure on line 19 in maths/Transforms/Fourier_Transform.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/Transforms/Fourier_Transform.py:19:89: E501 Line too long (97 > 88)
"""
n = len(signal)
result = []
for k in range(n):

Check failure on line 23 in maths/Transforms/Fourier_Transform.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

maths/Transforms/Fourier_Transform.py:23:23: W291 Trailing whitespace
summation = 0 + 0j
for t in range(n):
angle = -2j * np.pi * t * k / n
summation += signal[t] * np.exp(angle)
result.append(summation)
return result

if __name__ == "__main__":
sample_signal = [1, 2, 3, 4]
result = fourier_transform(sample_signal)
print("Fourier Transform of the signal:", result)
Loading