How to extract the values
of the filtered signal
from Fourier transformation
? As ifft
returns a complex number which is hard to for further calculations.
My python code:
import numpy as np
# Create a simple signal with two frequencies
dt = 0.001
t = np.arange(0,1,dt)
f = np.sin(2*np.pi*50*t) + np.sin(2*np.pi*120*t) # Sum of 2 frequencies
f_clean = f
noise = 2.5*np.random.randn(len(t))
f = f + noise # Add some noise
## Compute the Fast Fourier Transform (FFT)
n = len(t)
fhat = np.fft.fft(f,n) # Compute the FFT
PSD = fhat * np.conj(fhat) / n # Power spectrum (power per freq)
freq = (1/(dt*n)) * np.arange(n) # Create x-axis of frequencies in Hz
L = np.arange(1,np.floor(n/2),dtype='int') # Only plot the first half of freqs
## Use the PSD to filter out noise
indices = PSD > 100 # Find all freqs with large power
PSDclean = PSD * indices # Zero out all others
fhat = indices * fhat # Zero out small Fourier coeffs. in Y
ffilt = np.fft.ifft(fhat) # Inverse FFT for filtered time signal
Here ffilt
is the filtered signal which returns a complex number
. I want to use this signal for mathematical computation but not sure on the process of extracting the values.
Below you can see how to acquire real values from a complex number, specifically its real and imaginary parts, and its magnitude.
a = ffilt[10]
# (1.09200370931126+4.0997278010904346e-17j)
# Get real part
a.real
# 1.09200370931126
# Get imaginary part
a.imag
# 4.0997278010904346e-17
# Get magnitude
abs(a)
# 1.09200370931126
# (the imaginary part is close to zero, so the magnitude
# is almost equal to the real part)
You can do the above for the whole array.
ffilt.real
ffilt.imag
abs(ffilt)
# Bonus: phase
np.angle(ffilt)
I don't know what your next calculations will be, but you probably want to use the magnitude (abs).