weilueluo
weilueluo

Reputation: 673

why my 1d fourier transform gives identical output

I am trying to implement fourier transform according to the following formula from Wikipedia:

enter image description here

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:

enter image description here

But I was expecting something similar to the result of np.fft.fft():

enter image description here

May I know what did I do wrong here? Thank you!

Upvotes: 0

Views: 205

Answers (1)

weilueluo
weilueluo

Reputation: 673

Thanks for the comments, I misread the formula, I should be implementing the discrete fourier transform instead:

enter image description here

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:

enter image description here

Upvotes: 1

Related Questions