Removing specific frequency from a signal
Below is a picture that has:


Top graph has the noiseless 20Hz sine wave sampled at 100Hz (blue) and the noisy sine wave (red).
Bottom graph has the actual noise signal added (red) and the noisy signal filtered with an IIR notch filter with a notch centred at 20Hz (blue).


Apart from the group delay between the original noise and the output of the notch filter, the two are very similar.

R code to implement this is below the graph.



The frequency response of the notch filter is as below.





R Code

#26866

f &lt;- 20
fs &lt;- 100
phi &lt;- 2*pi*0.12987892374

T &lt;- 100
t &lt;- 0:(T-1)

signal &lt;- sin(2*pi*f/fs*t+phi)
noise &lt;-  rnorm(T,0,1)
noisy_signal &lt;- signal + noise

alpha &lt;- 0.9

num &lt;- c(1, -2*cos(2*pi*f/fs), 1)
den &lt;- c(1, -2*alpha*cos(2*pi*f/fs), alpha*alpha)

filtered &lt;- signal::filter(num,den,noisy_signal)

par(mfrow=c(2,1))
plot(noisy_signal, type="l", col="red",  lwd=10)
lines(signal, type="l", col="blue", lwd = 3)
title("Noiseless signal (blue) and noisy signal (red)")

plot(filtered, col="blue", lwd = 4)
lines(noise, col="red",  lwd=3)
title("Filtered signal (blue) and original noise signal (red)")