I have signal containing a large amount of mains noise. I'm trying to feed this back into my signal to actively cancel it from future measurements, but I'm having trouble getting it to work. I have tried taking the fft bin with the maximum mains noise, generating a sin wave of the function:

bin.real * Sin ( bin.frequency * 2 * PI * T + bin.phase)

where bin.frequency is calculated to be the mains noise at the time of measurement (~$50\textrm{ Hz}$).

When I subtract this from my original signal it still looks very noisy, albeit a different kind of noisy. If my sample rate is $250\textrm{ Hz}$, I'm thinking I need to increment T by $1/250$ to correctly generate the signal.

What is the correct way to go about this?

  • 3
    $\begingroup$ if you're trying to remove a sinusoidal component of a known frequency from a signal, use a notch filter. $\endgroup$ Oct 21, 2016 at 3:06
  • 1
    $\begingroup$ Mains is a known frequency, that varies very little in comparison to the width of a typical filter. So, any reasonable notch filter (whether analog or digital) designed for 50Hz will cover any variation in mains frequency that you are likely to encounter. $\endgroup$
    – JRE
    Oct 21, 2016 at 8:36

2 Answers 2


If the transfer function between the mains signal and your contaminated signal is fixed forever, then you could cancel the interference with a sine wave that is locked to the mains, and adjusted in both amplitude and phase to cancel your interference. However, using an fft is not the optimal way to do this, as the true mains frequency will likely fall in between the bins, so you will have a frequency error. A better way is to use a dsp phase-locked loop, the details of which are too complex to describe here. However, it is unlikely that the transfer function is fixed. Therefore you will need an adaptive algorithm to adjust the amplitude and phase of your cancellation signal. This adaptive algorithm will look at (input signal - cancellation signal) and attempt to minimize the portion that is correlated to the mains frequency. Unfortunately the adaptive algorithm will be affected if there is a component in your desired signal that is close to the mains frequency, which will reduce the attenuation of the mains frequency and also add attenuation to the desired signal. So it behaves very much like a notch filter, so you might as well just use a notch filter as RBJ has recommended. You can minimize this interference effect by making the adaptive algorithm very slow, but again this is roughly equivalent to using a higher-q notch filter, and both systems will take a long time to settle out.


If you set your sampling rate to the mains frequency or a harmonic of the mains frequency, then the mains frequency is likely to be almost fully contained (if not fully contained) in its associated bin of the FFT. It should not be off significantly enough to produce artifacts in the other FFT bins of any amplitude significance. (If it does, you've probably set up the sampling or data processing steps incorrectly).

The easiest way to cancel this frequency in the resulting time domain output, is to zero out that frequency bin in the FFT, and then perform an inverse FFT of the data with the zeroed out mains frequency bin. (Yes, it is as simple as replacing that FFT bin's signal with a 0 or a 0+j0 answer). This assumes you have the luxury to collect the data, process the data with an FFT, zero out the mains bin, and inverse FFT the resulting data to produce a new time domain data sequence without the mains data present. (Sometimes, real time processing constraints will not allow you to take this approach, so a real time filter would have to be used instead).

This is similar to the first FFT bin (equal to frequency 0) being large in real world data, because DC (0 frequency) biases in the signal will accumulate into the 0 frequency bin. Usually, you are not looking for a 0 frequency signal, so you can also zero out the DC (0 frequency) bin of the FFT to remove this bias in the resulting data. This is especially useful because this signal can be large compared to the other frequency bins, so by removing the 0 frequency (and the mains frequency), the autoscaled results will show the resulting frequency bins with a higher amplitude.


Your Answer

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

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