Skip to content

Instantly share code, notes, and snippets.

@tterrag1098
Last active September 25, 2015 18:21
Show Gist options
  • Save tterrag1098/12bf8bed978d626689ab to your computer and use it in GitHub Desktop.
Save tterrag1098/12bf8bed978d626689ab to your computer and use it in GitHub Desktop.
Fourier Stuff
package tterrag.fourier;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
public class CoefficientTable
{
@Getter
@RequiredArgsConstructor
public static class Pair
{
private final double a, b;
}
private Pair[] coefts;
public CoefficientTable(int m)
{
coefts = new Pair[m];
}
public void addCoefts(int n, double a, double b)
{
coefts[n - 1] = new Pair(a, b);
}
public Pair getDataFor(int n)
{
return coefts[n - 1];
}
public double getAFor(int n)
{
return getDataFor(n).getA();
}
public double getBFor(int n)
{
return getDataFor(n).getB();
}
}
package tterrag.fourier;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class FourierFunction
{
private final double a0;
private final CoefficientTable table;
public double compute(double x, int n)
{
return (a0 / 2) + calcA(x, n) + calcB(x, n);
}
private double calcA(double x, int n)
{
double ret = 0;
for (int i = n; i > 0; i--)
{
ret += table.getAFor(i) * Math.cos(i * Math.PI * x);
}
return ret;
}
private double calcB(double x, int n)
{
double ret = 0;
for (int i = n; i > 0; i--)
{
ret += table.getBFor(i) * Math.sin(i * Math.PI * x);
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment