Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.Math;
- public class HelloWorld
- {
- public static void main(String[] args)
- {
- // Fourier equations from: http://www.continuummechanics.org/cm/fourierxforms.html
- int N = 33; // number of input points
- int terms = 4; // fourier terms
- float[] y_inputs = {0f,0.11f,0.24f,0.38f,0.5f,0.41f,0.33f,0.22f,0.31f,0.39f,0.45f,0.38f,0.32f,0.27f,0.21f,0.14f,0.07f,0.002f,-0.04f,-0.09f,-0.17f,-0.27f,-0.41f,-0.55f,-0.62f,-0.68f,-0.75f,-0.64f,-0.4f,-0.33f,-0.26f,-0.16f,-0.05f};
- float[] x = new float[N];
- float[] y = new float[N];
- float time = 0.032f;
- float omega = (float)(1f/time*2*Math.PI);
- float x_inc = time/(N-1);
- //Calculate x's
- for(int i=0; i<N; i++) {
- x[i] = i*x_inc;
- }
- // Calculate y's
- for(int i=0; i<N; i++) {
- float a_0 = 0; // For this problem
- for(int n=1;n<=terms;n++) {
- float a_n = 0;
- for (int a=0; a<N; a++) {
- a_n += y_inputs[a]*Math.cos(n*omega*x[a]);
- }
- a_n = a_n*2f/N;
- float b_n = 0;
- for (int a=1; a<N; a++) {
- b_n += y_inputs[a]*Math.sin(n*omega*x[a]);
- }
- b_n = b_n*2f/N;
- y[i] += (float) ( a_0/2 + a_n*Math.cos(n*omega*x[i]) + b_n*Math.sin(n*omega*x[i]) );
- }
- }
- System.out.println("-----------------");
- System.out.println("X:");
- for(int i=0; i<N; i++) {
- System.out.println("" + x[i]);
- }
- System.out.println("-----------------");
- System.out.println("Y:");
- for(int i=0; i<N; i++) {
- System.out.println("" + y[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement