Last active
September 25, 2015 18:21
-
-
Save tterrag1098/12bf8bed978d626689ab to your computer and use it in GitHub Desktop.
Fourier Stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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