Why do spikes in the negative range disappear after filtering with filtfilt?
1 view (last 30 days)
Show older comments
Hello, I am trying to filter signals from neuron cells using filtfilt in Matlab. However, once I use the filter, the spikes in the negative range disappear. Figure 1 is how the filtered signal should look like and Figure 2 is how the filtered signal looks like after applying the filtfilt along with my code. I'm also attaching the complete matlab file. Could someone please let me know what is wrong with my code?
<Figure 1>
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/701242/image.jpeg)
<Figure 2>
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/701247/image.jpeg)
1 Comment
Sean Brennan
on 3 Aug 2021
The spikes are not necessarily disappearing - they are being filtered (as are the positive ones!); you can see this in the severe change in amplitude between the before/after signals. It's just that, if we look closely at the positive spikes in the original signal, we see they are stronger than the negative ones. Specifically, their peaks have a wider duration - particularly the ones that remain after filtering. Namely, the positive spikes have a sequence of several very positive values right next to each other. Apparently the negative spikes do not have this property. Maybe this is something in the data that is trying to tell you something about the source of these spikes? (it's not random)
In other words, the filter appears to be working and your observation about negative spikes is due to your data being different in the positive direction than in the negative direction.
Since these spikes - positive and negative - may be outliers, if you want to remove them, it may be better just to perform a standard deviation calculation on the signal (the "std" command) and then eliminate signals that are more than 3 or 4 standard deviations from the mean. E.g., remove data larger than mean+4*sigma and less than mean-4*sigma. A common practice is to set these outliers to the mean, or even to NaN.
Filtering of outliers just using Butterworth filters (or others) can do very strange things to data as the outlier peak magnitudes are reduced, but the filtering process causes these tall peaks to spread out in time, and thus significantly distort the surrounding data. These distortions are nearly impossible to remove in later steps, so best to try to eliminate the outliers properly.
Another option is to use median filtering - but this can introduce other artifacts into the results.
For experimental work, outlier removal using standard deviations is generally better practice than butterworth filtering or median filtering.
Answers (1)
Dave B
on 3 Aug 2021
Your tutorial looks amazing!
I suspect this isn't the fact that they're negative, to check that: can you multiply the values by -1 and see if the problem reverses? Could it be that the bandwidth of the negative-going part and the bandwidth of the positive-going part are different and the negative going part is outside of the filter range?
0 Comments
See Also
Categories
Find more on Digital Filtering in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!