3
$\begingroup$

As we all know, Fourier transform of the sinc(t) is rect(f).
Simple python script to discover signal after fft and ifft with random normal noise.

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1234)

peak=np.fft.fft(np.sinc(np.linspace(-4, 4, 200)))
peak_ifft=np.fft.ifft(peak)
n=200
noise=np.random.normal(loc=0, scale=np.sqrt(2)/2, size=(n, 2)).view(np.complex128)
signal=noise+np.reshape(np.asarray(peak),(-1,1))
plt.figure()
plt.subplot(2,2,1)
plt.plot(np.abs(signal))
plt.title('power vs freq')
plt.subplot(2,2,2)
signal_ifft=np.fft.ifft(signal)
plt.plot(signal_ifft)
plt.title('should appear sinc,but not')
plt.subplot(2,2,3)
plt.plot(peak_ifft)
plt.title('should be sinc,and yes')
plt.show()

Output as below:
enter image description here

As x(t)+y(t)<->X(f)+Y(f),np.fft.ifft(np.fft(signal)) go back to origin signal,why cannot np.fft.ifft(np.fft(signal+noise))?

$\endgroup$
2
  • 2
    $\begingroup$ you don't add, you concatenate, completely different operations. $\endgroup$ Mar 8, 2022 at 16:10
  • 1
    $\begingroup$ Can you please confirm that you want to use np.concatenate, and that you have indeed read and understood it's documentation? $\endgroup$ Mar 8, 2022 at 16:28

1 Answer 1

4
$\begingroup$

You want to just do signal + noise, and you'll be fine for the noise part.

For the plot, you need the absolute of the fft, you're just plotting the real part.

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1234)
# make data
x = np.linspace(-4, 4, 200)
y = np.sinc(x)
# make fft and ifft
peak = np.fft.fft(y)
peak_ifft = np.fft.ifft(peak)
# make the frequency domain
freq = np.fft.fftfreq(len(x), np.diff(x).mean())
# make some noise!!!
n = 200
noise = np.random.normal(loc=0, scale=0.1, size=n)
# signal with noise, and the fft and ifft
signal = y + noise
peak2 = np.fft.fft(signal)
peak_ifft2 = np.fft.ifft(peak2)
# plotting
plt.figure()
plt.plot(x, y)
plt.plot(x, peak_ifft, ':')
plt.figure()
plt.plot(freq, np.abs(peak))
plt.xlim([-5,5])
plt.figure()
plt.plot(x, signal)
plt.plot(x, peak_ifft2, ':')
plt.figure()
plt.plot(freq, np.abs(peak2))
plt.xlim([-5.,5])

enter image description here

$\endgroup$
3
  • $\begingroup$ when you use add,get the similar output $\endgroup$
    – kittygirl
    Mar 8, 2022 at 18:11
  • $\begingroup$ I've refined my answer to be more explanatory and to have a full example. Does that help? $\endgroup$ Mar 8, 2022 at 18:11
  • 1
    $\begingroup$ Really thanks for your clear answer. $\endgroup$
    – kittygirl
    Mar 8, 2022 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.