0
\$\begingroup\$

The DFT function below works fine but I want to use the list, generated from that function, in the Update function.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FFT : MonoBehaviour
{
    public ArrayList amp = new ArrayList();
    public ArrayList freq = new ArrayList();

    void DFT()
    {
        float[]  X = {1, 2, 3};
        float[]  Y = {2, 3, 5};
        float  N = X.Length;

        for(int k = 0; k < N; k++)
        {
            float re = 0;
            float im = 0;

            for(int n = 0; n < N; n++)
            {
                float phi = (2 * Mathf.PI * k * n) / N;
                re += X[n] * Mathf.Cos(phi) + Y[n] * Mathf.Sin(phi);
                im += Y[n] * Mathf.Cos(phi) - X[n] * Mathf.Sin(phi);
            }

            var  freqq = k;
            var  ampp = Mathf.Sqrt(re*re + im*im);
            var  phasse = Mathf.Atan2(re, im);
            phase.Add(phasse);

            amp.Add(ampp);
            freq.Add(freqq);

        }
    }
}

I want to use the lists elements in another function (for example in Update)

void Update()
{
    for(var i = 0; i < 2; ++i)
    {
        var x = amp[i] * Mathf.Cos(freq[i]);
        var y = amp[i] * Mathf.Sin(freq[i]);

        var pos = new Vector3(x, y, 0);

        //Debug.Log(pos);
    }
}

The problem is that amp[i] and freq[i] doesn't work in another function.

\$\endgroup\$
4
  • \$\begingroup\$ Show your entire class. \$\endgroup\$ Commented Dec 11, 2019 at 18:25
  • \$\begingroup\$ What do you mean by "doesn't work in another function."? \$\endgroup\$
    – Vaillancourt
    Commented Dec 11, 2019 at 18:48
  • \$\begingroup\$ I have closed the question because the symptoms are similar to this other question of yours.Please update the question if it's not the same issue. \$\endgroup\$
    – Vaillancourt
    Commented Dec 11, 2019 at 18:58
  • 1
    \$\begingroup\$ You probably don't want to use ArrayList — the List<float> type will give you all the same functionality more efficiently. \$\endgroup\$
    – DMGregory
    Commented Dec 11, 2019 at 21:05

0

Browse other questions tagged .