# ulab brings numpy/scipy-like functionality to MicroPython
# Documentation: https://micropython-ulab.readthedocs.io/en/latest/
import ulab
from ulab import numpy as np
print('Running ulab version', ulab.__version__)
print()
# Sample rate and signal duration
sample_rate = 1000 # Hz
duration = 1 # second
# Time array
t = np.linspace(0, duration, sample_rate * duration, endpoint=False)
# Generating a sample signal: combination of two sine waves
frequency1 = 5 # Hz
frequency2 = 50 # Hz
signal = np.sin(2 * np.pi * frequency1 * t) + np.sin(2 * np.pi * frequency2 * t)
# Perform FFT
fft_result = np.fft.fft(signal)
# Compute the frequency bins
freq_bins = np.fft.fftfreq(len(signal), 1 / sample_rate)
# Calculate the magnitude (absolute value) of the FFT
fft_magnitude = np.abs(fft_result)
# Optional: Limit the output to the positive frequencies
half_point = len(freq_bins) // 2
freq_bins = freq_bins[:half_point]
fft_magnitude = fft_magnitude[:half_point]
# Print the results
print("Frequency Bins:")
print(freq_bins)
print()
print("FFT Magnitude:")
print(fft_magnitude)
print()