Last active
December 18, 2015 09:49
-
-
Save hayd/5764513 to your computer and use it in GitHub Desktop.
spectral stuff
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 | |
from numpy.fft import fft, fftshift, ifft, fftfreq | |
L = 10 | |
n = 128 | |
x2 = np.linspace(-L, L, n + 1) | |
x = x2[1:] | |
# k = (2 * np.pi / L) * np.concatenate((np.arange(0, n/2), np.arange(-n/2 + 1, 1))) | |
k = fftfreq(n) * L | |
u = np.exp(-x**2) | |
ut = fft(u) | |
# plot these on same graph | |
#plot(k, fftshift(ut)) # whacky | |
#plot(k, np.abs(fftshift(ut))) | |
#plot(fftshift(k), np.abs(fftshift(ut))) # the spectrum? | |
u2 = sech = 1 / np.cosh(x) | |
u2d1 = -sech * np.tanh(x) # sech' | |
u2d2 = sech - 2 * sech**3 # sech'' | |
u2t = fft(u2) | |
u2d1s = ifft(1j * k * u2t) # spectral approximation, 1j is (0+i) | |
u2d2s = ifft((1j * k)**2 * u2t) | |
# for some reason these are waaay out | |
plot(x, u2d1, 'b', label='"actual" derivative') # "actual" derivative | |
plot(x, u2d1s, 'g', label='spectral estimate') # spectral approximation | |
import matplotlib.pyplot as plt | |
t = np.arange(256) | |
sp = fft(np.sin(t)) | |
freq = np.fft.fftfreq(t.shape[-1]) | |
plt.plot(freq, sp.real, freq, sp.imag) | |
[<matplotlib.lines.Line2D object at 0x...>, <matplotlib.lines.Line2D object at 0x...>] | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment