Arithmetic Operations#
This example demonstrates basic arithmetic operations using SARComp’s mathematical functions.
import numpy as np
import sarcomp as sc
# Mul, Add and Sub example
size = 1024
arr0 = np.arange(size).astype('f4')
arr1 = np.arange(size).astype('f4')
mul = sc.math.mul(arr0, arr1)
add = sc.math.add(arr0, arr1)
sub = sc.math.sub(arr0, arr1)
Complex Exponential Calculation#
In this example, we calculate the complex exponential of a phase array using SARComp.
import numpy as np
import sarcomp as sc
# Calculate phase
size = 1000
array = np.random.rand(size).astype('f4')
iexp = sc.math.iexp(array) # equivalent to e^(1j*array)
Fast Fourier Transform (FFT)#
This tutorial demonstrates how to perform a Fast Fourier Transform (FFT) on two test signals using SARComp.
import numpy as np
from sarcomp import fft
import matplotlib.pyplot as plt
# Generate a test signal
size = 1024
fs = size // 2 # in Hz
n = fs * 2 # 2 seconds
t = np.arange(0, n) / fs
# Frequencies and amplitudes for the first signal
freqs = [12, 90, 150]
amps = [10, 15, 20]
# Frequencies and amplitudes for the second signal
freqs2 = [14, 80, 130]
amps2 = [25, 30, 35]
# Create two zero-initialized complex signals
signal = np.zeros(n, dtype='complex128')
signal2 = np.zeros(n, dtype='complex128')
# Loop over frequencies to create the first signal
for amp, freq in zip(amps, freqs):
signal += amp * np.cos(2 * np.pi * freq * t) + 1j * amp * np.sin(2 * np.pi * freq * t)
# Loop over frequencies to create the second signal
for amp, freq in zip(amps2, freqs2):
signal2 += amp * np.cos(2 * np.pi * freq * t) + 1j * amp * np.sin(2 * np.pi * freq * t)
# Sarcomp FFT always requires a plan
fft_plan = fft.fft(signal)
# Execute the FFT plan on the first signal
res = fft_plan()
# Create a new FFT plan for the second signal (if required)
fft_plan2 = fft.fft(signal2)
res2 = fft_plan2()
# Get the amplitude spectrum for both signals
amplitude_spectrum = np.abs(res)
amplitude_spectrum2 = np.abs(res2)
# Get the frequency bins
freqs = np.fft.fftfreq(len(signal), 1/fs)
# Only plot the positive frequencies
positive_freqs = freqs[:len(freqs)//2]
positive_amplitude_spectrum = amplitude_spectrum[:len(amplitude_spectrum)//2]
positive_amplitude_spectrum2 = amplitude_spectrum2[:len(amplitude_spectrum2)//2]
# Plot the amplitude spectrum
plt.figure(figsize=(12, 6))
plt.plot(positive_freqs, positive_amplitude_spectrum, label="Signal 1")
plt.plot(positive_freqs, positive_amplitude_spectrum2, label="Signal 2")
plt.title('Amplitude Spectrum')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.grid(True)
plt.legend()
plt.show()