13
$\begingroup$

I am processing an EEG signal (2 kHz sampling frequency) from hardware and the built-in filter doesn't work as intended. FIR gives a delay, and IIR is unstable.

The signal of interest is from 4 Hz to 360 Hz, with the goal to clear the utility signal (AC 50 Hz) from the signal. The main constraint is to get as many untouched (no noise, perturbance, and leaks) samples as possible. The tradeoff here is that gamma brain waves starts from about 40 Hz and goes up through 50 Hz and further.

Are there any options available to get no delay or at least a very small one (a 357 tap FIR gives more than 130 dB attenuation, but the delay with 2 kHz is about 180 ms. I aim at less than a 30 ms delay with at least 60 dB)?

The start of the pass band is 49 Hz and the end is at 51 Hz (in fact, it looks like this: https://www.swissgrid.ch/en/home/operation/regulation/grid-stability.html), so it varies in time and is non-stationary.

$\endgroup$
  • 1
    $\begingroup$ Please provide more information on the signal you need to pass--- how tight does the notch need to be and what is the requirement for the signal of interest. The more you can relax that - in particular your transition band between what you are passing and what you want to get out, the lower the delay. Can you do a highpass? $\endgroup$ – Dan Boschen Jan 8 at 2:19
  • $\begingroup$ @DanBoschen thank you for reply. I hope this is clear now. If not, I will picture it otherwise: I have 2kHz sampling frequency, I read 4-360Hz with unfortunate 50Hz bump. I want to salvage as many frequency bins from that range as possible - so constraints are untouched samples, negligible delay, short transition band and high signal damping at range 49-51Hz. I am aware that it is impossible to get all factors met, but maybe using FIR is simply bad approach. I can't do highpass or lowpass, only notch as tight as possible. $\endgroup$ – Evil Jan 8 at 2:58
  • $\begingroup$ I have a post for a second order IIR notch filter on here already that I would recommend trying - you can adjust the notch coefficient to get the minimum notch bandwidth within your delay constraint. Let me find the link for you if you didn’t already try that. You will also benefit by reducing your sampling rate —- 1 KHz or even 800 Hz would be feasible with the right filter prior to sampling. That will give you a tighter notch for the same delay. $\endgroup$ – Dan Boschen Jan 8 at 3:14
  • $\begingroup$ (But the analog filtering will have delay as well) $\endgroup$ – Dan Boschen Jan 8 at 3:16
  • $\begingroup$ Here is the link: dsp.stackexchange.com/questions/31028/… monitor delay while adjusting notch widtth using grpdelay function in matlab, octave or python (scipy.signal) $\endgroup$ – Dan Boschen Jan 8 at 3:19
19
$\begingroup$

You could use a 2nd order IIR notch filter as I describe in this post Transfer function of second order notch filter - That post demonstrates a 50 Hz IIR notch with 1 KHz sampling.

[Update: As @user47050 astutely points out in the comments, the IIR notch would also have minimal delay regardless of notch bandwidth, since the dominat delay in the IIR notch filter is specific to the signal components in the rejection bandwidth of the filter, not the signals that you want to pass through. The advantage of what follows is not necessarily then to eliminate a delay issue, but to provide tracking rejection of just the interference that is approximately 50Hz (but slowly varying in phase and amplitude) while minimizing impact to adjacent frequencies of interest.]

However to provide cancellation of the interference with zero delay, consider a feedback signal cancellation servo such as I depict in the diagram below. This would have no delay to your signal but will have a delayed response to changes in your interfering signal. Results will depend on the relative stability of the interference. If the interference is a relatively slowly changing sinusoidal tone, this should work very well.

This is an all digital nulling loop that can null down to the rms power level of your signal (at which point it can no longer estimate the cancellation coeeficient). The bold lines shown in green represent complex signals:The 50 Hz PLL is a NCO with sine/cosine outputs, the multiplier following it is a full complex multiplier (essentially a vector modulator providing complete control of amplitude or phase), the multiplier after the second BPF is essentially a quadrature down-converter providing a measure of the amplitude and phase of the residual.

nulling loop

The way this works can be described as follows: The input signal is assumed to have a strong 50 Hz signal, strong enough such that combined with the filtering of the first 50 Hz BPF the PLL can lock in phase to the tone (the tone needs to be 6 dB stronger than the noise at the PLL input). This will provide a phase locked copy of the 50 Hz signal as a quadrature NCO output. The quadrature PLL is basically a very tight tracking filter, providing a clean replica for purposes of cancellation.

Next we have a cancellation loop provided with a post cancellation band pass filter to detect the residual and adjust a complex coefficient to adjust the gain and phase of the 50 Hz PLL output prior to subtraction. I show the approach of quadrature downconverting the 50 Hz BPF output to complex baseband (by multiplying with the complex PLL output), and accumulating the result which will find the null with the convergence gain coefficient k. This is a cancellation loop using the method of stochastic gradient descent. The accumulator accumulates residual error as measured on the I and Q output of the quadature downcoverter resulting in the correct setting to the "Vector Modulator" multiplier resulting in a real sine wave output that will be equal in magnitude and opposite in phase to the interfering signal at the input.

The diagram can be simplified in that the first 50 Hz PLL BPF can be eliminated and the PLL replaced with a constant 50 Hz sine/cosine generator, since the vector modulator provides complete control of rotating phase which serves to to adjust the frequency positive or negative as needed, as well as gain as needed to provide the cancellation required. The proposed use of the 50 Hz BPF + PLL is that it would be feasible of providing faster corrections to phase changes in the input signal, once the cancellation loop has converged (since the PLL will track the input and can be designed with wider loop bandwidth than the nulling loop)- however if the amplitude change in the interference dominates, then I don't see a strong advantage to having the PLL.

Without the PLL this would reduce to the following block diagram. Here everything is done at complex baseband, so a low pass filter instead of a bandpass filter is used (which I would likely implement as an exponential averaging filter). k is the complex convergence gain and c is the determined complex correction coefficient that will scale the gain and phase of the generated 50 Hz cancellation signal (which as suggested earlier c would be a changing phase versus time to adjust for the small frequency offsets that would also exist).

The PLL implementation has a clear advantage over a 2nd order IIR notch filter in that it can track and null a tone interference over a relatively much wider bandwidth, within the tracking range of the PLL. Without further study of the implementation below, I am not confident that it would out-perform a 2nd order IIR notch filter, but it is interesting to consider given it is much simpler than the PLL approach.

No PLL Approach

As a fixed 50 Hz source with a 2KHz sampling rate, the tone generator is trivial to implement, but if you extended it for other applications and used an NCO for the fixed 50 Hz complex tone generator, you end up with a tuneable single tone adaptive canceller that will capture and cancel any single tone within the proximity to any frequency setting of the NCO, within range of the bandwidth of the LPF! The cancelling loop will null the 50 Hz interferor down to the noise floor coming out of the filter- so the tighter the filter, the deeper the null as long as the the signal is within the filter bandwidth.

The amount of cancellation will depend on the time rate of change of the amplitude and phase of the input signal. The approach as shown above has zero delay to the main signal of interest, but is estimating the characteristics of the interference from a lag time T in the past, where T is impacted by the bandwidth (delay) of the low pass filter and the loop BW of the nulling loop (set by k), so will have an upper frequency limit on how much of the noise bandwidth of the interference signal can be rejected (that noise bandwidth is visible in the power spectral density of the interfering signal, basically AM and PM noise side-bands). This nulling loop is a high pass filter to that interference; which is desired as the signal of interest is indistinguishable from noise.

If this approach proves useful, you can improve it further by instead using the method of recursive least squares (RLS adaptive algorithm) to determine the complex weight that is applied to the vector modulator based on the error terms out of the quadrature downconverter. This converges faster at the expense of computation complexity.

| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ And if you do pursue any of this more complicated solution, please do post your results here! $\endgroup$ – Dan Boschen Jan 8 at 14:35
  • 1
    $\begingroup$ It should work, I'm just not sure it's necessary. And while the PLL-based design should give an infinite null anywhere within its lock range (with really careful design), I think you'll find, if you grind through all the math, that the versions with the fixed-frequency oscillators are going to have the same frequency response as a plain old notch filter. $\endgroup$ – TimWescott Jan 8 at 19:49
  • 1
    $\begingroup$ Well it would give you closer approximation to two things you can’t get with the notch filter: zero delay with an infinitely thin notch (in the case of a fixed tone Interference that is very clear— to the extent the interference changes slowly it will approximate that). When the input is not changing the PLL component isn’t needed as the rest of it will converge to offsets within the filter bandwidth $\endgroup$ – Dan Boschen Jan 8 at 19:57
  • 1
    $\begingroup$ A 2nd degree IIR filter cannot have significant delay since its "memory" is just 2 slots large, and it uses those to track phase and magnitude of the 50Hz sinoid it nullifies. Basically the filter is a PLL that reacts with exponential decay to changes in the complex amplitude of the 50Hz component in its input, with a time constant that gets larger with the filter's selectivity. The signal passes without relevant delay; the delay only concerns how fast changes in the 50Hz component get tracked (and thus compensated). $\endgroup$ – user47050 Jan 8 at 23:39
  • 1
    $\begingroup$ @Dan Boschen: that group delay only becomes relevant for those signal components within the bandwidth with a duration longer than the inverse bandwidth. So it's the attenuated slow-changing noise that suffers significant group delay (i.e., switch it off and you get a 180° shifted 50Hz wave with slow exponential decay, starting as the original 50Hz compensation). The signal itself passes straightforwardly, and a 2nd degree IIR filter does not have "storage" to have it be otherwise since it passes almost all frequencies. $\endgroup$ – user47050 Jan 8 at 23:54
9
$\begingroup$

This is an uncertainty principle kind of problem: there is no way to make a reliable filter with little delay that will suppress a narrow band around 50Hz since the narrowness of a criterion in frequency space necessitates a certain width of observation in the time domain. Basically the compactness of a phenomenon in time and in frequency cannot be independently minimised. Gabor wavelets are "optimal" in one measure in that regard. The narrower your filter is, the slower it will react to changes of the disturbance. I'd not use an FIR filter here but rather a short steep IIR notch filter.

This will suppress sustained hum and will react robustly to signals passing through 50Hz comparatively quickly (they will be mostly admitted and their impact on the suppression operation will not be large and die down eventually). A 2-stage 50Hz IIR filter is more or less a damped 50Hz oscillator that slowly synchronises to the source signal and has its output subtracted from it. It is excited by the difference of source signal and its output, so it tends to track the 50Hz component of the input.

A different approach would be one of "echo compensation": if your device has a power plug, you already have a model of your noise there which is phase-locked to your problem signal. It would be quite unusual (though not impossible once backup generators or safety networks come into play) for mains hum to exist in several versions that are not phase-locked, and at least slow drift would be something the algorithm would adapt to.

| improve this answer | |
$\endgroup$
  • $\begingroup$ A fixed 2nd order IIR filter tuned to the 50 Hz bandwidth will not adapt its center frequency based on the input signal-- it has a fixed rejection band that predicts exactly how much the signal will be rejected at any given frequency. $\endgroup$ – Dan Boschen Jan 9 at 0:13
  • $\begingroup$ (See my post here that shows the frequency response of the digital 2nd order IIR filter with it's predicted rejection: the input frequency does not change this response: dsp.stackexchange.com/questions/31028/…) $\endgroup$ – Dan Boschen Jan 9 at 0:14
  • $\begingroup$ @Dan Boschen: you'll find that mains hum does not adapt its frequency either: the power companies basically guarantee that a clock synchronized to the mains frequency will not be off more than a few seconds over the course of a month. A hospital on backup power may have more fluctuations, of course. $\endgroup$ – user47050 Jan 9 at 0:35
  • 1
    $\begingroup$ @user47050: The average frequency of the power line is very accurate, but it does vary over time. The power companies jigger things so that the average over a long period is correct. $\endgroup$ – JRE Jan 9 at 12:18
  • 1
    $\begingroup$ @JRE: as I said: the IIR notch filter tracks amplitude and phase of a hypothetical (exact) 50Hz noise. The power company corrections occur by accelerating/decelerating phase, a change that the IIR filter can follow comparatively well as long as the frequency difference is within its bandwidth. You'll get some noise bleed while the frequency is off but no transients. $\endgroup$ – user47050 Jan 9 at 12:53
5
$\begingroup$

I joined this community only to answer your question as I had a similar problem about 2 years ago, in ECG domain though.

What I've found (unfortunately I cannot trace the source back) is a very simple solution for a digital notch filter of taking the signal delayed by half of the period of the frequency you want to filter out and get the average of it with the current signal.

In your case, 2 kHz sampling frequency, one full period of 50 Hz is 40 samples. Thus, you delay your signal by 20 samples and for each sample you calculate the average of the current sample and 20 samples ago:
enter image description here

And in your case the delay is 10 ms.

The results of this filter are presented in the pictures:
enter image description here
enter image description here

The only disadvantage is that the attenuation is far less than the 60 dB that you require. My rough calculations indicate something between 20 dB and 30 dB.

| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ What you describe is a Comb-filter, en.wikipedia.org/wiki/Comb_filter $\endgroup$ – Ben Jan 9 at 13:16
  • $\begingroup$ Thank you for clarifying, I was focused on the result and not on the method that much. That means that also 100 Hz, 200 Hz and so on will be filtered if I understand correctly. It's not that bad for the OP as, I assume, they want to get rid of the environment noise for their application (mains hum is 50 Hz, fluorescent lamps are 100 Hz, etc). This filter is thus killing two birds with one stone. $\endgroup$ – Mike Jan 9 at 14:13
  • $\begingroup$ However, the transient response is be pretty slow, this could a problem in certain applications... $\endgroup$ – Ben Jan 9 at 14:51
  • $\begingroup$ The drawback to mention with this approach is that it will be a comb filter and you will get notches at multiples of 50 Hz and a relatively wide notch at each location (the magnitude response is abs(sine) $\endgroup$ – Dan Boschen Jan 10 at 11:35
  • $\begingroup$ Ah i see you already said that! Still it would remove a lot of signal of interest given the relatively wide rejection bands —- however you could do what you suggest if you want harmonic rejection by doing zero insert of the coefficients for the 2nd order IIR nulling filter; providing a steep null at each harmonic. That would have a much longer transient while this solution you show should not have much of a transient at all—- unless I am missing something @Ben ? $\endgroup$ – Dan Boschen Jan 10 at 17:49
4
$\begingroup$

I assume this a for real-time processing. Otherwise, you could simply discard the number of samples corresponding to the group delay.

1st solution - Use an IIR notch filter. You could use this solution

Analytically designing a notch-filter for specified frequency 50 Hz

The group delay will be minimal, probably negligible, if you're not close to 50 Hz. However, this filter will still have a transient response, which could be problematic. Unfortunately, I cannot answer that for you.

2nd solution : Assuming your 50 Hz noise has a near constant amplitude, you could estimate this noise and subtract it. As long as your estimate is close. the effective delay will be 0. However, if the amplitude of the noise changes quickly this might not work.

| improve this answer | |
$\endgroup$
  • $\begingroup$ True, this is for real time processing. As in the website, the 50Hz is nowhere near constant, but the solution with IIR seems better than expectd. Thank you. $\endgroup$ – Evil Jan 8 at 13:52
  • 1
    $\begingroup$ The solution proposed by Dan Boschen with a PLL could work well though as the PLL would track the varying frequency. $\endgroup$ – Ben Jan 8 at 14:19
  • 1
    $\begingroup$ The subtraction will only be effective if the phases are the same, otherwise the result could be worse (1.41 times at its worst). $\endgroup$ – a concerned citizen Jan 13 at 14:47
  • $\begingroup$ I agree. However, if the estimator is accurate and the phase does not change quickly, it will work. $\endgroup$ – Ben Jan 13 at 15:48
3
$\begingroup$

For removing mains hum from EEG signals, you can take advantage of the fact that the noise is stable in phase and frequency, albeit not necessarily in amplitude depending on the overall electrical environment. For that, the most popular EEG analysis software all support sine-wave fitting:

  1. EEGLAB via the CleanLine plugin and installable via EEGLAB's plugins menu (MATLAB-based)
  2. FieldTrip via ft_preproc_dftfilter (MATLAB-based).
  3. MNE-Python via Raw.notch_filter and method='spectrum_fit' (Python based).

On a broader DSP note: it's possible to compute the FIR group delay and compensate for it by simply shifting the filtered signal backwards. But as noted elsewhere, this may not be practical for real-time processing. But if you're doing real-time processing of gamma-band signals in surface EEG, see my next note.

On a broader EEG note: Removing EEG signals below 4 Hz is an extremely harsh filter by modern standards. Even 1 or 2 Hz highpass are only used in very limited areas. In most application areas, a highpass between 0.1 and 0.3 is the norm. Beyond that, we know that the high-frequency ("gamma") range of surface EEG is heavily contaminated by muscle artifacts. It's the area of the signal with the weakest brain signals (in part because the skull, meninges and cerebro-spinal fluid works as a lowpass filte, see Duun-Henriksen et al. 2013 for a cool biological example, although the results are clear from the physics of it) and the most muscle artifacts and electrical noise. Many gamma effects have been found to disappear when participants are chemically paralyzed Whitham et al., 2007. This also explains the inconsistency of gamma effects in many areas (e.g. language) -- the noisiness of this signal area leads to low statistical power which leads to Type M & S errors (Gelman and Carlin, 2014).

| improve this answer | |
$\endgroup$
2
$\begingroup$

It is perfectly possible to build a notch filter with either FIR or IIR techniques that has little delay for frequencies away from the notch. However frequencies in and close to the notch must have rather large group delay, and therefore if there is a requirement for linear phase or constant group delay across all frequencies, then group delay inevitably is large at all frequencies. However, for most notch filter applications, constant group delay is not a requirement. A particularly simple notch filter is a first order IIR high pass filter. In this case the notch is at DC. The transfer function is $H(s)=-s\tau/(1+s\tau)$ for an analogue high-pass and first order discrete time high-pass filters equivalents are in the literature. A high-pass structure can be used to create a notch at any frequency in a a complex baseband signal by first rotating the notch frequency to DC, apply above high pass, then derotate back from DC to the notch.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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