diff --git a/contents/convolutions/convolutional_theorem/code/python/convolutional_theorem.py b/contents/convolutions/convolutional_theorem/code/python/convolutional_theorem.py new file mode 100644 index 000000000..f64f44a1d --- /dev/null +++ b/contents/convolutions/convolutional_theorem/code/python/convolutional_theorem.py @@ -0,0 +1,19 @@ +from scipy.fft import fft, ifft +import numpy as np + +# using the convolutional theorem +def convolve_fft(signal1, signal2): + return ifft(np.multiply(fft(signal1),fft(signal2))) + +# Sawtooth functions +x = [float(i)/200 for i in range(1,101)] +y = [float(i)/200 for i in range(1,101)] + +x /= np.linalg.norm(x) +y /= np.linalg.norm(y) + +# Convolving the two signals +fft_output = convolve_fft(x, y) + +np.savetxt("fft.dat", np.real(fft_output)) + diff --git a/contents/convolutions/convolutional_theorem/convolutional_theorem.md b/contents/convolutions/convolutional_theorem/convolutional_theorem.md index 7aa5c3fdf..1034f2018 100644 --- a/contents/convolutions/convolutional_theorem/convolutional_theorem.md +++ b/contents/convolutions/convolutional_theorem/convolutional_theorem.md @@ -44,6 +44,8 @@ For this example code, we will be using two sawtooth functions as we did in the {% method %} {% sample lang="jl" %} [import, lang:"julia"](code/julia/convolutional_theorem.jl) +{% sample lang="py" %} +[import, lang:"python"](code/python/convolutional_theorem.py) {% endmethod %} This should produce the following output: @@ -52,7 +54,6 @@ This should produce the following output:
-