Last active
March 11, 2021 08:56
-
-
Save maxrohleder/2df12e642375ee80036200f379974463 to your computer and use it in GitHub Desktop.
Square signal without for loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# first define our input values and all values for k | |
t = np.linspace(0, 1, 200) | |
k = np.arange(0, 1000) | |
f = 10 # frequency in Hertz | |
# from the formula, isolate all factors in the sinus term which include k | |
k = 2 * np.pi * (2 * k - 1) * f | |
# then construct a helper matrix in shape (200, 1000) | |
tk = np.outer(t, k) | |
# apply the rest of the multiplicative terms in the sum | |
F = np.sin(tk) / (2 * k - 1) | |
# then sum along the k axis, resulting in an array (200,) | |
res = np.sum(F, axis=1) | |
res *= (4 / np.pi) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment