Baseline Wander Removal using Wavelet in Python

M Iqbal Tawakal
3 min readDec 13, 2020
Photo by Fezbot2000 on Unsplash

Just (not so) recently, Github announced that they have completed Arctic Code Vault. It’s a project to store active open source software in multiple, different format, (visit the page if you want to know more about the detail here You know, just in case humanity faced their doom and only small number of people survive and has to restart the civilization from beginning, which is basically the entire plot of Dr Stone.

Out of curiosity, I check the profile page and it turns out that I also included as the contributor. Wow, nice! Finally something that I can leave behind for human species because my gene is not likely to be passed on at this rate. It is not even that impressive really, because any active project even though it is only hello world in the past one year is automatically included.

Sidu is no stranger, because that is where I put all of my jupyter notebooks for various purposes in the past few year. But the py-bwr is one of my oldest project and I have never touched it anymore. As it turns out, a github user named spebern fork this code and improve it to be better. Nice, that’s the power of open source.

So a bit of background on what kind of problem this code will solve. Any acquired digital signal from the wild (audio/music, image, or electrocardiogram) naturally comes with noise, unless it is acquired in highly specialized environment. Noise can be divided into 2 different types, low frequency noise, and high frequency noise. People will usually deal it by first doing some Fourier analysis, determining the frequency cut-off, and then build the filter itself.

In this case we are interested in one specific case of noise, which is called baseline wander or drift. This happens when the signal is not straight in the x-axis, but wander up and down. For ECG, it usually happens when there is movement from the acquisition process. Because this is in category of low frequency noise, usually you need to design high-pass filter to solve this. However, using this code, you don’t need to worry about that anymore. For more detail on the actual algorithm, you can check the paper directly here

Example of signal with baseline wander, before and after treatment

The improvement (beside basic code cleaning, like porting to python3!) boils down into two pieces. First, the Wavelet Daubechies coefficient is not hardcoded in the script, but taken from PyWavelet library. This way it can accommodate different basis function, one that maybe have better noise reduction. Second, the convolution is also done using pywt package instead of manual implementation using double for loop. This will result in faster and less buggy code.

The end result is similar, so if you are working with signal data and want to preprocess to remove the baseline drift, go check spebern repo posted above.

Happy signal processing.



M Iqbal Tawakal

Customer Engineer, Machine Learning at Google. Sometimes misspell planet Jupiter with letter y due to work.