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.interpolation;
031
032import org.openimaj.ml.timeseries.processor.interpolation.util.TimeSpanUtils;
033import org.openimaj.ml.timeseries.series.DoubleTimeSeries;
034
035/**
036 * Perform a linear interpolation such that the value of data at time t1 between t0 and t2 = 
037 * 
038 * data[t1] = data[t0] * (t1 - t0)/(t2-t0) + data[t2] * (t2 - t1)/(t2-t0)
039 * 
040 * Note that this means if data is known at t1, then t0 = t1 and data[t1] == data[t0]
041 * 
042 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
043 *
044 */
045public class LinearInterpolationProcessor extends TimeSeriesInterpolation{
046        
047        
048        
049        /**
050         * @see TimeSeriesInterpolation#TimeSeriesInterpolation(long[])
051         * @param times
052         */
053        public LinearInterpolationProcessor(long[] times) {
054                super(times);
055        }
056
057        /**
058         * @see TimeSeriesInterpolation#TimeSeriesInterpolation()
059         */
060        public LinearInterpolationProcessor() {
061                super();
062        }
063
064        
065
066        /**
067         * @param begin
068         * @param steps
069         * @param delta
070         */
071        public LinearInterpolationProcessor(long begin, int steps, long delta) {
072                super(begin, steps, delta);
073        }
074
075        /**
076         * @param begin
077         * @param end
078         * @param steps
079         */
080        public LinearInterpolationProcessor(long begin, long end, int steps) {
081                super(begin, end, steps);
082        }
083
084        /**
085         * @param begin
086         * @param end
087         * @param delta
088         */
089        public LinearInterpolationProcessor(long begin, long end, long delta) {
090                super(begin, end, delta);
091        }
092
093        @Override
094        public DoubleTimeSeries interpolate(DoubleTimeSeries timeSeries, long[] times) {
095                if(times == null){
096                        times = TimeSpanUtils.getTime(timeSeries.getTimes()[0],timeSeries.getTimes()[timeSeries.size()-1],1l);
097                }
098                double[] values = new double[times.length];
099                DoubleTimeSeries dataholder = new DoubleTimeSeries(3);
100                double[] holderdata = dataholder.getData();
101                long[] holdertimes = dataholder.getTimes();
102                int i = 0;
103                for (long t : times) {
104                        timeSeries.get(t, 1, 1,dataholder);
105                        if(dataholder.size() == 3){ // In the middle
106                                values[i++] = holderdata[1];
107                        }
108                        else if(dataholder.size() == 2){
109                                // Either left or right extreme
110                                if(holdertimes[0] == t){
111                                        values[i++] = holderdata[0];
112                                }
113                                else if(holdertimes[1] == t){
114                                        values[i++] = holderdata[1];
115                                }
116                                else{
117                                        // This is the only point we should interpolate
118                                        double sum = holdertimes[1] - holdertimes[0];
119                                        double weightLeft = sum - (t - holdertimes[0]);
120                                        double weightRight = sum - (holdertimes[1] - t);
121                                        values[i++] = ((holderdata[0] * weightLeft) + (holderdata[1] * weightRight))/sum;
122                                }
123                        }
124                        else{
125                                values[i++] = holderdata[0];
126                        }
127                }
128                return new DoubleTimeSeries(times,values);
129        }
130
131}