Coarse scaled NDVI (or FAPAR, EVI…) images have a high temporal frequency and are delivered as Maximum Value Composites (MVC) of several days, which means the highest value is taken, assuming that clouds and other contaminated pixels have low values. However, especially in areas with a rainy season, the composites over 10-16 days still contain clouds and other disturbances. This figure illustrates a raw MODIS NDVI MVC of 16 days, and it’s obvious that several pixels are contaminated, and it’s also obvious that analyses will be affected by the noisy data.

Datasets like GIMMS or GEOV1 provide already filtered data, but e.g. MODIS, SPOT VGT, PROBA-V and AVHRR data are raw. The solution is to smooth the time series, using a filter, which calculates a smooth time series and interpolates the bad values using the previous and following images. Here is the same image, but smoothed with a Savitzky Golay filter.

The data are mostly delivered with quality rasters, rating the quality of each pixel. This can be used to either filter the raster and set bad quality pixels to NA, or to weight the pixels. When the new time line is calculated, low weighted (i.e. contaminated) pixels are less considered in the calculation process. One possibility is the software TIMESAT (Eklundh & Jönsson, 2011), which offers different filter techniques. Here is an example how timesat smoothes the time line with a Savitzky Golay filter, omitting “bad” pixles and creating new rasters.

Filtering is also possible in R, and it’s very simple. First one has to decide if one wants to work with quality files, or simply use the raw data, both is possible. GIS software like GRASS has modules which allow an easy use of the quality files:

http://grass.osgeo.org/grass64/manuals/i.in.spotvgt.html

http://grass.osgeo.org/grass70/manuals/i.modis.qc.html

However, filtering without quality flags also provides reasonable results. Now we assume we have a time series of MODIS data from 2005-2010, with 23 images each year. This data is loaded in R in a raster stack or brick, called MODIS, bad values are masked as NA. We load the libraries, and create a function which uses the Savitzky Golay filter from the signal package. The parameters of the function need to be adapted (p, n, ts) (http://www.inside-r.org/packages/cran/signal/docs/sgolayfilt), also the time frame.

*library(raster)*

* library(rgdal)*

* library(signal)*

*library(zoo)*

*fun <- function(x) {*

* v=as.vector(x)*

* z=substituteNA(v, type=”mean”)*

* MODIS.ts2 = ts(z, start=c(2005,1), end=c(2010,23), frequency=23)*

* x=sgolayfilt(MODIS.ts2, p=1, n=3, ts=30)*

* }*

*MODIS.filtered <- calc(MODIS, fun)*

MODIS.filtered is a new brick containing the smoothed time series. Compare the raw with the filtered tims series:

*l=cellStats(MODIS, stat=mean)*

* MODIS.raw.ts = ts(l, start=c(2005,1), end=c(2010,23), frequency=23)*

* plot(MODIS.raw.ts)*

* l=cellStats(MODIS.filtered, stat=mean)*

* MODIS.filt.ts = ts(l, start=c(2005,1), end=c(2010,23), frequency=23)*

* plot(MODIS.filt.ts)*

One may find out the perfect fitting parameters by looking at the whole area, playing with the parameters:

*l=cellStats(MODIS, stat=’mean’)*

* MODIS.ts = ts(l, start=2005, end=c(2010,23), frequency=23)*

* sg=sgolayfilt(MODIS.ts, p=3, n=9, ts=20)*

* sg.ts = ts(sg, start=2005, end=c(2010,23), frequency=23)*

*plot(sg.ts)*

**20**.

**11**
sir i have MODIS ndvi 250m data in .tif format . i have to apply SG filter to smooth the values and use the smoothed data for LULC classification . i tried using timesat but the files are not loading. i am new to programming and i need help .

Thanks for your sharing. It is useful. What is the time scaling factor？ How to choose it？and if I have one year data, that means NDVI have 23 subfiles, each subfile contain 20 images. Should I need to mosic subfile images to one image at first or it can deal with directly and how to do it. Thanks

I found great and useful this website, thanks for this. I would like to know how to set the parameters, specially the time scaling factor? Why do you use 30 and then 20? If we compare the parameters in TIMESAT and in this function, which TIMESAT parameters are equivalent in this function?

Many thanks!

Thanks Martin! Very much appreciate you posting this information. I’m in the process of doing similar analyses for study areas in Chad and Kenya. Really helpful.

Great page and open my eyes on using R for other time series comparisons, sucha as correlating an image stack with MEI or SOI indices.

Hi Martin. Can you please explain what parameter ts (time scaling factor) means in SG filter. I looked at signal package documentation and also googled on it, but didn’t find anything helpful.

intéressant,je travaille actuellement sur des séries temporelles NDVI MODIS Sur un pays de l’Afrique de l’Ouest(Mali). J’aurais aimé que vous spécifier s’il vous les paramètres (p,n et ts).

Merci d’avance!!!

In a time series of 11 years with 23 images per year. What is the p, n, and ts?

Hi, I am smoothing MODIS timeseries from 2001-2017. I am getting this error below. Any idea how I can fix this. Thanks for your help:

Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :

cannot use this function

Hello Dr. Brandt,

While trying this on a time series data I am getting the following error. Any suggestion towards solving the issue?

fun1 savgol.trmm <- calc(sbst.trmm, fun1)

Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :

cannot use this function