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.TimeSeries; 033import org.openimaj.ml.timeseries.processor.TimeSeriesProcessor; 034import org.openimaj.ml.timeseries.processor.interpolation.util.TimeSpanUtils; 035import org.openimaj.ml.timeseries.series.DoubleTimeSeries; 036 037/** 038 * Interpolate values of a time series. Useful for filling in missing values and homogonising 039 * disperate sets of {@link TimeSeries} data 040 * 041 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 042 * 043 */ 044public abstract class TimeSeriesInterpolation implements TimeSeriesProcessor<double[],Double,DoubleTimeSeries> { 045 046 private long[] times; 047 048 /** 049 * The processor's times are set to default, i.e. from min to max time in steps of 1 long 050 */ 051 public TimeSeriesInterpolation() { 052 times = null; 053 } 054 055 056 /** 057 * @param begin 058 * @param end 059 * @param delta 060 */ 061 public TimeSeriesInterpolation(long begin, long end, long delta) { 062 this.times = TimeSpanUtils.getTime(begin,end, delta); 063 } 064 065 /** 066 * @param begin 067 * @param steps 068 * @param delta 069 */ 070 public TimeSeriesInterpolation(long begin, int steps, long delta) { 071 this.times = TimeSpanUtils.getTime(begin,steps, delta); 072 } 073 074 /** 075 * @param begin the start of the new time series 076 * @param end the end of the new time series 077 * @param steps the steps between (begin = 0, end = 10, 6 steps will give 0, 2, 4, 6, 8, 10 078 */ 079 public TimeSeriesInterpolation(long begin, long end, int steps) { 080 this.times = TimeSpanUtils.getTime(begin,end, steps); 081 } 082 083 /** 084 * @param times the times used by the processor 085 */ 086 public TimeSeriesInterpolation(long[] times) { 087 this.times = times; 088 } 089 090 /** 091 * Uses {@link #interpolate(DoubleTimeSeries, long[])} to return an interpolation of the construction 092 * {@link TimeSeries} between the times at the required interval 093 * @param series 094 * @param begin time to start 095 * @param end time to end 096 * @param delta the delta between time steps 097 * @return {@link DoubleTimeSeries} instance interpolated from the construction {@link TimeSeries} instance 098 */ 099 public DoubleTimeSeries interpolate(DoubleTimeSeries series, long begin, long end, long delta){ 100 long[] times = TimeSpanUtils.getTime(begin,end,delta); 101 return interpolate(series,times); 102 } 103 /** 104 * Uses {@link #interpolate(DoubleTimeSeries, long[])} to return an interpolation of the construction 105 * {@link TimeSeries} from begin, for a number of steps with a given delta between steps 106 * @param series 107 * @param begin 108 * @param steps 109 * @param delta 110 * @return {@link DoubleTimeSeries} instance interpolated from the construction {@link TimeSeries} instance 111 */ 112 public DoubleTimeSeries interpolate(DoubleTimeSeries series, long begin, int steps, long delta){ 113 long[] times = TimeSpanUtils.getTime(begin,steps,delta); 114 return interpolate(series,times); 115 } 116 /** 117 * Uses {@link #interpolate(DoubleTimeSeries,long[])} to return an interpolation of the construction 118 * {@link TimeSeries} from begin, until end with a delta which means that there are 119 * splits time instances 120 * @param series 121 * @param begin 122 * @param end 123 * @param splits 124 * @return {@link DoubleTimeSeries} instance interpolated from the construction {@link TimeSeries} instance 125 */ 126 public DoubleTimeSeries interpolate(DoubleTimeSeries series, long begin, long end, int splits){ 127 long[] times = TimeSpanUtils.getTime(begin,end,splits); 128 return interpolate(series,times); 129 } 130 131 /** 132 * @param series 133 * @return a new {@link DoubleTimeSeries} interpolated with times provided in the constructor 134 */ 135 public DoubleTimeSeries interpolate(DoubleTimeSeries series){ 136 return interpolate(series,this.times); 137 } 138 /** 139 * @param series 140 * @param times might be null, therefore some "defualt" time step should be used 141 * @return {@link DoubleTimeSeries} instance interpolated from the construction {@link TimeSeries} instance 142 */ 143 public abstract DoubleTimeSeries interpolate(DoubleTimeSeries series, long[] times); 144 145 @Override 146 public void process(DoubleTimeSeries ts) { 147 ts.internalAssign(this.interpolate(ts,times)); 148 } 149}