001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.ml.timeseries.processor;
031
032import org.openimaj.ml.timeseries.processor.interpolation.LinearInterpolationProcessor;
033import org.openimaj.ml.timeseries.series.DoubleTimeSeries;
034
035/**
036 * Calculates a moving average over a specified window in the past such that
037 *
038 * data[t_n] = sum^{m}_{i=1}{data[t_{n-i}}
039 *
040 * This processor returns a value for each time in the underlying time series.
041 * For sensible results, consider interpolating a consistent time span using an
042 * {@link LinearInterpolationProcessor} followed by this processor.
043 *
044 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
045 *
046 */
047public class GaussianTimeSeriesProcessor implements TimeSeriesProcessor<double[], Double, DoubleTimeSeries>
048{
049        private double[] kernel;
050        /**
051         * The default number of sigmas at which the Gaussian function is truncated
052         * when building a kernel
053         */
054        public static final double DEFAULT_GAUSS_TRUNCATE = 4.0d;
055
056        /**
057         * @param sigma
058         *            the sigma of the guassian function
059         */
060        public GaussianTimeSeriesProcessor(double sigma) {
061                this.kernel = makeKernel(sigma, DEFAULT_GAUSS_TRUNCATE);
062        }
063
064        /**
065         * Construct a zero-mean Gaussian with the specified standard deviation.
066         * 
067         * @param sigma
068         *            the standard deviation of the Gaussian
069         * @param truncate
070         *            the number of sigmas from the centre at which to truncate the
071         *            Gaussian
072         * @return an array representing a Gaussian function
073         */
074        public static double[] makeKernel(double sigma, double truncate) {
075                if (sigma == 0)
076                        return new double[] { 1f };
077                // The kernel is truncated at truncate sigmas from center.
078                int ksize = (int) (2.0f * truncate * sigma + 1.0f);
079                // ksize = Math.max(1, ksize); // size must be at least 3
080                if (ksize % 2 == 0)
081                        ksize++; // size must be odd
082
083                final double[] kernel = new double[ksize];
084
085                // build kernel
086                float sum = 0.0f;
087                for (int i = 0; i < ksize; i++) {
088                        final float x = i - ksize / 2;
089                        kernel[i] = (float) Math.exp(-x * x / (2.0 * sigma * sigma));
090                        sum += kernel[i];
091                }
092
093                // normalise area to 1
094                for (int i = 0; i < ksize; i++) {
095                        kernel[i] /= sum;
096                }
097
098                return kernel;
099        }
100
101        /**
102         * Convolve a double array
103         *
104         * @param data
105         *            the image to convolve.
106         * @param kernel
107         *            the convolution kernel.
108         */
109        public static void convolveHorizontal(double[] data, double[] kernel) {
110                final int halfsize = kernel.length / 2;
111
112                final double buffer[] = new double[data.length + kernel.length];
113
114                for (int i = 0; i < halfsize; i++)
115                        buffer[i] = data[0];
116                for (int i = 0; i < data.length; i++)
117                        buffer[halfsize + i] = data[i];
118
119                for (int i = 0; i < halfsize; i++)
120                        buffer[halfsize + data.length + i] = data[data.length - 1];
121
122                // convolveBuffer(buffer, kernel);
123                final int l = buffer.length - kernel.length;
124                for (int i = 0; i < l; i++) {
125                        float sum = 0.0f;
126
127                        for (int j = 0, jj = kernel.length - 1; j < kernel.length; j++, jj--)
128                                sum += buffer[i + j] * kernel[jj];
129
130                        buffer[i] = sum;
131                }
132                // end convolveBuffer(buffer, kernel);
133
134                for (int c = 0; c < data.length; c++)
135                        data[c] = buffer[c];
136        }
137
138        @Override
139        public void process(DoubleTimeSeries series) {
140                convolveHorizontal(series.getData(), this.kernel);
141        }
142}