title: How to convert from frequency domain to time domain in python
body: <p>I know this is basic for signal processing, but, I am not sure what is wrong about my approach. I have a signal that behaves as damped sine signal with a sampling frequency of 5076Hz and 15,000 number of samples. I found from the following website how to convert a signal from a time domain to frequency domain and managed to get the FFT and frequency values. The code can be found below the link:</p>
  <p><a href="http://ataspinar.com/2018/04/04/machine-learning-with-signal-processing-techniques/" rel="nofollow noreferrer">Machine Learning with Signal Processing Techniques</a></p>
  <pre><code>def get_fft_values(y_values, T_s, N, f_s):
      f_values = np.linspace(0.0, 1.0/(2.0*T), N//2)
      fft_values_ = np.fft.rfft(y_values)
      fft_values = 2.0/N * np.abs(fft_values_[0:N//2])
      return f_values, fft_values
  </code></pre>
  <p>I managed to get the frequency and FFT values. However, I need to implement filters to remove some noise from the signal, so, I created the following functions to implement the filter part:</p>
  <pre><code>def butter_bandpass(lowcut, highcut, fs, order):
      nyq = 0.5 * fs
      low = lowcut / nyq
      high = highcut / nyq
      b, a = butter(order, [low, high], btype='bandpass', output='ba')
      return b, a
  
  def butter_bandpass_filter(data, lowcut, highcut, fs, order):
      b, a = butter_bandpass(lowcut, highcut, fs, order=order)
      y = filtfilt(b=b, a=a, x=data)
      # y = lfilter(b=b, a=a, x=data)
      return y
  </code></pre>
  <p>I know that I would need to implement the following steps:</p>
  <ul>
  <li>Convert to the frequency domain</li>
  <li>apply a bandpass filter to get rid of frequencies you don't care
  about</li>
  <li>convert back to the time domain by inverse Fourier transform</li>
  </ul>
  <p>So, I created the following inverse transform function, but, I can't get the filtered signal back and the amplitudes don't almost match the original signal.
  (For my case, I need to resample)</p>
  <pre><code>def get_ifft_values(fft_values, T, N, f_s):
      # Time axis:
      N = 9903
      S_T = 1 / S_F
      t_n = S_T * N  # seconds of sampling
      # Obtaining data in order to plot the graph:
      x_time = np.linspace(0, t_n, N)
      ifft_val = np.fft.irfft(fft_values, n=N)
      y_s, x_time = scipy.signal.resample(x=ifft_val, num=N, t=x_time)
      return x_time, y_s
  </code></pre>
  <p>What am I doing wrong here?</p>
  <p><strong>Edit 1:</strong></p>
  <p>Based on the answer from @Han-Kwang Nienhuys. I edited the above code and applied it to the approach below:</p>
  <pre><code>##### Converting the signal into fft:
  f_val, fft_val = get_fft_values(y_values=y, T=S_T, N=N, f_s=S_F)
  # Applying bandpass filter:
  fft_filt_val = butter_bandpass_filter(data=fft_val, lowcut=50, highcut=600, fs=S_F, order=2)
  # Applying the inverse transform of the frequency domain:
  x_time, y = get_ifft_values(fft_values=fft_filt_val, T=S_T, N=N, f_s=S_F)
  </code></pre>
  <p>Here are the results from the signal:</p>
  <ul>
  <li>FFT of the original signal:</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/uHZS8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uHZS8.png" alt="FFT of the original signal" /></a></p>
  <ul>
  <li>Filtered FFT of the original signal:</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/Ltkui.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ltkui.png" alt="Filtered FFT from FFT values" /></a></p>
  <ul>
  <li>Converted Signal from Filtered FFT:</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/zqmBa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zqmBa.png" alt="Converted Signal from Filtered FFT" /></a></p>
  <ul>
  <li>Without Applying the bandpass filter:</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/ClNzh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ClNzh.png" alt="Without Applying the bandpass filter" /></a></p>