Skip to content

fourier_transform.py #1

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

Merged
merged 1 commit into from
Oct 11, 2024
Merged
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
from typing import List, Union

def fourier_transform(signal: List[Union[int, float]]) -> List[complex]:
"""
Compute the discrete Fourier transform (DFT) of a signal.

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

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

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.
"""
n = len(signal)
result = []
for k in range(n):
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)