Rlz
Rlz

Reputation: 1689

Python - Inverse Fourier Transform

Problem

I am trying to remove a frequency from a set of data obtained from an audio file.

To simplify down my problem, I have created the code below which creates a set of waves and merges them into a complex wave. Then it finds the fourier transform of this complex wave and inverses it.

I am expecting to see the original wave as a result since there should be no data loss, however I receive a very different wave instead.

Code:

import numpy as np
import matplotlib.pyplot as plt
import random

#Get plots
fig, c1 = plt.subplots()
c2 = c1.twinx()

fs = 100 # sample rate 
f_list = [5,10,15,20,100] # the frequency of the signal

x = np.arange(fs) # the points on the x axis for plotting

# compute the value (amplitude) of the sin wave for each sample
wave = []
for f in f_list:
    wave.append(list(np.sin(2*np.pi*f * (x/fs))))

#Adds the sine waves together into a single complex wave
wave4 = []
for i in range(len(wave[0])):
    data = 0
    for ii in range(len(wave)):
        data += wave[ii][i]
    wave4.append(data)

#Get frequencies from complex wave
fft = np.fft.rfft(wave4)
fft = np.abs(fft)

#Note: Here I will add some code to remove specific frequencies

#Get complex wave from frequencies
waveV2 = np.fft.irfft(fft)

#Plot the complex waves, should be the same
c1.plot(wave4, color="orange")
c1.plot(waveV2)
plt.show()

Results: (Orange is created wave, blue is original wave)

Results

Expected results:

The blue and orange lines (the original and new wave created) should have the exact same values

Upvotes: 1

Views: 3957

Answers (1)

JRE
JRE

Reputation: 357

You took the absolute value of the FFT before you do the inverse FFT. That changes things, and is probably the cause of your problem.

Upvotes: 2

Related Questions