Hampel Matlab vs R
Show older comments
I am struggeling rewriting matlab code in R as the hampel implementation in matlab seems to be different to the pracma package in R which, according to the window length leaves the lower and upper ends of times series untouched, whicle matlab seems to modify them. I have highlighted the problem already on stackoverflow: https://stackoverflow.com/questions/64631709/hampel-matlab-vs-r
As I would really like to know where the discrepancies are coming from, help is highly appreciated!
With kind regards
CV
Answers (1)
Bjorn Gustavsson
on 9 Nov 2020
0 votes
You seem to use different filter lengths in your call to hampel in matlab (30 - which I take to mean you will get a window of 2*30+1 samples, i.e. 30 samples before and 30 samples after the central point) and R (15 which I find very hard to believe gives you a total filter-length of 61.) Since the matlab-code is available to inspect you can simply go to the code to see what's done, R is an open source project so that source-code should also be available. Perhaps R uses median absolute deviation an matlab uses mean absolute deviation? I cant recall what the default of mad in matlab is at the moment.
HTH
4 Comments
Claudio Viecelli
on 9 Nov 2020
Bjorn Gustavsson
on 9 Nov 2020
Have you compared the source-codes?
Claudio Viecelli
on 9 Nov 2020
Bjorn Gustavsson
on 9 Nov 2020
My guess for the matlab-hampel's handling is that it simply cuts out all indices that fall outside your array-limits, something like this:
idxWindow = [max(1,idxCentre-K):min(array_length,idxCentre+K)]
and then calculates the mad on that array. You'll have to investigate using a suitably small array where you can change the values at the ends and compare the output with what you'd calculate "by hand", using sensibly small K (3, 4, 5?)...
How R can use the full window-length when it runs outside the array-limits, is a question for someone else, perhaps simply repicating the last element?
Categories
Find more on Digital Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!