I have a a time domain data set that records the magnetic field vs time, which must be processed to reveal an embedded signal. This data also contains power line harmonics (i.e. multiples of 60 Hz) that must be removed from the signal. I am processing the data with Python and am using the numpy, scipy.signal and scipy.fftpack modules to filter out information.
Instead of using a traditional notch/comb filter I am writing a function that will transform the data to the frequency domain with an array of values corresponding to amplitude and another array corresponding to frequency. The arrays are also reformatted to exist properly in the frequency spectrum. The code then determines where the frequency peaks are and the array indice that corresponds to each peak. Finally, the code analyzes each peak to see if it exists as a multiple of 60 Hz (i.e. a power line harmonic). If any of the data points corresponds to the first 50 multiples of 60 Hz, the code finds the amplitude and frequency array values for indice-1 and indice+1 and then does a linear interpolation between them to find a new amplitude. The new value of amplitude replaces the old value, thus eradicating the frequency peaks corresponding to each power line harmonic, without the depressions caused by normal notch filters. The Python function is shown below.
When I plot the frequency response I get a very nice looking plot that has clearly erased the harmonic peaks. However, when I used an ifft function to transform the data back to the time domain, I see that the power line harmonics all still exist, but the amplitude has been reduced. I suspect that python is only working on the even (real) series and is neglecting the odd (imaginary) data, but I am not sure what is causing this. Again, when I plot the frequency domain info, there are no harmonic peaks (60, 180, 300, 440, 600 Hz, etc...), but when I plot the time domain it is clearly there. Any help would be appreciated. Also, I am not including the peakdet and the linear_interpolate functions because I have tested those and am confident the problem does not lie with them.
def Interpolate_US_Power_Harmonics(XData,YData): from scipy.fftpack import fft import numpy as np # Determines time between sampling assuming uniform intervals Sample_Freq = XData - XData # Resize x and y axis for frequency domain Y_Data = fft(YData,n=YData.size) array_length = len(Y_Data) Y1_Data = 20*np.log10(Y_Data/1) # transform to decibels Y1_Data = 2.0/array_length*np.abs(Y1_Data[0:array_length/2]) XTicks = np.linspace(0.0,1.0/(2*Sample_Freq),array_length/2) # - Determine the frequency peaks (maxima) and troughs (minima) and their array # positions. Odd array positions correspond to peaks and even points # correspond to minimas maxima, minima, position = peakdet(Y1_Data, 200*Sample_Freq, XTicks) # Determine array position of frequency peaks new_position =  for i in range(0,len(position)): if i% 2 != 0: new_position.append(position[i]) # - Determine if a peak is a powerline harmonic (multiple of 60 Hz) and if so # replace magnitude array point with one interpolated from the previous # and next array points. for x in range(0,len(maxima)): frequency = 60.0 for y in range(100): if maxima[x] > frequency - 1 and maxima[x] < frequency + 1: X1 = XTicks[new_position[x-1]-1]; X2 = XTicks[new_position[x-1]+1] Y1 = Y_Data[new_position[x-1]-1]; Y2 = Y_Data[new_position[x-1]+1] X3 = maxima[x] Y_Data[new_position[x-1]] = Linear_Interpolate(X1,X2,Y1,Y2,X3) print frequency, XTicks[new_position[x-1]] frequency = frequency + 60 # Return frequency domain data to main program return Y_Data # Main program import scipy.fftpack.ifft # meta code to show time and amplitude array as well as plotting Time = insert data here Amplitude = insert data here # used pylab to plot original time and frequency domain signals # Produce new frequency series filtered new_data = Interpolate_US_Power_Harmonics(Time,Amplitude) # - used pylab to plot filtered data. The frequency domain showed no harmonics. # but the time series plots still contained 60, 180, etc.. harmonics with # a reduced amplitude