FFT: find and cut noisy 50Hz in signal

1.4k Views Asked by At

I have noisy data (peaks with period 1.8s, 2048 bins per period) for which I want to calculate frequency and delete 50Hz. I'm pretty sure that frequency what I looking for is 50Hz, cause I find it by use originlab.

When I try to do the same in python the mean peak is ~47Hz. I'm looking tutorials and examples but the result is the still the same.

import numpy as np
from scipy.fftpack import fft
from scipy.fftpack import fftfreq
import matplotlib.pyplot as plt
data = np.loadtxt('3.dat', comments="#")
t = data[:, 0]
y = data[:, 2]
len_data = len(data)
bins = 2048
plt.figure(figsize=(7, 9))
plt.subplot(211)
plt.plot(t, y, 'b-')
plt.xlabel("time[sec]")
plt.ylabel("original signal")
plt.subplot(212)
F = fft(y)
freq = fftfreq(len(t), (t[1] - t[0]))
ipos = np.where(freq > 0)
freqs = freq[ipos]
mags = np.abs(F[ipos])
plt.plot(freqs, mags, 'b-')
plt.xlabel("freq")
plt.ylabel("POWER")
plt.savefig('stoc.png')
plt.show()

Can someone help me how to fix?

I have to resume the question about cut off noise. When I subtract the frequency, the signal amplitude is significantly decreased. Is this correct?

data = np.loadtxt('3.dat', comments="#")
t = data[:, 0]
phase = data[:, 1]
y = data[:, 2]
pulse_no = data[:, 3]
len_data = len(data)
bins = 2048
ti = np.linspace(t[0], t[-1], len_data)
yi = np.interp(ti, t, y)
t, y = ti, yi

plt.figure(figsize=(10, 10))
plt.subplot(511)
plt.plot(t, y, 'b-')
plt.xlabel("time[sec]")
plt.ylabel("original signal")
plt.subplot(512)
F = fft(y)
N = len(t)
w = fftfreq(N, (t[1] - t[0]))
ipos = np.where(w > 0)
freq = w[ipos]
mags = abs(F[ipos])
plt.plot(freq, mags)
ip = np.where(F > 0)[0]
Fs = np.copy(F)
yf = ifft(Fs)
ip = np.where(F > 0)[0]
Ff = np.copy(F)
Ff[ip > ip[[(181)]]] = 0
Ff[ip < ip[[(175)]]] = 0
magsf = abs(Ff[ipos])
plt.plot(freq, magsf, 'r-')
plt.subplot(513)
Fr = mags - magsf
plt.plot(freq, Fr)
plt.subplot(514)
yf = ifft(Ff)
yr = ifft(Fr)
plt.plot(t, yf)
plt.subplot(515)
flux = y - np.real(yf)
plt.plot(t, flux)
plt.plot(t, y)
plt.show()
1

There are 1 best solutions below

1
On BEST ANSWER

Your problem seems to be that your time-grid is not evenly-spaced:

In [83]: d = np.diff(data[:,0])

In [84]: d
Out[84]: 
array([ 0.0006144 ,  0.0006144 ,  0.00049152, ...,  0.0006144 ,
        0.0006144 ,  0.00049152])

If I interpolate the values to a constant-spacing in time:

data = np.loadtxt('3.dat', comments="#")
t = data[:, 0]
y = data[:, 2]
len_data = len(data)

ti = np.linspace(t[0], t[-1], len_data)
yi = np.interp(ti, t, y)
t, y = ti, yi

The peak is at 50 Hz:

enter image description here