001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math.optimization.fitting;
019
020 import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
021 import org.apache.commons.math.util.FastMath;
022
023 /** Harmonic function of the form <code>f (t) = a cos (ω t + φ)</code>.
024 * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 ao??t 2010) $
025 * @since 2.0
026 */
027 public class HarmonicFunction implements DifferentiableUnivariateRealFunction {
028
029 /** Amplitude a. */
030 private final double a;
031
032 /** Pulsation ω. */
033 private final double omega;
034
035 /** Phase φ. */
036 private final double phi;
037
038 /** Simple constructor.
039 * @param a amplitude
040 * @param omega pulsation
041 * @param phi phase
042 */
043 public HarmonicFunction(double a, double omega, double phi) {
044 this.a = a;
045 this.omega = omega;
046 this.phi = phi;
047 }
048
049 /** {@inheritDoc} */
050 public double value(double x) {
051 return a * FastMath.cos(omega * x + phi);
052 }
053
054 /** {@inheritDoc} */
055 public HarmonicFunction derivative() {
056 return new HarmonicFunction(a * omega, omega, phi + FastMath.PI / 2);
057 }
058
059 /** Get the amplitude a.
060 * @return amplitude a;
061 */
062 public double getAmplitude() {
063 return a;
064 }
065
066 /** Get the pulsation ω.
067 * @return pulsation ω
068 */
069 public double getPulsation() {
070 return omega;
071 }
072
073 /** Get the phase φ.
074 * @return phase φ
075 */
076 public double getPhase() {
077 return phi;
078 }
079
080 }