1
$\begingroup$

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?

$\endgroup$
5
  • 1
    $\begingroup$ It looks like you're creating noise that is not zero-mean. Why? $\endgroup$ Mar 29, 2023 at 15:49
  • $\begingroup$ Im new to this. How can i make it zero-mean? $\endgroup$
    – Tom
    Mar 29, 2023 at 16:50
  • $\begingroup$ 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. $\endgroup$ Mar 29, 2023 at 16:53
  • $\begingroup$ I have removed the mean, but i still dont receive the original signal $\endgroup$
    – Tom
    Mar 29, 2023 at 16:55
  • $\begingroup$ See my answer below. What exactly are you attempting to accomplish? Just filtering cannot achieve what it seems that you are trying to do. $\endgroup$ Mar 29, 2023 at 16:58

1 Answer 1

4
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ Okay, so i have to use a different noise? $\endgroup$
    – Tom
    Mar 29, 2023 at 16:58
  • 1
    $\begingroup$ 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. $\endgroup$ Mar 29, 2023 at 17:00
  • $\begingroup$ this is just for educational purposes: i want to understand how to "de-noise" a signal $\endgroup$
    – Tom
    Mar 29, 2023 at 17:01
  • $\begingroup$ 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. $\endgroup$ Mar 29, 2023 at 18:14
  • $\begingroup$ 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 $\endgroup$
    – Tom
    Mar 30, 2023 at 11:11

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.