Moving Average Filter in Python | Step-by-Step
Key Takeaways
The video demonstrates how to implement a moving average filter in Python to remove noise from a signal, using the NumPy library to generate a noisy sinusoidal signal and apply the filter.
Full Transcript
In this lecture, we will implement a moving average filter in Python and see how it can remove noise from a signal. First, we need to import all the necessary Python modules into our environment. Let's run this cell to import them. Now that the modules are imported, we need to generate the signals, the noise, and finally the noisy signal. First we generate a noiseless signal then a noise and then we add the noise to the signal to create a noisy signal. We start by selecting a sampling rate because for a sinosoidal signal the sampling rate allows us to calculate the time vector. We have chosen a sampling rate of 256 hertz. Using this information, we create a time vector from zero to 3 seconds using np dot array. The interval is 1 over the sampling rate. Since one over the sampling rate is very small, this gives us a large number of samples between 0 and 3 seconds. Next, we calculate the length of the time vector, which we call points. The number of points equals the number of samples in the sinosidal wave. Now we generate a noiseless cenosidal signal with amplitude 1 and frequency 2 using the equation MP do sin of 2 NPPFT. Here f is the frequency of the signal. Since we want to add noise to the signal, the number of samples in the noise must equal the number of samples in the signal which is equal to points. We generate random noise using np.random.random random with the number of samples equal to points. We then multiply the noise magnitude by five and add it to the sinosoidal signal to obtain the noisy signal. So the process has three steps. First, generate a noiseless signal. Second, generate a noise. And third, add the noise to the signal. Now we can plot the noisy signal. From the plot, we can see the signal from 0 to 3 seconds with the amplitude showing the noisy vibration. The next task is to recover or smooth the signal using a moving average filter. Up to this point, we have created the signal, the noise, and the noisy signal. The plotting steps are standard. setting the figure size, background, and tick sizes, and then plotting the noisy signal in green with the label noisy signal. The xaxis is labeled time in seconds, and the wiaxis is labeled amplitude. Now, we set the parameters for the moving average filter. We are using a filter of order 30, although any order can be selected. You can choose 20, 40 or any other number depending on the smoothness required. Remember the order of the filter determines the filter window length which is 2 n + 1. So if the order n is 30, the filter length is 61. If the filter is centered at the 100th sample, it takes 30 samples before and 30 samples after the center, calculates their mean and assigns it as the filtered value. Following the symmetric moving average filter principle we discussed in the previous lecture. To check the size of the noisy signal, we use noisy signal.shape zero. Running this command gives 768 which confirms that the signal is onedimensional with 768 samples. This is important because we will use a four loop to apply the moving average filter and the number of iterations depends on the signal length. Before running the loop, we need to set up an empty placeholder for the filtered signal which will store the results as the filter moves across the noisy signal. Now this is our filtered signal. This will be the required signal once the filtering is complete and then it will be called the filter signal. We have initialized the filter signal using np.zer of noisy signal.shape zero. Since noisy signal dotshape is 768, this command generates 768 zeros which are stored as the filter signal. When we apply the for loop, the real values will replace these zeros using filter.shape I. Now we are applying a for loop to implement the moving average filter on the noisy signal. The loop runs from 0 to 768 because the length of our signal is 768. Here we replace the placeholder with noisy signal.shape zero. np.m mean is used to calculate the mean which is the essence of the moving average filter. The filter moves across the noisy signal and calculates the mean of the values according to its length or order. The for loop iterates from 0 to 768 which is the length of the signal. For each iteration, the filter moves according to its length which is 2 n +1. For the first iteration when i equals 0, we take the mean of the noisy signal samples from 0 to 2 n + 1. In this case with order 30, the filter length is 61. So we calculate the mean of samples 0 to 60. In the second iteration when i equals 1, we calculate the mean from the second sample to the 62nd sample and so on. This sliding continues until the filter reaches the last sample. Always taking the mean of the 61 points at each step. Running this cell generates the filtered signal. np.mme implements the moving average filter by calculating the mean of the noisy signal for its filter length not the entire signal at once. The for loop runs 760 eight times sliding the filter over the entire noisy signal and reducing the noise step by step. Now we plot the results. The noisy signal is shown in green and the filtered signal is shown in red. You can see that the filtered signal successfully smooths the noisy signal producing the required output. The high spikes and extreme noise values are removed resulting in a smooth waveform. You can still observe the edge effect at the signal boundaries. As discussed in the previous lecture, this occurs because the moving average filter requires samples before and after the current point. At the last sample, there are 30 samples available before it, but 30 samples after it are missing. This lack of samples after the edges prevents proper filtering, creating the edge effect. We will address this problem in the next topic. Here the code plots the filtered signal on top of the noisy signal in red with appropriate titles, axis labels, and figure size. In this way, we have successfully implemented the moving average filter in Python and removed the noise from the noisy signal. The important points to remember are that the for loop should run from zero to the length of the noisy signal which is 768. During each iteration, the filter calculates the mean of 61 samples for order 30. The sliding of the filter must follow its length which is 2 n +1. This ensures that the moving average filter fully covers the noisy signal from the first point to the last. We stop this lecture here. In the next lecture we will explore another type of denoising filter.
Original Description
https://www.youtube.com/watch?v=DCOqVC34o94&list=PLLlTVphLQsuMO2HsKm9I72gFcuBFlBSP6&index=1
Learn how to implement a moving average filter in Python to remove noise and smooth signals. This tutorial shows step-by-step how to generate a noisy sinusoidal signal and apply a moving average filter for effective signal denoising.
In this video, we cover the Python implementation of a moving average filter, including generating noisy signals, defining the filter order and length, and using a sliding window approach with a for loop to calculate the average across samples. The tutorial demonstrates how the filtered signal reduces high-frequency noise while maintaining the underlying waveform. We also discuss the edge effect, where filtering near the boundaries can be inaccurate, and explain how to handle it effectively.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Related Reads
📰
📰
📰
📰
7 AI Notion Workflows That Actually Run in 2026 (Honest Comparison, incl. Easlo & Thomas Frank)
Dev.to AI
ChatGPT Plus and Claude Pro reject your card? It is probably the billing country, not the card
Dev.to · Tung@fizen
👾 🧚🏼♀️Maximizing Fable for Life Admin
Dev.to · Anna Villarreal
Lost in the Cheese Aisle? Here’s How AI Can Identify Any Cheese From a Photo
Medium · Startup
🎓
Tutor Explanation
DeepCamp AI