Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions algorithms/python/fft.ipynb

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions algorithms/python/fft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import matplotlib.pylab as plt
import numpy as np
from random import uniform

# sampling rate (must be of base 2)
sr = 1024
# sampling interval
ts = 1/sr
t = np.arange(-0.5, 0.5, ts)

old_freqs = []
def randomSine():
# frequency of the signal
freq = uniform(1, 500)
randPhase = uniform(0,1)

# prevent from picking the same frequencies
while freq in old_freqs:
freq = uniform(1, 100)
old_freqs.append(freq)

t = np.arange(randPhase, randPhase+1, ts)
return np.sin(2*np.pi*freq*t)

def sum_curves(curves: list[list]):
summed = []
for i in range(sr):
sm = 0
for curve in curves:
sm += curve[i]
summed.append(sm)
return summed

complexJ = 0 + 1j
def ditff2(x: list) -> list:
n = len(x)
if n == 1:
return x
wn = np.e ** ((2*np.pi*complexJ)/n)
w = 1
half = int(n/2)
halfLeft = [even for (index, even) in enumerate(x[:-1]) if index % 2 == 0]
halfRight = [odd for (index, odd) in enumerate(x) if not index % 2 == 0]

yk_0 = ditff2(halfLeft)
yk_1 = ditff2(halfRight)
y = [None] * n
for i in range(half):
y[i] = yk_0[i] + w * yk_1[i]
y[i+half] = yk_0[i] - w * yk_1[i]
w = w*wn
return y

# number of peaks you want to see in fft
# number of different frequencies on the curve
N = 20
# Generate a radom curve with different frequencies
input = sum_curves([randomSine() for _ in range(N)])

fft = ditff2(input)

## plot stuff
fig, ax = plt.subplots()
bx = ax.twinx()

plt_1 = ax.plot(t, input, 'r')
plt_2 = bx.plot(t, fft)

# plot input
_, bx = plt.subplots()
bx.plot(t, input)

#Plot FFT
_, bx = plt.subplots()
half = len(fft)//2
bx.plot(t, fft[half:] + fft[:half])
plt.plot()

# numpy fft
sp = np.fft.fft(input)
freq = np.fft.fftfreq(t.shape[-1])
plt.plot(freq, sp.real)

plt.legend()
plt.show()
85 changes: 85 additions & 0 deletions algorithms/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
anyio==3.6.2
appnope==0.1.3
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
asttokens==2.1.0
attrs==22.1.0
autopep8==2.0.0
backcall==0.2.0
beautifulsoup4==4.11.1
bleach==5.0.1
cffi==1.15.1
contourpy==1.0.6
cycler==0.11.0
debugpy==1.6.3
decorator==5.1.1
defusedxml==0.7.1
entrypoints==0.4
executing==1.2.0
fastjsonschema==2.16.2
fonttools==4.38.0
idna==3.4
importlib-metadata==5.1.0
ipykernel==6.17.1
ipython==8.6.0
ipython-genutils==0.2.0
ipywidgets==8.0.2
jedi==0.18.2
Jinja2==3.1.2
jsonschema==4.17.1
jupyter==1.0.0
jupyter-console==6.4.4
jupyter-server==1.23.3
jupyter_client==7.4.7
jupyter_core==5.0.0
jupyterlab-pygments==0.2.2
jupyterlab-widgets==3.0.3
kiwisolver==1.4.4
MarkupSafe==2.1.1
matplotlib==3.6.2
matplotlib-inline==0.1.6
mistune==2.0.4
nbclassic==0.4.8
nbclient==0.7.0
nbconvert==7.2.5
nbformat==5.7.0
nest-asyncio==1.5.6
notebook==6.5.2
notebook_shim==0.2.2
numpy==1.23.5
packaging==21.3
pandocfilters==1.5.0
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.3.0
platformdirs==2.5.4
prometheus-client==0.15.0
prompt-toolkit==3.0.33
psutil==5.9.4
ptyprocess==0.7.0
pure-eval==0.2.2
pycodestyle==2.10.0
pycparser==2.21
Pygments==2.13.0
pyparsing==3.0.9
pyrsistent==0.19.2
python-dateutil==2.8.2
pyzmq==24.0.1
qtconsole==5.4.0
QtPy==2.3.0
Send2Trash==1.8.0
six==1.16.0
sniffio==1.3.0
soupsieve==2.3.2.post1
stack-data==0.6.1
terminado==0.17.0
tinycss2==1.2.1
tomli==2.0.1
tornado==6.2
traitlets==5.5.0
wcwidth==0.2.5
webencodings==0.5.1
websocket-client==1.4.2
widgetsnbextension==4.0.3
zipp==3.11.0