001    //////////////////////////////////////////////////
002    // This file is part of Sears project.
003    // Subtitle Editor And Re-Synch
004    // A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    // at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    ////////////////////////////////////////////////
017    package sears.tools;
018    
019    /**
020     * LinearInterpolation This class permits to perform linear interpolation by
021     * segments.
022     */
023    public class LinearInterpolation {
024            private double[] x;
025    
026            private double[] y;
027    
028            private double[] a;
029    
030            private double[] b;
031    
032            public LinearInterpolation(double[] x, double[] y) {
033                    this.x = x;
034                    this.y = y;
035                    computeInterpolation();
036            }
037    
038            private void computeInterpolation() {
039                    if (x.length == y.length && x.length > 1) {
040                            // We have to compute the a and b values for each intervals.
041                            a = new double[x.length - 1];
042                            b = new double[y.length - 1];
043                            for (int i = 0; i < a.length; i++) {
044                                    a[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
045                                    b[i] = y[i] - x[i] * a[i];
046                            }
047                    }else{
048                            throw new IllegalArgumentException("Linear interpolation must have at least 2 parameters");
049                    }
050            }
051    
052            public void resetDatas(double[] x, double[] y) {
053                    this.x = x;
054                    this.y = y;
055                    computeInterpolation();
056            }
057    
058            public double interpolate(double xx) {
059                    // the result of the method.
060                    double result = 0;
061                    // first is to know the interval of xx.
062                    int index = 0;
063                    if (xx <= x[0]) {
064                            // If x is before first point, take the first interpolation.
065                            index = 0;
066                    } else if (xx >= x[x.length-1]) {
067                            // If x is after last point, take the last point.
068                            index = x.length - 2;
069                    } else {
070                            while (xx >= x[index + 1]) {
071                                    index++;
072                            }
073                    }
074                    // We have the index.
075                    result = a[index] * xx + b[index];
076                    // return the result
077                    return result;
078            }
079    }