Signal Denoising using Convolution in Python
Key Takeaways
Shows signal denoising using convolution in Python to remove noise and recover clean signals
Full Transcript
In this lecture, we will study the first application of convolution which is called denoising. Now students, denoising means removing noise from a signal. If you are given a signal that contains noise, your task is to recover the original signal by removing that noise. Denoising can be done using convolution by selecting a suitable filter. So when you are given a noisy signal and your goal is to remove the noise, you must first choose an appropriate kernel or filter and then perform convolution of that kernel with the noisy signal to recover the clean original signal. Let us begin this lecture and see how we can denise a signal using convolution. First we import the necessary modules into our Python environment. The next step is to generate a noisy signal which we can later clean. To do this, we create a sinosoidal signal. We start by defining a sampling rate which we set to 256 hertz. Now, we need to generate a time vector so that we can plot our sine wave against it. We use numbum pi data range from 0 to 3 with a step size of 1 divided by the sampling rate. This means we are generating a time vector from 0 to 3 seconds where the interval between samples is 1 divided by the sampling rate. Since that interval is very small, it means we are generating a large number of samples between 0 and 3 seconds. We call this time vector t. And the number of samples in it are called time points. The variable time points is equal to the length of t. Let us check the length of this time vector. When we print the length of t, we can see that it contains 760 eight samples between 0 and 3 seconds. If we print t itself, we can see that it starts from zero and goes up to approximately 2.99 seconds, which we can round to three. So between 0 and 3 seconds, we have 768 samples. Now that we have generated the time vector t, we can generate the sinosoidal signal. We define x= num pi dot sign open parenthesis 2 * num pi * 2 * t close parenthesis. This is the standard equation for generating a sinosoidal signal which we have also seen in previous sections. So x is our clean noisefree signal. Next we create the noise. We use num pi.random.n to generate Gaussian noise. The number of noise samples is equal to the number of time points which is 768. We then multiply the noise by five to increase its amplitude. After that we create the noisy signal by adding the noise to the clean sinosoidal signal. In this way we generate a sine wave add random noise to it and obtain a noisy signal. When we plot this noisy signal, we can clearly see that the noise is superimposed on top of the clean sinosoidal wave. Our goal now is to remove as much noise as possible so that we can recover the original signal. To summarize the first part, we generated the time points between 0 and 3 seconds creating 768 samples. Then we generated a clean sinosoidal signal using the equation 2 *<unk> * frequency * t where the frequency was 2 herz. After that we generated Gaussian noise also containing 768 samples and added it to the clean signal to form the noisy signal. The next step is to select a filter to remove the noise. This is a very important step. We will use a moving average filter for denoising. Let us run this cell and see how we generate the moving average filter and what its coefficients look like. You can see that the length of our filter is 50. We create it using num pi doses which generates 50 samples of value one. Then we divide those ones by 10 so that each value becomes 0.1. And finally we multiply by two. As a result, every coefficient of the moving average filter has a value of 0.2. If you focus on its name, it is called a moving average filter. As the name suggests, it moves along the noisy signal and calculates the average value. From the concept of convolution, you already know that we have an original signal and we select a filter that moves or slides across this noisy signal to remove the noise. This is the basic idea behind convolution. The filter slides over the original signal and performs convolution at each step. Here we have a noisy signal and a moving average filter. The noisy signal contains 768 samples while the moving average filter contains 50 samples. This means the kernel is much smaller than the signal. As this small kernel moves over the noisy signal during each shift or movement, the convolution between the noisy signal and the kernel is calculated. Through this process, we can reduce the noise. Now, why do we use an average filter? The reason is that it moves along the signal and calculates the average while sliding. Since noise usually has both positive and negative amplitude values, taking the average helps to cancel them out. For example, if one sample has a value of five and another has a value of -5, their average is zero. Even if one is five and the other is -6, their average will still be close to zero. So if we have a moving average filter with all positive coefficients and the noise has both positive and negative values with random amplitudes, taking the average during the movement of the filter will bring the value of noise close to zero. This is why we use a moving average filter. It cancels out the positive and negative peaks of noise while averaging. The next step is performing the convolution. We use numpy.convolve to convolve the noisy signal with the moving average filter and we set the mode to same. The reason for using same mode is that we want to keep the length of the output signal the same as the input signal. For example, our original signal has 768 samples and we do not want the convolved output to increase to 800 or 850 samples which would happen in full mode convolution. Therefore, we use the same mode to keep the signal length unchanged. Now, num pi.convolve Convolve takes care of all four steps of convolution automatically. After performing convolution, the resulting signal is called the filtered signal. This filtered signal should ideally be the denoised version of the original. Let's plot this filtered signal. When we plot it, we can see that the filtered signal clearly shows the characteristics of a sinosoidal wave. It fluctuates smoothly and oscillates at regular time intervals. We can observe that although the sine wave is not perfectly recovered, perhaps not 100%, we have managed to remove approximately 70 to 80% of the noise through convolution. After filtering, we can recognize the pattern of the original signal which was a sinosoidal wave. Initially before filtering it was difficult to identify the original shape of the signal. Now we plot both the noisy signal and the filtered signal on the same graph for comparison. In this figure the green line represents the noisy signal while the red line represents the filtered signal. At certain points, you may notice that due to higher peaks of noise, the amplitude of the filtered signal is slightly higher than that of the noisy signal. This is acceptable and can be easily adjusted if needed. The most important observation is that by using this method, we have successfully removed around 70 to 80% of the noise from the red signal. It is visually clear that we have effectively recovered the underlying pattern of our original sinosoidal signal.
Original Description
https://www.youtube.com/watch?v=DCOqVC34o94&list=PLLlTVphLQsuMO2HsKm9I72gFcuBFlBSP6&index=1
Learn signal denoising using convolution in Python to remove noise and recover clean signals.
In this video, we demonstrate the first application of convolution: denoising a noisy sinusoidal signal. Using a moving average filter, we perform convolution to smooth out the noise while preserving the original signal pattern. This tutorial uses Python and NumPy to generate the signals, add Gaussian noise, and filter the noisy signal step-by-step.
If this tutorial helped you understand signal denoising using convolution, like, subscribe, and comment your questions below. Stay tuned for more practical Python signal processing tutorials!
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Related Reads
📰
📰
📰
📰
I built a free ATS resume checker that runs entirely in your browser - here's how
Dev.to · Dipesh Gyawali
3 Free AI Tools I Use to Save Hours Every Week
Dev.to · Hari Haran
My AI Bill Dropped 95% When I Switched to Chinese Models
Dev.to AI
AI Dubbing for Short Drama: The 2026 Playbook That Actually Scales
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI