# Denoising Signal With Butterworth-Filter

im trying to denoise a signal to which i added AWGN. Here is what ive done so far:

import numpy as np
import matplotlib.pyplot as plt
from scipy import signal

"""Creating a cosine and adding AWGN to it. Then denoising it.
"""
# Create cosing and cosine with noise
f_0 = 50
f_max = 2*f_0
t = np.linspace(0, 2*1/f_0, 256)
y = np.cos(2*np.pi*f_0*t)
sigma, mu = 3, 5
noise = sigma * np.random.randn(len(t)) + mu
y_noise = y + noise

# Plot both signals
figure, (ax1, ax2, ax3) = plt.subplots(3, 1)
ax1.plot(t, y, label='Signal')
ax1.set_xlabel('Time / $$s$$')
ax1.set_ylabel('Voltage / $$V$$')
ax1.legend(loc='upper right')

ax2.plot(t, y_noise, color='green', label='Signal with noise')
ax2.set_xlabel('Time / $$s$$')
ax2.set_ylabel('Voltage / $$V$$')
ax2.legend(loc='upper right')
#peaks, _ = signal.find_peaks(y_noise)
#ax2.scatter(t[peaks], y_noise[peaks], color='red')

# Design a low-pass filter for denoising
order = 6
f_c = 10
f_s = f_0 / 2
b, a = signal.butter(order, f_c / f_s)

# Apply filter to signal
y_denoised = signal.lfilter(b, a, y_noise)

# Plot transformed signal
ax3.plot(t, y_denoised, label='Denoised Signal')
ax3.set_xlabel('Time / $$s$$')
ax3.set_ylabel('Voltage / $$V$$')
ax3.legend(loc='upper right')


Any ideas why this doesnt get me the cosine signal?

• It looks like you're creating noise that is not zero-mean. Why? Mar 29 at 15:49
• Im new to this. How can i make it zero-mean?
– Tom
Mar 29 at 16:50
• Set mu to 0. The only reason not to have zero mean is if for some reason in your application there is a DC value somehow being added. I've never seen that situation, though. Mar 29 at 16:53
• I have removed the mean, but i still dont receive the original signal
– Tom
Mar 29 at 16:55
• See my answer below. What exactly are you attempting to accomplish? Just filtering cannot achieve what it seems that you are trying to do. Mar 29 at 16:58

AWGN, by definition, is "white" and therefore has a constant power spectral density expectation across all frequencies. Therefore, your signal is buried in noise that is also partly at the exact same frequency. You cannot simply filter to "de-noise" because your filter passband will pass both signal and noise.

• Okay, so i have to use a different noise?
– Tom
Mar 29 at 16:58
• Can you elaborate on what you are trying to accomplish? The only way that what you're doing can work is if the noise is not white and at different frequencies than your signal. Mar 29 at 17:00
• this is just for educational purposes: i want to understand how to "de-noise" a signal
– Tom
Mar 29 at 17:01
• The reason that you're not seeing your signal is that it is still buried in noise. If you try to filter the noise that is at the same frequency as the signal, you will also filter the signal. Mar 29 at 18:14
• So b, a = signal.butter(order, f_c / f_s) should be b, a = signal.butter(order, f_c / f_s, fs=f_0)? With this i am almost getting the original signal, but it seems that the original signal is lost
– Tom
Mar 30 at 11:11