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 {@link LinearInterpolationProcessor} 042 * 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 * @param sigma the sigma of the guassian function 057 */ 058 public GaussianTimeSeriesProcessor(double sigma) { 059 this.kernel = makeKernel(sigma,DEFAULT_GAUSS_TRUNCATE); 060 } 061 062 /** 063 * Construct a zero-mean Gaussian with the specified standard deviation. 064 * @param sigma the standard deviation of the Gaussian 065 * @param truncate the number of sigmas from the centre at which to 066 * truncate the Gaussian 067 * @return an array representing a Gaussian function 068 */ 069 public static double[] makeKernel(double sigma, double truncate) { 070 if(sigma == 0) return new double[]{1f}; 071 //The kernel is truncated at truncate sigmas from center. 072 int ksize = (int) (2.0f * truncate * sigma + 1.0f); 073// ksize = Math.max(1, ksize); // size must be at least 3 074 if( ksize % 2 == 0 ) ksize++; // size must be odd 075 076 double [] kernel = new double[ksize]; 077 078 //build kernel 079 float sum = 0.0f; 080 for(int i = 0; i < ksize; i++) { 081 float x = i - ksize / 2; 082 kernel[i] = (float) Math.exp( -x * x / (2.0 * sigma * sigma) ); 083 sum += kernel[i]; 084 } 085 086 //normalise area to 1 087 for(int i = 0; i < ksize; i++) { 088 kernel[i] /= sum; 089 } 090 091 return kernel; 092 } 093 094 /** 095 * Convolve a double array 096 * 097 * @param data the image to convolve. 098 * @param kernel the convolution kernel. 099 */ 100 public static void convolveHorizontal(double[] data, double [] kernel) { 101 int halfsize = kernel.length / 2; 102 103 double buffer[] = new double[data.length + kernel.length]; 104 105 for(int i = 0; i < halfsize; i++) 106 buffer[i] = data[0]; 107 for(int i = 0; i < data.length; i++) 108 buffer[halfsize + i] = data[i]; 109 110 for(int i = 0; i < halfsize; i++) 111 buffer[halfsize + data.length + i] = data[data.length- 1]; 112 113// convolveBuffer(buffer, kernel); 114 int l = buffer.length-kernel.length; 115 for(int i = 0; i < l; i++) { 116 float sum = 0.0f; 117 118 for(int j = 0, jj=kernel.length-1; j < kernel.length; j++, jj--) 119 sum += buffer[i + j] * kernel[jj]; 120 121 buffer[i] = sum; 122 } 123// end convolveBuffer(buffer, kernel); 124 125 for(int c=0; c<data.length; c++) data[c] = buffer[c]; 126 } 127 128 @Override 129 public void process(DoubleTimeSeries series) { 130 convolveHorizontal(series.getData(),this.kernel); 131 } 132}