Advertisement
Guest User

sadfdf

a guest
May 5th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import java.lang.Math;
  2. public class HelloWorld
  3. {
  4. public static void main(String[] args)
  5. {
  6. // Fourier equations from: http://www.continuummechanics.org/cm/fourierxforms.html
  7. int N = 33; // number of input points
  8. int terms = 4; // fourier terms
  9. 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};
  10. float[] x = new float[N];
  11. float[] y = new float[N];
  12. float time = 0.032f;
  13. float omega = (float)(1f/time*2*Math.PI);
  14. float x_inc = time/(N-1);
  15. //Calculate x's
  16. for(int i=0; i<N; i++) {
  17. x[i] = i*x_inc;
  18. }
  19. // Calculate y's
  20. for(int i=0; i<N; i++) {
  21. float a_0 = 0; // For this problem
  22. for(int n=1;n<=terms;n++) {
  23. float a_n = 0;
  24. for (int a=0; a<N; a++) {
  25. a_n += y_inputs[a]*Math.cos(n*omega*x[a]);
  26. }
  27. a_n = a_n*2f/N;
  28. float b_n = 0;
  29. for (int a=1; a<N; a++) {
  30. b_n += y_inputs[a]*Math.sin(n*omega*x[a]);
  31. }
  32. b_n = b_n*2f/N;
  33. y[i] += (float) ( a_0/2 + a_n*Math.cos(n*omega*x[i]) + b_n*Math.sin(n*omega*x[i]) );
  34. }
  35. }
  36. System.out.println("-----------------");
  37. System.out.println("X:");
  38. for(int i=0; i<N; i++) {
  39. System.out.println("" + x[i]);
  40. }
  41. System.out.println("-----------------");
  42. System.out.println("Y:");
  43. for(int i=0; i<N; i++) {
  44. System.out.println("" + y[i]);
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement