Advertisement
Guest User

Untitled

a guest
Sep 6th, 2020
1,581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class TimeSignal
  7. {
  8. public float[] x;
  9. public float[] y;
  10.  
  11. public TimeSignal(int N)
  12. {
  13. x = new float[N];
  14. y = new float[N];
  15. }
  16. }
  17.  
  18. public class LineFFT : MonoBehaviour
  19. {
  20. LineRenderer lr;
  21. TimeSignal signal = new TimeSignal(1000);
  22. void Start()
  23. {
  24. lr = GetComponent<LineRenderer>();
  25.  
  26. for(int i = 0; i < 1000; i++)
  27. {
  28. //x-values
  29. signal.x[i] = Mathf.Lerp(0, 100, (float)i / 1000f);
  30. //y-values
  31. signal.y[i] = 2 * Mathf.Sin(1 * signal.x[i]) + 1* Mathf.Cos(2 * signal.x[i] + Mathf.PI/4.2f);
  32. }
  33.  
  34. lr.SetPositions(DFT(signal, 3.0f));
  35.  
  36. }
  37.  
  38. // Update is called once per frame
  39. void Update()
  40. {
  41. lr.SetPositions(DFT(signal, Time.time * 0.25f));
  42. }
  43.  
  44. Vector3[] DFT(TimeSignal X, float k)
  45. {
  46. int N = X.x.Length;
  47.  
  48. Vector3[] c = new Vector3[N];
  49.  
  50. for(int n = 0; n < N; n++)
  51. {
  52. c[n] = X.y[n] * new Vector3(
  53. Mathf.Cos((2 * Mathf.PI) / N * k * n),
  54. -Mathf.Sin((2 * Mathf.PI) / N * k * n),
  55. 0);
  56. }
  57.  
  58. return c;
  59. }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement