Last active
November 5, 2022 17:34
-
-
Save binary10/941db9d7bae3ecf1ae40c5104babf8b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Compute the convolution of a signal with itself, | |
| # then use the fft of the signal to arrive at the same convolution. | |
| import numpy as np | |
| import scipy as sp | |
| # Initialize a signal | |
| v = np.random.randint(0,255, 2**5) | |
| # Convolve with self | |
| cv = sp.convolve(v,v) | |
| # Copy and pad signal | |
| vv = v.copy() | |
| vv.resize(len(cv), refcheck=False) | |
| # Compute FFT | |
| fv = sp.fft(vv) | |
| # Inverse FFT of frequency signal | |
| fcv = sp.ifft(fv * fv) | |
| # Compare | |
| abs(fcv - cv) < .0001 |
Author
@timviera I'm glad it helped you -- thanks for sharing back. I especially enjoyed learning about np.allclose()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this was helpful - thanks!
I tweaked it to support the convolution of any two vectors (of possibly different lengths).