Reputation: 673
I am trying to implement fourier transform according to the following formula from Wikipedia:
So I do:
import numpy as np
def fft1d(data):
result = []
for i in range(len(data)):
result.append(data[i] * np.exp(-2 * np.pi * 1j * i))
return np.array(result)
and when I give it some random audio:
IPython.display.display(IPython.display.Audio(input_audio, rate=audio_rate))
plt.plot(input_audio)
arr = fft1d(input_audio)
plt.figure()
plt.plot(arr.real)
plt.plot(arr.imag)
I got:
But I was expecting something similar to the result of np.fft.fft()
:
May I know what did I do wrong here? Thank you!
Upvotes: 0
Views: 205
Reputation: 673
Thanks for the comments, I misread the formula, I should be implementing the discrete fourier transform instead:
So:
def fft1d(data):
max_freq = len(data)
result = []
for freq in range(max_freq):
total = 0
for i in range(len(data)):
total += data[i] * np.exp(-1j * 2 * np.pi / len(data) * freq * i)
result.append(total)
return np.array(result)
So now I get the correct result:
Upvotes: 1